/** * @param $command string command text given in slash command * @param $user string user * @param $channel string channel where command is given * @return string Message to be returned * @throws \InvalidArgumentException Exception message to be returned */ public function execute($command, GameAction $action) { $parts = explode(" ", $command); $cmd = array_shift($parts); $action->setGame($this->gm->getGameInProgress($action->getChannelId())); switch ($cmd) { case 'start': // start {word} {hint} if (count($parts) == 0) { throw new \InvalidArgumentException("You must specify a word"); } $word = array_shift($parts); if (count($parts) > 0) { $hint = implode(" ", $parts); } else { $hint = null; } $this->gm->startGame($action, $word, $hint); return "Game has started"; case 'help': $this->gm->showHelp($action); return ""; case 'hint': // hint {hint} if (count($parts) > 0) { $hint = implode(" ", $parts); $this->gm->changeHint($action, $hint); return "Hint is changed"; } else { throw new \InvalidArgumentException("Please provide a hint"); } case 'guess': // guess {char or word} if (count($parts) > 0) { $word = array_shift($parts); if (strlen($word) == 1) { $this->gm->guessCharacter($action, $word); } else { $this->gm->guessWord($action, $word); } return ""; } else { throw new \InvalidArgumentException("Please provide a character or guess the entire word"); } case 'abort': // abort $this->gm->abort($action); return "Game aborted"; case 'highscore': return "This is not implemented yet, sorry!"; default: throw new \InvalidArgumentException("Invalid command given"); } }
public function startGame(GameAction $action, $word, $hint = null) { if ($action->getGame()) { throw new \InvalidArgumentException("A game is already in progress"); } $game = new Game($action->getPlayerId(), $action->getPlayerName(), $action->getChannelId(), $word); if ($hint) { $game->setHint($hint); } $action->setGame($game); $this->slack->postStarting($action); $this->em->persist($game); $this->em->flush(); return $game; }