/**
  * (non-PHPdoc)
  * @see IActionController::executeAction()
  */
 public function executeAction($parameters)
 {
     $user = $this->_websoccer->getUser();
     if ($this->_isNationalTeam) {
         $teamId = NationalteamsDataService::getNationalTeamManagedByCurrentUser($this->_websoccer, $this->_db);
     } else {
         $teamId = $user->getClubId($this->_websoccer, $this->_db);
     }
     // check and get next match
     // next x matches
     $nextMatches = MatchesDataService::getNextMatches($this->_websoccer, $this->_db, $teamId, $this->_websoccer->getConfig('formation_max_next_matches'));
     if (!count($nextMatches)) {
         throw new Exception($this->_i18n->getMessage('formation_err_nonextmatch'));
     }
     // currently selected match
     $matchId = $parameters['id'];
     foreach ($nextMatches as $nextMatch) {
         if ($nextMatch['match_id'] == $matchId) {
             $matchinfo = $nextMatch;
             break;
         }
     }
     if (!isset($matchinfo)) {
         throw new Exception('illegal match id');
     }
     // get team players and check whether provided IDs are valid players (in team and not blocked)
     $players = PlayersDataService::getPlayersOfTeamById($this->_websoccer, $this->_db, $teamId, $this->_isNationalTeam, $matchinfo['match_type'] == 'cup', $matchinfo['match_type'] != 'friendly');
     $this->validatePlayer($parameters['player1'], $players);
     $this->validatePlayer($parameters['player2'], $players);
     $this->validatePlayer($parameters['player3'], $players);
     $this->validatePlayer($parameters['player4'], $players);
     $this->validatePlayer($parameters['player5'], $players);
     $this->validatePlayer($parameters['player6'], $players);
     $this->validatePlayer($parameters['player7'], $players);
     $this->validatePlayer($parameters['player8'], $players);
     $this->validatePlayer($parameters['player9'], $players);
     $this->validatePlayer($parameters['player10'], $players);
     $this->validatePlayer($parameters['player11'], $players);
     $this->validatePlayer($parameters['bench1'], $players, TRUE);
     $this->validatePlayer($parameters['bench2'], $players, TRUE);
     $this->validatePlayer($parameters['bench3'], $players, TRUE);
     $this->validatePlayer($parameters['bench4'], $players, TRUE);
     $this->validatePlayer($parameters['bench5'], $players, TRUE);
     // validate substitutions
     $validSubstitutions = array();
     for ($subNo = 1; $subNo <= 3; $subNo++) {
         $playerIn = $parameters['sub' . $subNo . '_in'];
         $playerOut = $parameters['sub' . $subNo . '_out'];
         $playerMinute = $parameters['sub' . $subNo . '_minute'];
         if ($playerIn != null && $playerIn > 0 && $playerOut != null && $playerOut > 0 && $playerMinute != null && $playerMinute > 0) {
             $this->validateSubstitution($playerIn, $playerOut, $playerMinute, $players);
             $validSubstitutions[] = $subNo;
         }
     }
     // save formation
     $this->saveFormation($teamId, $matchinfo['match_id'], $parameters, $validSubstitutions);
     // create success message
     $this->_websoccer->addFrontMessage(new FrontMessage(MESSAGE_TYPE_SUCCESS, $this->_i18n->getMessage('saved_message_title'), ''));
     return null;
 }
 public function executeAction($parameters)
 {
     $now = $this->_websoccer->getNowAsTimestamp();
     $user = $this->_websoccer->getUser();
     $teamId = $user->getClubId($this->_websoccer, $this->_db);
     if ($teamId < 1) {
         throw new Exception($this->_i18n->getMessage("feature_requires_team"));
     }
     $team = TeamsDataService::getTeamSummaryById($this->_websoccer, $this->_db, $teamId);
     // check if duration is in range
     $min = $this->_websoccer->getConfig("trainingcamp_min_days");
     $max = $this->_websoccer->getConfig("trainingcamp_max_days");
     if ($parameters["days"] < $min || $parameters["days"] > $max) {
         throw new Exception(sprintf($this->_i18n->getMessage("trainingcamp_booking_err_invaliddays"), $min, $max));
     }
     // check if date is in future
     $startDateObj = DateTime::createFromFormat($this->_websoccer->getConfig("date_format") . " H:i", $parameters["start_date"] . " 00:00");
     $startDateTimestamp = $startDateObj->getTimestamp();
     $endDateTimestamp = $startDateTimestamp + 3600 * 24 * $parameters["days"];
     if ($startDateTimestamp <= $now) {
         throw new Exception($this->_i18n->getMessage("trainingcamp_booking_err_dateinpast"));
     }
     // check if too far in future
     $maxDate = $now + $this->_websoccer->getConfig("trainingcamp_booking_max_days_in_future") * 3600 * 24;
     if ($startDateTimestamp > $maxDate) {
         throw new Exception($this->_i18n->getMessage("trainingcamp_booking_err_datetoofar", $this->_websoccer->getConfig("trainingcamp_booking_max_days_in_future")));
     }
     // get camp details
     $camp = TrainingcampsDataService::getCampById($this->_websoccer, $this->_db, $parameters["id"]);
     if (!$camp) {
         throw new Exception("Illegal ID");
     }
     // check if user still has an open training camp
     $existingBookings = TrainingcampsDataService::getCampBookingsByTeam($this->_websoccer, $this->_db, $teamId);
     if (count($existingBookings)) {
         throw new Exception($this->_i18n->getMessage("trainingcamp_booking_err_existingbookings"));
     }
     // check if team can afford it.
     $playersOfTeam = PlayersDataService::getPlayersOfTeamById($this->_websoccer, $this->_db, $teamId);
     $totalCosts = $camp["costs"] * $parameters["days"] * count($playersOfTeam);
     if ($totalCosts >= $team["team_budget"]) {
         throw new Exception($this->_i18n->getMessage("trainingcamp_booking_err_tooexpensive"));
     }
     // check if there are matches within the time frame
     $matches = MatchesDataService::getMatchesByTeamAndTimeframe($this->_websoccer, $this->_db, $teamId, $startDateTimestamp, $endDateTimestamp);
     if (count($matches)) {
         throw new Exception($this->_i18n->getMessage("trainingcamp_booking_err_matcheswithintimeframe"));
     }
     // debit amount
     BankAccountDataService::debitAmount($this->_websoccer, $this->_db, $teamId, $totalCosts, "trainingcamp_booking_costs_subject", $camp["name"]);
     // create camp booking
     $columns["verein_id"] = $teamId;
     $columns["lager_id"] = $camp["id"];
     $columns["datum_start"] = $startDateTimestamp;
     $columns["datum_ende"] = $endDateTimestamp;
     $this->_db->queryInsert($columns, $this->_websoccer->getConfig("db_prefix") . "_trainingslager_belegung");
     // success message
     $this->_websoccer->addFrontMessage(new FrontMessage(MESSAGE_TYPE_SUCCESS, $this->_i18n->getMessage("trainingcamp_booking_success"), ""));
     return "trainingcamp";
 }
 /**
  * (non-PHPdoc)
  * @see IModel::getTemplateParameters()
  */
 public function getTemplateParameters()
 {
     $teamId = $this->_websoccer->getUser()->getClubId($this->_websoccer, $this->_db);
     $captain_id = TeamsDataService::getTeamCaptainIdOfTeam($this->_websoccer, $this->_db, $teamId);
     $players = array();
     if ($teamId > 0) {
         $players = PlayersDataService::getPlayersOfTeamById($this->_websoccer, $this->_db, $teamId);
     }
     return array("players" => $players, "captain_id" => $captain_id);
 }
 public static function executeCamp(WebSoccer $websoccer, DbConnection $db, $teamId, $bookingInfo)
 {
     $players = PlayersDataService::getPlayersOfTeamById($websoccer, $db, $teamId);
     if (count($players)) {
         $playerTable = $websoccer->getConfig("db_prefix") . "_spieler";
         $updateCondition = "id = %d";
         $duration = round(($bookingInfo["date_end"] - $bookingInfo["date_start"]) / (24 * 3600));
         // update players
         foreach ($players as $player) {
             if ($player["matches_injured"] > 0) {
                 continue;
             }
             $columns = array();
             $columns["w_staerke"] = min(100, max(1, $bookingInfo["effect_strength"] * $duration + $player["strength"]));
             $columns["w_technik"] = min(100, max(1, $bookingInfo["effect_strength_technique"] * $duration + $player["strength_technic"]));
             $columns["w_kondition"] = min(100, max(1, $bookingInfo["effect_strength_stamina"] * $duration + $player["strength_stamina"]));
             $columns["w_frische"] = min(100, max(1, $bookingInfo["effect_strength_freshness"] * $duration + $player["strength_freshness"]));
             $columns["w_zufriedenheit"] = min(100, max(1, $bookingInfo["effect_strength_satisfaction"] * $duration + $player["strength_satisfaction"]));
             $db->queryUpdate($columns, $playerTable, $updateCondition, $player["id"]);
         }
     }
     // delete booking
     $db->queryDelete($websoccer->getConfig("db_prefix") . "_trainingslager_belegung", "id = %d", $bookingInfo["id"]);
 }
 /**
  * (non-PHPdoc)
  * @see IModel::getTemplateParameters()
  */
 public function getTemplateParameters()
 {
     $isNationalTeam = $this->_websoccer->getRequestParameter("nationalteam") ? TRUE : FALSE;
     $players = PlayersDataService::getPlayersOfTeamById($this->_websoccer, $this->_db, $this->_teamid, $isNationalTeam);
     return array("players" => $players);
 }
 private function trainPlayers($teamId, $trainer, $unit)
 {
     // compute effect on every player
     $players = PlayersDataService::getPlayersOfTeamById($this->_websoccer, $this->_db, $teamId);
     // freshness decrease for stamina and technique training
     $freshnessDecrease = round(1 + $unit["intensity"] / 100 * 5);
     $fromTable = $this->_websoccer->getConfig("db_prefix") . "_spieler";
     $whereCondition = "id = %d";
     $trainingEffects = array();
     foreach ($players as $player) {
         // injured player only refreshes and looses stamina
         $effectFreshness = 0;
         $effectStamina = 0;
         $effectTechnique = 0;
         $effectSatisfaction = 0;
         if ($player["matches_injured"]) {
             $effectFreshness = 1;
             $effectStamina = -1;
         } else {
             // regeneration training
             if ($unit["focus"] == "FR") {
                 $effectFreshness = 5;
                 $effectStamina = -2;
                 $effectSatisfaction = 1;
                 // motivation training
             } else {
                 if ($unit["focus"] == "MOT") {
                     $effectFreshness = 1;
                     $effectStamina = -1;
                     $effectSatisfaction = 5;
                     // stamina training
                 } else {
                     if ($unit["focus"] == "STA") {
                         $effectSatisfaction = -1;
                         // freshness depends on intensity
                         $effectFreshness = -$freshnessDecrease;
                         // success depends on trainer skills and intensity
                         $staminaIncrease = 1;
                         if ($unit["intensity"] > 50) {
                             $successFactor = $unit["intensity"] * $trainer["p_stamina"] / 100;
                             $pStamina[5] = $successFactor;
                             $pStamina[1] = 100 - $successFactor;
                             $staminaIncrease += SimulationHelper::selectItemFromProbabilities($pStamina);
                         }
                         $effectStamina = $staminaIncrease;
                         // technique
                     } else {
                         $effectFreshness = -$freshnessDecrease;
                         if ($unit["intensity"] > 20) {
                             $effectStamina = 1;
                         }
                         $techIncrease = 0;
                         if ($unit["intensity"] > 75) {
                             $successFactor = $unit["intensity"] * $trainer["p_technique"] / 100;
                             $pTech[2] = $successFactor;
                             $pTech[0] = 100 - $successFactor;
                             $techIncrease += SimulationHelper::selectItemFromProbabilities($pTech);
                         }
                         $effectTechnique = $techIncrease;
                     }
                 }
             }
         }
         // call plugins
         $event = new PlayerTrainedEvent($this->_websoccer, $this->_db, $this->_i18n, $player["id"], $teamId, $trainer["id"], $effectFreshness, $effectTechnique, $effectStamina, $effectSatisfaction);
         PluginMediator::dispatchEvent($event);
         // update player
         $columns = array("w_frische" => min(100, max(1, $player["strength_freshness"] + $effectFreshness)), "w_technik" => min(100, max(1, $player["strength_technic"] + $effectTechnique)), "w_kondition" => min(100, max(1, $player["strength_stamina"] + $effectStamina)), "w_zufriedenheit" => min(100, max(1, $player["strength_satisfaction"] + $effectSatisfaction)));
         $this->_db->queryUpdate($columns, $fromTable, $whereCondition, $player["id"]);
         // add effect
         $trainingEffects[$player["id"]] = array("name" => $player["pseudonym"] ? $player["pseudonym"] : $player["firstname"] . " " . $player["lastname"], "freshness" => $effectFreshness, "technique" => $effectTechnique, "stamina" => $effectStamina, "satisfaction" => $effectSatisfaction);
     }
     $this->_websoccer->addContextParameter("trainingEffects", $trainingEffects);
 }