Пример #1
0
 protected function run()
 {
     if ($this->check == self::OK) {
         $cardBaseTypeRepository = new CardBaseTypeRepository();
         $validCardBaseTypes = $cardBaseTypeRepository->getByValid(1);
         $validCardBaseTypesIdList = array();
         foreach ($validCardBaseTypes as $cardBaseType) {
             $validCardBaseTypesIdList[] = $cardBaseType['id'];
         }
         $cardRepository = new CardRepository();
         $where = array('column' => 'card_base_type', 'value' => $validCardBaseTypesIdList);
         $cardRepository->addAdditionalWhere($where);
         $gameSets = unserialize($this->room['game_sets']);
         if ($gameSets) {
             $where = array('column' => 'game_set', 'value' => $gameSets);
             $cardRepository->addAdditionalWhere($where);
         }
         $cards = $cardRepository->getCardIds();
         shuffle($cards);
         // TODO prepare High noon draw pile
         // TODO - other extensions
         $players = $this->players;
         foreach ($players as $player) {
             $playerCards = array();
             $character = $player->getAdditionalField('character');
             $role = $player->getAdditionalField('role');
             $player['actual_lifes'] = $character['lifes'];
             $cardsCount = $player->getIsBigSpencer() ? 5 : $player['actual_lifes'];
             for ($i = 0; $i < $cardsCount; $i++) {
                 $playerCards[] = array_pop($cards);
             }
             if ($role['type'] == Role::SHERIFF) {
                 $player['phase'] = Player::PHASE_DRAW;
                 $player['actual_lifes'] = $player['actual_lifes'] + 1;
             }
             $player['max_lifes'] = $player['actual_lifes'];
             $player['hand_cards'] = serialize($playerCards);
             $player['table_cards'] = serialize(array());
             $player['wait_cards'] = serialize(array());
             $player->save();
         }
         $this->game['draw_pile'] = serialize($cards);
         $this->game['throw_pile'] = serialize(array());
         // musime ulozit hru lebo hracom sa zmenili charaktery
         $this->game = $this->game->save(TRUE);
         $this->game = GameUtils::changePositions($this->game);
         foreach ($this->game->getPlayers() as $player) {
             if ($player['role'] == Role::SHERIFF) {
                 $this->game['turn'] = $player['id'];
                 break;
             }
         }
         $this->game['game_start'] = time();
         $this->game['status'] = Game::GAME_STATUS_STARTED;
         $matrix = GameUtils::countMatrix($this->game);
         $this->game['distance_matrix'] = serialize($matrix);
         $this->game = $this->game->save(TRUE);
     }
 }
Пример #2
0
 protected function removePlayerFromGame()
 {
     $this->actualPlayer['actual_lifes'] = 0;
     $this->actualPlayer['position'] = 0;
     $this->actualPlayer['phase'] = 0;
     $this->actualPlayer = $this->actualPlayer->save(TRUE);
     // TODO message ze hrac zomrel
     // ak je v hre Vera Custer tak moze mat jeden z tychto charakterov
     // preto su vsetky premenne array a nie len Player
     $vultureSams = array();
     $gregDiggers = array();
     $herbHunters = array();
     foreach ($this->getPlayers() as $player) {
         // pozrieme sa na vsetkych hracov ktori este nie su mrtvi a ani nie su aktualny hrac (bohvie ako je on ulozeny v $this->players :)
         if ($player['actual_lifes'] > 0 && $this->actualPlayer['id'] != $player['id']) {
             // najprv pozrieme ci hrac nie je vera custer s charakterom zabiteho hraca, ak ano, vera uz nemoze mat jeho vlastnost
             if ($player->getIsVeraCuster($this->game)) {
                 $notices = $player->getNoticeList();
                 $actualPlayerCharacter = $this->actualPlayer->getCharacter();
                 if (isset($notices['selected_character']) && $notices['selected_character'] == $actualPlayerCharacter['id']) {
                     unset($notices['selected_character']);
                 }
                 $player->setNoticeList($notices);
                 $player->save();
             }
             if ($player->getIsVultureSam($this->game)) {
                 $vultureSams[] = $player;
             } elseif ($player->getIsGregDigger($this->game)) {
                 $gregDiggers[] = $player;
             } elseif ($player->getIsHerbHunter($this->game)) {
                 $herbHunters[] = $player;
             }
         }
     }
     // pridame vsetkym gregom diggerom 2 zivoty (resp. tolko kolko potrebuju)
     if ($gregDiggers) {
         foreach ($gregDiggers as $gregDigger) {
             $newLifes = min($gregDigger['actual_lifes'] + 2, $gregDigger['max_lifes']);
             $gregDigger['actual_lifes'] = $newLifes;
             $gregDigger->save();
         }
     }
     // potiahneme pre kazdeho herba huntera 2 karty
     if ($herbHunters) {
         foreach ($herbHunters as $herbHunter) {
             $drawnCards = GameUtils::drawCards($this->game, 2);
             $handCards = unserialize($herbHunter['hand_cards']);
             foreach ($drawnCards as $card) {
                 $handCards[] = $card;
             }
             $herbHunter['hand_cards'] = serialize($handCards);
             $herbHunter->save();
         }
     }
     if ($vultureSams) {
         if (count($vultureSams) == 1) {
             $vultureSamPlayer = $vultureSams[0];
             $retVal = GameUtils::moveCards($this->game, $this->actualPlayer->getHandCards(), $this->actualPlayer, 'hand', $vultureSamPlayer, 'hand');
             $vultureSamPlayer = $retVal['playerTo'];
             $this->actualPlayer = $retVal['playerFrom'];
             $retVal = GameUtils::moveCards($this->game, $this->actualPlayer->getTableCards(), $this->actualPlayer, 'hand', $vultureSamPlayer, 'table');
             $vultureSamPlayer = $retVal['playerTo'];
             $this->actualPlayer = $retVal['playerFrom'];
             $retVal = GameUtils::moveCards($this->game, $this->actualPlayer->getWaitCards(), $this->actualPlayer, 'hand', $vultureSamPlayer, 'wait');
             $vultureSamPlayer = $retVal['playerTo'];
             $this->actualPlayer = $retVal['playerFrom'];
         } else {
             throw new Exception("More than one Vulture Sam in a game", 1352146582);
         }
     } else {
         $retVal = GameUtils::throwCards($this->game, $this->actualPlayer, $this->actualPlayer->getHandCards(), 'hand');
         $this->game = $retVal['game'];
         $this->actualPlayer = $retVal['player'];
         $retVal = GameUtils::throwCards($this->game, $this->actualPlayer, $this->actualPlayer->getTableCards(), 'table');
         $this->game = $retVal['game'];
         $this->actualPlayer = $retVal['player'];
         $retVal = GameUtils::throwCards($this->game, $this->actualPlayer, $this->actualPlayer->getWaitCards(), 'wait');
         $this->game = $retVal['game'];
         $this->actualPlayer = $retVal['player'];
     }
     // znovunacitame game z databazy, lebo sa par veci zmenilo medzitym
     $gameRepository = new GameRepository();
     $this->game = $gameRepository->getOneById($this->game['id']);
     // TODO po zmene positions sa pravdepodobne zmeni aj pozicia hraca ktory
     // je na tahu, treba to tu na tomto mieste znovu preratat a nastavit game[position]
     // na poziciu hraca s ideckom ktore ma attacking player a rovnako aj inter_turn bude treba preratat
     $this->game = GameUtils::changePositions($this->game);
     $matrix = GameUtils::countMatrix($this->game);
     $this->game['distance_matrix'] = serialize($matrix);
     $this->game = $this->game->save(TRUE);
     // najst hraca ktory ma fazu != 0 a nastavit ho v hre ako hraca ktory je na tahu
     // znovu nacitame z databazy utociaceho hraca ( pre istotu )
     $attackingPlayerId = $this->interTurnReason['from'];
     $playerRepository = new PlayerRepository();
     $this->attackingPlayer = $playerRepository->getOneById($attackingPlayerId);
     $playerRepository = new PlayerRepository();
     $role = $this->actualPlayer->getRoleObject();
     if ($role['type'] == Role::BANDIT) {
         if ($playerRepository->getCountLivePlayersWithRoles($this->game['id'], array(Role::ROLE_BANDIT_1, Role::ROLE_BANDIT_2, Role::ROLE_BANDIT_3, Role::ROLE_RENEGARD_1, Role::ROLE_RENEGARD_2)) == 0) {
             $this->endGame(array(Role::ROLE_SHERIFF, Role::ROLE_VICE_1, Role::ROLE_VICE_2));
         } else {
             // TODO doplnit pocty kariet ak su ine pre rozne charaktery utociacich hracov
             // TODO doplnit podmienky pre typy utokov ktorych sa tieto tahania tykaju - indiani tam myslim nepatria
             // TODO message o tom ze si tento hrac potiahol 3 karty za banditu
             // za banditu dostane utocnik 3 karty - ale len ak slo o priamy utok
             if ($this->attackingPlayer) {
                 $drawnCards = GameUtils::drawCards($this->game, 3);
                 $handCards = unserialize($this->attackingPlayer['hand_cards']);
                 foreach ($drawnCards as $card) {
                     $handCards[] = $card;
                 }
                 $this->attackingPlayer['hand_cards'] = serialize($handCards);
                 $this->attackingPlayer = $this->attackingPlayer->save(TRUE);
             }
         }
     } elseif ($role['type'] == Role::SHERIFF) {
         if ($playerRepository->getCountLivePlayersWithRoles($this->game['id']) == 1) {
             if ($playerRepository->getCountLivePlayersWithRoles($this->game['id'], array(Role::ROLE_RENEGARD_1)) == 1) {
                 $this->endGame(array(Role::ROLE_RENEGARD_1));
             } elseif ($playerRepository->getCountLivePlayersWithRoles($this->game['id'], array(Role::ROLE_RENEGARD_1)) == 1) {
                 $this->endGame(array(Role::ROLE_RENEGARD_2));
             } else {
                 $this->endGame(array(Role::ROLE_BANDIT_1, Role::ROLE_BANDIT_2, Role::ROLE_BANDIT_3));
             }
         } else {
             $this->endGame(array(Role::ROLE_BANDIT_1, Role::ROLE_BANDIT_2, Role::ROLE_BANDIT_3));
         }
     } elseif ($role['type'] == Role::RENEGARD) {
         if ($playerRepository->getCountLivePlayersWithRoles($this->game['id'], array(Role::ROLE_BANDIT_1, Role::ROLE_BANDIT_2, Role::ROLE_BANDIT_3, Role::ROLE_RENEGARD_1, Role::ROLE_RENEGARD_2)) == 0) {
             $this->endGame(array(Role::ROLE_SHERIFF, Role::ROLE_VICE_1, Role::ROLE_VICE_2));
         }
     } elseif ($role['type'] == Role::VICE) {
         if ($this->attackingPlayer) {
             $attackingRole = $this->attackingPlayer->getRoleObject();
             if ($attackingRole['type'] == Role::SHERIFF) {
                 $retVal = GameUtils::throwCards($this->game, $this->attackingPlayer, $this->attackingPlayer->getHandCards(), 'hand');
                 $this->game = $retVal['game'];
                 $this->attackingPlayer = $retVal['player'];
                 $retVal = GameUtils::throwCards($this->game, $this->attackingPlayer, $this->attackingPlayer->getTableCards(), 'table');
                 $this->game = $retVal['game'];
                 $this->attackingPlayer = $retVal['player'];
                 $retVal = GameUtils::throwCards($this->game, $this->attackingPlayer, $this->attackingPlayer->getWaitCards(), 'wait');
                 $this->game = $retVal['game'];
                 $this->attackingPlayer = $retVal['player'];
                 // kedze je mozne ze rusime nejaku modru kartu ktora ovplyvnuje vzdialenost, preratame maticu
                 // ak to bude velmi pomale, budeme to robit len ak je medzi zrusenymi kartami fakt takato karta
                 $matrix = GameUtils::countMatrix($this->game);
                 $this->game['distance_matrix'] = serialize($matrix);
                 $this->game->save();
             }
         }
     }
 }