-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auction-bid.ts
67 lines (52 loc) · 2.15 KB
/
auction-bid.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
import { chance, getPlayers, getRoles, hasUsedScroll, sumBy } from './common';
import { logScrolls, logPlayerChances, logRoleChances, logRoles } from './debug';
const roles = getRoles();
const players = getPlayers();
console.time('auction-bid');
// iterate over roles
for (const role of roles) {
// filter out players without a role
const remainingPlayers = players.filter(player => !player.role);
// map out chances for each player based on current role
const chances = remainingPlayers.map(player => {
// find out if a player has a scroll for the current role (use first found)
const scrolls = player.scrolls.filter(scroll => !hasUsedScroll(role, scroll));
// if player has a scroll, return a chance of 100%+{effect??0}%
return 1 + sumBy(scrolls, scroll => scroll?.effect, 0);
});
const isLastPlayer = remainingPlayers.length === 1;
const selectedPlayer = isLastPlayer
? remainingPlayers[0]
: chance.weighted(remainingPlayers, chances);
// assign role to selected player
selectedPlayer.role = role;
// log out the selected player
const playerIndex = remainingPlayers.indexOf(selectedPlayer);
let scaleFactor = sumBy(chances, chance => chance) / chances.length;
for(let i = 0; i < chances.length; i++)
chances[i] = chances[i] / scaleFactor;
const calculatedChance = (chances[playerIndex] / remainingPlayers.length * 100).toFixed(2);
let outcome = `${selectedPlayer.name} (${playerIndex}) is a ${selectedPlayer.role} - ${calculatedChance}%\n`;
// log remaining player chances
for (const [index, player] of remainingPlayers.entries()) {
if(playerIndex === index) {
continue;
}
const chance = (chances[index] / remainingPlayers.length * 100).toFixed(2);
outcome += `\t${player.name} (${index}) had a ${chance}% chance\n`;
}
console.debug(outcome);
}
// determine if any players have used active scrolls
for (const player of players) {
for (const scroll of player.scrolls) {
if (hasUsedScroll(player.role!, scroll)) {
scroll.use();
}
}
}
console.timeEnd('auction-bid');
logRoles(roles);
logScrolls(players);
logPlayerChances(players, roles);
logRoleChances(players, roles);