public function fire()
{
$client = $this->client;
$gameManager = $this->gameManager;
$message = $this->message;
$loadPlayers = true;
// Check to see that a game does not currently exist
if ($this->gameManager->hasGame($this->channel)) {
if ($this->game->getState() == GameState::LOBBY) {
$loadPlayers = false;
if (count($this->args) > 0 && count($this->game->getLobbyPlayers()) > 0) {
$this->client->getChannelGroupOrDMByID($this->channel)->then(function (ChannelInterface $channel) use($client) {
$client->send('A game lobby is open, you must !end the current game before starting a new one specifying players.', $channel);
});
return;
}
} else {
$this->client->getChannelGroupOrDMByID($this->channel)->then(function (ChannelInterface $channel) use($client) {
$client->send('A game is already in progress.', $channel);
});
return;
}
}
if ($loadPlayers) {
$this->client->getChannelGroupOrDMByID($this->channel)->then(function (Channel $channel) {
return $channel->getMembers();
})->then(function (array $users) use($gameManager, $message, $client) {
/** @var \Slack\User[] $users */
$this->filterChosen($users);
if (count($users) < 3) {
$this->client->getChannelGroupOrDMByID($this->channel)->then(function (ChannelInterface $channel) use($client) {
$client->send("Cannot start a game with less than 3 players.", $channel);
});
return;
}
try {
$gameManager->newGame($message->getChannel(), $users, new RoleStrategy\Classic());
} catch (Exception $e) {
$this->client->getChannelGroupOrDMByID($this->channel)->then(function (ChannelInterface $channel) use($client, $e) {
$client->send($e->getMessage(), $channel);
});
}
});
}
$gameManager->startGame($message->getChannel());
}