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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
|
"use strict";
const GREECE = "Greece";
const PERSIA = "Persia";
const SPACES = [
"Abydos",
"Athenai",
"Delphi",
"Ephesos",
"Eretria",
"Korinthos",
"Larissa",
"Naxos",
"Pella",
"Sparta",
"Thebai",
"reserve",
"extra",
];
const PORTS = {
"Abydos":{"x":866,"y":625,"w":138,"h":138,"layout_x":855,"layout_y":585,"wrap":4},
"Ephesos":{"x":450,"y":765,"w":138,"h":138,"layout_x":424,"layout_y":743,"wrap":3},
"Athenai":{"x":515,"y":353,"w":138,"h":138,"layout_x":521,"layout_y":379,"wrap":4},
"Eretria":{"x":682,"y":481,"w":138,"h":138,"layout_x":683,"layout_y":510,"wrap":4},
"Naxos":{"x":475,"y":575,"w":138,"h":138,"layout_x":503,"layout_y":581,"wrap":3},
"Pella":{"x":931,"y":317,"w":138,"h":138,"layout_x":919,"layout_y":345,"wrap":4},
"Sparta":{"x":259,"y":449,"w":138,"h":138,"layout_x":251,"layout_y":470,"wrap":4},
"Thebai":{"x":689,"y":282,"w":138,"h":138,"layout_x":701,"layout_y":311,"wrap":4}
};
const CITIES = {
"Abydos":{"x":863,"y":654,"w":92,"h":90},
"Ephesos":{"x":509,"y":766,"w":92,"h":90},
"Athenai":{"x":537,"y":293,"w":84,"h":81},
"Delphi":{"x":607,"y":92,"w":84,"h":81},
"Eretria":{"x":668,"y":436,"w":84,"h":81},
"Korinthos":{"x":442,"y":137,"w":84,"h":81},
"Larissa":{"x":799,"y":107,"w":84,"h":81},
"Naxos":{"x":408,"y":590,"w":84,"h":81},
"Pella":{"x":960,"y":266,"w":84,"h":81},
"Sparta":{"x":278,"y":344,"w":84,"h":81},
"Thebai":{"x":671,"y":221,"w":84,"h":81}
};
let ui = {
cards: {},
backs: {},
cities: {},
ports: {},
greek_fleet: {},
greek_army: {},
persian_fleet: {},
persian_army: {},
all_fleets: [],
all_armies: [],
selected_armies: null,
selected_fleets: null,
};
create_log_entry = function (text) {
let p = document.createElement("div");
text = text.replace(/&/g, "&");
text = text.replace(/</g, "<");
text = text.replace(/>/g, ">");
text = text.replace(/card (\d+)/g,
'<span class="tip" onmouseenter="on_focus_card_tip($1)" onmouseleave="on_blur_card_tip()">card $1</span>');
if (text.match(/^Start Campaign /)) {
p.className = 'st';
text = text.substring(6);
}
p.innerHTML = text;
return p;
}
function remove_from_array(array, item) {
let i = array.indexOf(item);
if (i >= 0)
array.splice(i, 1);
}
function on_focus_card_tip(card_number) {
document.getElementById("tooltip").className = "card show card_" + card_number;
}
function on_blur_card_tip() {
document.getElementById("tooltip").classList = "card";
}
function on_focus_discard(evt) {
if (game.discard)
document.getElementById("tooltip").className = "card show card_" + game.discard;
else
document.getElementById("tooltip").className = "card";
}
function on_blur_discard(evt) {
document.getElementById("tooltip").classList = "card";
}
function on_focus_bridge(evt) { document.getElementById("status").textContent = "Hellespont"; }
function on_focus_city(evt) { document.getElementById("status").textContent = evt.target.city; }
function on_focus_port(evt) { document.getElementById("status").textContent = evt.target.port + " (port)"; }
function on_blur(evt) { document.getElementById("status").textContent = ""; }
function on_click_bridge(evt) {
if (game.actions && game.actions.destroy)
send_action('destroy');
else if (game.actions && game.actions.build)
send_action('build');
}
function on_click_army(evt) {
if (game.land_movement && player) {
let here = (player == PERSIA ? ui.persian_army : ui.greek_army)[game.land_movement];
if (here.includes(evt.target)) {
if (ui.selected_armies && ui.selected_armies.includes(evt.target))
remove_from_array(ui.selected_armies, evt.target);
else
ui.selected_armies.push(evt.target);
}
update_ui();
}
if (game.naval_transport && player) {
let here = (player == PERSIA ? ui.persian_army : ui.greek_army)[game.naval_movement];
if (here.includes(evt.target)) {
if (ui.selected_armies && ui.selected_armies.includes(evt.target)) {
remove_from_array(ui.selected_armies, evt.target);
} else {
if (ui.selected_armies.length < ui.selected_fleets.length && ui.selected_armies.length < 3)
ui.selected_armies.push(here[ui.selected_armies.length]);
}
}
update_ui();
}
}
function on_click_fleet(evt) {
if (game.naval_movement && player) {
let here = (player == PERSIA ? ui.persian_fleet : ui.greek_fleet)[game.naval_movement];
if (here.includes(evt.target)) {
if (ui.selected_fleets && ui.selected_fleets.includes(evt.target)) {
remove_from_array(ui.selected_fleets, evt.target);
while (ui.selected_armies.length > ui.selected_fleets.length)
ui.selected_armies.pop();
} else {
ui.selected_fleets.push(evt.target);
}
}
update_ui();
}
}
function on_click_city(evt) {
if (!game.land_movement)
return send_action('city', evt.target.city);
if (game.actions && game.actions.city && game.actions.city.includes(evt.target.city)) {
let armies = ui.selected_armies.length;
if (armies > 0)
socket.emit('action', 'city', [evt.target.city, armies]);
}
}
function on_click_port(evt) {
if (!game.naval_movement)
send_action('port', evt.target.port);
if (game.actions && game.actions.port && game.actions.port.includes(evt.target.port)) {
let fleets = ui.selected_fleets.length;
if (fleets > 0) {
let armies = ui.selected_armies.length;
socket.emit('action', 'port', [evt.target.port, fleets, armies]);
}
}
}
function build_ui() {
for (let c = 1; c <= 16; ++c) {
ui.cards[c] = document.getElementById("card_"+c);
ui.cards[c].card = c;
ui.cards[c].addEventListener("click", on_card);
ui.backs[c] = document.getElementById("back_"+c);
}
for (let city in CITIES) {
let info = CITIES[city];
let e = ui.cities[city] = document.getElementById("city_" + city);
e.city = city;
e.addEventListener("mouseenter", on_focus_city);
e.addEventListener("mouseleave", on_blur);
e.addEventListener("click", on_click_city);
e.style.left = Math.round(info.x - info.w/2) + "px";
e.style.top = Math.round(info.y - info.h/2) + "px";
e.style.width = info.w + "px";
e.style.height = info.h + "px";
}
for (let port in PORTS) {
let info = PORTS[port];
let e = ui.ports[port] = document.getElementById("port_" + port);
e.port = port;
e.addEventListener("mouseenter", on_focus_port);
e.addEventListener("mouseleave", on_blur);
e.addEventListener("click", on_click_port);
e.style.left = Math.round(info.x - info.w/2) + "px";
e.style.top = Math.round(info.y - info.h/2) + "px";
e.style.width = info.w + "px";
e.style.height = info.h + "px";
}
for (let city in CITIES) {
ui.greek_army[city] = [];
ui.greek_fleet[city] = [];
ui.persian_army[city] = [];
ui.persian_fleet[city] = [];
}
ui.greek_army.reserve = [];
ui.greek_fleet.reserve = [];
ui.persian_army.reserve = [];
ui.persian_fleet.reserve = [];
ui.greek_army.extra = [];
ui.greek_fleet.extra = [];
ui.persian_army.extra = [];
ui.persian_fleet.extra = [];
for (let i = 0; i < 9; ++i) {
let e = document.getElementById("ga"+(i+1));
e.sort_index = i;
ui.greek_army.extra.push(e);
ui.all_armies.push(e);
e.addEventListener("click", on_click_army);
}
for (let i = 0; i < 5; ++i) {
let e = document.getElementById("gf"+(i+1));
e.sort_index = i;
ui.greek_fleet.extra.push(e);
ui.all_fleets.push(e);
e.addEventListener("click", on_click_fleet);
}
for (let i = 0; i < 24; ++i) {
let e = document.getElementById("pa"+(i+1));
e.sort_index = i;
ui.persian_army.extra.push(e);
ui.all_armies.push(e);
e.addEventListener("click", on_click_army);
}
for (let i = 0; i < 6; ++i) {
let e = document.getElementById("pf"+(i+1));
e.sort_index = i;
ui.persian_fleet.extra.push(e);
ui.all_fleets.push(e);
e.addEventListener("click", on_click_fleet);
}
document.getElementById("bridge").addEventListener("click", on_click_bridge);
document.getElementById("bridge").addEventListener("mouseenter", on_focus_bridge);
document.getElementById("bridge").addEventListener("mouseleave", on_blur);
document.getElementById("discard").addEventListener("mouseenter", on_focus_discard);
document.getElementById("discard").addEventListener("mouseleave", on_blur_discard);
}
function greek_info() {
let text = "";
if (game.g_cards == 1)
text += "1 card in hand";
else
text += game.g_cards + " cards in hand";
if (game.trigger.acropolis_on_fire)
text += "\nAcropolis on Fire!";
if (game.trigger.carneia_festival)
text += "\nCarneia Festival!";
return text;
}
function persian_info() {
if (game.p_cards == 1)
return "1 card in hand";
return game.p_cards + " cards in hand";
}
function show_marker(id, class_name, show = 1, enabled = 0) {
let elt = document.getElementById(id);
if (show)
class_name += " show";
if (enabled)
class_name += " enabled";
elt.className = class_name;
}
function show_undo_button(sel, action, use_label = false) {
let button = document.querySelector(sel);
if (game.actions) {
button.classList.remove("hide");
if (game.actions && action in game.actions) {
if (game.actions[action]) {
if (use_label)
button.textContent = game.actions[action];
button.disabled = false;
} else {
button.disabled = true;
}
} else {
button.disabled = true;
}
} else {
button.classList.add("hide");
}
}
function on_update() {
document.getElementById("greek_info").textContent = greek_info();
document.getElementById("persian_info").textContent = persian_info();
if (!game.discard)
document.getElementById("discard").className = "card show card_back";
else
document.getElementById("discard").className = "card show card_" + game.discard;
document.getElementById("deck_info").textContent =
"Deck: " + game.deck_size + " \u2014 Discard: " + game.discard_size;
show_action_button("#button_battle", "battle");
show_action_button("#button_build", "build");
show_action_button("#button_destroy", "destroy");
show_action_button("#button_draw", "draw");
show_action_button("#button_next", "next");
show_action_button("#button_pass", "pass");
show_undo_button("#button_undo", "undo");
if (game.actions && game.actions.destroy)
document.getElementById("bridge").className = "show destroy";
else if (game.actions && game.actions.build)
document.getElementById("bridge").className = "show build"
else if (game.trigger.hellespont)
document.getElementById("bridge").className = "show";
else
document.getElementById("bridge").className = "";
show_marker("darius", "persian_army", game.trigger.darius);
show_marker("xerxes", "persian_army", game.trigger.xerxes);
show_marker("artemisia", "persian_fleet", game.trigger.artemisia);
show_marker("miltiades", "greek_army", game.trigger.miltiades);
show_marker("themistocles", "greek_army", game.trigger.themistocles);
show_marker("leonidas", "greek_army", game.trigger.leonidas);
show_marker("campaign", "marker campaign_" + game.campaign);
if (game.vp < 0)
show_marker("vp", "marker vp_g" + (-game.vp));
else if (game.vp > 0)
show_marker("vp", "marker vp_p" + game.vp);
else
show_marker("vp", "marker vp_0");
let hand = game.hand;
let draw = game.draw;
for (let c = 1; c <= 16; ++c) {
ui.cards[c].classList.remove('enabled');
if (hand && hand.includes(c))
ui.cards[c].classList.add('show');
else
ui.cards[c].classList.remove('show');
if (c <= draw)
ui.backs[c].classList.add('show');
else
ui.backs[c].classList.remove('show');
}
if (game.show_greek_hand)
document.querySelector(".hand").classList.add("greek");
else
document.querySelector(".hand").classList.remove("greek");
function update_units(index, elements) {
let overflow = [];
let extra = elements.extra;
// remove if too many
for (let space in game.units) {
let list = elements[space];
let n = game.units[space][index] | 0;
while (list.length > n)
overflow.push(list.shift());
}
// add if not enough
for (let space in game.units) {
let list = elements[space];
let n = game.units[space][index];
while (list.length < n) {
if (overflow.length > 0) {
list.unshift(overflow.pop());
} else {
let e = extra.pop();
e.classList.add("show");
list.unshift(e);
}
}
}
// and hide the overflow
while (overflow.length > 0) {
let e = overflow.pop();
e.classList.remove("show");
extra.push(e);
}
}
update_units(0, ui.greek_army);
update_units(1, ui.persian_army);
update_units(2, ui.greek_fleet);
update_units(3, ui.persian_fleet);
ui.selected_armies = null;
if (game.land_movement) {
if (player == PERSIA)
ui.selected_armies = ui.persian_army[game.land_movement].slice();
if (player == GREECE)
ui.selected_armies = ui.greek_army[game.land_movement].slice();
}
ui.selected_fleets = null;
if (game.naval_movement) {
if (player == PERSIA) {
ui.selected_fleets = ui.persian_fleet[game.naval_movement].slice();
ui.selected_armies = [];
}
if (player == GREECE) {
ui.selected_fleets = ui.greek_fleet[game.naval_movement].slice();
ui.selected_armies = [];
}
}
for (let city in CITIES)
ui.cities[city].classList.remove('enabled');
for (let port in PORTS)
ui.ports[port].classList.remove('enabled');
if (game.actions && game.actions.city) {
for (let city of game.actions.city)
ui.cities[city].classList.add('enabled');
}
if (game.actions && game.actions.port) {
for (let port of game.actions.port)
ui.ports[port].classList.add('enabled');
}
update_ui();
}
function update_ui() {
function layout_fleets(a, b, xorig, yorig, wrap) {
if (a.length + b.length > 0) {
let w = 26;
let h = 20;
let xstep = w + 2;
let ystep = h + 0;
let stagger = 14;
let line, para = [];
let i = 0, k = 0;
para.push(line = []);
for (let e of a) {
if (i == wrap - k) { para.push(line = []); i = 0; k = 1 - k; }
line.push(e);
++i;
}
if (i != 0 && b.length > 0) { para.push(line = []); i = 0; k = 1 - k; }
for (let e of b) {
if (i == wrap - k) { para.push(line = []); i = 0; k = 1 - k; }
line.push(e);
++i;
}
let y = yorig - Math.floor(ystep * para.length / 2);
k = 0;
let cw = (para.length > 1 ? wrap : para[0].length);
for (let row = 0; row < para.length; ++row) {
let x = xorig - Math.floor(xstep * cw / 2) + k * stagger;
for (let col = 0; col < para[row].length; ++col) {
para[row][col].style.left = x + "px";
para[row][col].style.top = y + "px";
x += xstep;
}
y += ystep;
k = 1 - k;
}
}
}
function layout_armies(list, xorig, yorig) {
const dx = 12;
const dy = 8;
if (list.length > 0) {
let ncol = Math.round(Math.sqrt(list.length));
let nrow = Math.ceil(list.length / ncol);
function layout_army(row, col, e, z) {
let x = xorig - (row * dx - col * dx) - 10 + (nrow-ncol) * 6;
let y = yorig - (row * dy + col * dy) - 13 + (nrow-1) * 8;
e.style.left = x + "px";
e.style.top = y + "px";
e.style.zIndex = z;
}
let z = 50;
let i = 0;
if (player == GREECE)
for (let row = nrow-1; row >= 0; --row)
for (let col = ncol-1; col >= 0 && i < list.length; --col)
layout_army(row, col, list[i++], z--);
else
for (let row = 0; row < nrow; ++row)
for (let col = 0; col < ncol && i < list.length; ++col)
layout_army(row, col, list[list.length-(++i)], z--);
}
}
function list_armies(city) {
let ga = ui.greek_army[city];
let pa = ui.persian_army[city];
if (game.transport && game.transport.where == city) {
if (game.transport.who == GREECE)
ga = ga.slice(game.transport.count);
if (game.transport.who == PERSIA)
pa = pa.slice(game.transport.count);
}
if (game.naval_movement) {
ga = ga.filter(a => !ui.selected_armies.includes(a));
pa = pa.filter(a => !ui.selected_armies.includes(a));
}
return ga.concat(pa);
}
layout_fleets(ui.greek_fleet.reserve, [], 95, 150, 5);
layout_fleets(ui.persian_fleet.reserve, [], 1240-95, 878-150, 6);
layout_armies(ui.greek_army.reserve, 80, 220);
layout_armies(ui.persian_army.reserve, 1240-80, 878-220)
for (let port in PORTS)
layout_fleets(ui.greek_fleet[port], ui.persian_fleet[port],
PORTS[port].layout_x, PORTS[port].layout_y, PORTS[port].wrap);
for (let city in CITIES)
layout_armies(list_armies(city),
CITIES[city].x, CITIES[city].y);
function layout_transport(a, f) {
a.style.left = (parseInt(f.style.left) + 13 - 11) + "px";
if (player == GREECE)
a.style.top = (parseInt(f.style.top) + 10 - 13 + 10) + "px";
else
a.style.top = (parseInt(f.style.top) + 10 - 13 - 10) + "px";
a.style.zIndex = 2;
}
if (game.transport) {
let city = game.transport.where;
let alist = (game.transport.who == GREECE ? ui.greek_army : ui.persian_army)[city];
let flist = (game.transport.who == GREECE ? ui.greek_fleet : ui.persian_fleet)[city];
for (let i = 0; i < game.transport.count; ++i)
layout_transport(alist[i], flist[i]);
}
if (game.naval_movement) {
for (let i = 0; i < ui.selected_armies.length; ++i)
layout_transport(ui.selected_armies[i], ui.selected_fleets[i]);
}
for (let e of ui.all_armies)
if (ui.selected_armies && ui.selected_armies.includes(e))
e.classList.add("selected");
else
e.classList.remove("selected");
for (let e of ui.all_fleets)
if (ui.selected_fleets && ui.selected_fleets.includes(e))
e.classList.add("selected");
else
e.classList.remove("selected");
}
function on_destroy() { if (game.actions) { send_action('destroy'); } }
function on_battle() { if (game.actions) { send_action('battle'); } }
function on_build() { if (game.actions) { send_action('build'); } }
function on_draw() { if (game.actions) { send_action('draw'); } }
function on_next() { if (game.actions) { send_action('next'); } }
function on_pass() { if (game.actions) { send_action('pass'); } }
function on_undo() { if (game.actions) { send_action('undo'); } }
let current_popup_card = 0;
function show_popup_menu(evt, list) {
document.querySelectorAll("#popup div").forEach(e => e.classList.remove('enabled'));
for (let item of list) {
let e = document.getElementById("menu_" + item);
e.classList.add('enabled');
}
let popup = document.getElementById("popup");
popup.style.display = 'block';
popup.style.left = (evt.clientX-50) + "px";
popup.style.top = (evt.clientY-12) + "px";
ui.cards[current_popup_card].classList.add("selected");
}
function hide_popup_menu() {
let popup = document.getElementById("popup");
popup.style.display = 'none';
if (current_popup_card) {
ui.cards[current_popup_card].classList.remove("selected");
current_popup_card = 0;
}
}
function on_card_event() {
send_action('card_event', current_popup_card);
hide_popup_menu();
}
function on_card_move() {
send_action('card_move', current_popup_card);
hide_popup_menu();
}
function is_card_action(action, card) {
return game.actions && game.actions[action] && game.actions[action].includes(card);
}
function on_card(evt) {
if (game.actions) {
let card = evt.target.card;
if (is_card_action('discard', card)) {
send_action('discard', card);
} else {
let menu = [];
if (is_card_action('card_event', card))
menu.push('card_event');
if (is_card_action('card_move', card))
menu.push('card_move');
if (menu.length > 0) {
current_popup_card = card;
show_popup_menu(evt, menu);
}
}
}
}
function toggle_markers() {
document.querySelector(".map").classList.toggle("hide_markers");
}
if (param_role == GREECE)
document.getElementById("map").classList.add("greek");
build_ui();
scroll_with_middle_mouse("#grid_center", 2);
init_client(["Greece", "Persia"]);
|