Example #1
0
 /** protected function _pull
  *		Pulls the data from the database
  *		and sets up the objects
  *
  * @param void
  * @action pulls the game data
  * @return void
  */
 protected function _pull()
 {
     call(__METHOD__);
     if (!$this->id) {
         return false;
     }
     if (!$_SESSION['player_id']) {
         throw new MyException(__METHOD__ . ': Player id is not in session when pulling game data');
     }
     // grab the game data
     $query = "\n\t\t\tSELECT *\n\t\t\tFROM " . self::GAME_TABLE . "\n\t\t\tWHERE game_id = '{$this->id}'\n\t\t";
     $result = $this->_mysql->fetch_assoc($query);
     call($result);
     if (!$result) {
         throw new MyException(__METHOD__ . ': Game data not found for game #' . $this->id);
     }
     // set the properties
     $this->state = $result['state'];
     $this->paused = (bool) $result['paused'];
     $this->create_date = strtotime($result['create_date']);
     $this->modify_date = strtotime($result['modify_date']);
     $this->_extra_info = array_merge_plus(self::$_EXTRA_INFO_DEFAULTS, unserialize($result['extra_info']));
     $this->_quarto->small_square_matches = $this->_extra_info['small_square_matches'];
     $this->_quarto->small_square_torus = $this->_extra_info['small_square_matches'] && $this->_extra_info['small_square_torus'];
     $this->_quarto->diagonal_torus = $this->_extra_info['diagonal_torus'];
     // set up the players
     $this->_players['white']['player_id'] = $result['white_id'];
     $this->_players['white']['object'] = new GamePlayer($result['white_id']);
     $this->_players['black']['player_id'] = $result['black_id'];
     if (0 != $result['black_id']) {
         // we may have an open game
         $this->_players['black']['object'] = new GamePlayer($result['black_id']);
     }
     // we test this first one against the black id, so if it fails because
     // the person viewing the game is not playing in the game (viewing it
     // after it's finished) we want "player" to be equal to "white"
     if ($_SESSION['player_id'] == $result['black_id']) {
         $this->_players['player'] =& $this->_players['black'];
         $this->_players['player']['color'] = 'black';
         $this->_players['player']['opp_color'] = 'white';
         $this->_players['opponent'] =& $this->_players['white'];
         $this->_players['opponent']['color'] = 'white';
         $this->_players['opponent']['opp_color'] = 'black';
     } else {
         $this->_players['player'] =& $this->_players['white'];
         $this->_players['player']['color'] = 'white';
         $this->_players['player']['opp_color'] = 'black';
         $this->_players['opponent'] =& $this->_players['black'];
         $this->_players['opponent']['color'] = 'black';
         $this->_players['opponent']['opp_color'] = 'white';
     }
     // set up the board
     $query = "\n\t\t\tSELECT *\n\t\t\tFROM " . self::GAME_BOARD_TABLE . "\n\t\t\tWHERE game_id = '{$this->id}'\n\t\t\tORDER BY move_date DESC\n\t\t";
     $result = $this->_mysql->fetch_array($query);
     call($result);
     if ($result) {
         $this->_history = $result;
         $this->last_move = strtotime($result[0]['move_date']);
         $this->turn = 'white';
         if (0 != count($result) % 2) {
             $this->turn = 'black';
         }
         try {
             $this->_quarto->board = $this->_history[0]['board'];
         } catch (MyException $e) {
             throw $e;
         }
         if (!is_null($result[0]['next_piece'])) {
             $this->_quarto->next_piece = $result[0]['next_piece'];
         }
     } else {
         $this->last_move = $this->create_date;
         $this->turn = 'white';
     }
     $this->_players[$this->turn]['turn'] = true;
 }
Example #2
0
 /** static public function extra_info
  *		Returns the extra info merged with the default values
  *
  * @param array [optional] extra info
  * @return array extra info
  */
 public static function extra_info($extra_info = array())
 {
     $extra_info = array_clean($extra_info, array_keys(self::$EXTRA_INFO_DEFAULTS));
     return array_merge_plus(self::$EXTRA_INFO_DEFAULTS, $extra_info);
 }
Example #3
0
 /**
  * Get the list of games with parsed info
  *
  * @param int $player_id not used
  *
  * @return array games list
  */
 public static function get_list($player_id = 0)
 {
     $games = self::get_files();
     $return = array();
     foreach ($games as &$game) {
         $entry = array();
         $entry['file'] = $game;
         $entry['short_file'] = substr($game, 0, -4);
         // remove the extension
         $file = fopen($game, 'r');
         // read the first 3 sections of each file and parse it
         $n = 0;
         $section = 0;
         do {
             $line = trim(fgets($file));
             if ('=====' === substr($line, 0, 5)) {
                 ++$section;
                 $n = 0;
                 continue;
             }
             switch ($section) {
                 case 0:
                     // game info section section
                     switch ($n) {
                         case 0:
                             // game info
                             $data = explode(' - ', $line);
                             if (3 < count($data)) {
                                 $data[0] = substr($line, 0, strpos($line, ' - '));
                                 $data[1] = substr($line, strpos($line, ' - ') + 3, strrpos($line, ' - ') - 6);
                                 $data[2] = substr($line, strrpos($line, ' - ') + 3);
                             }
                             list($entry['game_id'], $entry['name'], $entry['game_type']) = $data;
                             break;
                         case 1:
                             // game start date
                             $entry['start_date'] = $line;
                             break;
                         case 2:
                             // game end date
                             $entry['end_date'] = $line;
                             break;
                         case 3:
                             // game winner
                             $winner = explode(' - ', $line, 2);
                             $entry['winner'] = $winner[0];
                             if (2 === count($winner)) {
                                 list($entry['winner_id'], $entry['winner']) = $winner;
                             }
                             break;
                         default:
                             continue;
                             break;
                     }
                     break;
                 case 1:
                     // extra info section
                     $entry['extra_info'] = array_merge_plus(self::$_EXTRA_INFO_DEFAULTS, json_decode($line, true));
                     break;
                 case 2:
                     // player data section
                     $player = array('order' => $n + 1);
                     list($player['id'], $player['color'], $player['name']) = explode(' - ', $line, 3);
                     $entry['player_data'][$player['id']] = $player;
                     break;
             }
             ++$n;
         } while (3 > $section);
         fclose($file);
         $entry['get_fortify'] = self::_get_fortify($entry['extra_info']);
         $entry['get_kamikaze'] = self::_get_kamikaze($entry['extra_info']);
         $entry['get_warmonger'] = self::_get_warmonger($entry['extra_info']);
         $entry['get_conquer_limit'] = self::_get_conquer_limit($entry['extra_info']);
         $entry['get_custom_rules'] = self::_get_custom_rules($entry['extra_info']);
         $entry['get_fog_of_war'] = self::_get_fog_of_war($entry['extra_info']);
         $entry['get_fog_of_war_armies'] = $entry['get_fog_of_war']['armies'];
         $entry['get_fog_of_war_colors'] = $entry['get_fog_of_war']['colors'];
         $entry['clean_name'] = htmlentities($entry['name'], ENT_QUOTES, 'UTF-8', false);
         $entry['clean_custom_rules'] = htmlentities($entry['get_custom_rules'], ENT_QUOTES, 'UTF-8', false);
         $entry['players'] = count($entry['player_data']);
         $return[] = $entry;
     }
     return $return;
 }
Example #4
0
 /** protected function _set_player_data
  *		Adds a player to the game and risk data
  *
  * @param array $data player data
  * @param int $count optional total player count
  *
  * @return void
  * @throws MyException
  */
 protected function _set_player_data($data, $count = null)
 {
     call(__METHOD__);
     $player = array_merge_plus(self::$_PLAYER_DEFAULTS, $data);
     if (empty($player['player_id'])) {
         throw new MyException(__METHOD__ . ': Missing player ID');
     }
     // temp fix for old serialized data
     fix_extra_info($player['extra_info']);
     $player['extra_info'] = array_merge_plus(self::$_PLAYER_EXTRA_INFO_DEFAULTS, json_decode($player['extra_info'], true));
     if (!empty($player['cards'])) {
         array_trim($player['cards'], 'int');
     } else {
         $player['cards'] = array();
     }
     $player['game_id'] = $this->id;
     // move any data we need to over to the risk class player data
     $risk_player = $player;
     $player_keys = array('player_id', 'color', 'name');
     $player = array_clean($player, $player_keys);
     $risk_player['armies'] = $this->_risk->get_start_armies($count);
     $risk_player['state'] = 'Placing';
     $risk_player_keys = array('player_id', 'order_num', 'cards', 'armies', 'state', 'extra_info');
     $risk_player = array_clean($risk_player, $risk_player_keys);
     $this->_players[$player['player_id']] = $player;
     $this->_risk->players[$player['player_id']] = $risk_player;
 }
Example #5
0
 /** static public function get_list
  *		Returns a list array of all games in the database
  *		with games which need the users attention highlighted
  *
  * @param int optional player's id
  * @return array game list (or bool false on failure)
  */
 public static function get_list($player_id = 0)
 {
     $Mysql = Mysql::get_instance();
     $player_id = (int) $player_id;
     $query = "\n\t\t\tSELECT G.*\n\t\t\t\t-- this stops the query from pulling 0 from the player table if no moves have been made\n\t\t\t\t-- or if there are no players in the game yet (don't know why that would be, but...)\n\t\t\t\t, IF((0 = MAX(GP.move_date)) OR MAX(GP.move_date) IS NULL, G.create_date, MAX(GP.move_date)) AS last_move\n\t\t\t\t, 0 AS in_game\n\t\t\t\t, 0 AS highlight\n\t\t\t\t, COUNT(DISTINCT GP.player_id) AS players\n\t\t\t\t, P.username AS hostname\n\t\t\t\t, C.username AS username\n\t\t\tFROM `" . self::GAME_TABLE . "` AS `G`\n\t\t\t\tLEFT JOIN `" . self::GAME_PLAYER_TABLE . "` AS `GP`\n\t\t\t\t\tON GP.game_id = G.game_id\n\t\t\t\tLEFT JOIN `" . self::GAME_PLAYER_TABLE . "` AS `CP`\n\t\t\t\t\tON (CP.game_id = G.game_id\n\t\t\t\t\t\tAND CP.state NOT IN ('Waiting', 'Resigned', 'Dead'))\n\t\t\t\tLEFT JOIN `" . Player::PLAYER_TABLE . "` AS `P`\n\t\t\t\t\tON P.player_id = G.host_id\n\t\t\t\tLEFT JOIN `" . Player::PLAYER_TABLE . "` AS `C`\n\t\t\t\t\tON C.player_id = CP.player_id\n\t\t\tGROUP BY game_id\n\t\t\tORDER BY state ASC\n\t\t\t\t, last_move DESC\n\t\t";
     $list = $Mysql->fetch_array($query);
     // get player's state for games they are in
     $query = "\n\t\t\tSELECT GP.game_id\n\t\t\t\t, GP.state\n\t\t\tFROM `" . self::GAME_PLAYER_TABLE . "` AS `GP`\n\t\t\t\tLEFT JOIN `" . self::GAME_TABLE . "` AS `G`\n\t\t\t\t\tUSING (game_id)\n\t\t\tWHERE GP.player_id = '{$player_id}'\n\t\t";
     $results = $Mysql->fetch_array($query);
     $states = array();
     foreach ($results as $row) {
         $states[$row['game_id']] = $row['state'];
     }
     // run though the list and add extra data
     if ($list) {
         foreach ($list as $key => $game) {
             // remove current player if the game has not started yet
             if (!in_array($game['state'], array('Playing', 'Finished'))) {
                 $game['username'] = '';
             }
             // temp fix for old serialized data
             fix_extra_info($game['extra_info']);
             $extra_info = array_merge_plus(self::$_EXTRA_INFO_DEFAULTS, json_decode($game['extra_info'], true));
             foreach ($extra_info as $field => $value) {
                 $game[$field] = $value;
             }
             $game['get_fortify'] = self::_get_fortify($extra_info);
             $game['get_kamikaze'] = self::_get_kamikaze($extra_info);
             $game['get_warmonger'] = self::_get_warmonger($extra_info);
             $game['get_conquer_limit'] = self::_get_conquer_limit($extra_info);
             $game['get_custom_rules'] = self::_get_custom_rules($extra_info);
             $game['get_fog_of_war'] = self::_get_fog_of_war($extra_info);
             $game['get_fog_of_war_armies'] = $game['get_fog_of_war']['armies'];
             $game['get_fog_of_war_colors'] = $game['get_fog_of_war']['colors'];
             $game['clean_name'] = htmlentities($game['name'], ENT_QUOTES, 'UTF-8', false);
             $game['clean_custom_rules'] = htmlentities($game['get_custom_rules'], ENT_QUOTES, 'UTF-8', false);
             $game['in_game'] = isset($states[$game['game_id']]);
             $game['highlight'] = $game['in_game'] && 'Finished' != $game['state'] && !in_array($states[$game['game_id']], array('Waiting', 'Resigned', 'Dead'));
             $list[$key] = $game;
         }
     }
     return $list;
 }
Example #6
0
 /** public function set_extra_info
  *		Sets the extra info for the game
  *
  * @param array $extra_info
  *
  * @return void
  */
 public function set_extra_info($extra_info)
 {
     call(__METHOD__);
     $extra_info = array_clean($extra_info, array_keys(self::$EXTRA_INFO_DEFAULTS));
     $this->_extra_info = array_merge_plus(self::$EXTRA_INFO_DEFAULTS, $extra_info);
     // the update trade value function depends on the extra info
     $this->_update_trade_value($log = false);
     if ('none' != $this->_extra_info['conquer_type']) {
         // the conquer limit calculation depends on the trade value info and extra info
         $this->calculate_conquer_limit();
     }
 }
Example #7
0
 /** static public function get_invites
  *		Returns a list array of all the invites in the database
  *		for the given player
  *
  * @param int player's id
  * @return 2D array invite list
  */
 public static function get_invites($player_id)
 {
     call(__METHOD__);
     $Mysql = Mysql::get_instance();
     $player_id = (int) $player_id;
     $query = "\n\t\t\tSELECT G.*\n\t\t\t\t, DATE_ADD(NOW( ), INTERVAL -1 DAY) AS resend_limit\n\t\t\t\t, R.username AS invitor\n\t\t\t\t, E.username AS invitee\n\t\t\t\t, S.name AS setup\n\t\t\tFROM " . self::GAME_TABLE . " AS G\n\t\t\t\tLEFT JOIN " . Setup::SETUP_TABLE . " AS S\n\t\t\t\t\tON S.setup_id = G.setup_id\n\t\t\t\tLEFT JOIN " . Player::PLAYER_TABLE . " AS R\n\t\t\t\t\tON R.player_id = G.white_id\n\t\t\t\tLEFT JOIN " . Player::PLAYER_TABLE . " AS E\n\t\t\t\t\tON E.player_id = G.black_id\n\t\t\tWHERE G.state = 'Waiting'\n\t\t\t\tAND (G.white_id = {$player_id}\n\t\t\t\t\tOR G.black_id = {$player_id}\n\t\t\t\t\tOR G.black_id IS NULL\n\t\t\t\t\tOR G.black_id = FALSE\n\t\t\t\t)\n\t\t\tORDER BY G.create_date DESC\n\t\t";
     $list = $Mysql->fetch_array($query);
     call($list);
     $in_vites = $out_vites = $open_vites = array();
     foreach ($list as $item) {
         $extra_info = array_merge_plus(self::$_EXTRA_INFO_DEFAULTS, unserialize($item['extra_info']));
         $white_color = 'random' == $extra_info['white_color'] ? 'Random' : ('white' == $extra_info['white_color'] ? 'Silver' : 'Red');
         $black_color = 'random' == $extra_info['white_color'] ? 'Random' : ('white' == $extra_info['white_color'] ? 'Red' : 'Silver');
         $hover = array();
         if (!empty($item['extra_info'])) {
             $hover = unserialize($item['extra_info']);
             unset($hover['invite_setup']);
             unset($hover['white_color']);
         }
         $hover_text = array();
         foreach ($hover as $key => $value) {
             if (is_bool($value)) {
                 $value = $value ? 'Yes' : 'No';
             }
             $hover_text[] = humanize($key) . ': ' . $value;
         }
         $item['hover_text'] = implode(' | ', $hover_text);
         $item['board'] = 'setup_display';
         if (!empty($extra_info['invite_setup'])) {
             $item['board'] = expandFEN($extra_info['invite_setup']);
         }
         if ($player_id == $item['white_id']) {
             $item['color'] = $white_color;
             $out_vites[] = $item;
         } elseif ($player_id == $item['black_id']) {
             $item['color'] = $black_color;
             $in_vites[] = $item;
         } else {
             $item['color'] = $black_color;
             $open_vites[] = $item;
         }
     }
     return array($in_vites, $out_vites, $open_vites);
 }