-
Notifications
You must be signed in to change notification settings - Fork 7
/
firstBlood.ts
38 lines (34 loc) · 1.24 KB
/
firstBlood.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
import _ from "lodash";
import { StatDefinition } from "../types";
export const firstBlood: StatDefinition = {
name: "First Blood",
type: "number",
betterDirection: "higher",
recommendedRounding: 0,
calculate(games, playerIndex) {
// For each game return either the first blood stock if taken or null if lost
const firstBloodStocks = games.map((game, i) => {
const deathStocks = game.stats.stocks.filter((stock) => {
const hasEndPercent = stock.endPercent !== null;
return hasEndPercent;
});
const orderedDeathStocks = _.orderBy(deathStocks, ["endFrame"], ["asc"]);
const firstStock = orderedDeathStocks[0];
if (!firstStock || firstStock.playerIndex === playerIndex) {
// console.log(`player ${playerIndex} did not draw first blood in game ${i + 1}`);
return null;
}
return firstStock;
});
const firstBloodCount = firstBloodStocks.reduce((count, item) => (item !== null ? count + 1 : count), 0);
const ratio = firstBloodCount / firstBloodStocks.length;
const simple = {
text: isNaN(ratio) ? "N/A" : `${(ratio * 100).toFixed(this.recommendedRounding)}%`,
number: ratio,
};
return {
result: firstBloodStocks,
simple,
};
},
};