-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.html
544 lines (455 loc) · 21.1 KB
/
client.html
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
<!DOCTYPE html>
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.1.1.min.js"
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
crossorigin="anonymous"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script type="text/javascript">
var HOST ='46.101.223.26'
var PORT = '8765'
var client_uid = null
var client_username= null
var room_uid = null
var room_name = null
var room_type = null
var websocket = null
var creatures = {}
function hide_registration_menu(){
$('#registration_menu').hide()
}
function update_self_ui(){
}
function update_room_ui(){
creatures = {}
$('#room_left_panel').html('')
$('#output').html('')
$('#lobby_input').val('')
$('#input').val('')
$('#room_name').text('')
$('#lobby_validation_report').text('')
if (room_name != null){
$('#room_name').text(room_name)
}
else{
$('#room_name').text('')
}
switch(room_type){
case 'lobby':
$('#lobby_menu').show()
$('#main_menu').hide()
break
case 'skeleton':
$('#lobby_menu').hide()
$('#main_menu').show()
break
default:
console.log('Unknown room type!')
}
}
function update_clients_ui(){
$('#room_name').text(room_name)
}
function setup_new_creature(args){
//uid, name, alive, max_health, health, state, target_uid
uid = args[0]
name = args[1]
alive = args[2]
max_health = args[3]
health = args[4]
state = args[5]
target = args[6]
if (target == undefined){
target = 'none'
}
creature = {
'name': name,
'alive': alive,
'max_health': max_health,
'health': health,
'state': state,
'target': target,
}
creatures[uid] = creature
if (!$('#room_left_panel #'+uid).length){
new_elem = [
'<div id="'+uid+'" style="width:100%;text-align:left;margin-bottom:10px;">',
'<div class="creature_name">'+name+'</div>',
'<div><span class="health">'+health+'</span>/<span class="health_max">'+max_health+'</span></div>',
'<div class="target">target: <u>'+target+'</u></div>',
'<div class="state">state: <u>'+ state + '</u></div>',
'</div>'
]
if (!alive){
new_elem.push('</s>')
new_elem.unshift('<s>')
}
$('#room_left_panel').append(new_elem.join('\n'))
}
}
function find_creature_by_uid(uid){
return creatures[uid]
}
function handle_system_message(message){
system_msg_components = message.split('|')
emitter_id = system_msg_components[1];
msg_type = system_msg_components[2];
args = system_msg_components.slice(3,system_msg_components.length);
//console.log(emitter_id, msg_type, args)
/*
"creature_took_damage",
"creature_health_report",
"creature_blocked_damage",
"creature_changed_state",
"creature_death",
"creature_action_interrupted",
"creature_attack_started",
"creature_attack_finished",
"creature_def",
"creature_no_def",
"creature_start",
"ai_new_target",
"ply_notify",
*/
switch(msg_type) {
case 'username_prompt':
break;
case 'username_invalid':
$('#username_validation_report').text('Username invalid or taken, try another.')
break;
case 'registered':
hide_registration_menu()
client_uid = args[0]
client_username = args[1]
update_self_ui()
break;
case 'joined_room':
room_uid = args[0]
room_name = args[1]
room_type = args[2]
update_room_ui()
break;
case 'client_joined_room':
client_uid = args[0]
client_uname = args[1]
if (room_type == 'skeleton'){
handle_chat_message('<u>'+client_uname+'</u> joined the fight!')
}
break;
case 'client_left_room':
client_uid = args[0]
client_uname = args[1]
if (room_type == 'skeleton'){
handle_chat_message('<u>'+client_uname+'</u> left the fight!')
}
$('#'+client_uid).remove()
break;
case 'ui_setup_creature':
setup_new_creature(args)
break;
case 'creature_health_report':
uid = emitter_id
health = args[0]
$('#'+uid+' .health').text(health)
break
case 'creature_took_damage':
uid = emitter_id
dmg = args[0]
creature = find_creature_by_uid(uid)
if (creature){
message = '<u>'+creature['name']+'</u>'+' took '+String(dmg)+' damage.'
if (uid == client_uid){
message = "<span class='text-danger'>"+message+"</span>"
}
handle_chat_message(message)
}
break
case 'creature_changed_state':
uid = emitter_id
state = args[0]
$('#'+uid+' .state').html('state: <u>'+state+'</u>')
// creature = find_creature_by_uid(uid)
// if (creature){
// handle_chat_message('<u>'+creature['name']+'</u>'+' is now '+state+'.')
// }
break
case 'creature_death':
uid = emitter_id
$('#'+uid).html('<s>'+$('#'+uid).html()+'</s>')
creature = find_creature_by_uid(uid)
if (creature){
message = '<u>'+creature['name']+'</u>'+' died'
if (uid == client_uid){
message = "<span class='text-danger'>"+message+"</span>"
}
handle_chat_message(message)
}
break
case 'ai_new_target':
uid = emitter_id
target = args[0]
target_name =find_creature_by_uid(target)['name']
$('#'+uid+' .target').html('target: <u>'+target_name+'</u>')
creature = find_creature_by_uid(uid)
if (creature){
handle_chat_message('<u>'+creature['name']+'</u>'+' selected a new target: '+target_name)
}
break
case 'creature_blocked_damage':
uid = emitter_id
creature = find_creature_by_uid(uid)
if (creature){
message = '<u>'+creature['name']+'</u>'+' blocked the attack!'
if (creature['name'] == 'skeleton'){
message = "<span class='text-warning'>"+message+"</span>"
}
else if (uid == client_uid){
message = "<span class='text-success'>"+message+"</span>"
}
handle_chat_message(message)
}
break
case 'creature_action_interrupted':
uid = emitter_id
creature = find_creature_by_uid(uid)
if (creature){
message = '<u>'+creature['name']+'</u>'+' was interrupted!'
if (creature['name'] == 'skeleton'){
message = "<span class='text-success'>"+message+"</span>"
}
else if (uid == client_uid){
message = "<span class='text-danger'>"+message+"</span>"
}
handle_chat_message(message)
}
break
case 'creature_attack_started':
uid = emitter_id
creature = find_creature_by_uid(uid)
if (creature){
message = '<u>'+creature['name']+'</u>'+' prepares to attack!'
if (creature['name'] == 'skeleton'){
message = "<span class='text-warning'>"+message+"</span>"
}
handle_chat_message(message)
}
break
case 'creature_attack_finished':
uid = emitter_id
creature = find_creature_by_uid(uid)
if (creature){
handle_chat_message('<u>'+creature['name']+'</u>'+' lands the attack!')
}
break
case 'creature_def':
uid = emitter_id
creature = find_creature_by_uid(uid)
if (creature){
message = '<u>'+creature['name']+'</u>'+' is blocking!'
if (creature['name'] == 'skeleton'){
message = "<span class='text-warning'>"+message+"</span>"
}
else if (uid == client_uid){
message = "<span class='text-info'>"+message+"</span>"
}
handle_chat_message(message)
}
break
case 'creature_no_def':
uid = emitter_id
creature = find_creature_by_uid(uid)
if (creature){
message = '<u>'+creature['name']+'</u>'+' is exposed!'
if (uid == client_uid){
message = "<span class='text-warning'>"+message+"</span>"
}
handle_chat_message(message)
}
break
case 'validation_error':
error_text = args[0]
$('#lobby_validation_report').text(error_text)
break;
default:
console.log('Unhandled message', msg_type, args)
}
}
function handle_chat_message(message){
var output = document.getElementById("output");
messages = message.split("\n");
for (var i=0;i<messages.length;i++){
$('#output').append("<p>"+messages[i]+"</p>")
}
$("#output").animate({
scrollTop: $("#output")[0].scrollHeight
}, 0);
}
function handle_websocket_message(message){
if(message.includes('sysmsg')){
handle_system_message(message)
}
else{
handle_chat_message(message)
}
}
window.addEventListener("load", function() {
$('#main_menu').hide()
$('#lobby_menu').hide()
// create websocket instance
try {
websocket = new WebSocket("ws://"+HOST+":"+PORT+"/ws");
}
catch (e){
OnSocketError(e)
}
// Handler for onerror:
function OnSocketError(ev)
{
alert("Couldn't connect to server on specified host and port!")
}
websocket.onerror = OnSocketError;
websocket.onmessage = function (event) {
handle_websocket_message(event.data)
};
$('#registration_form').submit(function (e) {
// on forms submission send input to our server
input_text = $('#username_input').val();
websocket.send(input_text);
e.preventDefault()
})
$("#lobby_form").submit(function (e) {
// on forms submission send input to our server
input_text = $('#lobby_input').val();
command = '::skeleton'
websocket.send(command+' '+input_text);
e.preventDefault()
})
$('#main_form').submit(function (e) {
// on forms submission send input to our server
input_text = $('#input').val();
websocket.send(input_text);
$('#input').val('')
e.preventDefault()
})
$('#attack_btn').click(function(e){
var attack_command = "::attack";
websocket.send(attack_command);
e.preventDefault()
})
$('#defend_btn').click(function(e){
var defend_command = "::defense";
websocket.send(defend_command);
e.preventDefault()
})
$('#leave_btn').click(function(e){
var command = "::leave";
websocket.send(command);
e.preventDefault()
})
});
</script>
<style>
/* just some super basic css to make things bit more readable */
.container {
padding:0;
}
#room_left_panel{
height:100%;
}
#output p {
padding:0;
margin:0;
}
#output {
padding: 10px 5px;
height:100%;
overflow-y: scroll;
text-align: left;
}
#input_container {
text-align: left;
}
.vertical-center {
height:100%;
width:100%;
text-align: center; /* align the inline(-block) elements horizontally */
font: 0/0 a; /* remove the gap between inline(-block) elements */
}
.vertical-center:before { /* create a full-height inline block pseudo=element */
content: " ";
display: inline-block;
vertical-align: middle; /* vertical alignment of the inline element */
height: 100%;
}
.vertical-center > .container {
max-width: 100%;
display: inline-block;
vertical-align: middle; /* vertical alignment of the inline element */
/* reset the font property */
font: 16px/1 "Helvetica Neue", Helvetica, Arial, sans-serif;
}
</style>
</head>
<body>
<div class='container' style="height: 800px;width:600px;">
<div class='vertical-center'>
<div id='registration_menu' class='container'>
<div class='container' style='width:300px;'>
<p>Enter username:</p>
<form id='registration_form' class="form">
<div class="form-group">
<div id='username_validation_report'></div>
<input id="username_input" class="form-control" ></input>
</div>
<div class="form-group">
<input type="submit" class="form-control"></input>
</div>
</form>
</div>
</div>
<div id='lobby_menu' class='container'>
<div class='container' style='width:300px;'>
<p>Enter name of a skeleton you want to fight (leave blank to fight a new, random skeleton):</p>
<form id='lobby_form' class="form">
<div class="form-group">
<div id='lobby_validation_report'></div>
<input id="lobby_input" class="form-control" ></input>
</div>
<div class="form-group">
<input type="submit" class="form-control" value='Muder a skeleton'></input>
</div>
</form>
</div>
</div>
<div id='main_menu' class='container'>
<div id="room_info"><h1 id='room_name'></h1><small>send this name to a friend and he will be able to join you</small><span id="leave_btn" class='btn-info btn-xs btn pull-right'>quit</span></div>
<br/>
<div class='container' style='width:550px;'>
<div class="row" style="height:300px;">
<div class='panel panel-default' id="room_left_panel" style="float:left;width:200px;padding:10px 5px;"></div><div class='panel panel-default' style="float:left;width:350px;" id="output"></div>
</div>
</div>
<div class='input_container'>
<form id="main_form" class="form-inline">
<div class="form-group">
<div id="attack_btn" class='btn-danger btn'>Attack</div>
</div>
<div class="form-group">
<div id="defend_btn" class='btn-primary btn'>Defend</div>
</div>
<div class="form-group">
<input id="input" class="form-control" placeholder='Say something' style="min-width:125px"></input>
</div>
<div class="form-group">
<input type="submit" class="form-control"></input>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>