Ejemplo n.º 1
0
 private function parseCreationForm(array &$data)
 {
     $game = array();
     if (isset($_POST['name']) && preg_match("/^([a-zA-Z0-9]+[a-zA-Z0-9' -]+[a-zA-Z0-9']+)?\$/", $_POST['name'])) {
         $game['name'] = $_POST['name'];
     }
     if (isset($_POST['players']) && preg_match("/[2-6]{1}/", $_POST['players'])) {
         $game['players'] = $_POST['players'];
     }
     $data['game'] = $game;
     // colors
     $colors = array();
     $iter = ModelColor::iterator();
     while ($iter->hasNext()) {
         $_Color = $iter->next();
         $color = array();
         $color['id'] = $_Color->getId();
         $color['name'] = $_Color->getName();
         $color['color'] = $_Color->getColor();
         $colors[] = $color;
     }
     $data['colors'] = $colors;
     return $data;
 }
Ejemplo n.º 2
0
 /**
  * @return array(int id => dict(id = int, color = string))
  */
 public function getFreeColors()
 {
     $colors_taken = array();
     $output = array();
     // array with taken colors
     $iter = ModelIsInGameInfo::iterator(null, $this->id);
     while ($iter->hasNext()) {
         $colors_taken[] = $iter->next()->getIdColor();
     }
     $iter = ModelColor::iterator();
     while ($iter->hasNext()) {
         $_Color = $iter->next();
         if (in_array($_Color->getId(), $colors_taken)) {
             continue;
         }
         $output[$_Color->getId()] = array('id' => $_Color->getId(), 'color' => $_Color->getName());
     }
     return $output;
 }
Ejemplo n.º 3
0
 /**
  * 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);
 }