-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
267 lines (257 loc) · 9.25 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta charset="utf-8">
<title>dafencec</title>
<meta name="description" content="test client for dafence server. Now, with an extra cook in the kitchen!">
<style>
#state{
padding: 0.2em;
border: 0.2em solid black;
margin: 0.2em;
}
#mapContainer{
float: left;
width: 70%;
}
table{
border-collapse: collapse;
width: 100%;
height: 30em;
}
td{
border: 0.1em solid black;
text-align: center;
}
#log{
width: 25%;
float: right;
}
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
function log(msg){
console.log(msg);
if('string' === typeof msg) $('#log').prepend($('<div>').text(msg));
};
function get(url, callback){
$.ajax({
url: url,
dataType: 'jsonp',
success:function(data){
if(null !== data && 'string' === typeof data.error) log(data.error);
callback(data);
},
error:function(){
log('Unable to retrieve ' + url);
}
});
};
var cookie = {
name: escape('dafence'),
expression: new RegExp('(^|[; ])*' + this.name + '=([^;]*)'),
write: function(value){
document.cookie = cookie.name + '=' + escape(value) + '; path=/';
},
read: function(){
var match = document.cookie.match(cookie.expression)
if(null !== match && 3 === match.length) return unescape(match[2]);
return false;
}
}
var player = {
login: function(){
if('' === player.nameInp.val()) return player.nameInp.focus().css('background-color', '#FF9999');
player.inputs.css('background-color', 'white');
var query = '?name=' + escape(player.nameInp.val());
if(player.colorSpan.is(':visible')){
if('' === player.colorInp.val()) player.colorInp.focus().css('background-color', '#FF9999');
else{
player.state.css('background-color', player.colorInp.val());
query += '&color=' + escape(player.state.css('background-color'));
}
}
get('player.jsonp' + query, function(data){
if('string' === typeof data.error){
if('No color specified' === data.error){
player.colorSpan.fadeIn();
player.colorInp.focus();
}
else log('Could not log in');
return false;
}
player.name = player.nameInp.val();
player.nameEsc = escape(player.name);
player.color = data.color;
player.stakes = data.stakes;
player.stakesSpan.text(data.stakes);
player.ownedSpan.text(data.ownerships);
player.inputs.css('background-color', 'white').val('');
player.state.css('background-color', player.color).text('playing as ' + player.name);
player.colorSpan.fadeOut();
cookie.write(player.name);
});
},
claim: function(row, col, pow){
if('undefined' === typeof player.name) return log('You have to be logged in to stake a claim');
get(
'claim.jsonp' +
'?name=' + player.nameEsc +
'&level=' + map.level +
'&row=' + row +
'&col=' + col +
'&pow=' + pow,
function(data){
if('number' === typeof data){
log(player.name + ' just made ' + data + 'pts');
if(parseInt(player.ownedSpan.text, 10) / map.zoom > 9) map.zoom *= 1.5;
}
});
}
};
var map = {
make: function(colors){
var table, row, col, rowIdx, colIdx;
table = $('<table>');
for(rowIdx = map.top; rowIdx < map.bottom; rowIdx++){
row = $('<tr>').addClass('row' + rowIdx).appendTo(table);
for(colIdx = map.left; colIdx < map.right; colIdx++){
cell = $('<td>').addClass('col' + colIdx).appendTo(row);
try{cell.css('background-color', colors[rowIdx][colIdx]);}
catch(e){;}
}
}
map.mapDiv.append(table);
map.posEl.text(map.level + ' - [' + map.top + ':' + map.bottom + '][' + map.left + ':' + map.right + ']');
map.colors = colors;
},
update: function(colors){
var row, rowIdx, colIdx;
for(rowIdx = map.top; rowIdx < map.bottom; rowIdx++){
row = map.mapDiv.find('table tr.row' + rowIdx);
for(colIdx = map.left; colIdx < map.right; colIdx++){
var oldColor, color;
try{oldColor = map.colors[rowIdx][colIdx];}
catch(e){oldColor = 'transparent';}
try{color = colors[rowIdx][colIdx];}
catch(e){color = 'transparent';}
if(oldColor !== color) row.find('td.col' + colIdx).css('background-color', color);
}
}
map.colors = colors;
},
get: function(){
var
halfWidth = 0.5 * (map.right - map.left),
halfHeight = 0.5 * (map.bottom - map.top),
center = {left: halfWidth, top: halfHeight},
owned = parseInt(player.ownedSpan.text(), 10);
if(owned > halfWidth * halfHeight){
halfHeight = Math.round(1.2 * halfHeight);
halfWidth = Math.round(1.2 * halfWidth);
map.top = Math.round(center.top - halfHeight);
map.bottom = Math.round(center.top + halfHeight);
map.left = Math.round(center.left - halfWidth);
map.right = Math.round(center.left + halfWidth);
map.mapDiv.empty();
}
get(
'map.jsonp' +
'?level=' + map.level +
'&top=' + map.top +
'&bottom=' + map.bottom +
'&left=' + map.left +
'&right=' + map.right +
('string' === typeof player.nameEsc ? '&name=' + player.nameEsc : ''),
function(data){
if('number' === typeof data.stakes) player.stakesSpan.text(parseInt(data.stakes, 10));
if('number' === typeof data.ownerships) player.ownedSpan.text(data.ownerships);
if(0 === map.mapDiv.find('table').size()) map.make(data.colors);
else map.update(data.colors);
map.get();
});
},
zoom: function(factor, center){
var
halfFact = factor / 2,
width = halfFact * (map.right - map.left),
height = halfFact * (map.bottom - map.top);
map.top = Math.round(center.top - height);
map.bottom = Math.round(center.top + height);
map.left = Math.round(center.left - width);
map.right = Math.round(center.left + width);
map.mapDiv.empty();
}
};
$(function(){
player.state = $('#state').text('not logged in');
player.stakesSpan = $('#stakes');
player.ownedSpan = $('#owned');
player.nameSpan = $('#nameSpan');
player.colorSpan = $('#colorSpan');
player.nameInp = $('#name').focus();
player.colorInp = $('#color');
player.inputs = player.nameInp.add(player.colorInp).keypress(function(e){
if(13 === e.which) player.login();
});
$('#submit').click(function(e){
player.login();
});
cookie.value = cookie.read();
if('string' === typeof(cookie.value)){
player.nameInp.val(cookie.value);
player.login();
}
try{
map.pos = location.search.match(/[?&]pos=([^&#\/]*)/)[1].split(',');
$.map(map.pos, function(s){
var i = parseInt(s, 10);
if(isNaN(i)) throw new Error('NaN positional param');
return i;
});
if(map.pos.length !== 5) throw new Error('Not enough positional parameters');
map.level = map.pos[0];
map.top = map.pos[1];
map.bottom = map.pos[2];
map.left = map.pos[3];
map.right = map.pos[4];
}catch(e){
log(['caught', e]);
map.level = 0;
map.top = 0;
map.bottom = 9;
map.left = 0;
map.right = 9;
}
map.mapDiv = $('#map').click(function(e){
cell = $(e.target);
col = parseInt(cell.attr('class').slice(3), 10);
row = parseInt(cell.parent().attr('class').slice(3), 10);
player.claim(row, col, 1);
});
map.posEl = $('#curpos');
map.get();
});
</script>
</head>
<body oncontextmenu="return false;">
<div id="player">
<div id="state"></div>
<div>Stakes: <span id="stakes"></span></div>
<div>Owned: <span id="owned"></span></div>
<span id="nameSpan">Play as <input id="name"></span>
<span id="colorSpan" style="display: none;">with color <input id="color"></span>
<button id="submit">ok</button>
</div>
<div id="mapContainer">
<h3 id="mapHead">Current position: <span id="curpos"></span></h3>
<div id="map">
</div>
</div>
<div id="log"></div>
</body>
</html>
<!--
vim: ft=javascript
-->