public static function checkCurrentGame() { // check if a user is logged in if (ModelUser::getCurrentUser()->getId() <= 0) { unset($_SESSION['id_game']); return; } // check if user selected another game if (isset($_POST['select_game'])) { $_SESSION['id_game'] = (int) $_POST['select_game']; } if (!isset($_SESSION['id_game'])) { return; } // check if game exists and is running/started try { $game = ModelGame::getGame($_SESSION['id_game']); } catch (NullPointerException $ex) { unset($_SESSION['id_game']); return; } if ($game->getStatus() !== GAME_STATUS_STARTED && $game->getStatus() !== GAME_STATUS_RUNNING) { unset($_SESSION['id_game']); return; } // check if user is in this game try { ModelIsInGameInfo::getIsInGameInfo(ModelUser::getCurrentUser()->getId(), $_SESSION['id_game']); } catch (NullPointerException $ex) { unset($_SESSION['id_game']); return; } // set current game ModelGame::setCurrentGame($_SESSION['id_game']); }
protected function checkCreator($id_game) { if (ModelGame::getGame($id_game)->getCreator() !== ModelUser::getCurrentUser()) { throw new GameAdministrationException('not-creator.'); } return true; }
private function validateMoves() { $game = ModelGame::getGame($this->id_game); $round = $game->getRound(); $move_iter = ModelProductionMove::iterator(null, $this->id_game, $round); $controllerForUser = array(); $controller = null; // run through moves while ($move_iter->hasNext()) { /* @var $move ModelProductionMove */ $move = $move_iter->next(); $id_user = $move->getIdUser(); // validate moves if (!isset($controllerForUser[$id_user])) { $controllerForUser[$id_user] = new ProductionController($id_user, $this->id_game); } try { $controller = $controllerForUser[$id_user]; /* @var $controller ProductionController */ $controller->validateProductionMove($move); } catch (ControllerException $ex) { $this->logger->error($ex); $move->flagMoveDeleted(); continue; } // add move to moves $this->moves[] = $move; } }
public function run(array &$data) { $data['template'] = $this->getTemplate(); $this->id_game = intval($data['id_game']); try { $this->game = ModelGame::getGame($this->id_game); } catch (NullPointerException $ex) { $data['errors'] = array('message' => 'Game not found!'); return true; } if (isset($_POST['join'])) { $this->joinGame($data); } $this->parseGame($data); }
/** * selects a game and throws exception if game is not found * * @param int $id_game * @throws NullPointerException, ControllerException * @return boolean */ public function selectGame($id_game) { if (!$this->checkInGame($id_game)) { throw new ControllerException('User is not in this game.'); } $game = ModelGame::getGame($id_game); if ($game->getStatus() === GAME_STATUS_NEW) { throw new ControllerException('Unable to select game as it is not yet started.'); } if ($game->getStatus() === GAME_STATUS_DONE) { throw new ControllerException('Unable to select game as it is already done.'); } $_SESSION['id_game'] = $id_game; return true; }
private function run_game($id_game) { $this->logger->debug('run logic for game: ' . $id_game); $game = ModelGame::getGame($id_game); foreach ($this->factories as $factory) { /* @var $factory LogicFactoryInterface */ if ($factory->getIdPhase() === $game->getIdPhase()) { $phaseLogic = $factory->getOperation($id_game); } } if (!isset($phaseLogic)) { throw new ControllerException('missing phase-logic-operation for phase: ' . $game->getIdPhase()); } /* @var $phaseLogic PhaseLogic */ $phaseLogic->run(); }
protected function finishProcessing() { // set game to next phase $game = ModelGame::getGame($this->id_game); $game->moveToNextPhase(); // update is_ready for user $iter = ModelInGamePhaseInfo::iterator(null, $this->id_game); while ($iter->hasNext()) { $igpi = $iter->next(); $igpi->setIsReady($this->id_phase, false); } // TODO: write mails // set game to processing done $game = ModelGame::getGame($this->id_game); $game->setProcessing(false); // commit everything DataSource::getInstance()->commit(); }
/** * @param array $data * @return void */ public function run(array &$data) { $data['template'] = $this->getTemplate(); $this->id_game = intval($data['id_game']); try { $this->game = ModelGame::getGame($this->id_game); } catch (NullPointerException $ex) { $data['errors'] = array('message' => 'Game not found!'); return; } if (isset($_POST['creator_action'])) { try { $this->do_creator_action($data); } catch (GameAdministrationException $ex) { $data['errors'] = array('message' => $ex->getMessage()); return; } } $this->showGame($data); }
private function sortAndValidateMoves() { $game = ModelGame::getGame($this->id_game); $round = $game->getRound(); $move_iter = ModelLandMove::iterator(null, $this->id_game, $round); $controllerForUser = array(); $controller = null; // run through moves while ($move_iter->hasNext()) { /* @var $move ModelLandMove */ $move = $move_iter->next(); $id_move = $move->getId(); $id_user = $move->getIdUser(); // validate moves if (!isset($controllerForUser[$id_user])) { $controllerForUser[$id_user] = new LandMoveController($id_user, $this->id_game); } try { $controller = $controllerForUser[$id_user]; /* @var $controller LandMoveController */ $controller->validateLandMoveByid($id_move); } catch (ControllerException $ex) { $this->logger->error($ex); $move->flagMoveDeleted(); continue; } // sort moves $steps = $move->getSteps(); $zArea = ModelGameArea::getGameArea($this->id_game, end($steps)); if ($zArea->getIdUser() !== $id_user) { if (!isset($this->attack_moves_to[end($steps)])) { $this->attack_moves_to[end($steps)] = array(); } $this->attack_moves_to[end($steps)][] = $id_move; } else { $this->troop_moves[] = $id_move; } } }
public static function parseCurrentUser(array &$data) { $user = ModelUser::getCurrentUser()->getViewData(); $currGame = ModelGame::getCurrentGame(); $user['games'] = array(); $iter = ModelIsInGameInfo::iterator(ModelUser::getCurrentUser()->getId()); while ($iter->hasNext()) { $gameModel = ModelGame::getGame($iter->next()->getIdGame()); if ($gameModel->getStatus() !== GAME_STATUS_STARTED && $gameModel->getStatus() !== GAME_STATUS_RUNNING) { continue; } $game = array('id' => $gameModel->getId(), 'name' => $gameModel->getName()); if ($currGame !== null && $currGame === $gameModel) { $game['selected'] = true; } $user['games'][] = $game; } if ($currGame === null) { $user['noGameSelected'] = true; } else { $user['currGame'] = $currGame->getViewData(); } $data['user'] = $user; }
/** * get list of all ships that are still placebale * * @return array - array(int id_unit => int numberof) */ public function getStillAvailableShips() { // 1. get list of all StartShips try { $startShips = ModelStartShips::getStartShipsForPlayers(ModelGame::getGame($this->id_game)->getNumberOfPlayers())->getShips(); } catch (NullPointerException $ex) { return array(); } // 2. get list of all StartShipMoves and reduce list of Startships $moves = ModelSetShipsMove::getSetShipMovesForUser($this->id_user, $this->id_game); while ($moves->hasNext()) { /** @var $move ModelSetShipsMove */ $move = $moves->next(); $ship = ModelInGameShip::getShipById($this->id_game, $move->getIdZunit()); $id_unit = $ship->getIdUnit(); if (isset($startShips[$id_unit])) { --$startShips[$id_unit]; } } // 3. return return $startShips; }
/** * check if a move exists, then flag it as deleted * * @param int $id_move * @return void * @throws ControllerException * @throws \AttOn\Exceptions\NullPointerException */ public function deleteProductionMove($id_move) { $id_move = intval($id_move); // check if move exists $move = ModelProductionMove::getProductionMove($this->id_game, $id_move); // check if move is from user if ($this->id_user !== $move->getIdUser()) { throw new ControllerException('Unable to delete move from another user.'); } // check if already fixated if ($this->checkIfDone()) { throw new ControllerException('ProductionMove already finished.'); } // check if processing $game = ModelGame::getGame($this->id_game); if ($game->checkProcessing()) { throw new ControllerException('Unable to delete moves at this moment as the game-logic is currently processing.'); } // check if move is a landmove from the current round $move_round = $move->getRound(); $round = $game->getRound(); $phase = $game->getIdPhase(); if ($phase > PHASE_PRODUCTION) { ++$round; } if ($round != $move_round) { throw new ControllerException('Unable to delete move as it is not from the correct round.'); } // delete move $move->flagMoveDeleted(); return; }
/** * checks if this move is valid, throws exception if not valid * * @param int $id_move * @throws NullPointerException * @throws ControllerException * @return boolean */ public function validateLandMoveByid($id_move) { $id_move = intval($id_move); // check if move is not already over $game = ModelGame::getGame($this->id_game); $round = $game->getRound(); $phase = $game->getIdPhase(); if ($phase > PHASE_LANDMOVE) { ++$round; } $move = ModelLandMove::getLandMove($this->id_game, $id_move); $move_round = $move->getRound(); if ($round != $move_round) { throw new ControllerException('Cannot validate move as it is not from the correct round.'); } return $this->validateLandMove($id_move, $round, $move->getSteps(), $move->getUnits()); }
/** * sets the game status (and if necessary also changes the phase) * * @param int $id_game * @param string $status - ENUM(new, started, running, done) * @return void * @throws GameAdministrationException */ public function setStatus($id_game, $status) { $id_game = intval($id_game); if (!$this->checkMod()) { throw new GameAdministrationException('User not allowed to set game status.'); } ModelGame::getGame($id_game)->setStatus($status); }
} $app->render('map.twig', $data); }); $app->get('/cron(/:id_game)(/)', function ($id_game = null) use($app, $debug, $logger) { echo '<pre>'; if (empty($id_game)) { $id_game = null; $logger->debug('running cron-calculation for all applicable games'); } else { if ($id_game !== null) { $id_game = (int) $id_game; $logger->debug('running cron-calculation for single game: ' . $id_game); } } if ($debug && $id_game !== null) { $game = ModelGame::getGame($id_game); $game->setProcessing(false); $logger->debug('debug mode - forcing reset of processing status of single game: ' . $id_game); } $cron = new CronMain(); $cron->execute($id_game); if ($cron->hasErrors()) { $msg = "cron failed for:\n "; foreach ($cron->getErrors() as $key => $error) { $msg .= $key . ' - ' . $error . "\n "; } $logger->error($msg); } else { $logger->debug('cron route successfully finished'); } });
/** * changes the color if it is free * * @param $id_color int * @throws NullPointerException * @throws GameAdministrationException * @return void */ public function setColor($id_color) { $id_color = intval($id_color); ModelColor::getModelColor($id_color); if (!ModelGame::getGame($this->id_game)->checkIfColorIsFree($id_color)) { throw new GameAdministrationException('Color already taken!'); } $this->id_color = $id_color; DataSource::Singleton()->epp('update_user_color_for_game', array(':id_user' => $this->id_user, ':id_game' => $this->id_game, ':id_color' => $this->id_color)); }