private function showGame(array &$data) { $gameInfo = array(); $gameInfo['name'] = $this->game->getName(); $gameInfo['creator'] = $this->game->getCreator()->getLogin(); $gameInfo['id'] = $this->game->getId(); $gameInfo['status'] = $this->game->getStatus(); $data['game'] = $gameInfo; if (isset($_POST['delete'])) { $data['delete'] = true; } if (ModelUser::getCurrentUser() === $this->game->getCreator()) { $data['isCreator'] = true; } $players = array(); $iter = ModelUser::iterator(STATUS_USER_ALL, $this->game->getId()); while ($iter->hasNext()) { $user = $iter->next(); $player = array(); $player['login'] = $user->getLogin(); $player['color'] = ModelIsInGameInfo::getIsInGameInfo($user->getId(), $this->game->getId())->getColor()->getName(); $player['id'] = $user->getUserId(); $players[] = $player; } $data['game']['player'] = $players; }
private function parseGame(array &$data) { $gameinfo = array(); $gameinfo['name'] = $this->game->getName(); $gameinfo['creator'] = $this->game->getCreator()->getLogin(); $gameinfo['id'] = $this->game->getId(); $gameinfo['passwordProtected'] = $this->game->checkPasswordProtection(); $players = array(); $iter_players = ModelUser::iterator(STATUS_USER_ALL, $this->id_game); while ($iter_players->hasNext()) { $players[] = $iter_players->next()->getLogin(); } $gameinfo['players'] = $players; $gameinfo['availColors'] = $this->game->getFreeColors(); $data['game'] = $gameinfo; }
/** * checks if the user is in the given game * * @param int $id_game * @return bool */ public function checkIfUserIsInThisGame($id_game) { $iter = ModelUser::iterator($id_game); while ($iter->hasNext()) { if ($iter->next() == $this) { return true; } } return false; }
/** * joins the user in given game * * @param $id_user int * @param $id_game int * @param $id_color int * @throws NullPointerException * @throws JoinUserException * @return ModelIsInGameInfo */ public static function joinGame($id_user, $id_game, $id_color = null) { $id_user = intval($id_user); $id_game = intval($id_game); if ($id_color !== null) { $id_color = intval($id_color); } // check if user and game exist $user = ModelUser::getUser($id_user); $game = ModelGame::getGame($id_game); if ($game->getFreeSlots() <= 0) { throw new JoinUserException('Game is full.'); } // check if user is in the game $iter = ModelUser::iterator($id_game); while ($iter->hasNext()) { if ($iter->next() === $user) { throw new JoinUserException('User allready in this game.'); } } // check if color is free if ($id_color !== null && !$game->checkIfColorIsFree($id_color)) { $id_color = null; } // get first free color if ($id_color === null) { $color_iter = ModelColor::iterator(); while ($color_iter->hasNext()) { $color = $color_iter->next(); if (!$game->checkIfColorIsFree($color->getId())) { continue; } $id_color = $color->getId(); break; } } if ($id_color === null) { throw new JoinUserException('No free color found.'); } // insert player $dict = array(); $dict[':id_user'] = $id_user; $dict[':id_game'] = $id_game; $dict[':id_color'] = $id_color; DataSource::Singleton()->epp('join_game', $dict); // set user notification rules ModelInGamePhaseInfo::getInGamePhaseInfo($id_user, $id_game); return self::getIsInGameInfo($id_user, $id_game); }