Slackwolf\Game\Game::vote PHP Method

vote() public method

public vote ( $voterId, $voteForId )
$voterId
$voteForId
    public function vote($voterId, $voteForId)
    {
        if (!isset($this->votes[$voteForId])) {
            $this->votes[$voteForId] = [];
        }
        $this->votes[$voteForId][] = $voterId;
    }

Usage Example

Example #1
0
 public function vote(Game $game, $voterId, $voteForId)
 {
     if (!$game->hasPlayer($voterId)) {
         return;
     }
     if (!$game->hasPlayer($voteForId)) {
         return;
     }
     if ($game->hasPlayerVoted($voterId)) {
         return;
     }
     $game->vote($voterId, $voteForId);
     $voteMsg = VoteSummaryFormatter::format($game);
     $client = $this->client;
     $client->getChannelGroupOrDMByID($game->getId())->then(function (Channel $channel) use($client, $voteMsg) {
         $client->send($voteMsg, $channel);
     });
     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) {
             $players_to_be_lynched[] = $lynch_player_id;
         }
     }
     $lynchMsg = "\r\n: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})";
         $game->removePlayer($player_id);
     }
     $lynchMsg .= implode(', ', $lynchedNames) . "\r\n";
     $client->getChannelGroupOrDMByID($game->getId())->then(function (Channel $channel) use($client, $lynchMsg) {
         $client->send($lynchMsg, $channel);
     });
     $this->changeGameState($game->getId(), GameState::NIGHT);
 }
All Usage Examples Of Slackwolf\Game\Game::vote