/**
  * @see ISimulatorObserver::onMatchCompleted()
  */
 public function onMatchCompleted(SimulationMatch $match)
 {
     // players
     $isFriendlyMatch = $match->type == 'Freundschaft';
     if ($isFriendlyMatch) {
         $this->updatePlayersOfFriendlymatch($match->homeTeam);
         $this->updatePlayersOfFriendlymatch($match->guestTeam);
     } else {
         // player statistics and salary
         $isTie = $match->homeTeam->getGoals() == $match->guestTeam->getGoals();
         $this->updatePlayers($match, $match->homeTeam, $match->homeTeam->getGoals() > $match->guestTeam->getGoals(), $isTie);
         $this->updatePlayers($match, $match->guestTeam, $match->homeTeam->getGoals() < $match->guestTeam->getGoals(), $isTie);
         // sponsor
         if (!$match->homeTeam->isNationalTeam) {
             $this->creditSponsorPayments($match->homeTeam, TRUE, $match->homeTeam->getGoals() > $match->guestTeam->getGoals());
         }
         if (!$match->guestTeam->isNationalTeam) {
             $this->creditSponsorPayments($match->guestTeam, FALSE, $match->homeTeam->getGoals() < $match->guestTeam->getGoals());
         }
         // points and statistics
         if ($match->type == 'Ligaspiel') {
             $this->updateTeams($match);
         } else {
             if (strlen($match->cupRoundGroup)) {
                 $this->updateTeamsOfCupGroupMatch($match);
                 SimulationCupMatchHelper::checkIfMatchIsLastMatchOfGroupRoundAndCreateFollowingMatches($this->_websoccer, $this->_db, $match);
             }
         }
         // highscore and fan popularity
         $this->updateUsers($match);
     }
     // delete formations
     $this->_db->queryDelete($this->_websoccer->getConfig('db_prefix') . '_aufstellung', 'match_id = %d', $match->id);
 }
 /**
  * Simulates the specified match by the number of specified minutes.
  * 
  * @param SimulationMatch $match match to simulate.
  * @param int $minutes number of minutes to simuate.
  */
 public function simulateMatch(SimulationMatch $match, $minutes)
 {
     if ($match->minute == null) {
         $match->minute = 0;
     }
     // start the match
     if ($match->minute == 0) {
         foreach ($this->_observers as $observer) {
             $observer->onBeforeMatchStarts($match);
         }
     }
     // match might be completed already before simulation, e.g. when there is no formation provided
     if ($match->isCompleted) {
         $this->completeMatch($match);
         return;
     }
     // complete match if team has no players
     if (!$this->_hasPlayers($match->homeTeam) || !$this->_hasPlayers($match->guestTeam)) {
         $this->completeMatch($match);
         return;
     }
     for ($simMinute = 1; $simMinute <= $minutes; $simMinute++) {
         $match->minute = $match->minute + 1;
         if ($match->minute == 1) {
             $this->_simStrategy->kickoff($match);
         } else {
             SimulationHelper::checkAndExecuteSubstitutions($match, $match->homeTeam, $this->_observers);
             SimulationHelper::checkAndExecuteSubstitutions($match, $match->guestTeam, $this->_observers);
         }
         // execute next action
         $actionName = $this->_simStrategy->nextAction($match);
         $this->_simStrategy->{$actionName}($match);
         // increase minutes played
         $this->_increaseMinutesPlayed($match->homeTeam);
         $this->_increaseMinutesPlayed($match->guestTeam);
         // match ended?
         // two possibilities:
         // a. Normal matches end after regular time
         // b. if penalty shooting is enabled, play extension if there is no result after 90 minutes
         $lastMinute = 90 + SimulationHelper::getMagicNumber(1, 5);
         if ($match->penaltyShootingEnabled || $match->type == 'Pokalspiel') {
             // match ended after regular or extension time with a winner
             if (($match->minute == 91 || $match->minute == 121) && ($match->type != 'Pokalspiel' && $match->homeTeam->getGoals() != $match->guestTeam->getGoals() || $match->type == 'Pokalspiel' && !SimulationCupMatchHelper::checkIfExtensionIsRequired($this->_websoccer, $this->_db, $match))) {
                 $this->completeMatch($match);
                 break;
                 // no winner after extension time -> penalty shooting
             } elseif ($match->minute == 121 && ($match->type != 'Pokalspiel' && $match->homeTeam->getGoals() == $match->guestTeam->getGoals() || $match->type == 'Pokalspiel' && SimulationCupMatchHelper::checkIfExtensionIsRequired($this->_websoccer, $this->_db, $match))) {
                 $this->_simStrategy->penaltyShooting($match);
                 // we have a winner now
                 if ($match->type == 'Pokalspiel') {
                     // home team won
                     if ($match->homeTeam->getGoals() > $match->guestTeam->getGoals()) {
                         SimulationCupMatchHelper::createNextRoundMatchAndPayAwards($this->_websoccer, $this->_db, $match->homeTeam->id, $match->guestTeam->id, $match->cupName, $match->cupRoundName);
                         // guest team won
                     } else {
                         SimulationCupMatchHelper::createNextRoundMatchAndPayAwards($this->_websoccer, $this->_db, $match->guestTeam->id, $match->homeTeam->id, $match->cupName, $match->cupRoundName);
                     }
                 }
                 $this->completeMatch($match);
                 break;
             }
             // regular match
         } elseif ($match->minute >= $lastMinute) {
             $this->completeMatch($match);
             break;
         }
     }
 }
        $columns["M.gast_w" . $subNo . "_rein"] = "guest_sub_" . $subNo . "_in";
        $columns["M.gast_w" . $subNo . "_minute"] = "guest_sub_" . $subNo . "_minute";
        $columns["M.gast_w" . $subNo . "_condition"] = "guest_sub_" . $subNo . "_condition";
    }
    $result = $db->querySelect($columns, $fromTable, "M.id = %d", $matchId);
    $matchinfo = $result->fetch_array();
    $result->free();
    // init default simulation strategry in order to include dependend constants. Yeah, refactor this once having to much time...
    $dummyVar = new DefaultSimulationStrategy($website);
    $matchModel = SimulationStateHelper::loadMatchState($website, $db, $matchinfo);
    // compute audience
    if ($website->getRequestParameter("computetickets")) {
        SimulationAudienceCalculator::computeAndSaveAudience($website, $db, $matchModel);
    }
    if ($matchinfo["type"] == "Pokalspiel") {
        SimulationCupMatchHelper::checkIfExtensionIsRequired($website, $db, $matchModel);
    }
    // complete match
    $observer = new DataUpdateSimulatorObserver($website, $db);
    $observer->onMatchCompleted($matchModel);
    // show success message
    echo createSuccessMessage($i18n->getMessage("match_manage_complete_success"), "");
}
echo "<h3><a href=\"" . $website->getInternalUrl("team", "id=" . $match["match_home_id"]) . "\" target=\"_blank\">" . escapeOutput($match["match_home_name"]) . "</a> - <a href=\"" . $website->getInternalUrl("team", "id=" . $match["match_guest_id"]) . "\" target=\"_blank\">" . escapeOutput($match["match_guest_name"]) . "</a></h3>";
echo "<div class=\"well\">" . $i18n->getMessage("match_manage_complete_intro") . "</div>";
echo "<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"post\" class=\"form-horizontal\">";
echo "<input type=\"hidden\" name=\"site\" value=\"{$site}\"/>";
echo "<input type=\"hidden\" name=\"action\" value=\"complete\"/>";
echo "<input type=\"hidden\" name=\"match\" value=\"{$matchId}\"/>";
echo FormBuilder::createFormGroup($i18n, "computetickets", array("type" => "boolean", "value" => "1"), "1", "match_manage_complete_");
echo "<div class=\"form-actions\">";