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; } }
protected function checkCreator($id_game) { if (ModelGame::getGame($id_game)->getCreator() !== ModelUser::getCurrentUser()) { throw new GameAdministrationException('not-creator.'); } return true; }
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 checkCurrentPhase(array &$data, $id_phase) { $game = ModelGame::getCurrentGame(); if ($game->getIdPhase() !== (int) $id_phase) { $data['notCurrentPhase'] = true; return true; } return false; }
public function run(array &$data) { $data['template'] = $this->getTemplate(); $this->addCurrentGameInfo($data); $this->moveController = new SetShipsController(ModelUser::getCurrentUser()->getId(), ModelGame::getCurrentGame()->getId()); // update moves if (isset($_POST['setship'])) { $this->setShip($data); } if (isset($_POST['fixate_start'])) { $this->fixateMove($data); } if (isset($_POST['delete'])) { $this->deleteMove($data); } // show already set ships $data['currentShips'] = array(); $iterator = ModelSetShipsMove::getSetShipMovesForUser(ModelUser::getCurrentUser()->getId(), ModelGame::getCurrentGame()->getId()); while ($iterator->hasNext()) { /** @var ModelSetShipsMove $move */ $move = $iterator->next(); $zShip = ModelInGameShip::getShipById(ModelGame::getCurrentGame()->getId(), $move->getIdZunit()); $id_ship = $zShip->getIdUnit(); $ship = ModelShip::getModelById($id_ship); $zAreaInPort = ModelGameArea::getGameArea(ModelGame::getCurrentGame()->getId(), $move->getIdZareaInPort()); $zAreaAtSea = ModelGameArea::getGameArea(ModelGame::getCurrentGame()->getId(), $move->getIdZarea()); $data['currentShips'][] = array('id' => $move->getId(), 'ship_type' => $ship->getName(), 'ship_name' => $zShip->getName(), 'zarea_in_port' => $zAreaInPort->getName() . ' ' . $zAreaInPort->getNumber(), 'zarea_at_sea' => $zAreaAtSea->getName() . ' ' . $zAreaAtSea->getNumber()); } // show still available ships $data['availableShips'] = array(); $stillAvailableShips = $this->moveController->getStillAvailableShips(); foreach ($stillAvailableShips as $id_unit => $count) { if ($count <= 0) { continue; } $data['availableShips'][] = array('id' => $id_unit, 'count' => $count, 'name' => ModelShip::getModelById($id_unit)->getName()); } // show available countries $data['availableZAreasInPort'] = array(); $data['availableZAreasAtSea'] = array(); $iterator = ModelGameArea::iterator(ModelUser::getCurrentUser()->getId(), ModelGame::getCurrentGame()->getId()); while ($iterator->hasNext()) { /** @var ModelGameArea $zArea */ $zArea = $iterator->next(); $data['availableZAreasInPort'][] = array('id_zarea_in_port' => $zArea->getId(), 'name' => $zArea->getName(), 'number' => $zArea->getNumber()); } $iterator = ModelArea::iterator(TYPE_SEA); while ($iterator->hasNext()) { /** @var ModelArea $area */ $area = $iterator->next(); $data['availableZAreasAtSea'][] = array('id_zarea_at_sea' => ModelGameArea::getGameAreaForArea(ModelGame::getCurrentGame()->getId(), $area->getId())->getId(), 'name' => $area->getName(), 'number' => $area->getNumber()); } $this->checkFixate($data, PHASE_SETSHIPS); $this->checkCurrentPhase($data, PHASE_SETSHIPS); }
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; }
protected function checkAuth($required_session) { $current_user = Model\User\ModelUser::getCurrentUser(); $status = $current_user->getStatus(); $current_game = Model\Game\ModelGame::getCurrentGame(); switch ($required_session) { case CHECK_SESSION_NONE: break; case CHECK_SESSION_USER: if ($status !== STATUS_USER_ACTIVE && $status !== STATUS_USER_ADMIN && $status !== STATUS_USER_MODERATOR) { throw new SessionException('No active user.'); } break; case CHECK_SESSION_ADMIN: if ($status !== STATUS_USER_ADMIN) { throw new SessionException('Non-admin tried to login at content: ' . $this->getName() . '.'); } break; case CHECK_SESSION_MOD: if ($status !== STATUS_USER_ADMIN && $status !== STATUS_USER_MODERATOR) { throw new SessionException('Non-moderator tried to login at content: ' . $this->getName() . '.'); } break; case CHECK_SESSION_GAME: if ($current_game === null) { throw new SessionException('Choose game first'); } break; case CHECK_SESSION_GAME_START: if ($current_game === null) { throw new SessionException('Choose game first'); } if ($current_game->getStatus() !== GAME_STATUS_STARTED) { throw new SessionException('Game is not in the starting phase.'); } break; case CHECK_SESSION_GAME_RUNNING: if ($current_game === null) { throw new SessionException('Choose game first'); } if ($current_game->getStatus() !== GAME_STATUS_RUNNING) { throw new SessionException('Game is not running.'); } break; default: throw new SessionException('Invalid Session Type.'); } }
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); }
public function getUserInfo($ingame) { $user = ModelUser::getUser($ingame->getIdUser()); // money on bank $money = $ingame->getMoney(); // money from resources $resproduction = 0; $combos = array(); $combos[RESOURCE_OIL] = 0; $combos[RESOURCE_TRANSPORT] = 0; $combos[RESOURCE_INDUSTRY] = 0; $combos[RESOURCE_MINERALS] = 0; $combos[RESOURCE_POPULATION] = 0; $iter = ModelGameArea::iterator($user->getId(), ModelGame::getCurrentGame()->getId()); while ($iter->hasNext()) { $area = $iter->next(); $resproduction += $area->getProductivity(); $combos[$area->getIdResource()]++; } // money from traderoutes $traderoutes = 0; // money from combos $combo_count = $combos[RESOURCE_OIL]; foreach ($combos as $res) { if ($res < $combo_count) { $combo_count = $res; } } $combo_money = $combo_count * 4; // sum $sum = $money + $resproduction + $traderoutes + $combo_money; $userData = array(); $userData['login'] = $user->getLogin(); $userData['money'] = $money; $userData['resproduction'] = $resproduction; $userData['trproduction'] = $traderoutes; $userData['comboproduction'] = $combo_money; $userData['sum'] = $sum; return $userData; }
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; }
private function handleInput(array &$data) { if (empty($_POST)) { return; } $controller = new LandMoveController(ModelUser::getCurrentUser()->getId(), ModelGame::getCurrentGame()->getId()); // fixating land move if (isset($_POST['fixate_land_move'])) { $controller->finishMove(); return; } // deleting land move if (isset($_POST['delete'])) { try { $controller->deleteLandMove(intval($_POST['delete'])); $data['status'] = array('message' => 'Landzug gelöscht.'); } catch (NullPointerException $ex) { $data['errors'] = array('message' => $ex->getMessage()); } catch (ControllerException $ex) { $data['errors'] = array('message' => $ex->getMessage()); } finally { return; } } // creating new land move if (isset($_POST['newmove'])) { try { if (!isset($_POST['start']) || !isset($_POST['destination'])) { $data['errors'] = array('message' => 'Missing parameter!'); return; } $units = array(); $iter = ModelLandUnit::iterator(); while ($iter->hasNext()) { /* @var $unit ModelLandUnit */ $unit = $iter->next(); $abbr = $unit->getAbbreviation(); $id_unit = $unit->getId(); $units[$id_unit] = isset($_POST[$abbr]) ? intval($_POST[$abbr]) : 0; } $controller->createLandMove(intval($_POST['start']), intval($_POST['destination']), $units); $data['status'] = array('message' => 'Landzug erstellt.'); } catch (NullPointerException $ex) { $data['errors'] = array('message' => $ex->getMessage()); } catch (ControllerException $ex) { $data['errors'] = array('message' => $ex->getMessage()); } } }
private function parseOptions(array &$data) { $viewData = array(); foreach ($this->possibleStartRegions as $id_option_type => $option_types) { $optionType = ModelOptionType::getOptionType($id_option_type); foreach ($option_types as $option_number => $options) { $optionViewData = array(); $optionViewData['number'] = $option_number; $optionViewData['countrySelectUnitCount'] = $optionType->getUnits(); $optionViewData['countrySelectCount'] = $optionType->getCountries(); $optionViewData['areas'] = array(); foreach ($options as $id_area => $startRegion) { // get area infos $areaModel = ModelArea::getArea($id_area); $area = array(); $area['id_area'] = $id_area; $area['number'] = $areaModel->getNumber(); $area['name'] = $areaModel->getName(); // check if country already selected $gameArea = ModelGameArea::getGameAreaForArea(ModelGame::getCurrentGame()->getId(), $id_area); $id_zarea = $gameArea->getId(); $modelMove = ModelSelectStartMove::getSelectStartMoveForUser(ModelUser::getCurrentUser()->getId(), ModelGame::getCurrentGame()->getId()); if ($modelMove->checkIfAreaIsSelected($option_number, $id_zarea)) { $area['checked'] = true; } $optionViewData['areas'][] = $area; } $viewData[] = $optionViewData; } } $data['options'] = $viewData; }
/** * 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()); }
/** * 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; }
public function run(array &$data) { $game = ModelGame::getCurrentGame(); $id_game = $game->getId(); SQLCommands::init($id_game); // running game (or newly started but countries are already picked) if ($game->getStatus() === GAME_STATUS_RUNNING || $game->getStatus() === GAME_STATUS_STARTED && $game->getIdPhase() === PHASE_SETSHIPS) { $query = 'get_map_for_running_game'; } else { if ($game->getStatus() === GAME_STATUS_STARTED && $game->getIdPhase() === PHASE_SELECTSTART) { $query = 'get_map_for_new_game'; } else { throw new MapException('invalid game selected: ' . $id_game); } } $result = DataSource::getInstance()->epp($query); $countryData = array(); foreach ($result as $country) { // newly started game countries have to be picked -> no landunits/ships available if (array_key_exists('countrySelectOption', $country)) { $countryData[] = $country; continue; } // running game (or newly started but countries are already picked) // check landunits $unitCount = 0; $id_user = (int) $country['id_user']; if ($id_user <= 0) { $id_user = NEUTRAL_COUNTRY; } $units = ModelInGameLandUnit::getUnitsByIdZAreaUser($id_game, (int) $country['id'], $id_user); $unitsViewData = array(); /* @var $unit ModelInGameLandUnit */ foreach ($units as $unit) { $unitCount += $unit->getCount(); $landUnit = ModelLandUnit::getModelById($unit->getIdUnit()); $unitViewData = array('name' => $landUnit->getName(), 'count' => $unit->getCount()); $unitsViewData[] = $unitViewData; } if ($unitCount > 0) { $country['units'] = $unitsViewData; } $country['unitCount'] = $unitCount; // check ships $shipCount = 0; $shipViewData = array(); if ((int) $country['area_type'] === TYPE_LAND) { $ships = ModelInGameShip::getShipsInPort($id_game, (int) $country['id_zarea']); } else { $ships = ModelInGameShip::getShipsInAreaNotInPort($id_game, (int) $country['id_zarea']); } while ($ships->hasNext()) { /* @var $ship ModelInGameShip */ $ship = $ships->next(); $id_ship_owner = $ship->getIdUser(); if (!isset($shipViewData[$id_ship_owner])) { $shipViewData[$id_ship_owner] = array('username' => ModelUser::getUser($id_ship_owner)->getLogin(), 'ships' => array()); } $shipType = ModelShip::getModelById($ship->getIdUnit()); $currShipViewData = array('name' => $ship->getName(), 'type' => $shipType->getName(), 'diveStatus' => $ship->getDiveStatus(), 'experience' => $ship->getExperience()); if ((int) $country['area_type'] === TYPE_LAND) { $portToArea = ModelGameArea::getGameArea($id_game, $ship->getIdZArea()); $currShipViewData['port'] = $portToArea->getName(); $currShipViewData['portNumber'] = $portToArea->getNumber(); } $shipViewData[$id_ship_owner]['ships'][] = $currShipViewData; ++$shipCount; } if ($shipCount > 0) { $country['ships'] = $shipViewData; } $country['shipCount'] = $shipCount; $countryData[] = $country; } $data['countryData'] = $countryData; return $data; }
} $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'); } });
/** * 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; }
private function showNewMove(array &$data) { $areasViewData = array(); $areas = ModelArea::iterator(TYPE_LAND); while ($areas->hasNext()) { /* @var $area ModelArea */ $area = $areas->next(); $zArea = ModelGameArea::getGameAreaForArea(ModelGame::getCurrentGame()->getId(), $area->getId()); $areaViewData = array(); $areaViewData['id_zarea'] = $zArea->getId(); $areaViewData['number'] = $area->getNumber(); $areaViewData['name'] = $area->getName(); if ($zArea->getIdUser() === ModelUser::getCurrentUser()->getId()) { $areasViewData[] = $areaViewData; } } $data['areas'] = $areasViewData; $unitsViewData = array(); $unit_iter = ModelLandUnit::iterator(); while ($unit_iter->hasNext()) { /* @var $unit ModelLandUnit */ $unit = $unit_iter->next(); $unitsViewData[] = array('id' => $unit->getId(), 'name' => $unit->getName()); } $data['units'] = $unitsViewData; }
/** * 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); }
/** * 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)); }