-
Notifications
You must be signed in to change notification settings - Fork 1
/
arber.ts
176 lines (152 loc) · 4.13 KB
/
arber.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
170
171
172
173
174
175
176
import { Bookie } from '@bookies';
import { WSBroker } from '@broker/broker';
import { ArberStatus } from '@broker/models/arber-instance';
import { WSStore } from '@broker/store';
import { BookieName, BookieRetrieverTuple } from '@models';
import { Money } from '@money/types';
import { Subject } from 'rxjs';
import { container } from 'tsyringe';
import { v4 } from 'uuid';
export abstract class Arber {
public readonly id: string;
public readonly name: string;
public closed = new Subject<boolean>();
// Broker
// protected wsBroker: WSBroker;
// Block rxjs chain
protected blocked = false;
// Bookie Childs intances
protected instances: Bookie[] = [];
protected store: WSStore;
constructor(
protected retrievers: BookieRetrieverTuple[],
protected investment: Money,
) {
// Assign unique id for this arber
this.id = v4();
this.name = this.constructor.name;
// Initialize controls
const readline = require('readline');
readline.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) process.stdin.setRawMode(true);
this.listenKeyboard();
// Save instances
this.instances = this.retrievers.map((r) => r.bookie);
// Resolve store
this.store = container.resolve(WSStore);
this.store.addArber(this);
// Notify WS Broker
// this.wsBroker = container.resolve(WSBroker);
// this.wsBroker.arberCreated(
// this.id,
// this.constructor.name,
// this.instances.map((i) => i.id),
// this.investment,
// );
}
/**
* Starts listening to keyboard events
*/
private listenKeyboard() {
process.stdin.on('keypress', async (chunk, key) => {
// console.log(key);
// Init pause and resume controls
if (key && key.name == 'p') {
this.pause();
}
if (key && key.name == 'r') {
this.resume();
}
// When terminating
if (key && key.sequence == '\x03') {
this.close();
this.onClose();
this.closed.next(true);
}
});
}
/**
* Pauses arber
*/
public pause() {
console.log(`Pausing arber ${this.name}: ${this.id}`);
this.blocked = true;
this.wsBroker.arberPaused(this.id);
}
/**
* Resumes arber
*/
public resume() {
console.log(`Resuming arber ${this.name}: ${this.id}`);
this.blocked = false;
this.wsBroker.arberResumed(this.id);
}
/**
* Updates arber investment
* @param money
*/
public setInvestment(money: Money) {
this.investment = money;
this.wsBroker.arberInvestmentUpdated(this.id, money);
}
/**
* Notifies when arber is postulating
*
* @optional bookies Bookies to set with postulate state
*/
public notifyPostulating(bookies: Bookie[] = []) {
bookies.forEach((b) => b.postulating());
this.wsBroker.arberStatusChanged(this.id, ArberStatus.Postulating);
}
/**
* Notifies when arber is placing
*
* @optional bookies Bookies to set with postulate state
*/
public notifyPlacing(bookies: Bookie[] = []) {
bookies.forEach((b) => b.placing());
this.wsBroker.arberStatusChanged(this.id, ArberStatus.Placing);
}
/**
* Notifies when arber is placing
*
* @optional bookies Bookies to set with postulate state
*/
public notifyPlaced(bookies: Bookie[] = []) {
bookies.forEach((b) => b.placed());
this.wsBroker.arberStatusChanged(this.id, ArberStatus.Placed);
}
/**
* Notifies when arber back to inital state
*
* @optional bookes Bookies to set to initial state
*/
public resetStatus(bookies: Bookie[] = []) {
bookies.forEach((b) => b.resetStatus());
this.wsBroker.arberStatusChanged(this.id, ArberStatus.Created);
}
/**
* Closes arber
* @returns
*/
public close() {
const skip: BookieName[] = [BookieName.Bet365];
// Close bookies. No need to wait
this.instances.forEach((i) => {
if (!skip.includes(i.name)) {
return i.close();
}
});
// Close this instance
this.store.closeArber(this.id);
this.wsBroker.arberClosed(this.id);
}
/**
* Fire arber
*/
protected abstract start(): void;
/**
* On close event
*/
protected abstract onClose(): void;
}