Exemple #1
0
 /** static public function invite
  *		Creates the game from _POST data
  *
  * @param void
  * @action creates an invite
  * @return int game id
  */
 public static function invite()
 {
     call(__METHOD__);
     call($_POST);
     $Mysql = Mysql::get_instance();
     // DON'T sanitize the data
     // it gets sani'd in the MySQL->insert method
     $_P = $_POST;
     // translate (filter/sanitize) the data
     $_P['white_id'] = (int) $_SESSION['player_id'];
     $_P['black_id'] = (int) $_P['opponent'];
     $_P['setup_id'] = (int) $_P['setup'];
     $_P['laser_battle'] = is_checked($_P['laser_battle_box']);
     $_P['battle_front_only'] = is_checked($_P['battle_front_only']);
     $_P['battle_hit_self'] = is_checked($_P['battle_hit_self']);
     $_P['move_sphynx'] = is_checked($_P['move_sphynx']);
     call($_P);
     // grab the setup
     $query = "\n\t\t\tSELECT setup_id\n\t\t\tFROM " . Setup::SETUP_TABLE . "\n\t\t";
     $setup_ids = $Mysql->fetch_value_array($query);
     call($setup_ids);
     // check for random setup
     if (0 == $_P['setup_id']) {
         shuffle($setup_ids);
         shuffle($setup_ids);
         $_P['setup_id'] = (int) reset($setup_ids);
         sort($setup_ids);
     }
     // make sure the setup id is valid
     if (!in_array($_P['setup_id'], $setup_ids)) {
         throw new MyException(__METHOD__ . ': Setup is not valid');
     }
     $query = "\n\t\t\tSELECT board\n\t\t\tFROM " . Setup::SETUP_TABLE . "\n\t\t\tWHERE setup_id = '{$_P['setup_id']}'\n\t\t";
     $setup = $Mysql->fetch_value($query);
     // laser battle cleanup
     // only run this if the laser battle box was open
     if ($_P['laser_battle']) {
         ifer($_P['battle_dead'], 1, false);
         ifer($_P['battle_immune'], 1);
         // we can only hit ourselves in the sides or back, never front
         if ($_P['battle_front_only']) {
             $_P['battle_hit_self'] = false;
         }
         $extra_info = array('battle_dead' => (int) max((int) $_P['battle_dead'], 1), 'battle_immune' => (int) max((int) $_P['battle_immune'], 0), 'battle_front_only' => (bool) $_P['battle_front_only'], 'battle_hit_self' => (bool) $_P['battle_hit_self']);
     }
     $extra_info['white_color'] = $_P['color'];
     $extra_info['move_sphynx'] = $_P['move_sphynx'];
     $setup = expandFEN($setup);
     try {
         if (is_checked($_P['convert_to_1']) || is_checked($_P['rand_convert_to_1'])) {
             $setup = Setup::convert_to_1($setup);
         } elseif (is_checked($_P['convert_to_2']) || is_checked($_P['rand_convert_to_2'])) {
             $setup = Setup::convert_to_2($setup);
         }
     } catch (MyException $e) {
         throw $e;
     }
     $extra_info['invite_setup'] = packFEN($setup);
     call($extra_info);
     $diff = array_compare($extra_info, self::$_EXTRA_INFO_DEFAULTS);
     $extra_info = $diff[0];
     ksort($extra_info);
     call($extra_info);
     if (!empty($extra_info)) {
         $_P['extra_info'] = serialize($extra_info);
     }
     // create the game
     $required = array('white_id', 'setup_id');
     $key_list = array_merge($required, array('black_id', 'extra_info'));
     try {
         $_DATA = array_clean($_P, $key_list, $required);
     } catch (MyException $e) {
         throw $e;
     }
     $_DATA['state'] = 'Waiting';
     $_DATA['create_date '] = 'NOW( )';
     // note the trailing space in the field name, this is not a typo
     $_DATA['modify_date '] = 'NOW( )';
     // note the trailing space in the field name, this is not a typo
     $insert_id = $Mysql->insert(self::GAME_TABLE, $_DATA);
     if (empty($insert_id)) {
         throw new MyException(__METHOD__ . ': Invite could not be created');
     }
     // send the email
     if ($_DATA['black_id']) {
         Email::send('invite', $_DATA['black_id'], array('opponent' => $GLOBALS['_PLAYERS'][$_DATA['white_id']], 'page' => 'invite.php'));
     }
     return $insert_id;
 }