-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto_html.py
547 lines (468 loc) · 13 KB
/
to_html.py
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
#!/usr/local/bin/python3
import datetime
import json
import re
import sys
with open('data.json') as f:
data = json.load(f)
books=['I', 'II', 'III', 'IV']
base_order= [
# Elephants
'El',
# Knights
'3Kn', '4Kn', '6Kn', 'HCh',
# Cavalry
'Cv', '6Cv', 'LCh',
# Light Horse
'LH', 'LCm',
# Scythed Chariots
'SCh',
# Camelry
'Cm',
# Spears
'Sp', '8Sp',
# Pikes
'4Pk', '3Pk',
# Blades
'4Bd', '3Bd', '6Bd',
# Auxilia
'4Ax','3Ax',
# Bows
'4Bw','3Bw', '8Bw',
'4Cb', '3Cb', '8Cb',
'4Lb', '3Lb', '8Lb',
'Mtd-4Bw', 'Mtd-3Bw',
'Mtd-4Lb',
'Mtd-4Cb',
# Psiloi
'Ps',
# Warbands
'4Wb','3Wb',
# Hordes
'7Hd', '5Hd',
# Artillery
'Art',
# War Wagons
'WWg',
# Ceneral
'CP', 'Lit', 'CWg',
'Camp'
]
army_name_re = re.compile("([^/]*)/(\d+)(\S*)")
def key_army_name(army_name) :
k= list(army_name_re.match(army_name).groups())
k[1] = int(k[1])
return k
def key_base_order(base_name) :
i = base_order.index(base_name)
return i
def key_army_name_str(army_name) :
k=key_army_name(army_name)
return k[0] + '/' + str(k[1]) + k[2]
def army_names():
"""Return all the army names"""
names = {}
result=[]
for book in books :
book_str = "Book " + book
for army in data['armies'][book_str] :
# Ignore same army with plain vs models
k = key_army_name(army)
plain = str(k)
if plain not in names :
names[plain] = 1
result.append(army)
result.sort(key=key_army_name)
return result
def remove_suffix(name, suffix) :
l = len(suffix)
if name[-l:] == suffix :
return name[:-l]
return name
def remove_general(name) :
"""Remove general suffix"""
return remove_suffix(name, "_Gen")
def remove_mounted(name) :
"""Remove mounted suffix"""
name = remove_suffix(name, "_Mtd")
name = remove_suffix(name, "-Mtd")
name = remove_suffix(name, ":Mtd")
if name == "Mtd-4Bw" :
return "4Bw"
return name
def normalize_base_name(name) :
name = remove_general(name)
name = remove_mounted(name)
return name
def book_from_army_name(army_name):
k=key_army_name(army_name)
return "Book " + k[0]
def bases_for_army(army_name) :
"""Write out the javascript for the names of the bases that are in the army, e.g aux, ps."""
book = book_from_army_name(army_name)
army = data['armies'][book][army_name]
bases=[]
for base in army:
if base.startswith("base") :
name=normalize_base_name(army[base]['name'])
if name not in bases :
bases.append(name)
bases.sort(key=key_base_order)
sys.stdout.write( json.dumps(bases) )
sys.stdout.write(";\n")
def army_bases() :
"""Generate the bases for all the armies"""
sys.stdout.write("var bases={};\n")
for army_name in army_names() :
k=key_army_name_str(army_name)
sys.stdout.write("bases['%s'] = " % (k))
bases_for_army(army_name)
print("""
<html>
<head>
<style>
.nobreak {
page-break-inside: avoid;
}
.base_abbreviation {
font-weight: bold;
}
.base {
margin-top: 20px;
page-break-inside: avoid;
border-style: double;
width: 100%;
}
.red {
color: red;
}
.blue {
color: blue;
}
.lt {
margin-top: 0px;
margin-bottom: 0px;
padding-top: 0px;
padding-bottom: 0px;
}
ul { padding-left: 1.2em; }
.li {
margin-top: 0px;
margin-bottom: 0px;
margin-left: 0px;
border-left: 0px;
padding-top: 0px;
padding-bottom: 0px;
padding-left: 0px;
}
.col {
vertical-align: top;
}
@media print {
.base {page-break-inside: avoid;}
}
</style>
<script>
""")
army_bases()
sys.stdout.write("var tool_tips=")
sys.stdout.write( json.dumps(data['tool_tips']) )
sys.stdout.write(";\n")
sys.stdout.write("var base_order=")
sys.stdout.write( json.dumps( base_order ) )
sys.stdout.write(";\n")
print("""
function rules(node, key, title, tip) {
if (! (key in tip)) {
return
}
var list = tip[key]
if ( list.length <= 0 ) {
return;
}
var div = document.createElement("DIV")
node.appendChild(div);
var div_class = document.createAttribute("class");
div_class.value = "lt";
div.setAttributeNode(div_class)
div.appendChild(document.createTextNode(title + ":"));
var ul = document.createElement("UL")
node.appendChild(ul)
var ul_class = document.createAttribute("class");
ul_class.value = "lt";
ul.setAttributeNode(ul_class)
for (i in list ) {
var li = document.createElement("LI")
ul.appendChild(li)
var li_class = document.createAttribute("class");
li_class.value = "li";
li.setAttributeNode(li_class)
li.appendChild(document.createTextNode(list[i]));
}
}
function can_quick_kill(node, tip) {
rules(node, 'can_quick_kill', 'Quick Kills', tip)
}
function quick_killed_by(node, tip) {
rules(node, 'quick_killed_by', 'Quick Killed By', tip)
}
function only_killed_by(node, tip) {
rules(node, 'only_killed_by', 'Only Killed By', tip)
}
function makes_flee(node, tip) {
rules(node, 'makes_flee', 'Makes Flee', tip)
}
function flees_from(node, tip) {
rules(node, 'flees_from', 'Flees From', tip)
}
function cannot_destroy(node, tip) {
rules(node, 'cannot_destroy', 'Cannot Destroy', tip)
}
function combat_notes(node, tip) {
rules(node, 'combat_notes', 'Combat', tip)
}
function movement_notes(node, tip) {
rules(node, 'movement_notes', 'Movement', tip)
}
function deployment_notes(node, tip) {
rules(node, 'deployment_notes', 'Deployment', tip)
}
function victory_notes(node, tip) {
rules(node, 'victory_notes', 'Victory', tip)
}
function speed(node, tip) {
var speed = tip['speed'];
var gg = speed['GG'];
var gg_str = gg.toString(10);
var bg = speed['BG'];
var bg_str = bg.toString(10);
var speed_text = "speed GG: " + gg_str + "BW BG/RG: " + bg_str + "BW";
var speed_node = document.createTextNode(speed_text);
node.appendChild(speed_node);
}
function combat(node, tip) {
var combat = tip['combat'];
var combat_text = "combat foot: ";
if ( 'foot' in combat ) {
combat_text = combat_text + combat['foot'].toString(10);
}
combat_text = combat_text + " mounted: "
if ( 'mounted' in combat ) {
combat_text = combat_text + combat['mounted'].toString(10);
}
combat_text = combat_text + " shot at: "
var shot_at = ( 'shot_at' in combat ) ? combat['shot_at'] : combat['foot']
combat_text = combat_text + shot_at.toString(10);
var combat_node = document.createTextNode(combat_text);
node.appendChild(combat_node);
}
function add_tool_tips(elem, base_name) {
if ( ! (base_name in tool_tips) ) {
return;
}
var tip = tool_tips[base_name];
var table_node = document.createElement("table");
elem.appendChild(table_node);
var base_node_class = document.createAttribute("class");
base_node_class.value = "base";
table_node.setAttributeNode(base_node_class);
var tbody = document.createElement("tbody");
table_node.appendChild(tbody);
var title_tr = document.createElement("tr")
tbody.appendChild(title_tr)
var title_base_name_td = document.createElement("td")
var title_base_attributes_td = document.createElement("td")
title_tr.appendChild(title_base_name_td)
title_tr.appendChild(title_base_attributes_td)
var base_name_span = document.createElement("span");
var base_name_span_class = document.createAttribute("class");
base_name_span_class.value = "base_abbreviation";
base_name_span.setAttributeNode(base_name_span_class);
title_base_name_td.appendChild(base_name_span);
var base_name_node = document.createTextNode(base_name);
base_name_span.appendChild(base_name_node);
title_base_name_td.appendChild(document.createTextNode(" "));
var name_node = document.createTextNode(tip['name']);
title_base_name_td.appendChild(name_node);
title_base_attributes_td.appendChild(document.createTextNode(" ["));
if (('mounted' in tip) && (tip['mounted'])) {
title_base_attributes_td.appendChild(document.createTextNode(" mounted"));
}
if (('solid' in tip) && (tip['solid'])) {
title_base_attributes_td.appendChild(document.createTextNode(" solid"));
}
if (('fast' in tip) && (tip['fast'])) {
title_base_attributes_td.appendChild(document.createTextNode(" fast"));
}
title_base_attributes_td.appendChild(document.createTextNode(" ]"));
var tr = document.createElement("tr");
tbody.appendChild(tr);
var combat_td = document.createElement("td");
var movement_td = document.createElement("td");
tr.appendChild(combat_td);
tr.appendChild(movement_td);
var combat_td_class = document.createAttribute("class");
combat_td_class.value = "col";
combat_td.setAttributeNode(combat_td_class);
var movement_td_class = document.createAttribute("class");
movement_td_class.value = "col";
movement_td.setAttributeNode(movement_td_class);
speed(movement_td, tip)
combat(combat_td, tip)
// wins
can_quick_kill(combat_td, tip)
makes_flee(combat_td, tip)
cannot_destroy(combat_td, tip)
// looses
quick_killed_by(combat_td, tip)
only_killed_by(combat_td, tip)
flees_from(combat_td, tip)
combat_notes(combat_td, tip)
movement_notes(movement_td, tip)
deployment_notes(movement_td, tip)
victory_notes(movement_td, tip)
}
function get_bases(army_key) {
if (army_key == "None") {
return [];
}
if (army_key == "All") {
""");
sys.stdout.write("return ")
sys.stdout.write( json.dumps( base_order ) )
sys.stdout.write(";")
sys.stdout.write("""
}
return bases[army_key]
}
function get_red_bases()
{
var army_key = document.getElementById('red').value
return get_bases(army_key)
}
function get_blue_bases()
{
var army_key = document.getElementById('blue').value
return get_bases(army_key)
}
function get_combined_bases() {
var red = get_red_bases();
var bases = new Set(red)
var blue = get_blue_bases();
for (i in blue) {
var base_name = blue[i];
bases.add(base_name);
}
return bases;
}
function add_army_color(elem, color) {
var span = document.createElement("span");
elem.appendChild(span);
span.innerHTML = color;
var span_class = document.createAttribute("class");
span_class.value = color;
span.setAttributeNode(span_class)
var br = document.createElement("br")
elem.appendChild(br);
}
function update_bases() {
var army_elem = document.getElementById('armies');
army_elem.innerHTML = '';
while (army_elem.lastElementChild) {
army_elem.removeChild(army_elem.lastElementChild);
}
var red_bases = new Set(get_red_bases())
var blue_bases = new Set(get_blue_bases())
var base_names = get_combined_bases()
var table_node = document.createElement("table");
army_elem.appendChild(table_node);
var tbody = document.createElement("tbody");
table_node.appendChild(tbody);
for (i in base_order) {
var base_name = base_order[i]
if (base_names.has(base_name)) {
var tr = document.createElement("tr")
var tr_class = document.createAttribute("class");
tr_class.value = "nobreak";
tr.setAttributeNode(tr_class)
tbody.appendChild(tr)
var armies_td = document.createElement("td")
var tips_td = document.createElement("td")
tr.appendChild(armies_td)
tr.appendChild(tips_td)
if (base_name in tool_tips) {
add_tool_tips(tips_td, base_name);
} else {
tips_td.innerHTML = base_name;
}
if ( red_bases.has(base_name)) {
add_army_color(armies_td, "red");
}
if ( blue_bases.has(base_name)) {
add_army_color(armies_td, "blue");
}
}
}
}
function red_selected() {
update_bases();
}
function blue_selected() {
update_bases();
}
""")
print("""
</script>
<title> DBA 3.0 Battle Cheat Sheet</title>
</head>
<body>
<h1>DBA 3.0 Battle Cheat Sheet</h1>
<form>
""")
def generate_army_selector(color):
sys.stdout.write('<tr class="%s">' % (color))
sys.stdout.write("<td>")
sys.stdout.write('<label for="%s">%s</label>' % (color, color))
sys.stdout.write("</td>")
sys.stdout.write("<td>")
sys.stdout.write('<select name="%s" id="%s" onchange="%s_selected()">\n' % (color,color,color))
sys.stdout.write('<option value="None">None</option>\n')
sys.stdout.write('<option value="All">All</option>\n')
for army_name in army_names() :
k=key_army_name_str(army_name)
if army_name.endswith('(Plain)'):
army_name = army_name[:-7]
army_name = army_name.strip()
sys.stdout.write('<option value="%s">%s</option>\n' % (k, army_name))
sys.stdout.write('</select>\n')
sys.stdout.write("</td>")
sys.stdout.write("</tr>")
sys.stdout.write("<table>\n")
sys.stdout.write("<tbody>\n")
generate_army_selector('red')
generate_army_selector('blue')
sys.stdout.write("</tbody>\n")
sys.stdout.write("</table>\n")
print("""
</form>
<p id="armies">
</p>
<p>References:<br/>
<a href="https://steamcommunity.com/sharedfiles/filedetails/?id=2255983207">De Bellis Antiquitatis Version 3.0 Paperback – May 29 2019
by Phil Barker (Author), Sue Laflin-Barker (Author)</a><br/>
<a href="https://github.com/leberechtreinhold/dba3_tts">DBA 3.0 Table Top Simulator</a><br/>
</p>
""")
sys.stdout.write("<p>Web site version:<br/>")
sys.stdout.write( datetime.date.today().isoformat() )
sys.stdout.write("<br/>")
sys.stdout.write('''
<a href="https://github.com/marcpawl/dba3_battle_cheatsheet">
https://github.com/marcpawl/dba3_battle_cheatsheet
</a>
''')
sys.stdout.write("</p>")
print("""
</body>
</html>
""")