-
Notifications
You must be signed in to change notification settings - Fork 1
/
turn.js
47 lines (42 loc) · 1.37 KB
/
turn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { getUsersInRoom, changeTurn } from "./users.js";
import { chooseWord } from "./words.js";
// list of rounds per room
const rounds = {};
// A function to add a room to the rounds object with start round value to 0
export const addRound = (room) => {
const round = 1;
rounds[room] = round;
return round;
};
// A function increase the round by 1
export const increaseRound = (room) => {
const old = rounds[room];
const n = old + 1;
rounds[room] = n;
return n;
};
// A function to get round value for a room
export const getRound = (room) => {
return rounds[room];
};
// A function to choose the next person to draw in a round
export const whoseTurn = (room) => {
const users = getUsersInRoom(room);
const users_false = users.filter((u) => u.turn == false);
if (users_false.length > 0) {
const chosen = users_false[Math.floor(Math.random() * users_false.length)];
changeTurn(chosen.id, true);
const round = getRound(room);
const { word1, word2, word3 } = chooseWord(round, room);
return { chosen, word1, word2, word3, round };
} else {
const round = increaseRound(room);
const usersReset = users.map((u) => changeTurn(u.id, false));
const chosen = users[Math.floor(Math.random() * users.length)];
if (chosen) {
changeTurn(chosen.id, true);
}
const { word1, word2, word3 } = chooseWord(round, room);
return { chosen, word1, word2, word3, round };
}
};