/** * @param Game $game * @param User $user * @return null|TeamTaskSequence */ public function getCurrentMainTask(Game $game, User $user) { $team = $user->getTeam(); /** @var TeamTaskSequence[] $tasks */ $tasks = $this->manager->getRepository('sdGameBundle:Task')->findSequenceTasks($game, $team)->execute(); $task = null; $prevIndex = 0; foreach ($tasks as $num => $task) { if ($task->getCompleted()) { continue; } if (!$task->getStarted()) { if ($num > 0) { $task->setStarted($tasks[$prevIndex]->getCompleted()); } else { $task->setStarted($task->getGame()->getStartDate()); } $this->manager->persist($task); $this->manager->flush(); } $prevIndex = $num; if (time() - $task->getStarted()->getTimestamp() >= $task->getTask()->getTaskTime() * 60) { $task->setCompleted((new \DateTime())->setTimestamp($task->getStarted()->getTimestamp() + $task->getTask()->getTaskTime() * 60)); $task->setCompleteType(TeamTaskSequence::COMPLETE_TIMEOUT); $this->manager->persist($task); $this->manager->flush(); continue; } if ($this->isLevelCompleted($task->getTask(), $user->getTeam())) { $task->setCompleted(new \DateTime()); $task->setCompleteType(TeamTaskSequence::COMPLETE_CODE); $this->manager->persist($task); $this->manager->flush(); continue; } break; } $currentTask = null; if ($task && !$task->getCompleted()) { $currentTask = $task; $currentTask->getTask()->getCodes()->map(function (Code $code) use($team) { $enteredCodes = $this->getEnteredCodes($team, $code->getTask()); $valid = false; foreach ($enteredCodes as $codeEnter) { $valid = $codeEnter->getCode()->getId() === $code->getId(); if ($valid) { break; } } $code->setCodeEntered($valid); return $code; }); $currentTask->getTask()->getHelps()->map(function (Help $help) use($currentTask) { if ($currentTask->getStarted()->getTimestamp() + $help->getShowTime() * 60 > time()) { $help->setText(''); $help->setTime($currentTask->getStarted()->getTimestamp() + $help->getShowTime() * 60 - time()); } }); } return $currentTask; }
/** * @param User $object */ public function preUpdate($object) { if ($object->getPlainPassword()) { $this->userManager->updatePassword($object); } }