-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
49 lines (38 loc) · 1.02 KB
/
helpers.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
48
49
const { GAME_BEAT_RULES } = require("./consts")
const getWinner = (gameResult) => {
const playersIds = Object.keys(gameResult)
if(playersIds.length === 0){
return {
winner: 'none',
reason: 'players didnt play!'
}
}
if(playersIds.length === 1) {
return {
winner: playersIds[0],
reason: 'the opponent left the game'
}
}
const playersTouchs = Object.values(gameResult)
const player1touch = playersTouchs[0];
const player2touch = playersTouchs[1];
if(player2touch == GAME_BEAT_RULES[player1touch]){
return {
winner: playersIds[0],
reason: `${player1touch} beat ${player2touch}`
}
}else if(player2touch == player1touch){
return {
winner: 'none',
reason: 'draw'
}
}else{
return {
winner: playersIds[1],
reason: `${player2touch} beat ${player1touch}`
}
}
}
module.exports = {
getWinner
}