-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstats.ts
92 lines (86 loc) · 2.46 KB
/
stats.ts
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
84
85
86
87
88
89
90
91
92
import * as R from "ramda";
import { Player, PlayerStats } from "./types";
import * as db from "./db";
import logger from "./logger";
export const addStats = async (
player: Player,
fn: (input: PlayerStats) => PlayerStats
) => {
const stats = await db.userStats(player.id);
const stats_ = fn(stats);
// logger.debug("stats", stats_);
return await db.updateUserStats(player.id, stats_);
};
export const addRoll = async (
attacker: Player,
fromRoll: number[],
defender: Player | null,
toRoll: number[]
) => {
const fromPoints = fromRoll.length;
const toPoints = toRoll.length;
const fromSum = R.sum(fromRoll);
const toSum = R.sum(toRoll);
if (!attacker.bot) {
await addStats(attacker, stats => {
let luck = stats.luck ?? [0, 0];
if (fromPoints <= toPoints && fromSum > toSum) {
luck = [luck[0] + 1, luck[1]];
} else if (fromPoints > toPoints && fromSum < toSum) {
luck = [luck[0], luck[1] + 1];
}
return {
...stats,
rolls: addRolls(stats.rolls, fromRoll),
attacks: addAttacks(stats.attacks, R.sum(fromRoll) > R.sum(toRoll)),
luck,
};
});
}
if (defender && !defender.bot) {
await addStats(defender, stats => {
let luck = stats.luck ?? [0, 0];
if (fromPoints < toPoints && fromSum > toSum) {
luck = [luck[0], luck[1] + 1];
} else if (fromPoints >= toPoints && fromSum < toSum) {
luck = [luck[0] + 1, luck[1]];
}
return { ...stats, rolls: addRolls(stats.rolls, toRoll), luck };
});
}
};
export const addElimination = async (player: Player, position) => {
await addStats(player, stats => {
const eliminations = stats.eliminations ?? [0, 0, 0, 0, 0, 0, 0, 0, 0];
eliminations[position - 1] = eliminations[position - 1] + 1;
return { ...stats, eliminations };
});
};
export const addKill = async (player: Player) => {
await addStats(player, stats => {
const kills = (stats.kills ?? 0) + 1;
return { ...stats, kills };
});
};
const addRolls = (
rolls: [number, number, number, number, number, number] | undefined = [
0,
0,
0,
0,
0,
0,
],
roll: number[]
) => {
return roll.reduce((rolls, die) => {
rolls[die - 1] = (rolls[die - 1] ?? 0) + 1;
return rolls;
}, rolls);
};
const addAttacks = (
attacks: [number, number] | undefined = [0, 0],
success: boolean
): [number, number] => {
return success ? [attacks[0] + 1, attacks[1]] : [attacks[0], attacks[1] + 1];
};