Prohibit voting for dead people and fix vote logging

Previously, on top of allowing voting dead people, it also
allowed vote logging even on invalid votes. Fix that by
moving the logging below the checking.
This commit is contained in:
Ultracoolguy 2020-11-08 12:24:23 -04:00 committed by Eyal Sawady
parent 96448759d6
commit b22111593d
No known key found for this signature in database
GPG Key ID: 604D3459E53A9952
1 changed files with 10 additions and 5 deletions

15
main.c
View File

@ -658,16 +658,21 @@ discussion(size_t pid, char *input)
}
}
if (vote == -1) {
printf("[%s] voted to skip\n", players[pid].name);
} else {
printf("[%s] voted for %jd\n", players[pid].name, vote);
}
if(vote < -1 || vote > NUM_PLAYERS-1 || players[vote].fd == -1) {
snprintf(buf, sizeof(buf), "Invalid vote, no such player\n");
write(players[pid].fd, buf, strlen(buf));
return;
}
if (!alive(players[vote])) {
snprintf(buf, sizeof(buf), "Invalid vote, that person is dead\n");
write(players[pid].fd, buf, strlen(buf));
return;
}
if (vote == -1) {
printf("[%s] voted to skip\n", players[pid].name);
} else {
printf("[%s] voted for %jd\n", players[pid].name, vote);
}
players[pid].voted = 1;
if (vote == -1) {
state.skips++;