-
Notifications
You must be signed in to change notification settings - Fork 5
/
results.js
83 lines (77 loc) · 2.49 KB
/
results.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
export const GameResultSimplified = {
WIN: 'win',
LOSS: 'loss',
TIE: 'tie',
};
export const GameResult = {
WIN: 'win', //Win
CHECKMATED: 'checkmated', //Checkmated
AGREED: 'agreed', //Draw agreed
REPETITION: 'repetition', //Draw by repetition
TIMEOUT: 'timeout', //Timeout
RESIGNED: 'resigned', //Resigned
STALEMATE: 'stalemate', //Stalemate
LOSE: 'lose', //Lose
INSUFFICIENT: 'insufficient', //Insufficient material
MOVE50: '50move', //Draw by 50-move rule
ABANDONED: 'abandoned', //Abandoned
KINGOFTHEHILL: 'kingofthehill', //Opponent king reached the hill
THREECHECK: 'threecheck', //Checked for the 3rd time
TIMEVSINSUFFICIENT: 'timevsinsufficient', //Draw by timeout vs insufficient material
BUGHOUSEPARTNERLOSE: 'bughousepartnerlose', //Bughouse partner lost
simplified: function (str) {
switch (str) {
case GameResult.WIN:
return GameResultSimplified.WIN;
case GameResult.AGREED:
case GameResult.REPETITION:
case GameResult.STALEMATE:
case GameResult.INSUFFICIENT:
case GameResult.MOVE50:
case GameResult.TIMEVSINSUFFICIENT:
return GameResultSimplified.TIE;
case GameResult.CHECKMATED:
case GameResult.TIMEOUT:
case GameResult.RESIGNED:
case GameResult.LOSE:
case GameResult.ABANDONED:
case GameResult.KINGOFTHEHILL:
case GameResult.THREECHECK:
case GameResult.BUGHOUSEPARTNERLOSE:
return GameResultSimplified.LOSS;
default:
throw new Error('Invalid game result string: ' + str);
}
},
};
/**
* Gives won, lost, or drew based on the result.
* @param {string} result - The result of the game ('win', 'resigned', 'lose', 'checkmated' aka GameResult)
* @returns {[number, number, number]} - An array with the updated win, loss, and draw counts.
*/
export function getResults(result) {
let win = 0;
let loss = 0;
let draw = 0;
try {
// If the result string is valid and has a corresponding GameResult object
// Get the simplified result (win, loss, or tie)
const simplifiedResult = GameResult.simplified(result.toLowerCase());
switch (simplifiedResult) {
case GameResultSimplified.WIN:
win = 1;
break;
case GameResultSimplified.LOSS:
loss = 1;
break;
case GameResultSimplified.TIE:
draw = 1;
break;
}
} catch (err) {
// If the result string is not valid, display an error message
console.log(err.message);
return [0, 0, 0];
}
return [win, loss, draw];
}