/**
  * @param int $match_id
  * @return match
  */
 public function load($match_id = null)
 {
     if (!is_null($match_id)) {
         $this->set_match_id($match_id);
     }
     $db = db::obtain();
     $query_for_match = 'SELECT * FROM ' . db::real_tablename('matches') . ' WHERE match_id=?';
     $query_for_slots = 'SELECT * FROM ' . db::real_tablename('slots') . ' WHERE match_id=?';
     $match_info = $db->query_first_pdo($query_for_match, array($this->get_match_id()));
     $match = new match();
     if (!$match_info) {
         return $match;
     }
     $match->set_array($match_info);
     $slots = $db->fetch_array_pdo($query_for_slots, array($this->get_match_id()));
     $slot_ids = '';
     foreach ($slots as $slot) {
         $slot_ids .= $slot['id'] . ',';
     }
     $query_for_ability_upgrades = 'SELECT * FROM ' . db::real_tablename('ability_upgrades') . ' WHERE slot_id IN (' . rtrim($slot_ids, ',') . ')';
     $ability_upgrade = $db->fetch_array_pdo($query_for_ability_upgrades);
     $ability_upgrade_formatted = array();
     foreach ($ability_upgrade as $a) {
         if (!isset($ability_upgrade_formatted[$a['slot_id']])) {
             $ability_upgrade_formatted[$a['slot_id']] = array();
         }
         array_push($ability_upgrade_formatted[$a['slot_id']], $a);
     }
     $query_for_additional_units = 'SELECT * FROM ' . db::real_tablename('additional_units') . ' WHERE slot_id IN  (' . rtrim($slot_ids, ',') . ')';
     $additional_units = $db->fetch_array_pdo($query_for_additional_units);
     $additional_units_formatted = array();
     foreach ($additional_units as $additional_unit) {
         if (!isset($additional_units_formatted[$additional_unit['slot_id']])) {
             $additional_units_formatted[$additional_unit['slot_id']] = array();
         }
         array_push($additional_units_formatted[$additional_unit['slot_id']], $additional_unit);
     }
     foreach ($slots as $s) {
         $slot = new slot();
         $slot->set_array($s);
         if (isset($ability_upgrade_formatted[$slot->get('id')])) {
             $slot->set_abilities_upgrade($ability_upgrade_formatted[$slot->get('id')]);
         }
         if (isset($additional_units_formatted[$slot->get('id')])) {
             $slot->set_additional_unit_items($additional_units_formatted[$slot->get('id')]);
         }
         $match->add_slot($slot);
     }
     if ($match->get('game_mode') == match::CAPTAINS_MODE) {
         $query_for_picks_bans = 'SELECT `is_pick`, `hero_id`, `team`, `order` FROM ' . db::real_tablename('picks_bans') . ' WHERE match_id = ? ORDER BY `order`';
         $picks_bans = $db->fetch_array_pdo($query_for_picks_bans, array($match->get('match_id')));
         $match->set_all_pick_bans($picks_bans);
     }
     return $match;
 }
 /**
  * Load match info by match_id
  */
 public function load()
 {
     $request = new request(self::steam_match_url, array('match_id' => $this->get_match_id()));
     $match_info = $request->send();
     if (is_null($match_info)) {
         return null;
     }
     $match = new match();
     $players = array();
     foreach ($match_info->players->player as $key => $player) {
         $players[] = $player;
     }
     $data = (array) $match_info;
     unset($data['players']);
     $data['start_time'] = date('Y-m-d H:i:s', $data['start_time']);
     $data['radiant_win'] = $data['radiant_win'] == 'true' ? '1' : '0';
     $match->set_array($data);
     // slots info
     foreach ($players as $player) {
         $data = (array) $player;
         $data['match_id'] = $this->get_match_id();
         $slot = new slot();
         $slot->set_array($data);
         // additional_units
         if (isset($data['additional_units'])) {
             $slot->set_additional_unit_items((array) $data['additional_units']->unit);
         }
         // abilities
         if (isset($data['ability_upgrades'])) {
             $d = (array) $data['ability_upgrades'];
             $abilities_upgrade = $d['ability'];
             if (!is_array($abilities_upgrade)) {
                 $abilities_upgrade = array($abilities_upgrade);
             }
             foreach ($abilities_upgrade as $k => $v) {
                 $abilities_upgrade[$k] = (array) $abilities_upgrade[$k];
             }
             $slot->set_abilities_upgrade($abilities_upgrade);
         }
         $match->add_slot($slot);
     }
     if (isset($match_info->picks_bans)) {
         $picks_bans = (array) $match_info->picks_bans;
         foreach ($picks_bans['pick_ban'] as $k => $v) {
             $picks_bans['pick_ban'][$k] = (array) $v;
             if ($picks_bans['pick_ban'][$k]['is_pick'] == 'false') {
                 $picks_bans['pick_ban'][$k]['is_pick'] = '0';
             } else {
                 $picks_bans['pick_ban'][$k]['is_pick'] = '1';
             }
         }
         $match->set_all_pick_bans($picks_bans['pick_ban']);
     }
     return $match;
 }
 /**
  * Load matches from db
  *
  * Possible filters: league_id, hero_id, account_id, start_at_match_id
  * Also matches_requested used as LIMIT
  * @return array
  */
 public function load()
 {
     $matches_info = $this->_get_matches_ids_from_matches();
     $matches_ids = array();
     foreach ($matches_info as $match_info) {
         array_push($matches_ids, $match_info['match_id']);
     }
     if (count($matches_ids) == 0) {
         return array();
     }
     $slots_info = $this->_load_slots_info($matches_ids);
     $picks_bans_formatted_info = $this->_load_picks_bans_info($matches_ids);
     $slots_ids = array();
     foreach ($slots_info as $slot_info) {
         array_push($slots_ids, $slot_info['id']);
     }
     $abilities_upgrade_formatted_info = $this->_load_ability_upgrades_info($slots_ids);
     $additional_units_formatted_info = $this->_load_additional_units_info($slots_ids);
     // we load all matches info and now need to make proper match objects
     $matches = array();
     foreach ($matches_info as $match_info) {
         $match = new match();
         $match->set_array($match_info);
         $slots_count = 0;
         foreach ($slots_info as $slot_info) {
             if ($slots_count > 9) {
                 // match can't has more than 10 slots
                 break;
             }
             if ($slot_info['match_id'] == $match->get('match_id')) {
                 $slot = new slot();
                 $slot->set_array($slot_info);
                 if (isset($abilities_upgrade_formatted_info[$slot->get('id')])) {
                     $slot->set_abilities_upgrade($abilities_upgrade_formatted_info[$slot->get('id')]);
                 }
                 if (isset($additional_units_formatted_info[$slot->get('id')])) {
                     $slot->set_additional_unit_items($additional_units_formatted_info[$slot->get('id')]);
                 }
                 $match->add_slot($slot);
                 $slots_count++;
             }
         }
         if (isset($picks_bans_formatted_info[$match->get('match_id')])) {
             $match->set_all_pick_bans($picks_bans_formatted_info[$match->get('match_id')]);
         }
         $matches[$match->get('match_id')] = $match;
     }
     return $matches;
 }