/**
  * Loads the simulation state (statistics, players, results, etc.) from the database and builds the internal model
  * for continuing the match simulation.
  * 
  * @param WebSoccer $websoccer application context.
  * @param DbConnection $db database connection.
  * @param array $matchinfo match data from match database table.
  * @return SimulationMatch loaded match, ready for simulation.
  */
 public static function loadMatchState(WebSoccer $websoccer, DbConnection $db, $matchinfo)
 {
     $homeTeam = new SimulationTeam($matchinfo['home_id']);
     $guestTeam = new SimulationTeam($matchinfo['guest_id']);
     self::loadTeam($websoccer, $db, $matchinfo['match_id'], $homeTeam);
     self::loadTeam($websoccer, $db, $matchinfo['match_id'], $guestTeam);
     $homeTeam->setGoals($matchinfo['home_goals']);
     $homeTeam->offensive = $matchinfo['home_offensive'];
     $homeTeam->isNationalTeam = $matchinfo['home_nationalteam'];
     $homeTeam->isManagedByInterimManager = $matchinfo['home_interimmanager'];
     $homeTeam->noFormationSet = $matchinfo['home_noformation'];
     $homeTeam->setup = $matchinfo['home_setup'];
     $homeTeam->name = $matchinfo['home_name'];
     $homeTeam->longPasses = $matchinfo['home_longpasses'];
     $homeTeam->counterattacks = $matchinfo['home_counterattacks'];
     $homeTeam->morale = $matchinfo['home_morale'];
     $guestTeam->setGoals($matchinfo['guest_goals']);
     $guestTeam->offensive = $matchinfo['guest_offensive'];
     $guestTeam->isNationalTeam = $matchinfo['guest_nationalteam'];
     $guestTeam->isManagedByInterimManager = $matchinfo['guest_interimmanager'];
     $guestTeam->noFormationSet = $matchinfo['guest_noformation'];
     $guestTeam->setup = $matchinfo['guest_setup'];
     $guestTeam->name = $matchinfo['guest_name'];
     $guestTeam->longPasses = $matchinfo['guest_longpasses'];
     $guestTeam->counterattacks = $matchinfo['guest_counterattacks'];
     $guestTeam->morale = $matchinfo['guest_morale'];
     $match = new SimulationMatch($matchinfo['match_id'], $homeTeam, $guestTeam, $matchinfo['minutes']);
     $match->type = $matchinfo['type'];
     $match->penaltyShootingEnabled = $matchinfo['penaltyshooting'];
     $match->isSoldOut = $matchinfo['soldout'];
     $match->cupName = $matchinfo['cup_name'];
     $match->cupRoundName = $matchinfo['cup_roundname'];
     $match->cupRoundGroup = $matchinfo['cup_groupname'];
     $match->isAtForeignStadium = $matchinfo['custom_stadium_id'] ? TRUE : FALSE;
     //get and set player with ball
     if ($matchinfo['player_with_ball'] && isset(self::$_addedPlayers[$matchinfo['player_with_ball']])) {
         $match->setPlayerWithBall(self::$_addedPlayers[$matchinfo['player_with_ball']]);
     }
     if ($matchinfo['prev_player_with_ball'] && isset(self::$_addedPlayers[$matchinfo['prev_player_with_ball']])) {
         $match->setPreviousPlayerWithBall(self::$_addedPlayers[$matchinfo['prev_player_with_ball']]);
     }
     // set free kick takers
     if ($matchinfo['home_freekickplayer'] && isset(self::$_addedPlayers[$matchinfo['home_freekickplayer']])) {
         $homeTeam->freeKickPlayer = self::$_addedPlayers[$matchinfo['home_freekickplayer']];
     }
     if ($matchinfo['guest_freekickplayer'] && isset(self::$_addedPlayers[$matchinfo['guest_freekickplayer']])) {
         $guestTeam->freeKickPlayer = self::$_addedPlayers[$matchinfo['guest_freekickplayer']];
     }
     // substitutions
     for ($subNo = 1; $subNo <= 3; $subNo++) {
         if ($matchinfo['home_sub_' . $subNo . '_out'] && isset(self::$_addedPlayers[$matchinfo['home_sub_' . $subNo . '_in']]) && isset(self::$_addedPlayers[$matchinfo['home_sub_' . $subNo . '_out']])) {
             $sub = new SimulationSubstitution($matchinfo['home_sub_' . $subNo . '_minute'], self::$_addedPlayers[$matchinfo['home_sub_' . $subNo . '_in']], self::$_addedPlayers[$matchinfo['home_sub_' . $subNo . '_out']], $matchinfo['home_sub_' . $subNo . '_condition'], $matchinfo['home_sub_' . $subNo . '_position']);
             $homeTeam->substitutions[] = $sub;
         }
         if ($matchinfo['guest_sub_' . $subNo . '_out'] && isset(self::$_addedPlayers[$matchinfo['guest_sub_' . $subNo . '_in']]) && isset(self::$_addedPlayers[$matchinfo['guest_sub_' . $subNo . '_out']])) {
             $sub = new SimulationSubstitution($matchinfo['guest_sub_' . $subNo . '_minute'], self::$_addedPlayers[$matchinfo['guest_sub_' . $subNo . '_in']], self::$_addedPlayers[$matchinfo['guest_sub_' . $subNo . '_out']], $matchinfo['guest_sub_' . $subNo . '_condition'], $matchinfo['guest_sub_' . $subNo . '_position']);
             $guestTeam->substitutions[] = $sub;
         }
     }
     // reset cache
     self::$_addedPlayers = null;
     return $match;
 }