-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
112 lines (90 loc) · 2.44 KB
/
Copy pathindex.js
File metadata and controls
112 lines (90 loc) · 2.44 KB
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
/*!
* QuizJs Server
*
* Copyright (c) Alberto Congiu
* @4lbertoC
*
* Released under the MIT license.
*/
'use strict';
var events = require('events');
var CONST = require('./constants');
/**
* @constructor
*/
var QuizJsServer = function() {
this._playerQueue = [];
this._emitter = new events.EventEmitter();
};
/**
* Passes the turn to the next subscriber in the queue.
*/
QuizJsServer.prototype.nextSubscriber = function() {
this._playerQueue.shift();
this._updateState();
};
/**
* Registers a new player to the QuizJs server.
*
* @fires QuizJsServer#EVENT.PLAYER.REGISTER
* @return {number} The new player's ID
*/
QuizJsServer.prototype.registerPlayer = function() {
var playerId = this._registeredIds++;
this._emitter.emit(CONST.EVENT.PLAYER.REGISTER, {
playerId: playerId
});
return playerId;
};
/**
* Empties the current subscriber queue and allows all
* the players to subscribe for a new question.
*
* @fires QuizJsServer#EVENT.STATE.RESET
*/
QuizJsServer.prototype.resetState = function() {
this._playerQueue.length = 0;
this._emitter.emit(CONST.EVENT.STATE.RESET, {});
};
/**
* Subscribes a player to the current question.
*
* @param {number} playerId The ID of the player to subscribe.
*/
QuizJsServer.prototype.subscribe = function(playerId) {
if (this._playerQueue.indexOf(playerId) === -1) {
this._playerQueue.push(playerId);
this._updateState();
}
};
/**
* Updates the listeners with the current state.
*
* @fires QuizJsServer#EVENT.STATE.UPDATE
*/
QuizJsServer.prototype._updateState = function() {
// Can also be undefined
var speakerId = this._playerQueue[0];
var data = {
speakerId: speakerId
};
this._emitter.emit(CONST.EVENT.STATE.UPDATE, data);
};
QuizJsServer.prototype._registeredIds = 1;
QuizJsServer.prototype._playerQueue = null;
QuizJsServer.prototype._emitter = null;
QuizJsServer.prototype.on = function() {
this._emitter.on.apply(this._emitter, arguments);
};
QuizJsServer.prototype.once = function() {
this._emitter.once.apply(this._emitter, arguments);
};
QuizJsServer.prototype.removeListener = function() {
this._emitter.removeListener.apply(this._emitter, arguments);
};
QuizJsServer.prototype.removeAllListener = function() {
this._emitter.removeAllListeners.apply(this._emitter, arguments);
};
QuizJsServer.ACTION = CONST.ACTION;
QuizJsServer.EVENT = CONST.EVENT;
module.exports = QuizJsServer;