-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.html
97 lines (72 loc) · 1.85 KB
/
game.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Game</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="/js/socket.io.js"></script>
<script type='text/javascript'>
//<![CDATA[
io.setPath('/');
//]]>
</script>
<script>
socket = new io.Socket(location.host.split(':')[0]);
$(function() {
socket.connect();
socket.on('connect', function(test) {
});
socket.on('message', function(msg) {
message = JSON.parse(msg);
switch (message._type){
case "connect":
addPlayer(message.client)
break;
case "up":
upPlayer(message.client, message.score)
break;
case "winner":
winnerPlayer(message.client)
break;
case "disconnect":
console.log('hello');
removePlayer(message.client)
break;
}
});
socket.on('disconnect', function(data) {
});
$('body').keyup(function(e){
console.log(e.keyCode);
if(e.keyCode == 87){ //w
socket.send('up');
}
if(e.keyCode == 78){ //n
socket.send('new');
}
});
});
function addPlayer(player_id){
$('#game').append('<p data-player="'+player_id+'"><span>'+player_id+'</span><meter style="width:600px;height:60px" value="00" max="100"></meter><p>');
}
function removePlayer(player_id){
$('p[data-player='+player_id+']').remove();
}
function upPlayer(player_id, score){
var meter = $('p[data-player='+player_id+']').find('meter') ;
var value = meter.attr('value');
meter.attr('value', score);
}
function winnerPlayer(player_id){
$('#log').html(player_id+" WON");
}
</script>
</head>
<body>
<p id="log">
Crazy Game
</p>
<div id="game">
</div>
</body>
</html>