-
Notifications
You must be signed in to change notification settings - Fork 0
/
leg-handler.js
189 lines (167 loc) · 5.01 KB
/
leg-handler.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
const debug = require('debug')('kcapp-sio-client:leg-handler');
const io = require("socket.io-client");
const DART_MISS = { score: 0, multiplier: 1 };
/**
* Handle a connected event
* @param {object} data
*/
function onConnected(data) {
onScoreUpdate.bind(this)(data);
debug(`Connected to /legs/${this.id}`);
this.connectCallback(this);
}
/**
* Handle a score_update event
* @param {object} data - Data
*/
function onScoreUpdate(data) {
const players = data.players;
this.currentPlayer = players.find(player => player.player_id === data.leg.current_player_id);
this.leg = data.leg;
this.players = players;
// Reset variables keeping track of throws
this.throws = [];
this.dartsThrown = 0;
}
/**
* Handle possible throw event
* @param {object} data - Data
*/
function onPossibleThrow(data) {
if (data.origin === this.origin) {
// We don't need to handle updates from ourselves
return;
}
if (data.is_undo) {
this.dartsThrown--;
this.throws.splice(-1, 1);
} else {
this.throws.push({ score: data.score, multiplier: data.multiplier });
this.dartsThrown++;
}
}
/**
* Handle order changed events by updating current player
* @param {object} data - Data
*/
function onOrderChanged(data) {
const players = data.players;
this.leg = data.leg;
this.players = players;
this.currentPlayer = players.find(player => player.player_id === data.leg.current_player_id);
}
/**
* Register a callback for the given event
* @param {string} event - Socket IO Event to register callback for
* @param {function} callback - Callback when event is emitted
*/
exports.on = (event, callback) => {
this.socket.on(event, callback);
}
/**
* Emit the given event, with given data
* @param {string} - Socket IO event to emit
* @param {object} - Data to emit with event
*/
exports.emit = (event, data) => {
this.socket.emit(event, data);
}
/**
* Emit a throw, indicating that the given dart was just thrown
*
* @param {object} dart - Dart thrown
*/
exports.emitThrow = (dart) => {
this.dartsThrown++;
this.throws.push(dart);
const payload = {
current_player_id: this.currentPlayer.player_id,
score: dart.score,
multiplier: dart.multiplier,
darts_thrown: this.dartsThrown,
is_undo: false,
origin: this.origin
}
this.socket.emit('possible_throw', payload);
}
/**
* Emit a throw, indicating that the given dart was just thrown
*
* @param {object} dart - Dart thrown
*/
exports.undoThrow = (dart) => {
this.dartsThrown--;
this.throws.splice(-1, 1);
const payload = {
current_player_id: this.currentPlayer.player_id,
score: dart.score,
multiplier: dart.multiplier,
darts_thrown: this.dartsThrown,
is_undo: true,
origin: this.origin
}
this.socket.emit('possible_throw', payload);
}
/**
* Emit a visit, this will confirm the visit, and write all values to database
*/
exports.emitVisit = () => {
if (this.dartsThrown < 3) {
for (let i = this.dartsThrown; i < 3; i++) {
this.emitThrow(DART_MISS);
}
}
const payload = {
player_id: this.currentPlayer.player_id,
leg_id: this.id,
first_dart: { value: this.throws[0].score, multiplier: this.throws[0].multiplier },
second_dart: { value: this.throws[1].score, multiplier: this.throws[1].multiplier },
third_dart: { value: this.throws[2].score, multiplier: this.throws[2].multiplier }
};
this.socket.emit('throw', JSON.stringify(payload));
}
/**
* Connect to namespace of the given leg
* @param {int} id - Leg ID
*/
exports.connect = (id, callback) => {
this.id = id;
this.connectCallback = callback;
this.socket = io(`${this.baseURL}/legs/${id}`);
// Update leg, players, etc when score is updated
this.socket.on('connect', (data) => {
this.socket.emit('join', 'Client Connecting');
});
this.socket.on('connected', onConnected.bind(this));
this.socket.on('score_update', onScoreUpdate.bind(this));
this.socket.on('possible_throw', onPossibleThrow.bind(this));
this.socket.on('order_changed', onOrderChanged.bind(this));
this.socket.io.on('reconnect', () => {
debug(`Reconnected to '/legs/${id}'`);
});
this.socket.on('leg_finished', () => {
// Make sure we disconnect once leg is finished, to clean up our connections
this.socket.disconnect();
});
this.socket.on('disconnect', (reason) => {
debug(`Disconnected from '/legs/${id}' because: "${reason}"`);
});
return this;
}
/**
* Disconnect from the socket
*/
exports.disconnect = () => {
this.socket.disconnect();
debug(`Disconnected from "${this.socket.nsp}"`);
}
/**
* Configure the leg-handler
* @param {string} baseURL - BaseURL of socket.io endpoint
* @param {string} origin - Origin of events to send when emitting throws
*/
module.exports = (baseURL, origin) => {
this.baseURL = baseURL;
this.origin = origin;
return this;
}