/** 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; }
/** 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; }