public function vote(Game $game, $voterId, $voteForId)
{
if (!$game->isPlayerAlive($voterId)) {
return;
}
if (!$game->isPlayerAlive($voteForId) && ($voteForId != 'noone' || !$this->optionsManager->getOptionValue(OptionName::no_lynch)) && $voteForId != 'clear') {
return;
}
if ($game->dayEnded) {
return;
}
if ($game->hasPlayerVoted($voterId)) {
//If changeVote is not enabled and player has already voted, do not allow another vote
if (!$this->optionsManager->getOptionValue(OptionName::changevote)) {
throw new Exception("Vote change not allowed.");
}
$game->clearPlayerVote($voterId);
}
if ($voteForId != 'clear') {
//if voting for 'clear' just clear vote
$game->vote($voterId, $voteForId);
}
$voteMsg = VoteSummaryFormatter::format($game);
$this->sendMessageToChannel($game, $voteMsg);
if (!$game->votingFinished()) {
return;
}
$votes = $game->getVotes();
$vote_count = [];
foreach ($votes as $lynch_player_id => $voters) {
if (!isset($vote_count[$lynch_player_id])) {
$vote_count[$lynch_player_id] = 0;
}
$vote_count[$lynch_player_id] += count($voters);
}
$players_to_be_lynched = [];
$max = 0;
foreach ($vote_count as $lynch_player_id => $num_votes) {
if ($num_votes > $max) {
$max = $num_votes;
}
}
foreach ($vote_count as $lynch_player_id => $num_votes) {
if ($num_votes == $max && $lynch_player_id != 'noone') {
$players_to_be_lynched[] = $lynch_player_id;
}
}
$lynchMsg = "\r\n";
$hunterMsg = "\r\n";
if (count($players_to_be_lynched) == 0) {
$lynchMsg .= ":peace_symbol: The townsfolk decided not to lynch anybody today.";
} elseif (count($players_to_be_lynched) > 1) {
$lynchMsg .= ":peace_symbol: The townsfolk couldn't agree on who to lynch, so nobody is hung today.";
} else {
$lynchMsg .= ":newspaper: With pitchforks in hand, the townsfolk killed: ";
$lynchedNames = [];
foreach ($players_to_be_lynched as $player_id) {
$player = $game->getPlayerById($player_id);
$lynchedNames[] = "@{$player->getUsername()} ({$player->role->getName()})";
$game->killPlayer($player_id);
if ($player->role->isRole(Role::HUNTER)) {
$game->setHunterNeedsToShoot(true);
$hunterMsg .= ":bow_and_arrow: " . $player->getUsername() . " as hunter you may shoot one person. Type !shoot @playername, or !shoot noone.";
}
}
$lynchMsg .= implode(', ', $lynchedNames) . "\r\n";
}
$this->sendMessageToChannel($game, $lynchMsg);
if ($game->hunterNeedsToShoot) {
$this->sendMessageToChannel($game, $hunterMsg);
}
$game->setDayEnded(true);
$this->changeGameState($game->getId(), GameState::NIGHT);
}