-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsourceCheck.js
More file actions
52 lines (50 loc) · 1.68 KB
/
sourceCheck.js
File metadata and controls
52 lines (50 loc) · 1.68 KB
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
48
49
50
51
52
/*
sourceCheck is a module that provides helpers for checking the source of truth.
**/
module.exports = {
isUsernameUnique: (username, roomData) => {
return roomData.savedPlayersList.indexOf(username) == -1
},
isUsernameConnected: (username, roomData) => {
return roomData.connectedPlayersList.indexOf(username) == -1
},
modifyUsername: (username, roomData) => {
let reg = /\d+$/
let sub = username.match(reg)
// if substring doesn't have any numbers(null) else add on to it
username = sub == null ? username+'1' : username.replace(reg,(Number(sub)+1))
while (roomData.savedPlayersList.indexOf(username) != -1){
sub = username.match(reg)
username = username.replace(reg,(Number(sub)+1))
}
return username
},
diceToss: (sides) => {
let num = Math.floor(Math.random() * sides) + 1
return {result: num, diceType: sides}
}, coinToss: () => {
return Math.floor(Math.random() * 6) + 5;
//return number of flips from 5 to 10
}, choosePlayer: (playersList) => {
let array = [];
if(playersList.length >= 3){
while (array.length < 13){
let num = Math.floor(Math.random() * playersList.length)
array.push(playersList[num])
}
} else if (playersList.length == 2) {
let num = Math.floor(Math.random() * 2);
while (array.length < 13){
array.push(playersList[num])
if(num == 1){
num = 0
} else {
num = 1
}
}
}else {
array = playersList;
}
return array;
}
}