Skip to content

Commit

Permalink
update the mission entry
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybesson committed Jul 25, 2022
1 parent 8936879 commit 707b29d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/Controller/Frontend/CrosswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ public function playAction()
$letters = str_split($word->getSolution());
foreach($letters as $letter) {
if ($word->getOrientation() == "across") {
$grid[$word->getLayoutRow()][$word->getLayoutColumn() + $position] = strtolower($letter);
$grid[$word->getLayoutRow()][$word->getLayoutColumn() + $position] = strtoupper($letter);
} else {
$grid[$word->getLayoutRow() + $position][$word->getLayoutColumn()] = strtolower($letter);
$grid[$word->getLayoutRow() + $position][$word->getLayoutColumn()] = strtoupper($letter);
}
$position++;
}
Expand Down
14 changes: 9 additions & 5 deletions src/Controller/Frontend/MissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,23 @@ public function playAction()
$subViewModel = $this->forward()->dispatch(
$classGame,
array(
'controller' => $classGame,
'action' => 'home',
'id' => $subGame->getIdentifier()
'controller' => $classGame,
'action' => 'home',
'id' => $subGame->getIdentifier()
)
);

// If the subgame redirect to the result page
if ($this->getResponse()->getStatusCode() == 302) {

$entry = $this->getGameService()->missionWinner($this->game, $this->user, $entry, $subGame);
return $this->redirect()->toUrl(
$this->frontendUrl()->fromRoute(
'mission/result',
array(
'id' => $this->game->getIdentifier(),
'gameId' => $subGame->getIdentifier()
'id' => $this->game->getIdentifier(),
'gameId' => $subGame->getIdentifier(),
'missionEntry' => $entry,
),
array('force_canonical' => true)
)
Expand Down
78 changes: 58 additions & 20 deletions src/Service/Mission.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class Mission extends Game
* @var MissionMapperInterface
*/
protected $missionMapper;

/**
* @var MissionGameMapperInterface
*/
protected $missionGameMapper;

protected $options;

/**
* Find games associated to a mission and add the last entry of the user if it exists
* @param unknown $mission
Expand All @@ -35,10 +35,10 @@ public function getMissionGames($mission, $user)
$games[$missionGame->getGame()->getIdentifier()]['game'] = $missionGame;
$games[$missionGame->getGame()->getIdentifier()]['entry'] = $entry;
}

return $games;
}

/**
* findMissionGameByMission : find associated games to a mission
* @param Mission $mission
Expand All @@ -49,52 +49,52 @@ public function findGamesByMission($mission)
{
return $this->getMissionGameMapper()->findBy(array('mission'=>$mission));
}

public function checkMissionCondition($game, $winner, $prediction, $entry)
{
$missionGame = $this->findMissionGameByGame($game);
if (empty($missionGame)) {
return false;
}

if ($missionGame->getMission()->getActive() === false) {
return false;
}

$nextMissionGame = $this->getMissionGameMapper()->getNextGame(
$missionGame->getMission(),
$missionGame->getPosition()
);

if (empty($nextMissionGame)) {
return false;
}

$missionGameConditions = $this->findMissionGameConditionByMissionGame($nextMissionGame);

if (empty($missionGameConditions)) {
return false;
}

foreach ($missionGameConditions as $missionGameCondition) {
if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::NONE) {
continue;
}

// On passe au suivant si on a gagné
if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::VICTORY) {
if (!($winner || $prediction)) {
return false;
}
}

// On passe au suivant si on a perdu
if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::DEFEAT) {
if ($winner || $prediction) {
return false;
}
}

// On passe au suivant si on a perdu
if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::GREATER) {
if (!$entry) {
Expand All @@ -104,7 +104,7 @@ public function checkMissionCondition($game, $winner, $prediction, $entry)
return false;
}
}

// On passe au suivant si on a perdu
if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::LESS) {
if (!$entry) {
Expand All @@ -115,10 +115,48 @@ public function checkMissionCondition($game, $winner, $prediction, $entry)
}
}
}

return $nextMissionGame->getGame();
}

public function missionWinner($game, $user, $entry, $subGame)
{
$entry->setStep($entry->getStep() + 1);
$lastEntry = $this->findLastInactiveEntry($subGame, $user);
$entry->setPoints($entry->getPoints() + $lastEntry->getPoints());

if (
$lastEntry->getWinner() &&
(
$entry->getWinner() !== false ||
$entry->getStep() <= 1
)
) {
$entry->setWinner(true);
}

if ($entry->getStep() == count($game->getMissionGames())) {
$entry->setActive(false);
}
$entry = $this->getEntryMapper()->update($entry);

if (!$entry->getActive()) {
$this->getEventManager()->trigger(
__FUNCTION__ .'.post',
$this,
array('user' => $user, 'entry' => $entry, 'game' => $game)
);
} else {
$this->getEventManager()->trigger(
__FUNCTION__ .'.step',
$this,
array('user' => $user, 'entry' => $entry, 'game' => $game, 'subGame' => $subGame)
);
}

return $entry;
}

public function getGameEntity()
{
return new \PlaygroundGame\Entity\Mission;
Expand Down Expand Up @@ -150,7 +188,7 @@ public function setMissionMapper($missionMapper)

return $this;
}

/**
* getMissionGameMapper
*
Expand All @@ -161,10 +199,10 @@ public function getMissionGameMapper()
if (null === $this->missionGameMapper) {
$this->missionGameMapper = $this->serviceLocator->get('playgroundgame_mission_game_mapper');
}

return $this->missionGameMapper;
}

/**
* setMissionMapper
*
Expand All @@ -174,7 +212,7 @@ public function getMissionGameMapper()
public function setMissionGameMapper($missionGameMapper)
{
$this->missionGameMapper = $missionGameMapper;

return $this;
}
}

0 comments on commit 707b29d

Please sign in to comment.