-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroom.js
149 lines (127 loc) · 3.81 KB
/
room.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
var Room = function(roomId, maxPlayers, sendData, getWord) {
this.roomId = roomId;
this.maxPlayers = maxPlayers;
this.playerData = {};
this.state = 'EMPTY';
this.sendData = sendData;
this.drawId = 0;
this.getWord = getWord;
this.currentWord = "Waiting For Players";
this.currentHelp = "Waiting For Players";
this.roundId = 0;
};
Room.prototype.addPlayer = function(id, name) {
this.playerData[id] = [name, 0];
if (this.getPlayers() > 1) {
if (this.state != 'PLAYING') {
this.state = 'PLAYING';
this.nextRound();
return;
}
} else {
this.state = 'WAITING';
}
this.sendGameData();
};
Room.prototype.removePlayer = function(id) {
delete(this.playerData[id]);
if (this.getPlayers() < 1) {
this.state = 'EMPTY';
} else if (this.getPlayers() === 1) {
this.state = 'WAITING';
this.currentWord = "Waiting For Players";
this.currentHelp = "Waiting For Players";
} else {
if (id == this.drawId) {
this.nextRound();
}
}
this.sendGameData();
};
Room.prototype.getState = function(id) {
return this.state;
};
Room.prototype.getPlayers = function() {
return Object.keys(this.playerData).length;
};
// TODO: Need to sanitize data
Room.prototype.onNewData = function(senderId, data) {
switch (data[0]) {
case "chat":
this.onNewChat(senderId, data[1]);
break;
case "setName":
this.playerData[senderId][0] = data[1];
this.sendGameData();
break;
default:
this.onNewDraw(senderId, data);
}
};
Room.prototype.onNewChat = function(senderId, message) {
if (message.toUpperCase() != this.currentWord) {
this.sendToAllOthers(senderId, ["chat", this.playerData[senderId][0], message]);
} else {
if (senderId != this.drawId && this.state == 'PLAYING') {
for (clientId in this.playerData) {
this.sendData(clientId, ["chat", "Server", this.playerData[senderId][0] + " Wins!"]);
}
this.playerData[senderId][1]++;
this.nextRound();
}
}
};
Room.prototype.onNewDraw = function(senderId, drawData) {
// Check if user is drawing
/*
*/
// Send chat
this.sendToAllOthers(senderId, drawData);
};
Room.prototype.sendGameData = function() {
var myArray = new Array();
myArray[0] = "gameData";
myArray[1] = "";
for (clientId in this.playerData) {
myArray[1] += this.playerData[clientId][0] + "," + this.playerData[clientId][1] + ";";
}
myArray[2] = this.currentHelp;
myArray[3] = false;
myArray[4] = this.roundId;
this.sendToAllOthers(this.drawId, myArray);
if (this.playerData[this.drawId] === undefined) {
return;
}
myArray[2] = this.currentWord;
myArray[3] = true;
this.sendData(this.drawId, myArray);
};
Room.prototype.sendToAllOthers = function(senderId, data) {
for (clientId in this.playerData) {
if (clientId != senderId) {
this.sendData(clientId, data);
}
}
};
Room.prototype.nextRound = function() {
this.roundId++;
// Get next drawID
this.drawId = Object.keys(this.playerData)[(Object.keys(this.playerData).indexOf(this.drawId) + 1 )% this.getPlayers()];
// Get new word
this.currentWord = this.getWord();
this.currentHelp = "";
// Get help, should move it to wordManager
for (var i = 0; i < this.currentWord.length - 1; i++) {
if (this.currentWord.charAt(i) != " ") {
this.currentHelp += "_ ";
} else {
this.currentHelp += " ";
}
}
this.currentHelp += "_";
for (clientId in this.playerData) {
this.sendData(clientId, ["chat", "server", "New Round"]);
}
this.sendGameData();
};
module.exports = Room;