private function updatePlayer(SimulationMatch $match, SimulationPlayer $player, $isTeamWinner, $isTie)
 {
     $fromTable = $this->_websoccer->getConfig('db_prefix') . '_spieler';
     $whereCondition = 'id = %d';
     $parameters = $player->id;
     $minMinutes = (int) $this->_websoccer->getConfig('sim_played_min_minutes');
     $blockYellowCards = (int) $this->_websoccer->getConfig('sim_block_player_after_yellowcards');
     $staminaChange = (int) $this->_websoccer->getConfig('sim_strengthchange_stamina');
     $satisfactionChange = (int) $this->_websoccer->getConfig('sim_strengthchange_satisfaction');
     if ($player->team->isNationalTeam) {
         $columns['gesperrt_nationalteam'] = $player->blocked;
     } elseif ($match->type == 'Pokalspiel') {
         $columns['gesperrt_cups'] = $player->blocked;
     } else {
         $columns['gesperrt'] = $player->blocked;
     }
     // get previous player statistics and lending info
     $pcolumns = 'id,vorname,nachname,kunstname,verein_id,vertrag_spiele,st_tore,st_assists,st_spiele,st_karten_gelb,st_karten_gelb_rot,st_karten_rot,sa_tore,sa_assists,sa_spiele,sa_karten_gelb,sa_karten_gelb_rot,sa_karten_rot,lending_fee,lending_owner_id,lending_matches';
     $result = $this->_db->querySelect($pcolumns, $fromTable, $whereCondition, $parameters);
     $playerinfo = $result->fetch_array();
     $result->free();
     // update statistic
     $columns['st_tore'] = $playerinfo['st_tore'] + $player->getGoals();
     $columns['sa_tore'] = $playerinfo['sa_tore'] + $player->getGoals();
     $columns['st_assists'] = $playerinfo['st_assists'] + $player->getAssists();
     $columns['sa_assists'] = $playerinfo['sa_assists'] + $player->getAssists();
     $columns['st_spiele'] = $playerinfo['st_spiele'] + 1;
     $columns['sa_spiele'] = $playerinfo['sa_spiele'] + 1;
     if ($player->redCard) {
         $columns['st_karten_rot'] = $playerinfo['st_karten_rot'] + 1;
         $columns['sa_karten_rot'] = $playerinfo['sa_karten_rot'] + 1;
     } else {
         if ($player->yellowCards) {
             if ($player->yellowCards == 2) {
                 $columns['st_karten_gelb_rot'] = $playerinfo['st_karten_gelb_rot'] + 1;
                 $columns['sa_karten_gelb_rot'] = $playerinfo['sa_karten_gelb_rot'] + 1;
                 if ($player->team->isNationalTeam) {
                     $columns['gesperrt_nationalteam'] = '1';
                 } elseif ($match->type == 'Pokalspiel') {
                     $columns['gesperrt_cups'] = '1';
                 } else {
                     $columns['gesperrt'] = '1';
                 }
             } elseif (!$player->team->isNationalTeam) {
                 $columns['st_karten_gelb'] = $playerinfo['st_karten_gelb'] + 1;
                 $columns['sa_karten_gelb'] = $playerinfo['sa_karten_gelb'] + 1;
                 // block after certain number of matches ('Gelbsperre')
                 if ($match->type == 'Ligaspiel' && $blockYellowCards > 0 && $columns['sa_karten_gelb'] % $blockYellowCards == 0) {
                     $columns['gesperrt'] = 1;
                 }
             }
         }
     }
     if (!$player->team->isNationalTeam) {
         $columns['vertrag_spiele'] = max(0, $playerinfo['vertrag_spiele'] - 1);
         if ($columns['vertrag_spiele'] == 5) {
             $this->_teamsWithSoonEndingContracts[$player->team->id] = TRUE;
         }
     }
     // update other fields
     if (!$player->team->isNationalTeam || $this->_websoccer->getConfig('sim_playerupdate_through_nationalteam')) {
         $columns['w_frische'] = $player->strengthFreshness;
         $columns['verletzt'] = $player->injured;
         if ($player->getMinutesPlayed() >= $minMinutes) {
             $columns['w_kondition'] = min(100, $player->strengthStamina + $staminaChange);
             $columns['w_zufriedenheit'] = min(100, $player->strengthSatisfaction + $satisfactionChange);
         } else {
             $columns['w_kondition'] = max(1, $player->strengthStamina - $staminaChange);
             $columns['w_zufriedenheit'] = max(1, $player->strengthSatisfaction - $satisfactionChange);
         }
         // result dependent satisfaction change
         if (!$isTie) {
             if ($isTeamWinner) {
                 $columns['w_zufriedenheit'] = min(100, $columns['w_zufriedenheit'] + $satisfactionChange);
             } else {
                 $columns['w_zufriedenheit'] = max(1, $columns['w_zufriedenheit'] - $satisfactionChange);
             }
         }
     }
     if (!$player->team->isNationalTeam && $playerinfo['lending_matches'] > 0) {
         $this->handleBorrowedPlayer($columns, $playerinfo);
     }
     $this->_db->queryUpdate($columns, $fromTable, $whereCondition, $parameters);
 }
 private static function getPlayerColumns($matchId, SimulationPlayer $player, $fieldArea)
 {
     $columns['spiel_id'] = $matchId;
     $columns['spieler_id'] = $player->id;
     $columns['team_id'] = $player->team->id;
     $columns['name'] = $player->name;
     $columns['note'] = $player->getMark();
     $columns['minuten_gespielt'] = $player->getMinutesPlayed();
     $columns['karte_gelb'] = $player->yellowCards;
     $columns['karte_rot'] = $player->redCard;
     $columns['verletzt'] = $player->injured;
     $columns['gesperrt'] = $player->blocked;
     $columns['tore'] = $player->getGoals();
     $columns['feld'] = $fieldArea;
     $columns['position'] = $player->position;
     $columns['position_main'] = $player->mainPosition;
     $columns['age'] = $player->age;
     $columns['w_staerke'] = $player->strength;
     $columns['w_technik'] = $player->strengthTech;
     $columns['w_kondition'] = $player->strengthStamina;
     $columns['w_frische'] = $player->strengthFreshness;
     $columns['w_zufriedenheit'] = $player->strengthSatisfaction;
     $columns['ballcontacts'] = $player->getBallContacts();
     $columns['wontackles'] = $player->getWonTackles();
     $columns['losttackles'] = $player->getLostTackles();
     $columns['shoots'] = $player->getShoots();
     $columns['passes_successed'] = $player->getPassesSuccessed();
     $columns['passes_failed'] = $player->getPassesFailed();
     $columns['assists'] = $player->getAssists();
     return $columns;
 }
 /**
  * Update overall player statistics and match records of player.
  * 
  * @param SimulationMatch $match
  * @param SimulationPlayer $player
  * @param $isOnPitch TRUE if player is on pitch in the end, FALSE if got removed (substitution or red card or injury).
  */
 private function _updatePlayer(SimulationMatch $match, SimulationPlayer $player, $isOnPitch)
 {
     // update match statistics
     $columns = array('name' => $player->name, 'position_main' => $player->mainPosition, 'grade' => $player->getMark(), 'minutes_played' => $player->getMinutesPlayed(), 'card_yellow' => $player->yellowCards, 'card_red' => $player->redCard, 'goals' => $player->getGoals(), 'strength' => $player->strength, 'ballcontacts' => $player->getBallContacts(), 'wontackles' => $player->getWonTackles(), 'shoots' => $player->getShoots(), 'passes_successed' => $player->getPassesSuccessed(), 'passes_failed' => $player->getPassesFailed(), 'assists' => $player->getAssists(), 'state' => $isOnPitch ? '1' : 'Ausgewechselt');
     $this->_db->queryUpdate($columns, $this->_websoccer->getConfig('db_prefix') . '_youthmatch_player', 'match_id = %d AND player_id = %d', array($match->id, $player->id));
     // update player record, if actually played
     if ($this->_websoccer->getConfig('sim_played_min_minutes') <= $player->getMinutesPlayed()) {
         // query existing statistics
         $result = $this->_db->querySelect('*', $this->_websoccer->getConfig('db_prefix') . '_youthplayer', 'id = %d', $player->id);
         $playerinfo = $result->fetch_array();
         $result->free();
         $strengthChange = $this->_computeStrengthChange($player);
         // trigger plug-ins
         $event = new YouthPlayerPlayedEvent($this->_websoccer, $this->_db, I18n::getInstance($this->_websoccer->getConfig('supported_languages')), $player, $strengthChange);
         PluginMediator::dispatchEvent($event);
         $yellowRedCards = 0;
         if ($player->yellowCards == 2) {
             $yellowCards = 1;
             $yellowRedCards = 1;
         } else {
             $yellowCards = $player->yellowCards;
         }
         // ensure that new strength does not exceed boundaries (max/min strength)
         $strength = $playerinfo['strength'] + $strengthChange;
         $maxStrength = $this->_websoccer->getConfig('youth_scouting_max_strength');
         $minStrength = $this->_websoccer->getConfig('youth_scouting_min_strength');
         if ($strength > $maxStrength) {
             $strengthChange = 0;
             $strength = $maxStrength;
         } else {
             if ($strength < $minStrength) {
                 $strengthChange = 0;
                 $strength = $minStrength;
             }
         }
         // save
         $columns = array('strength' => $strength, 'strength_last_change' => $strengthChange, 'st_goals' => $playerinfo['st_goals'] + $player->getGoals(), 'st_matches' => $playerinfo['st_matches'] + 1, 'st_assists' => $playerinfo['st_assists'] + $player->getAssists(), 'st_cards_yellow' => $playerinfo['st_cards_yellow'] + $yellowCards, 'st_cards_yellow_red' => $playerinfo['st_cards_yellow_red'] + $yellowRedCards, 'st_cards_red' => $playerinfo['st_cards_red'] + $player->redCard);
         $this->_db->queryUpdate($columns, $this->_websoccer->getConfig('db_prefix') . '_youthplayer', 'id = %d', $player->id);
     }
 }
 /**
  * @see ISimulationObserver::onBallPassFailure()
  */
 public function onBallPassFailure(SimulationMatch $match, SimulationPlayer $player)
 {
     // show mercy when player already hit at least twice or assisted twice or both scored and assisted
     if ($player->getGoals() < 2 && $player->getAssists() < 2 && ($player->getGoals() == 0 || $player->getAssists() == 0)) {
         $player->downgradeMark(MARK_DOWNGRADE_BALLPASS_FAILURE);
     }
     $player->setPassesFailed($player->getPassesFailed() + 1);
 }