Пример #1
0
 /**
  * This method is used to report the results of unplayed matches
  * 
  * returns false if this match has already been played
  * 
  * Note: For new matches, you must use set_winner() to define which 
  *      team won the match
  * 
  * You also have the option of using $match->game($game_number) to save details about
  *      each individual game within this match
  * 
  * @param boolean $strict       disabled by default - if you force strict, it will
  *      only report if it validate_winner_games tells us that the match winner was given enough wins to satisfy
  *      this round's best_of setting
  * 
  * @return int		returns the id if successfully, false otherwise
  */
 public function report($strict = false)
 {
     //Already reported
     if (!is_null($this->id)) {
         return $this->set_error('This match has already been reported, please use save() if you wish to change the details');
     }
     //No winner defined
     if (!$this->winner_set) {
         return $this->set_error('Please define a winner before reporting ($team->set_winner($winning_team)) You can refer to $match->team and $match->opponent for participant details');
     }
     /**
      * Do a quick last check to make sure that this match is still in the touranment's list of open_matches,
      *  if it's not, it could be caused by a number of things - like being reported elsewhere, or the tournament advancing
      *  to the next stage etc
      */
     $tournament =& $this->tournament();
     if (!in_array($this, $tournament->open_matches())) {
         return $this->set_error('This match is no longer listed as an open match for this tournament, perhaps it was reported elsewhere, or the tournament has begun the next stage');
     }
     //We can't report the match if the round has unsaved changes, because the API may not process it correctly, as it may
     //have a different value for this round's best_of
     $format =& $this->round_format();
     if (!is_null($format)) {
         //Stop now - round has to be saved first
         if ($format->changed) {
             return $this->set_error('The round for this match has unsaved changes, please save them first (either with $round->save(), $tournament->save_rounds(), or $tournament->save()');
         }
         //Strict - validate the winner has enough game wins first
         if ($strict) {
             if (!$this->validate_winner_games($strict)) {
                 return $this->set_error('Winning team does not have enough game wins! This round requires at least ' . $format->best_of . ' game wins');
             }
         }
     }
     //Let BBModel handle this
     if (!($result = parent::save(false, array('tourney_id' => $this->tournament->id)))) {
         return false;
     }
     //Report all of the game details
     if (!$this->save_games()) {
         return $this->set_error('Error saving game details');
     }
     //Wipe out all cached opponent / open match cache from the tournament
     $this->tournament->clear_match_cache();
     //Flag the teams for a reload (wins / lbwins etc may have changed), and reset opponent / match values etc, to force a fresh query next time they're accessed
     $this->team->flag_reload();
     $this->opponent->flag_reload();
     //
     $this->team->reset_opponents();
     $this->opponent->reset_opponents();
     //Return the save() result
     return $result;
 }
Пример #2
0
 /**
  * Save the game details
  *
  * {@inheritdoc}
  */
 public function save($return_result = false, $child_args = null)
 {
     //If the match hasn't been saved yet, stop now
     if (is_null($this->match->id)) {
         return $this->set_error("You must save the entire match before saving games (\$match->save() or \$match->report())");
     }
     //Ask for the result directly, so we can import new map / race values etc
     $result = parent::save(true, array('tourney_match_id' => $this->match->id));
     if (!$result) {
         return false;
     }
     if (!is_null($result->map_id)) {
         $this->set_current_data('map_id', $result->map_id);
     }
     if (!is_null($result->race_id)) {
         $this->set_current_data('race_id', $result->race_id);
     }
     if (!is_null($result->o_race_id)) {
         $this->set_current_data('o_race_id', $result->o_race_id);
     }
     //Success!
     return true;
 }
Пример #3
0
 /**
  * Overrides BBModel::save() so we can return false if trying to save an orphaned team
  * {@inheritdoc}
  */
 public function save($return_result = false, $child_args = null)
 {
     //Tournament must be saved first
     if (is_null($this->tournament->id)) {
         return $this->set_error('Tournament must be saved before adding teams');
     }
     //Remember whether or not we need to clear cache after the save is successful
     $reset_opponents = false;
     foreach (array_keys($this->new_data) as $key) {
         $key = strtolower($key);
         if ($key == 'wins' || $key == 'lb_wins' || $key == 'bronze_wins') {
             $reset_opponents = true;
             break;
         }
     }
     //If the race was changed, flag a reload
     $flag_reload = isset($this->new_data['race']);
     //json notes
     $notes = null;
     if (array_key_exists('notes', $this->new_data)) {
         $notes = $this->new_data['notes'];
         if (is_object($notes) || is_array($notes)) {
             $this->new_data['notes'] = json_encode($notes);
         }
     }
     //Let BBModel handle the rest
     if (!($save_result = parent::save($return_result, array('tourney_id' => $this->tournament->id)))) {
         return false;
     }
     //If bracket position was adjusted manually, reset opponents / cache etc
     if ($reset_opponents) {
         $this->tournament->clear_id_cache();
         $this->reset_opponents();
         $this->tournament->open_matches(true);
     }
     //If the race changed, flag a reload so that we download the race name, id, and icon values
     if ($flag_reload) {
         $this->flag_reload();
     }
     //Success!
     return $save_result;
 }
Пример #4
0
 /**
  * If creating a new tournament, we want to make sure that when the data is sent
  * to the API, that we request return_data => 2, therefore asking BinaryBeast to send
  * back a full TourneyInfo object for us to import all values that may have been
  * generated on BB's end
  * 
  * @param boolean $settings_only        false by default - can be used to skip saving children (BBTeams)
  * 
  * @return string       The new tourney_id for this object
  */
 private function save_new($settings_only = false)
 {
     //return_data => 2 asks BinaryBeast to include a full tourney_info dump in its response
     $args = array('return_data' => 2);
     /**
      * Teams to insert
      * @var BBTeam[]
      */
     $new_teams = array();
     /**
      * Raw team data for new teams
      * @var array[]
      */
     $teams_data = array();
     //If any teams have been added, include them too - the API can handle it
     if (!$settings_only) {
         /** @var BBTeam[] $new_teams */
         $new_teams = $this->get_changed_children('BBTeam');
         //Extract data arrays for each new team
         if (!empty($new_teams)) {
             foreach ($new_teams as &$team) {
                 $teams_data[] = $team->data;
             }
             //Add the new teams to the API arguments
             $args['teams'] = $teams_data;
         }
     }
     //Let BBModel handle it from here - but ask for the api response to be returned instead of a generic boolean
     if (!($result = parent::save(true, $args))) {
         return false;
     }
     //OH NOES!
     if ($result->result !== 200) {
         return false;
     }
     //Clear list and count api cache
     $this->clear_object_service_cache(array(self::SERVICE_COUNT, self::SERVICE_LIST, self::SERVICE_LIST_POPULAR));
     /**
      * Import the new data, and save the ID
      */
     $this->import_values($result);
     $this->set_id($result->tourney_id);
     /**
      * Update new teams with their new ids
      */
     if (!empty($new_teams)) {
         if (!empty($result->teams)) {
             /**
              * Iterate through each "changed" team (they will all be new, since this is a new tournament),
              *  and apply the new tourney_team_id
              */
             foreach ($new_teams as $x => &$team) {
                 //apply the entire "data" array as if it were an import
                 $team->import_values($teams_data[$x]);
                 //Apply the new team id
                 $team->set_id($result->teams[$x]);
             }
         }
         //Reset count of changed children
         $this->reset_changed_children('BBTeam');
     }
     //Success!
     return $this->id;
 }