sporz

PHP implementation of the board game "sporz"
git clone https://0x1A4.1337.cx/code2/sporz.git
Log | Files | Refs

commit 92116aa8a2d887e4bbd7c4e8701c582c69b8c407
parent 4050c9016ac9e19cc5f05f879662924d476a410f
Author: gitus <git@shroomrider.com>
Date:   Sat Nov 19 22:37:07 +0100

Merge pull request #7 from pips-/master

Just a few commits
Diffstat:
app/Helpers/Security.php | 28+++++++++++-----------------
app/Models/Game.php | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------
app/Views/game/detail.tpl.php | 8++++++++
app/Views/layout.tpl.php | 7+++++++
app/Views/session/login-form.tpl.php | 8++------
db/migrations/20161116215557_remove_uniqueness_from_player_token.php | 26++++++++++++++++++++++++++
public/index.php | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------
7 files changed, 176 insertions(+), 53 deletions(-)
diff --git a/app/Helpers/Security.php b/app/Helpers/Security.php @@ -24,28 +24,22 @@ class Security return substr(implode($arr), 0, 5); } - public static function checkAuthentification(\Pragma\Router\Router $app) + public static function requireAuthentication(\Pragma\Router\Router $app) { return function () use ($app) { - if (empty($_SESSION['auth'])) { - unset($_SESSION['auth']); - Redirect::to($app->url_for('login-form')); - } - - if (empty($_SESSION['auth']['userid'])) { - unset($_SESSION['auth']); - Redirect::to($app->url_for('login-form')); - } - - if (empty($_SESSION['auth']['username'])) { - unset($_SESSION['auth']); - Redirect::to($app->url_for('login-form')); - } - - if (empty($_SESSION['auth']['token'])) { + if (!self::checkAuthentication()) { unset($_SESSION['auth']); Redirect::to($app->url_for('login-form')); } }; } + + public static function checkAuthentication() + { + return !(empty($_SESSION['auth']) + || empty($_SESSION['auth']['userid']) + || empty($_SESSION['auth']['username']) + || empty($_SESSION['auth']['token']) + ); + } } diff --git a/app/Models/Game.php b/app/Models/Game.php @@ -7,33 +7,48 @@ class Game extends \Pragma\ORM\Model const PHASE_DAY = 0; const PHASE_NIGHT = 1; - private $players; - private $id; - private $started; - private $phase; - private $leader; + public $players; public function __construct() { return parent::__construct('game'); } + public function open($gameId) + { + $ret = parent::open($gameId); + + $this->players = Player::forge()->where('game_id', '=', $this->id)->get_objects(); + + return $ret; + } + //the KeyId is the string used by a player in order to authenticate in-game (security is not a concern here) public static function genKeyId() { //TODO } - public function addPlayer($name) + public function addPlayer(Player $player) { - $keyId = null; - if (!($this->started) && getPlayerByName($name) == null) { - $new_player = new Player($name); - $keyId = $new_player->getKeyId(); - array_push($this->players, $new_player); - } + if ($player == null) { + return false; + } + + if ($this->started) { + return false; + } - return $keyId; + if ($this->getPlayerByName($player->name)) { + return false; + } + + $player->game_id = $this->id; + $player->save(); + + $this->players[] = $player; + + return true; } public function getPlayerByRole($role) @@ -53,6 +68,7 @@ class Game extends \Pragma\ORM\Model return null; } + public function getPlayerByName($name) { foreach ($this->players as $player) { @@ -64,11 +80,23 @@ class Game extends \Pragma\ORM\Model return null; } + public function isAttached(Player $player) + { + foreach ($this->players as $attachedPlayer) { + if ($player->is == $attachedPlayer) { + return true; + } + } + + return false; + } + public function startGame() { if (count($this->players) >= 7) { $this->started = 1; $this->initGame(); + $this->save(); } } @@ -77,14 +105,15 @@ class Game extends \Pragma\ORM\Model if (count($this->players) < 7) { return false; } + shuffle($this->players); $bad_guy = array_shift($this->players); - $bad_guy->mutate(); $bad_guy->setGenome(Player::GENOME_HOST); + $bad_guy->mutate(); $first_doc = array_shift($this->players); - $fist_doc->role = 'medic'; + $first_doc->role = 'medic'; $second_doc = array_shift($this->players); $second_doc->role = 'medic'; @@ -93,20 +122,25 @@ class Game extends \Pragma\ORM\Model $this->players[$index_of_modified_genomes[0]]->setGenome(Player::GENOME_HOST); $this->players[$index_of_modified_genomes[1]]->setGenome(Player::GENOME_RESISTANT); - array_push($this->players, $bad_guy, $first_doc, $second_doc); + $this->players[] = $bad_guy; + $this->players[] = $first_doc; + $this->players[] = $second_doc; $some_guy = array_shift($this->players); $some_guy->role = 'psy'; - array_push($this->players, $some_guy); + $this->players[] = $some_guy; + $some_guy = array_shift($this->players); $some_guy->role = 'geneticist'; - array_push($this->players, $some_guy); + $this->players[] = $some_guy; + $some_guy = array_shift($this->players); $some_guy->role = 'it'; - array_push($this->players, $some_guy); + $this->players[] = $some_guy; + $some_guy = array_shift($this->players); $some_guy->role = 'hacker'; - array_push($this->players, $some_guy); + $this->players[] = $some_guy; return true; } diff --git a/app/Views/game/detail.tpl.php b/app/Views/game/detail.tpl.php @@ -1,6 +1,14 @@ <h2><?= $this->get('game')->name; ?></h2> <a href="<?= $this->get('edit-link'); ?>">Edit</a> +<?php +if ($this->get('userid') != null) { + ?> + <a href="<?= $this->get('join-link'); ?>">Join</a> + <?php +} +?> <div> + Players: <?= count($this->get('game')->players); ?><br> Created at: <?= $this->get('game')->created; ?><br> Last modified: <?= $this->get('game')->updated; ?> </div> diff --git a/app/Views/layout.tpl.php b/app/Views/layout.tpl.php @@ -7,6 +7,13 @@ </head> <body> <h1>Sporz</h1> + <?php + if ($this->get('username') != null) { + ?> + <p>Hello, <?= $this->get('username'); ?>!</p> + <?php + } + ?> <?php foreach ($this->flushFlash() as $flash) { ?> diff --git a/app/Views/session/login-form.tpl.php b/app/Views/session/login-form.tpl.php @@ -1,16 +1,12 @@ -<?php -$username = $this->get('username'); -$token = $this->get('token'); -?> <form method="post" action="/login"> <h2>Sign in</h2> <div class="form-group"> <label for="name">Name:</label> - <input type="name" name="name" id="name" placeholder="john" value="<?= $username; ?>"> + <input type="name" name="name" id="name" placeholder="john" value="<?= $this->get('username'); ?>"> </div> <div class="form-group"> <label for="token">Password:</label> - <input type="text" name="token" id="token" value="<?= $token; ?>"> + <input type="text" name="token" id="token" value="<?= $this->get('usertoken'); ?>"> </div> <button type="submit" class="btn btn-default">Login</button> </form> diff --git a/db/migrations/20161116215557_remove_uniqueness_from_player_token.php b/db/migrations/20161116215557_remove_uniqueness_from_player_token.php @@ -0,0 +1,26 @@ +<?php + +use Phinx\Migration\AbstractMigration; + +class RemoveUniquenessFromPlayerToken extends AbstractMigration +{ + public function up() + { + $player = $this->table('player'); + + $player->removeIndex(array('token')); + $player->addIndex(array('token')); + + $player->save(); + } + + public function down() + { + $player = $this->table('player'); + + $player->removeIndex(array('token')); + $player->addIndex(array('token'), array('unique' => true)); + + $player->save(); + } +} diff --git a/public/index.php b/public/index.php @@ -14,10 +14,7 @@ use App\Helpers\Security; session_start(); -$view = View::getInstance(); -$view->set_tpl_path(APP_PATH.'/Views/'); -$view->setLayout('layout.tpl.php'); - +/* Init global session structure */ if (empty($_SESSION['auth'])) { $_SESSION['auth'] = array( 'userid' => null, @@ -27,14 +24,29 @@ if (empty($_SESSION['auth'])) { } if (!isset($_SESSION['view']['flash-messages'])) { - $_SESSION['view']['flash-messages'] = array(); + $_SESSION['view']['flash-messages'] = array(); } +/* View configuration */ +$view = View::getInstance(); +$view->set_tpl_path(APP_PATH.'/Views/'); +$view->setLayout('layout.tpl.php'); $view->initFlashStructure($_SESSION['view']['flash-messages']); +/* Global view variable */ +if ($_SESSION['auth']['userid'] == null) { + $view->assign('userid', null); + $view->assign('username', null); + $view->assign('usertoken', null); +} else { + $view->assign('userid', $_SESSION['auth']['userid']); + $view->assign('username', $_SESSION['auth']['username']); + $view->assign('usertoken', $_SESSION['auth']['token']); +} + +/* Get application router & start route definitions */ $app = Router::getInstance(); -//define your routes here $app->get('/', function () use ($app) { //HOME PAGE View::getInstance()->assign('joinableGames', Game::getNonStartedGame()); @@ -46,9 +58,6 @@ $app->group('/login', function () use ($app) { $app->get('', function () use ($app) { $view = view::getinstance(); - $view->assign('username', $_SESSION['auth']['username']); - $view->assign('token', $_SESSION['auth']['token']); - $view->assign('form-action', $app->url_for('login-post')); $view->render('session/login-form.tpl.php'); @@ -118,6 +127,7 @@ $app->group('/game', function () use ($app) { $view->assign('game', $game); $view->assign('edit-link', $app->url_for('game-edit', ['gameid' => $game->id])); + $view->assign('join-link', $app->url_for('game-join', ['gameid' => $game->id])); $view->render('game/detail.tpl.php'); })->alias('game-detail'); @@ -144,10 +154,53 @@ $app->group('/game', function () use ($app) { Redirect::to($app->url_for('game-detail', ['gameid' => $game->id])); })->alias('game-save'); - $app->group('', Security::checkAuthentification($app), function () use ($app) { + $app->group('', Security::requireAuthentication($app), function () use ($app) { // User is signed in, we can open the corresponding objet if necessary $userId = $_SESSION['auth']['userid']; + $app->get('/join', function ($gameId) use ($app, $userId) { + $game = new Game(); + $game->open($gameId); + + if ($game == null) { + View::getInstance()->flash('Inexistent game', 'danger'); + Redirect::to($app->url_for('index')); + } + + $player = new Player(); + $player->open($userId); + + if ($player == null) { + View::getInstance()->flash('Inexistent player', 'danger'); + Redirect::to($app->url_for('index')); + } + + if (!$game->isAttached($player) && !$game->addPlayer($player)) { + View::getInstance()->flash('Can not join game', 'danger'); + Redirect::to($app->url_for('index')); + } + + Redirect::to($app->url_for('game-dashboard', ['gameid' => $game->id])); + })->alias('game-join'); + $app->get('/dashboard', function ($gameId) use ($app, $userId) { + $game = new Game(); + $game->open($gameId); + + if ($game == null) { + View::getInstance()->flash('Inexistent game', 'danger'); + Redirect::to($app->url_for('index')); + } + + $player = new Player(); + $player->open($userId); + + if ($player == null) { + View::getInstance()->flash('Inexistent player', 'danger'); + Redirect::to($app->url_for('index')); + } + + // TODO: Main view - game data sum up - ajax refreshing - web stuff + })->alias('game-dashboard'); // $app->get('/secret', function ($gameId) use ($userId) { // $game = new Game(); // $game->open($gameId); @@ -181,9 +234,14 @@ $app->post('/joinGame', function () { //si possible de créer un joueur avec POST[name] alors joinGame et redirige vers /:keyId }); +/* Start main loop - catch exception if any */ try { $app->run(); $view->compute(); } catch (Pragma\Router\RouterException $e) { + if ($e->getCode() == Pragma\Router\RouterException::NO_ROUTE_CODE) { + Redirect::to($app->url_for('index')); + } + var_dump($e); }