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
/
timer.js
63 lines (51 loc) · 2.21 KB
/
timer.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
/**
* timer submodule
*
* handles updating the controlpoints.
*
* for example, a red player gets to uncaptured trollBridge, and scans the QR code.
* the state changes from UNK to CRE.
* without a timer, the state would forever remain in CRE.
* the timer uses gameState.controlPoints.timers to find the length of time that trollBridge
* should stay in CRE before updating the controlpoint to RED.
*
* The way timers are entered in gameState.controlPoints.timers is as follows.
* "stay in this state for this length of time, before automatically switching to the next logical state"
*/
var gameState = require('./state');
var capture = require('./capture');
var moment = require('moment');
var timerHandle = '';
var start = module.exports.start = function start() {
timerHandle = setInterval(tick, 200);
}
var tick = module.exports.tick = function tick() {
// for each controlpoint, find out if a state update is due.
var cps = gameState.controlPoints;
for (var c in cps) {
var state = cps[c].state;
if (state === 'unk' || state === 'red' || state === 'blu') {
//console.log('state=%s cp=%s so doing nothing', state, c);
}
else {
var updateTime = moment(cps[c].updateTime);
var relevantTimer = gameState.timers[state];
var dueTime = moment(updateTime).add(relevantTimer, 'ms');
var direction = cps[c].direction;
if (moment().isAfter(dueTime)) {
//console.log('%s is overdue for a state update. (updateTime=%s, relevantTimer=%s, dueTime=%s state=%s direction=%s)', c, updateTime.format(), relevantTimer, dueTime.format(), state, direction);
return capture.adminAdvance(c, direction)
.then(function(result) {
console.log(result);
console.log('timer sending socket update.');
})
.catch(function(e) {
console.error(e);
})
}
else {
//console.log('%s is not due for a state update. (updateTime=%s, relevantTimer=%s, dueTime=%s state=%s direction=%s)', c, updateTime.format(), relevantTimer, dueTime.format(), state, direction);
}
}
}
}