-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbet.js
46 lines (39 loc) · 1.29 KB
/
bet.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
const readline = require('readline');
const { printResults, calculateStakes, orderItems } = require('./utils');
class Bet {
constructor() {
this.data = [];
}
async start() {
await this.readContents();
const { pools, result } = orderItems(this.data);
const race = this.calculateResults(pools, result);
printResults({ ...race, result });
}
async readContents() {
return await new Promise((resolve, reject) => {
const rl = readline.createInterface({
terminal: true,
output: process.stdout,
input: process.stdin,
});
rl.on('line', async (line) => this.data.push(line));
rl.on('close', () => resolve(this.data));
});
}
calculateResults(pools, result) {
const win = calculateStakes('w', result.first, pools);
const p1 = calculateStakes('p', result.first, pools);
const p2 = calculateStakes('p', result.second, pools);
const p3 = calculateStakes('p', result.third, pools);
const exacta = calculateStakes('e', [result.first, result.second], pools);
return {
win,
p1,
p2,
p3,
exacta,
};
}
}
module.exports = Bet;