public function fire()
{
$client = $this->client;
if ($this->game->getState() != GameState::NIGHT) {
$client->getChannelGroupOrDMByID($this->channel)->then(function (Channel $channel) use($client) {
$client->send(":warning: You can only kill at night.", $channel);
});
throw new Exception("Killing occurs only during the night.");
}
// Voter should be alive
if (!$this->game->hasPlayer($this->userId)) {
$client->getChannelGroupOrDMByID($this->channel)->then(function (Channel $channel) use($client) {
$client->send(":warning: You aren't alive in the specified channel.", $channel);
});
throw new Exception("Can't kill if dead.");
}
// Person player is voting for should also be alive
if (!$this->game->hasPlayer($this->args[0])) {
$client->getChannelGroupOrDMByID($this->channel)->then(function (Channel $channel) use($client) {
$client->send(":warning: Could not find that player.", $channel);
});
throw new Exception("Voted player not found in game.");
}
// Person should be werewolf
$player = $this->game->getPlayerById($this->userId);
if ($player->role != Role::WEREWOLF) {
$client->getChannelGroupOrDMByID($this->channel)->then(function (Channel $channel) use($client) {
$client->send(":warning: YOu have to be a werewolf to kill.", $channel);
});
throw new Exception("Only werewolves can kill.");
}
if ($this->game->hasPlayerVoted($this->userId)) {
$client->getChannelGroupOrDMByID($this->channel)->then(function (Channel $channel) use($client) {
$client->send(":warning: You have already voted.", $channel);
});
throw new Exception("You have already voted.");
}
$this->game->vote($this->userId, $this->args[1]);
$msg = KillFormatter::format($this->game);
foreach ($this->game->getPlayersOfRole(Role::WEREWOLF) as $player) {
$client->getDMByUserID($player->getId())->then(function (DirectMessageChannel $channel) use($client, $msg) {
$client->send($msg, $channel);
});
}
foreach ($this->game->getPlayersOfRole(Role::WEREWOLF) as $player) {
if (!$this->game->hasPlayerVoted($player->getId())) {
return;
}
}
$votes = $this->game->getVotes();
if (count($votes) > 1) {
$this->game->clearVotes();
foreach ($this->game->getPlayersOfRole(Role::WEREWOLF) as $player) {
$client->getDMByUserID($player->getId())->then(function (DirectMessageChannel $channel) use($client) {
$client->send(":warning: The werewolves did not unanimously vote on a member of the town. Vote again.", $channel);
});
}
return;
}
$this->game->setWolvesVoted(true);
$this->gameManager->changeGameState($this->game->getId(), GameState::DAY);
}