This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
livestatus.js
83 lines (66 loc) · 3.15 KB
/
livestatus.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
var gameState = require('./state');
var radio = require('radio');
var capture = require('./capture');
module.exports = LiveStatus;
function LiveStatus(io) {
if (!(this instanceof LiveStatus)) return new LiveStatus(io);
this.io = io;
}
LiveStatus.prototype.start = function start() {
var self = this;
// when receiving a radio message telling us to update the client map, update the client map.
radio('update').subscribe(function update() {
console.log('>> received radio transmission UPDATE');
self.io.emit('state', gameState);
});
self.io.on('connection', function(socket){
console.log('a user connected');
// send up-to-date map data to new connections
socket.emit('state', gameState);
socket.on('querystate', function(data) {
self.io.to('/').emit('state', gameState );
});
socket.on('sitrep', function(data) {
console.log('state update report received');
console.log(data);
var controlPoint, state;
typeof data.controlPoint === 'undefined' ? controlPoint = null : controlPoint = data.controlPoint;
typeof data.state === 'undefined' ? state = null : state = data.state;
capture.adminAbsolute(controlPoint, state)
.then(function(result) {
console.log('livestatus::sitrep control point %s changed to state %s', controlPoint, state);
})
.catch(function(reason) {
console.log('livestatus::sitrep error. no state change condition satisfied. reason is "%s"', reason);
});
});
socket.on('capture', function(data) {
console.log('capture report received');
console.log(data);
var controlPoint = data.controlPoint;
var team = data.team;
capture.capmantle(controlPoint, team)
.then(function(result) {
var controlPoint = result.controlPoint;
var team = result.team;
var captureTime = result.captureTime;
console.log('control point %s captured by team %s at %s', controlPoint, team, captureTime.format());
self.io.emit('update', {'controlPoint': controlPoint, 'team': team, 'captureTime': captureTime});
// check win conditions
capture.checkWinConditions(controlPoint, team, captureTime)
.then(function(res) {
endGame(res.controlPoint, res.team, res.captureTime, res.message);
})
.catch(function(reason) {
console.log('no win condition satisfied. reason is "%s"', reason);
});
})
.catch(function(reason) {
console.log('no capture condition satisfied. reason is "%s"', reason);
});
});
socket.on('disconnect', function() {
console.log('user disconnected');
});
});
};