-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
169 lines (152 loc) · 4.89 KB
/
utils.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { Horse, LineUp } from ".";
// 1/1 => 0.5
export const convertFractionOddsToChance = (
oddsNumerator: number,
oddsDenominator: number
): number => {
return 1 / convertFractionOddsToDecimal(oddsNumerator, oddsDenominator);
};
// 1/1 => 2
export const convertFractionOddsToDecimal = (
oddsNumerator: number,
oddsDenominator: number
): number => {
return oddsNumerator / oddsDenominator + 1;
};
export const calculateChances = (
lineUp: LineUp,
type:
| "proportionate"
| "first-horse"
| "equal"
| "odds"
| "custom" = "proportionate"
): number[] => {
const decimalOdds = lineUp.map((horse) =>
convertFractionOddsToDecimal(horse.oddsNumerator, horse.oddsDenominator)
);
const oddsChances = decimalOdds.map((odds) => 100 / odds);
if (type === "proportionate") return adjustChancesProportionally(oddsChances);
if (type === "first-horse") return adjustChancesToFirstHorse(oddsChances);
if (type === "equal") return adjustChancesEqually(oddsChances);
if (type === "odds") return adjustChancesLikeOdds(oddsChances);
if (type === "custom") {
const myOddsChances = oddsChances.map((x) => (x === 50 ? 65 : x));
const chances = adjustChancesEqually(myOddsChances);
return chances;
}
throw new Error("Invalid type");
// const adjustedChances =
// adjustGoodChancesToFirstHorseAndBadChancesToLast(oddsChances);
};
export const adjustChancesLikeOdds = (chances: number[]) => {
return chances.map((x) => x / 100);
};
export const adjustChancesEqually = (chances: number[]) => {
const totalOddsChance = chances.reduce((a, b) => a + b, 0);
const diff = 100 - totalOddsChance;
return chances
.map((chance) => chance + diff / chances.length)
.map((x) => x / 100);
};
export const adjustChancesToFirstHorse = (chances: number[]) => {
const totalOddsChance = chances.reduce((a, b) => a + b, 0);
let diff = 100 - totalOddsChance;
for (let i = 0; i < chances.length; i++) {
if (diff < 0) {
const subtract = Math.min(-diff, chances[i]);
chances[i] -= subtract;
diff += subtract;
} else {
chances[i] += diff;
diff = 0;
}
}
return chances.map((x) => x / 100);
};
export const adjustChancesToFirstHorse2 = (chances: number[]) => {
const totalOddsChance = chances.reduce((a, b) => a + b, 0);
let diff = 100 - totalOddsChance;
return chances
.map((x) => {
return Math.max(0, x + diff);
})
.map((x) => x / 100);
};
export const adjustGoodChancesToFirstHorseAndBadChancesToLast = (
chances: number[]
) => {
const totalOddsChance = chances.reduce((a, b) => a + b, 0);
let diff = 100 - totalOddsChance;
const shouldReverse = diff < 0;
if (shouldReverse) chances = chances.reverse();
for (let i = 0; i < chances.length; i++) {
if (diff < 0) {
const subtract = Math.min(-diff, chances[i]);
chances[i] -= subtract;
diff += subtract;
} else {
chances[i] += diff;
diff = 0;
}
}
if (shouldReverse) chances = chances.reverse();
return chances.map((x) => x / 100);
};
const adjustChancesProportionally = (chances: number[]) => {
const totalOddsChance = chances.reduce((a, b) => a + b, 0);
return chances.map((chance) => chance / totalOddsChance);
};
export const sortByOdds = (lineUp: LineUp) => {
return lineUp.slice().sort((a, b) => {
const oddsA = convertFractionOddsToDecimal(
a.oddsNumerator,
a.oddsDenominator
);
const oddsB = convertFractionOddsToDecimal(
b.oddsNumerator,
b.oddsDenominator
);
return oddsA - oddsB;
});
};
// simulate the game and randomly pick a winner based on the odds
export const determineWinner = (lineUp: LineUp): Horse => {
const chances = calculateChances(lineUp);
const random = Math.random();
let sum = 0;
for (let i = 0; i < chances.length; i++) {
sum += chances[i];
if (random <= sum) {
// console.log(chances, random, i);
return lineUp[i];
}
}
return lineUp[chances.length - 1];
};
export const generateLineUp = (horses: Horse[]): LineUp => {
const lineUp: LineUp = [];
const horsesOptions = [...horses];
for (let group of ["favourites", "outsiders", "underdogs"]) {
for (let i = 0; i < 2; i++) {
const horsesInGroup = horsesOptions.filter(
(horse) => horse.group === group
);
const randomIndex = randomIntFromInterval(0, horsesInGroup.length - 1);
const horse = horsesInGroup[randomIndex];
lineUp.push(horse);
horsesOptions.splice(horsesOptions.indexOf(horse), 1);
}
}
return lineUp;
};
function randomIntFromInterval(min: number, max: number) {
// min and max included
return Math.floor(Math.random() * (max - min + 1) + min);
}
const DataSources = ["1st", "3rd", "all"] as const;
export type DataSource = (typeof DataSources)[number];
export const isValidDataSource = (
dataSource: unknown
): dataSource is DataSource =>
typeof dataSource === "string" && DataSources.includes(dataSource as any);