forked from rauchg/socket.io-computer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turn.js
33 lines (27 loc) · 774 Bytes
/
turn.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
var turnQueue = [];
var activeTurn = false;
var redis = require('./redis').emu();
var TURN_TIME = 15000;
module.exports.push = function(sockid) {
turnQueue.push(sockid);
}
module.exports.checkQueue = checkQueue;
function checkQueue(newReq) {
var io = require('socket.io-emitter')(redis, {key: 'xpemu'});
if (!activeTurn && turnQueue.length >= 1) {
activeTurn = true;
var sockid = turnQueue.shift();
io = io.in(sockid);
io.emit('your-turn');
setTimeout(function() {
io.emit('lose-turn');
activeTurn = false;
checkQueue(false);
}, TURN_TIME);
} else if (newReq) {
var time = turnQueue.length * TURN_TIME;
var sockid = turnQueue[turnQueue.length - 1];
io = io.in(sockid);
io.emit('turn-ack', time);
}
}