-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathindex.html
More file actions
234 lines (199 loc) · 7.49 KB
/
index.html
File metadata and controls
234 lines (199 loc) · 7.49 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
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
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Snakepit</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-2.2.1.min.js"></script>
<script type="text/javascript">
var ws;
var playerName;
var playerId;
$().ready(function () {
$("#btnConnect").click(function () {
playerName = $("#playerName").val()
$("#status").text("connecting...");
ws_url = "ws://" + location.host + "/connect"
ws = new WebSocket(ws_url);
ws.onopen = openHandler;
ws.onmessage = messageHandler;
ws.onerror = function (e) {
$("#status").text(e.message);
};
});
$("#btnDisconnect").click(function () {
ws.close();
$("#playScreen").hide();
$("#mainScreen").show();
$(document).unbind("keypress", keyHandler);
});
$("#btnJoin").click(function () {
sendMessage(["join"]);
});
});
function sendMessage(msgArray) {
var msg = JSON.stringify(msgArray);
ws.send(msg);
}
function openHandler(e){
$("#status").text("connected to server");
playerName = $('#playerName').val();
sendMessage(["new_player", playerName]);
$("#mainScreen").hide();
$("#playScreen").show();
$(document).bind("keydown", keyHandler);
}
function messageHandler(e){
//$("#status").text(e.data);
json = JSON.parse(e.data);
if (!(json[0] instanceof Array))
json = [json];
for(var i = 0; i < json.length; i++){
var args = json[i];
var cmd = json[i][0];
switch(cmd){
case("handshake"):
$("#username").text(args[1]);
playerId = args[2];
break
case("world"):
initWorld(args[1]);
break
case("reset_world"):
resetWorld();
break
case("render"):
var x = args[1];
var y = args[2];
var cell_id = '#cell' + y + '_' + x;
var symbol = args[3];
if(symbol == " ")
symbol = " ";
$(cell_id).html(symbol);
$(cell_id).attr('class', "color" + args[4]);
break
case("p_joined"):
var id = args[1];
var name = args[2];
var color = args[3];
var score = args[4];
addPlayer(id, color, name, score);
if(id == playerId)
$("#btnJoin").css("visibility", "hidden");
break
case("p_gameover"):
id = args[1];
$("#player" + id).remove();
if(id == playerId)
$("#btnJoin").css("visibility", "visible");
break
case("p_score"):
id = args[1];
score = args[2];
$("#player" + id + " .score").text(score);
break
case("top_scores"):
$("#topScoresList").empty();
players = args[1];
for(var n = 0; n < players.length; n++) {
name = players[n][0];
score = players[n][1];
color = players[n][2];
addTopScore(color, name, score);
}
}
}
}
function initWorld(data) {
var rows = [];
for(var y = 0; y < data.length; y++){
var row = "";
for(var x = 0; x < data[y].length; x++){
symbol = data[y][x][0];
color = data[y][x][1];
if(symbol == " ")
symbol = " ";
row += '<td id="cell' + y + '_' + x +
'" class="color' + color + '">' + symbol + '</td>';
}
rows.push("<tr>" + row + "</tr>");
}
var tableContent = rows.join("\n");
$("#world").html(tableContent);
}
function resetWorld() {
$("#world td").html(" ");
}
function addPlayer(id, color, name, score) {
var color_class = 'color' + color;
$("#activePlayers").append('<div id="player' + id + '">'
+ '<div class="name ' + color_class +'">' + name + '</div>'
+ '<div class="score ' + color_class + '">' + score + '</div></div>');
}
function addTopScore(color, name, score) {
var color_class = 'color' + color;
$("#topScoresList").append('<div>'
+ '<div class="name ' + color_class +'">' + name + '</div>'
+ '<div class="score ' + color_class + '">' + score + '</div></div>');
}
function keyHandler(event) {
var code = event.keyCode;
if (!code && event.charCode)
code = event.charCode;
if (ws && ws.readyState == WebSocket.OPEN) {
ws.send(code);
event.preventDefault();
}
else {
$("#status").text("Connection is closed");
}
}
</script>
</head>
<body>
<div id="status" style="text-align:left"> </div>
<div style="font-size: 3em;">Snakepit</div>
<div>ver 0.12</div>
<div style="margin-top:1em">
<div id="mainScreen">
<div id="connectForm">
<span style="color: white">Name</span>
<input id="playerName" type="text" maxlength="15" />
<button id="btnConnect">Connect</button>
</div>
<div>
<p><b style="color: white">Snakepit</b> is an open-source multiplayer snake game.</p>
<p>It is designed as a demo of <a href="https://docs.python.org/3/library/asyncio.html">asyncio</a> library for Python 3.5.</p>
<p>Check out source code at our <a href="https://github.com/7WebPages/snakepit-game">github repo</a>.</p>
<p style="color:red; margin-top: 7em">Rules:</p>
<div style="text-align: center;">
<span style="display: inline-block; text-align: left">
<span class="color2">1. Use arrows to control the snake.</span></br/></br/>
<span class="color3">2. Eat digits to grow up.</span></br/></br/>
<span class="color4">3. Do not bite: stones (#), snakes, borders.</span></br/></br/>
<span class="color6">4. Invite friends and have fun together!</span>
</span>
</div>
<p style="margin-top: 7em">Written by Kyrylo Subbotin. (c) 2016 7webpages.</p>
</div>
</div>
<div id="playScreen">
<div id="userForm">
Welcome to the server, <span id="username"></span>! Invite some friends and Join.
</div>
<div id="userButtons">
<button id="btnJoin">Join!</button>
</div>
<div id="playField">
<div id="activePlayers">Active players</div>
<div id="worldHolder">
<table id="world" cellspacing="0" cellpadding="0"></table>
</div>
<div id="topScores">
<div>Top scores</div>
<div id="topScoresList"></div>
</div>
</div>
</div>
</div>
</body>
</html>