call($_POST); // make sure this user is not full if ($GLOBALS['Player']->max_games && $GLOBALS['Player']->max_games <= $GLOBALS['Player']->current_games) { Flash::store('You have reached your maximum allowed games !', false); } test_token(); try { Game::invite(); Flash::store('Invitation Sent Successfully', true); } catch (MyException $e) { Flash::store('Invitation FAILED !', false); } } // grab the full list of players $players_full = GamePlayer::get_list(true); $invite_players = array_shrink($players_full, 'player_id'); $invite_players = ife($invite_players, array(), false); // grab the players who's max game count has been reached $players_maxed = GamePlayer::get_maxed(); $players_maxed[] = $_SESSION['player_id']; // remove the maxed players from the invite list $players = array_diff($invite_players, $players_maxed); $opponent_selection = ''; $opponent_selection .= '<option value="">-- Open --</option>'; foreach ($players_full as $player) { if ($_SESSION['player_id'] == $player['player_id']) { continue; } if (in_array($player['player_id'], $players)) { $opponent_selection .= ' <option value="' . $player['player_id'] . '">' . $player['username'] . '</option>';
function arrayShrink($array, $key) { return array_shrink($array, $key); }
/** protected function _save * Saves all changed data to the database * * @param void * * @action saves the game data * * @return void * @throws MyException */ protected function _save() { call(__METHOD__); if (!$this->id) { return; } $Mysql = Mysql::get_instance(); // send an email if we have to if ($this->_risk->new_player) { Email::send('turn', $this->_risk->current_player, array('game_id' => $this->id, 'name' => $this->name)); } // make sure we don't have a MySQL error here, it may be causing the issues $run_once = false; do { if ($run_once) { // pause for 3 seconds, then try again sleep(3); } // update the game data $query = "\n\t\t\t\tSELECT extra_info\n\t\t\t\t\t, state\n\t\t\t\t\t, modify_date\n\t\t\t\tFROM " . self::GAME_TABLE . "\n\t\t\t\tWHERE game_id = '{$this->id}'\n\t\t\t"; $game = $Mysql->fetch_assoc($query); // make sure we don't have a MySQL error here, it may be causing the issues $error = $Mysql->error; $errno = preg_replace('/(\\d+)/', '$1', $error); $run_once = true; } while (2006 == $errno || 2013 == $errno); $update_modified = false; if (!$game) { throw new MyException(__METHOD__ . ': Game data not found for game #' . $this->id); } // test the modified date and make sure we still have valid data call($this->modify_date); call(strtotime($game['modify_date'])); if ($this->modify_date != strtotime($game['modify_date'])) { $this->_log('== FAILED == DATA SAVE: #' . $this->id . ' @ ' . time() . "\n" . ' - ' . $this->modify_date . "\n" . ' - ' . strtotime($game['modify_date'])); throw new MyException(__METHOD__ . ': Trying to save game (#' . $this->id . ') with out of sync data'); } $update_game = false; if ($game['state'] != $this->state) { $update_game['state'] = $this->state; } $update_game['extra_info'] = array_diff_recursive($this->_extra_info, self::$_EXTRA_INFO_DEFAULTS); ksort($update_game['extra_info']); $update_game['extra_info'] = json_encode($update_game['extra_info']); if ('[]' == $update_game['extra_info']) { $update_game['extra_info'] = null; } if (0 === strcmp($game['extra_info'], $update_game['extra_info'])) { unset($update_game['extra_info']); } if ($update_game) { $update_modified = true; $Mysql->insert(self::GAME_TABLE, $update_game, " WHERE game_id = '{$this->id}' "); } // update the player's data $query = "\n\t\t\tSELECT *\n\t\t\tFROM " . self::GAME_PLAYER_TABLE . "\n\t\t\tWHERE game_id = '{$this->id}'\n\t\t"; $db_players = $Mysql->fetch_array($query); // add missing players $db_player_ids = array_shrink($db_players, 'player_id'); if (!$db_player_ids) { $db_player_ids = array(); } $game_player_ids = array_keys($this->_players); $new_players = array_diff($game_player_ids, $db_player_ids); foreach ($new_players as $new_player_id) { $update_player = array('game_id' => $this->id, 'player_id' => $new_player_id, 'color' => $this->_players[$new_player_id]['color'], 'order_num' => $this->_risk->players[$new_player_id]['order_num'], 'cards' => $this->_risk->players[$new_player_id]['cards'], 'armies' => $this->_risk->players[$new_player_id]['armies'], 'state' => $this->_risk->players[$new_player_id]['state'], 'move_date' => null); $update_player['cards'] = implode(',', $update_player['cards']); $update_player['extra_info'] = array_diff_assoc($this->_risk->players[$new_player_id]['extra_info'], self::$_PLAYER_EXTRA_INFO_DEFAULTS); ksort($update_player['extra_info']); $update_player['extra_info'] = json_encode($update_player['extra_info']); if ('[]' == $update_player['extra_info']) { $update_player['extra_info'] = null; } call($update_player); $Mysql->insert(self::GAME_PLAYER_TABLE, $update_player); $update_modified = true; } // check the player parts foreach ($db_players as $db_player) { $update_player = array(); $player_id = $db_player['player_id']; $risk_player = $this->_risk->players[$player_id]; array_trim($db_player['cards'], 'int'); foreach ($db_player['cards'] as $key => $card_id) { if (0 === $card_id) { unset($db_player['cards'][$key]); } } sort($db_player['cards']); $cards = $risk_player['cards']; sort($cards); if (count($db_player['cards']) != count($cards) || array_diff($db_player['cards'], $cards)) { $update_player['cards'] = implode(',', $cards); } if ($db_player['armies'] != $risk_player['armies']) { $update_player['armies'] = (int) $risk_player['armies']; } if ($db_player['state'] != $risk_player['state']) { $update_player['state'] = $risk_player['state']; } if ($db_player['order_num'] != $risk_player['order_num']) { $update_player['order_num'] = $risk_player['order_num']; } $risk_player['extra_info'] = array_diff_assoc($risk_player['extra_info'], self::$_PLAYER_EXTRA_INFO_DEFAULTS); ksort($risk_player['extra_info']); $risk_player['extra_info'] = json_encode($risk_player['extra_info']); if ('[]' == $risk_player['extra_info']) { $risk_player['extra_info'] = null; } if (0 !== strcmp($db_player['extra_info'], $risk_player['extra_info'])) { $update_player['extra_info'] = $risk_player['extra_info']; } // game was started, reset all player's move dates to now if (array_key_exists('move_date', $risk_player)) { $update_player['move_date'] = null; } if (count($update_player)) { $update_modified = true; $Mysql->insert(self::GAME_PLAYER_TABLE, $update_player, array('game_id' => $this->id, 'player_id' => $player_id)); } } if ('Waiting' != $this->state) { // update the land data $query = "\n\t\t\t\tSELECT *\n\t\t\t\tFROM `" . self::GAME_LAND_TABLE . "`\n\t\t\t\tWHERE game_id = :game_id\n\t\t\t"; $params = array(':game_id' => $this->id); $db_lands = $Mysql->fetch_array($query, $params); if (!$db_lands) { $board = $this->_risk->board; foreach ($board as $land_id => $land) { $land['game_id'] = $this->id; $land['land_id'] = $land_id; $Mysql->insert(self::GAME_LAND_TABLE, $land); } $update_modified = true; } else { foreach ($db_lands as $db_land) { $update_land = array(); $land_id = $db_land['land_id']; $rland = $this->_risk->board[$land_id]; if ($db_land['player_id'] != $rland['player_id']) { $update_land['player_id'] = $rland['player_id']; } if ($db_land['armies'] != $rland['armies']) { $update_land['armies'] = $rland['armies']; } if ($update_land) { $update_modified = true; $Mysql->insert(self::GAME_LAND_TABLE, $update_land, " WHERE game_id = '{$this->id}' AND land_id = '{$land_id}' "); } } } } // update the game modified date if ($update_modified) { $Mysql->insert(self::GAME_TABLE, array('modify_date' => NULL), " WHERE game_id = '{$this->id}' "); } }