-
Notifications
You must be signed in to change notification settings - Fork 3
/
play_dwarf.html
351 lines (335 loc) · 8.14 KB
/
play_dwarf.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" type="text/css" href="chat.css">
</head>
<body>
<div class="wrapper">
<div id='item_list' style="z-index: 1; position: absolute; top: 0px; left: 0px; background: #000;display: none;"> Items:<br>
<select id="item_select"></select>
<button id='btn_haul' type="button" onclick="haul_cur_item()">Haul</button>
<button id='btn_equip' type="button" onclick="equip_cur_item()">Equip</button>
<button type="button" onclick="hide_item_list()">Cancel</button>
</div>
<canvas id="canvas" width="!!canvas_w!!" height="!!canvas_h!!"></canvas>
<div class="chat_window">
<label>Chat:</label>
<div class="messages" id="chat_messages">
</div>
<form name="message" action="">
<input name="usermsg" type="text" id="usermsg" size="63" onkeypress="return input_box(event)" />
<button type="button" onclick="send_message()">Send</button>
</form>
</div>
</div>
<div>
<button type="button" onclick="view_dz+=1">Look up</button>
<button type="button" onclick="view_dz=0">Reset</button>
<button type="button" onclick="view_dz-=1">Look down</button>
<br>
<button type="button" onclick="haul_item()">Haul Item</button>
<button type="button" onclick="drop_hauled_item()">Drop Haul</button>
<button type="button" onclick="equip_item()">Equip item</button>
<br>
<button type="button" onclick="location.href = 'new_job';">Issue a job</button>
</div>
<br>
<img id="tilesheet" src="http://i.imgur.com/fUGSAWC.png" alt="tilesheet" style="display: none;">
<script src="map.js"></script>
<script>
setup_map("canvas","tilesheet",16,16,!!size!!)
var c = document.getElementById("canvas");
var view_dz=0;
var movement={
Numpad8:[0,-1],
Numpad7:[-1,-1],
Numpad4:[-1,0],
Numpad1:[-1,1],
Numpad2:[0,1],
Numpad3:[1,1],
Numpad6:[1,0],
Numpad9:[1,-1]
}
function isNil(value) {
return value == undefined;
}
var cur_movement
document.onkeydown=function (key) {
cur_movement=movement[key.code]
}
document.onkeyup=function (key) {
cur_movement=null
}
function key_movement(){
if(!isNil(cur_movement))
{
move_unit(cur_movement[0],cur_movement[1],view_dz,function () {
setTimeout(key_movement, 100);
})
}
else
{
setTimeout(key_movement, 100);
}
}
key_movement()
function update_map() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if(this.status==200)
{
var json_map= JSON.parse(this.responseText);
draw_map(json_map)
}
setTimeout(update_map, 100);
}
};
xhttp.open("GET", "map?dz="+view_dz, true);
xhttp.send();
}
var mouse_pos={x:0,y:0}
function move_unit(dx,dy,dz,on_done) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 ) {
if(!isNil(on_done))
on_done(this.status == 200);
}
};
xhttp.open("GET", "move_unit?dx="+dx+"&dy="+dy+"&dz="+dz, true);
xhttp.send();
}
var mouseIsDown = false;
function update_mouse(e) {
var cx=Math.floor(e.x/tile_x)-(canvas_tile_count-1)/2;
var cy=Math.floor(e.y/tile_y)-(canvas_tile_count-1)/2;
mouse_pos={x:cx,y:cy}
}
c.onmousedown = function(e){
update_mouse(e)
//dragOffset.x = e.x - mainLayer.trans.x;
//dragOffset.y = e.y - mainLayer.trans.y;
move_unit(mouse_pos.x,mouse_pos.y,view_dz)
mouseIsDown = true;
}
c.onmouseup = function(e){
mouseIsDown = false;
}
/* //TODO issue commands each X time
c.onmousemove = function(e){
if(!mouseIsDown) return;
update_mouse(e)
return false;
}
*/
update_map();
function add_max(a,e,m)
{
return a.concat(e).slice(-m)
}
/*
item stuff
*/
function drop_hauled_item()
{
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "drop_hauled_item", true);
xhttp.send();
}
function equip_cur_item(id)
{
var cur_id
if(!isNil(id))
cur_id=id
else
{
var its=document.getElementById('item_select')
var cur_id=Number(its.value)
}
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "equip_item?id="+cur_id, true);
xhttp.send();
hide_item_list()
}
function haul_cur_item(id)
{
var cur_id
if(!isNil(id))
cur_id=id
else
{
var its=document.getElementById('item_select')
var cur_id=Number(its.value)
}
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "haul_item?id="+cur_id, true);
xhttp.send();
hide_item_list()
}
function hide_item_list()
{
var itl=document.getElementById('item_list')
itl.style.display='none'
}
function show_item_list(items)
{
var itl=document.getElementById('item_list')
itl.style.display='initial'
var its=document.getElementById('item_select')
for (a in its.options) { its.options.remove(0); }
items.forEach(function (e,i) {
its.options.add( new Option(e.name,[e.id], false) );
})
}
function haul_item()
{
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 ) {
if(this.status == 200)
{
document.getElementById('btn_equip').style.display='none'
document.getElementById('btn_haul').style.display='initial'
var items=JSON.parse(this.responseText)
if (items.length>1)
show_item_list(items)
else if (items.length==1)
haul_cur_item(items[0].id)
}
}
};
xhttp.open("GET", "look_items", true);
xhttp.send();
}
function equip_item()
{
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 ) {
if(this.status == 200)
{
document.getElementById('btn_equip').style.display='initial'
document.getElementById('btn_haul').style.display='none'
var items=JSON.parse(this.responseText)
if (items.length>1)
show_item_list(items)
else if (items.length==1)
equip_cur_item(items[0].id)
}
}
};
xhttp.open("GET", "look_items", true);
xhttp.send();
}
/*
chat stuff
*/
var message_log={last_seen:0,text:[]}
function input_box(e)
{
if(e.keyCode==13)
{
send_message()
return false;
}
}
function send_message(){
var el=document.getElementById('usermsg')
var xhttp = new XMLHttpRequest();
var msg=encodeURI(el.value)
xhttp.open("GET", "send_message?msg="+msg, true);
xhttp.send();
el.value=""
}
function update_message_log(){
element=document.getElementById("chat_messages")
element.innerHTML=""
message_log.text.forEach(function (el) {
element.innerHTML+=el+"<br>"
})
}
function get_message_log(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if(this.status == 200)
{
var json_report= JSON.parse(this.responseText);
message_log.last_seen=json_report.current_count
var print_lines=10
if(json_report.log.length>0)
{
message_log.text=add_max(message_log.text,json_report.log,print_lines)
update_message_log()
}
}
setTimeout(get_message_log, 200);
}
};
xhttp.open("GET", "get_message_log?last_seen="+message_log.last_seen, true);
xhttp.send();
}
get_message_log()
/*
general game status
*/
function get_status(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if(this.status == 200)
{
var json_response= JSON.parse(this.responseText);
element=document.getElementById("status")
element.innerHTML="Paused:"+json_response.paused+" Activity:"+json_response.activity+" Job:"+json_response.job
}
setTimeout(get_status, 200);
}
};
xhttp.open("GET", "get_status", true);
xhttp.send();
}
get_status()
/*
combat report stuff
*/
var combat_log={last_seen:0,text:[]}
function update_log(){
element=document.getElementById("combat_log_div")
element.innerHTML=""
combat_log.text.forEach(function (el) {
element.innerHTML+=el+"<br>"
})
}
function get_combat_log(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if(this.status == 200)
{
var json_report= JSON.parse(this.responseText);
combat_log.last_seen=json_report.current_count
var print_lines=10
if(json_report.log.length>0)
{
combat_log.text=add_max(combat_log.text,json_report.log,print_lines)
update_log()
}
}
setTimeout(get_combat_log, 200);
}
};
xhttp.open("GET", "get_report_log?last_seen="+combat_log.last_seen, true);
xhttp.send();
}
get_combat_log()
</script>
<button onclick="location.href = 'new_unit';">Ask for new unit</button>
<div id="status"></div>
<div id="combat_log_div">
</div>
</body>
</html>