-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
325 lines (296 loc) · 8.67 KB
/
game.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
var host = location.origin.replace(/^http/, 'ws');
var ws = new WebSocket(host);
var users = [];
var rooms = [];
function userIndexOf(users, id) {
for(var i = 0; i < users.length; i++)
{
if (users[i].id == id)
return i;
}
return -1;
}
// create and add room
function createRoom(room) {
var li = document.createElement('li');
li.className = "room";
li.innerHTML = room.name + "("+room.count + "/8)";
li.dataset.id = room.id
li.onclick = function(e) {
var enterRoom = {
'name': 'enter.room',
'data': this.dataset.id
};
console.log(enterRoom);
ws.send(JSON.stringify(enterRoom));
};
document.querySelector("#roomList").appendChild(li);
}
function localNotifcation(msg, className){
var chatBox = document.querySelector("#chatArea");
chatBox.innerHTML += "<span class='"+className+"'>"+msg+"</span><br />";
chatBox.scrollTop = chatBox.scrollHeight;
}
function localEvent(msg, className) {
var eventLog = document.querySelector("#eventLog");
eventLog.innerHTML += "<span class='"+className+"'>"+msg+"</span><br />";
eventLog.scrollTop = eventLog.scrollHeight;
}
function disableMovement() {
document.querySelector("#north").disabled = true;
document.querySelector("#south").disabled = true;
document.querySelector("#west").disabled = true;
document.querySelector("#east").disabled = true;
}
function enableMovement() {
document.querySelector("#north").disabled = false;
document.querySelector("#south").disabled = false;
document.querySelector("#west").disabled = false;
document.querySelector("#east").disabled = false;
}
var interval;
ws.onopen = function(event) {
document.querySelector("#createRoomButton").onclick = function(e) {
var name = document.querySelector("#roomNameText").value;
var create_room = {
'name': 'create.room',
'data': {'name': name}
};
console.log("trying to create room");
ws.send(JSON.stringify(create_room));
};
document.querySelector("#nickButton").onclick = function(e) {
var name = document.querySelector("#userNick").value;
if (name.length == 0)
return;
var updateNick = {
'name': 'user.nick',
'data': name
};
ws.send(JSON.stringify(updateNick));
};
document.querySelector("#chatButton").onclick = function(e) {
var chatMessageEl = document.querySelector("#chatMessage");
var message = chatMessageEl.value;
if (message.length == 0)
return;
chatMessageEl.value = "";
var userChat = {
'name': 'room.chat',
'data': message
};
ws.send(JSON.stringify(userChat));
chatMessageEl.focus();
};
document.querySelector("#chatMessage").onkeyup = function(e){
// hit enter to send message
if(e.keyCode == 13){
var chatMessageEl = document.querySelector("#chatMessage");
var message = chatMessageEl.value;
if (message.length == 0)
return;
chatMessageEl.value = "";
var userChat = {
'name': 'room.chat',
'data': message
};
ws.send(JSON.stringify(userChat));
}
};
document.querySelector("#north").onclick = function(e) {
disableMovement();
setTimeout(function(){
enableMovement();
}, 500);
var move = {
'name': 'move',
'data': 'N'
};
ws.send(JSON.stringify(move));
};
document.querySelector("#south").onclick = function(e) {
disableMovement();
setTimeout(function(){
enableMovement();
}, 500);
var move = {
'name': 'move',
'data': 'S'
};
ws.send(JSON.stringify(move));
};
document.querySelector("#west").onclick = function(e) {
disableMovement();
setTimeout(function(){
enableMovement();
}, 500);
var move = {
'name': 'move',
'data': 'W'
};
ws.send(JSON.stringify(move));
};
document.querySelector("#east").onclick = function(e) {
disableMovement();
setTimeout(function(){
enableMovement();
}, 500);
var move = {
'name': 'move',
'data': 'E'
};
ws.send(JSON.stringify(move));
};
setInterval(function(){
ws.send(JSON.stringify({name: 'ping', data:''}));
}, 50*1000);
};
ws.onmessage = function(event) {
console.log(event.data);
var obj = JSON.parse(event.data);
switch(obj.name){
case 'hub.enter':
var free_rooms = obj.data;
rooms = free_rooms;
for(var i = 0; i < free_rooms.length; i++) {
createRoom(free_rooms[i]);
}
break;
case 'room.add':
var room = obj.data;
room.count = 1;
rooms.push(room);
createRoom(room);
break;
case 'room.update':
var room = obj.data;
var roomListChildren = document.querySelector("#roomList").childNodes;
for(var i = 0; i < roomListChildren.length; i++) {
var child = roomListChildren[i];
console.log(child);
if(child.dataset.id == room.id) {
child.innerHTML = room.name + "("+room.count + "/8)";
break;
}
}
break;
case 'room.full':
var room = obj.data;
var roomList = document.querySelector("#roomList");
var roomListChildren = roomList.childNodes;
for(child in roomListChildren) {
if(roomListChildren[child].dataset.id == room.id) {
roomList.removeChild(roomListChildren[child]);
break;
}
}
break;
case 'user.join':
var user = obj.data;
users.push(user);
// TODO announce join
var li = document.createElement('li');
li.innerHTML = user.name;
li.dataset.id = user.id
document.querySelector("#userList").appendChild(li);
localNotifcation(user.name + " has joined the room!", "join");
break;
// This doubles as a "entered room" event
case 'user.sync':
// hide Hub and show room specific
document.querySelector("#hub").classList.add('hidden');
document.querySelector("#room").className = "";
document.querySelector("#chatSection").className = "";
users = [];
var user_list = obj.data;
var list_element = document.querySelector("#userList")
// empty out the element
while(list_element.firstChild) {
list_element.removeChild(list_element.firstChild);
}
for(var i = 0; i < user_list.length; i++) {
users.push(user_list[i]);
var li = document.createElement('li');
li.innerHTML = user_list[i].name;
li.dataset.id = user_list[i].id
list_element.appendChild(li);
}
document.querySelector("#chatMessage").focus();
break;
case 'user.nick':
var user = obj.data;
var user_index = userIndexOf(users, user.id);
if (user_index === -1)
return;
var user_names = document.querySelector("#userList").childNodes;
for(var i = 0; i < user_names.length; i++) {
var child = user_names[i];
if(child.dataset.id == users[user_index].id) {
child.innerHTML = user.name;
break;
}
}
localNotifcation(users[user_index].name + " is now known as " +user.name, "notice");
users[user_index].name = user.name;
break;
case 'user.remove':
var user = obj.data;
var user_index = userIndexOf(users, user.id);
if (user_index === -1)
return;
users.splice(user_index, 1);
var user_name_list = document.querySelector("#userList");
var user_names = user_name_list.childNodes;
// TODO announce left
for(var i = 0; i < user_names.length; i++) {
var child = user_names[i];
if(child.dataset.id == user.id) {
var name = child.innerHTML;
user_name_list.removeChild(child);
break;
}
}
localNotifcation(name + " has left the room.", "leave");
break;
case "chat":
var user_id = obj.data.id;
var msg = obj.data.message;
var user_index = userIndexOf(users, user_id);
if (user_index === -1)
return;
var name = users[user_index].name;
var chatBox = document.querySelector("#chatArea")
chatBox.innerHTML += "<strong>"+name+"</strong>: "+msg+"<br />";
chatBox.scrollTop = chatBox.scrollHeight;
break;
case "position":
var position = obj.data;
localEvent("You moved to " + position.x + ", " + position.y + " .", "info");
document.querySelector("#gamePosition").innerHTML = "You are at " + position.x + ", " + position.y + " .";
break;
case 'invalid.move':
var error = obj.data;
localEvent(error, "error");
break;
case 'user.found':
var found_users = obj.data;
var names = found_users.map(function(u) { return u.name; }).join(", ");
localEvent("You found " + names + "!", "join");
break;
case 'kill.message':
var msg = obj.data;
localEvent(msg, "notice");
break;
case 'dead':
localEvent("You died. You are now a monster.", "notice");
break;
case 'gameover':
var winner = obj.data.winner;
localEvent("Game is over! " + winner + " have won!", "notice");
break;
case 'team.join':
var msg = obj.data;
localEvent(msg, "notice");
break;
}
};