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']); }
/** * try to create a new game, returns the game as model on success * * @param string $game_name * @param int $players * @param string $password1 * @param string $password2 * @param bool $creator_joins * @param int $id_color * @throws GameCreationException * @return ModelGame * @throws JoinUserException */ public function create($game_name, $players, $password1, $password2, $creator_joins, $id_color) { $id_color = intval($id_color); $players = intval($players); if (empty($game_name) || empty($players)) { throw new GameCreationException('Fill in name and players.'); } if (!preg_match("/^([a-zA-Z0-9]+[a-zA-Z0-9' -]+[a-zA-Z0-9']+)?\$/", $game_name)) { throw new GameCreationException('Invalid name, only use those letters: a-Z 0-9 \'-'); } if (!preg_match("/[2-6]{1}/", $players)) { throw new GameCreationException('Invalid number of players.'); } if ($password1 !== $password2) { throw new GameCreationException('Passwords have to match.'); } if (!preg_match("/^([a-zA-Z0-9\$%'-]{5,})?\$/", $password1)) { throw new GameCreationException('Invalid password. At least 5 of the following letters: a-Z 0-9 $%\'-'); } $game = ModelGame::createGame($game_name, $players, ModelUser::getCurrentUser()->getId(), $password1); // join user if ($creator_joins) { ModelIsInGameInfo::joinGame(ModelUser::getCurrentUser()->getId(), $game->getId(), $id_color); } return $game; }
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); }
private function joinGame(array &$data) { $interaction = new UserGameInteraction(ModelUser::getCurrentUser()->getId()); $password = isset($_POST['password']) ? $_POST['password'] : null; $id_color = isset($_POST['color']) ? intval($_POST['color']) : null; try { $interaction->join($id_color, $this->id_game, $password); $data['status'] = array('message' => 'Game successfully joined.'); } catch (JoinUserException $ex) { $data['errors'] = array('message' => $ex->getMessage()); } }
/** * trys to leave the given game, returns true on success * * @param int $id_game * @throws ControllerException, NullPointerException * @return boolean */ public function leaveGame($id_game) { $game = ModelGame::getGame($id_game); if ($game->getStatus() !== GAME_STATUS_NEW) { throw new ControllerException('Can\'t leave game. It has allready started.'); } if ($game->checkProcessing()) { throw new ControllerException('Can\'t leave game. It has allready started.'); } ModelIsInGameInfo::leaveGame(ModelUser::getCurrentUser()->getId(), $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 createGame(array &$data) { if (!isset($_POST['name']) || empty($_POST['name'])) { throw new GameCreationException('Missing game name.'); } if (!isset($_POST['players'])) { throw new GameCreationException('Missing players.'); } if (!isset($_POST['password1'])) { $_POST['password1'] = ''; } if (!isset($_POST['password2'])) { $_POST['password2'] = ''; } if (!isset($_POST['color'])) { throw new GameCreationException('Missing color.'); } $creator_joins = isset($_POST['play']); $gamesModeration = new GamesModeration(ModelUser::getCurrentUser()->getId()); $gamesModeration->create($_POST['name'], $_POST['players'], $_POST['password1'], $_POST['password2'], $creator_joins, $_POST['color']); $data['status'] = array('message' => 'Game successfully created.'); return $data; }
private function do_creator_action(array &$data) { $gamesModeration = new GamesModeration(ModelUser::getCurrentUser()->getId()); if (isset($_POST['kick'])) { $gamesModeration->kickUser(intval($_POST['kick']), $this->id_game); $data['status'] = array('message' => 'User successfully kicked.'); return; } if (isset($_POST['change_pw'])) { $gamesModeration->changePassword($_POST['password1'], $_POST['password2'], $this->id_game); $data['status'] = array('message' => 'Password successfully changed.'); return; } if (isset($_POST['start'])) { $gamesModeration->startGame($this->id_game); $data['status'] = array('message' => 'Game successfully started.'); return; } if (isset($_POST['delete_affirmed'])) { $gamesModeration->deleteGame($this->id_game); $data['status'] = array('message' => 'Game successfully deleted.'); return; } }
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; }
<?php namespace AttOn; use Logger; use Slim\Slim; /* @var $app Slim */ /* @var $debug bool */ global $app, $debug; $app->post('/login/', function () use($app, $debug) { // logout user if logged in Controller\User\UserActions::logout(); try { $username = isset($_POST['username']) ? $_POST['username'] : ''; $password = isset($_POST['password']) ? $_POST['password'] : ''; $remember = isset($_POST['remember']) ? true : false; Controller\User\UserActions::login($username, $password, $remember); // successfully logged in, redirect to main route $app->redirect(ABS_REF_PREFIX); } catch (Exceptions\LoginException $ex) { $user = Model\User\ModelUser::getCurrentUser(); $data = array(); $data['user'] = $user->getViewData(); $data['errors'] = array('login' => $ex->getMessage()); if (isset($_POST['username']) && !empty($_POST['username'])) { $data['user']['username'] = $_POST['username']; } $data['template'] = 'login'; $app->render('main.twig', $data); } });
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; }
protected function checkFixate(array &$data, $id_phase) { $igpi = ModelInGamePhaseInfo::getInGamePhaseInfo(ModelUser::getCurrentUser()->getId(), ModelGame::getCurrentGame()->getId()); return $data['turnFixated'] = $igpi->getIsReadyForPhase((int) $id_phase) ? true : false; }
protected function checkInGame($id_game) { return ModelIsInGameInfo::isUserInGame(ModelUser::getCurrentUser()->getId(), $id_game); }