/** * @param array $data * @return mixed */ protected function createEntity($data) { $tournament = new Tournament(); $tournament->setName($data['name'])->setCurrentRound((int) @$data["current_round"]); if (isset($data['private'])) { $tournament->setPrivate($data['private']); } if (isset($data["status"])) { $this->container->get("core.handler.tournament")->changeTournamentStatus($tournament, new TournamentStatus($data["status"])); } $tournamentPlayersMap = []; if (isset($data['players'])) { foreach ($data['players'] as $referencePlayer) { /** @var User $player */ $player = $this->getReference($referencePlayer); $tournamentPlayer = new TournamentPlayer(); $countBlack = mt_rand(4, 5); $whiteInRow = mt_rand(0, 2); $tournamentPlayer->setTournament($tournament)->setPlayer($player)->setWhiteInRow($whiteInRow)->setBlackInRow($whiteInRow ? 0 : mt_rand(0, 2))->setCountBlack($countBlack)->setCountWhite(9 - $countBlack)->setPoints(mt_rand(0, 9))->setCoefficient(mt_rand(0, 2))->setMissedRound(!!mt_rand(0, 1)); $opponents = $this->getShuffleOpponents($data["players"], $referencePlayer, min(count($data["players"]), 9)); foreach ($opponents as $opponent) { $tournamentPlayer->addOpponent($opponent); } $tournament->getPlayers()->add($tournamentPlayer); $tournamentPlayersMap[$player->getId()] = $tournamentPlayer; } } if (isset($data["game_params"])) { /** @var GameParams $gameParams */ $gameParams = $this->container->get("jms_serializer")->deserialize(json_encode($data["game_params"]), 'CoreBundle\\Model\\Game\\GameParams', 'json'); $tournament->setGameParams($gameParams); } if (isset($data["tournament_params"])) { $tournamentParams = TournamentParamsFactory::create($data["tournament_params"]["type"]); if (isset($data["tournament_params"]["time_begin"])) { $tournamentParams->setTimeBegin($this->container->get("core.service.date")->getDateTime($data["tournament_params"]["time_begin"])); if (isset($data["tournament_params"]["games_vs_opponent"])) { $tournamentParams->setGamesVsOpponent($data["tournament_params"]["games_vs_opponent"]); } } $tournament->setTournamentParams($tournamentParams); } if (isset($data["games"])) { foreach ($data["games"] as $dataGame) { /** @var Game $game */ $game = $this->getReference($dataGame["reference"]); $this->container->get("core.handler.tournament")->addGameToTournament($tournament, $game, $dataGame["round"], $tournamentPlayersMap[$game->getUserWhite()->getId()], $tournamentPlayersMap[$game->getUserBlack()->getId()]); } } return $tournament; }
/** * @param User $player * @return Tournament */ public function addPlayer(User $player) : Tournament { $tournamentPlayer = new TournamentPlayer(); $tournamentPlayer->setTournament($this)->setPlayer($player)->setRating($player->getRating()); $this->players->add($tournamentPlayer); return $this; }