summaryrefslogtreecommitdiff
path: root/play.js
blob: 1499c2885f87344542e8d7b23d49cd2b4eca6226 (plain)
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
"use strict";

const GREECE = "Greece";
const PERSIA = "Persia";

const SPACES = [
	"Abydos",
	"Athenai",
	"Delphi",
	"Ephesos",
	"Eretria",
	"Korinthos",
	"Larissa",
	"Naxos",
	"Pella",
	"Sparta",
	"Thebai",
	"reserve",
	"extra",
];

const PERSIAN_EVENT_NAMES = {
	1: "Cavalry of Mardonius",
	2: "Tribute of Earth and Water",
	3: "Tribute of Earth and Water",
	4: "Carneia Festival",
	5: "The Immortals",
	6: "Ostracism",
	7: "The Great King",
	8: "The Royal Road",
	9: "Hippias",
	10: "Separate Peace",
	11: "Sudden Death of the Great King",
	12: "Defection of Thebes",
	13: "Tribute of Earth and Water",
	14: "Alliance with Carthage",
	15: "Acropolis on Fire",
	16: "Pacification of Babylon or Egypt",
};

const GREEK_EVENT_NAMES = {
	1: "Mines of Laurion",
	2: "Ionian Revolt",
	3: "Wrath of Poseidon",
	4: "Miltiades",
	5: "Themistocles",
	6: "Pausanias",
	7: "Oracle of Delphi",
	8: "Leonidas",
	9: "Artemisia I",
	10: "Evangelion",
	11: "Melas Zomos",
	12: "Molon Labe",
	13: "Triremes",
	14: "Support from Syracuse",
	15: "300 Spartans",
	16: "Desertion of Greek Soldiers",
};

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,
	popup_label: document.getElementById("menu_card_label"),
};

function on_log(text) {
	let p = document.createElement("div");
	text = text.replace(/&/g, "&");
	text = text.replace(/</g, "&lt;");
	text = text.replace(/>/g, "&gt;");
	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(/Greece played.*:\n/))
		text = text.replace(/:\n(.*)/, ':\n<span class="G">$1</span>');
	if (text.match(/Persia played.*:\n/))
		text = text.replace(/:\n(.*)/, ':\n<span class="P">$1</span>');
	if (text.match(/^Start Campaign /)) {
		p.className = "st";
		text = text.substring(6);
	}
	if (text.match(/^.hr$/)) {
		p.className = "hr";
		text = "";
	}
	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 (view.discard)
		document.getElementById("tooltip").className = "card show card_" + view.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 (view.actions && view.actions.destroy)
		send_action('destroy');
	else if (view.actions && view.actions.build)
		send_action('build');
}

function on_click_army(evt) {
	if (view.land_movement && player) {
		let here = (player === PERSIA ? ui.persian_army : ui.greek_army)[view.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 (view.naval_transport && player) {
		let here = (player === PERSIA ? ui.persian_army : ui.greek_army)[view.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 (view.naval_movement && player) {
		let here = (player === PERSIA ? ui.persian_fleet : ui.greek_fleet)[view.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 (!view.land_movement)
		return send_action('city', evt.target.city);
	if (view.actions && view.actions.city && view.actions.city.includes(evt.target.city)) {
		let armies = ui.selected_armies.length;
		if (armies > 0)
			send_action('city', [evt.target.city, armies]);
	}
}

function on_click_port(evt) {
	if (!view.naval_movement)
		send_action('port', evt.target.port);
	if (view.actions && view.actions.port && view.actions.port.includes(evt.target.port)) {
		let fleets = ui.selected_fleets.length;
		if (fleets > 0) {
			let armies = ui.selected_armies.length;
			send_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_stat() {
	let text = ""
	if (view.trigger.carneia_festival || view.trigger.acropolis_on_fire) {
		if (view.trigger.carneia_festival)
			text += "\u{1f3ad} "
		if (view.trigger.acropolis_on_fire)
			text += "\u{1f525} "
	}
	text += view.g_cards + " \u{1f3b4}"
	return text
}

function persian_stat() {
	return view.p_cards + " \u{1f3b4}"
}

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 on_update() {
	document.getElementById("greek_stat").textContent = greek_stat();
	document.getElementById("persian_stat").textContent = persian_stat();

	if (player === GREECE)
		document.getElementById("map").classList.add("greek");
	else
		document.getElementById("map").classList.remove("greek");

	if (!view.discard)
		document.getElementById("discard").className = "card show card_back";
	else
		document.getElementById("discard").className = "card show card_" + view.discard;

	let text = "Deck: " + view.deck_size + " \u2014 Discard: " + view.discard_size;
	document.getElementById("deck_info").textContent = text

	action_button("battle", "Battle");
	action_button("build", "Build bridge");
	action_button("destroy", "Destroy bridge");
	action_button("draw", "Draw");
	action_button("pass", "Pass");
	action_button("next", "Next");
	action_button("undo", "Undo");

	if (view.actions && view.actions.destroy)
		document.getElementById("bridge").className = "show destroy";
	else if (view.actions && view.actions.build)
		document.getElementById("bridge").className = "show build"
	else if (view.trigger.hellespont)
		document.getElementById("bridge").className = "show";
	else
		document.getElementById("bridge").className = "";

	show_marker("darius", "persian_army", view.trigger.darius);
	show_marker("xerxes", "persian_army", view.trigger.xerxes);
	show_marker("artemisia", "persian_fleet", view.trigger.artemisia);
	show_marker("miltiades", "greek_army", view.trigger.miltiades);
	show_marker("themistocles", "greek_army", view.trigger.themistocles);
	show_marker("leonidas", "greek_army", view.trigger.leonidas);
	show_marker("campaign", "marker campaign_" + view.campaign);

	if (view.vp < 0)
		show_marker("vp", "marker vp_g" + (-view.vp));
	else if (view.vp > 0)
		show_marker("vp", "marker vp_p" + view.vp);
	else
		show_marker("vp", "marker vp_0");

	let hand = view.hand;
	let draw = view.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 (view.show_greek_hand)
		document.getElementById("hand").classList.add("greek");
	else
		document.getElementById("hand").classList.remove("greek");

	function update_units(index, elements) {
		let overflow = [];
		let extra = elements.extra;

		// remove if too many
		for (let space in view.units) {
			let list = elements[space];
			let n = view.units[space][index] | 0;
			while (list.length > n)
				overflow.push(list.shift());
		}

		// add if not enough
		for (let space in view.units) {
			let list = elements[space];
			let n = view.units[space][index] | 0;
			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 (view.land_movement) {
		if (player === PERSIA)
			ui.selected_armies = ui.persian_army[view.land_movement].slice();
		if (player === GREECE)
			ui.selected_armies = ui.greek_army[view.land_movement].slice();
	}

	ui.selected_fleets = null;
	if (view.naval_movement) {
		if (player === PERSIA) {
			ui.selected_fleets = ui.persian_fleet[view.naval_movement].slice();
			ui.selected_armies = [];
		}
		if (player === GREECE) {
			ui.selected_fleets = ui.greek_fleet[view.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 (view.actions && view.actions.city) {
		for (let city of view.actions.city)
			ui.cities[city].classList.add('enabled');
	}
	if (view.actions && view.actions.port) {
		for (let port of view.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 (view.transport && view.transport.where === city) {
			if (view.transport.who === GREECE)
				ga = ga.slice(view.transport.count);
			if (view.transport.who === PERSIA)
				pa = pa.slice(view.transport.count);
		}
		if (view.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 (view.transport) {
		let city = view.transport.where;
		let alist = (view.transport.who === GREECE ? ui.greek_army : ui.persian_army)[city];
		let flist = (view.transport.who === GREECE ? ui.greek_fleet : ui.persian_fleet)[city];
		for (let i = 0; i < view.transport.count; ++i)
			layout_transport(alist[i], flist[i]);
	}
	if (view.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 is_action(action, card) {
	return view.actions && view.actions[action] && view.actions[action].includes(card);
}

function show_popup_menu(evt, menu_id, target_id, title) {
	let menu = document.getElementById(menu_id)

	let show = true
	for (let item of menu.querySelectorAll("li")) {
		let action = item.dataset.action
		if (action) {
			if (is_action(action, target_id)) {
				show = true
				item.classList.add("action")
				item.classList.remove("disabled")
				item.onclick = function () {
					send_action(action, target_id)
					hide_popup_menu()
					evt.stopPropagation()
				}
			} else {
				item.classList.remove("action")
				item.classList.add("disabled")
				item.onclick = null
			}
		}
	}

	if (show) {
		menu.onmouseleave = hide_popup_menu
		menu.style.display = "block"
		if (title) {
			let item = menu.querySelector("li.title")
			if (item) {
				item.onclick = hide_popup_menu
				item.textContent = title
			}
		}

		let w = menu.clientWidth
		let h = menu.clientHeight
		let x = Math.max(5, Math.min(evt.clientX - w / 2, window.innerWidth - w - 5))
		let y = Math.max(5, Math.min(evt.clientY - 12, window.innerHeight - h - 40))
		menu.style.left = x + "px"
		menu.style.top = y + "px"

		evt.stopPropagation()
	} else {
		menu.style.display = "none"
	}
}

function hide_popup_menu() {
	document.getElementById("popup").style.display = "none"
}

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 on_card(evt) {
	if (view.actions) {
		let card = evt.target.card;
		if (is_action('discard', card)) {
			send_action('discard', card);
		} else {
			if (player === GREECE)
				show_popup_menu(evt, "popup", card, GREEK_EVENT_NAMES[card])
			else
				show_popup_menu(evt, "popup", card, PERSIAN_EVENT_NAMES[card])
		}
	}
}

function toggle_markers() {
	document.getElementById("map").classList.toggle("hide_markers");
}

if (params.role === GREECE)
	document.getElementById("map").classList.add("greek");

build_ui();