Skip to content

Commit 9a17b26

Browse files
use dictionary to json for illustrated bo
1 parent b611beb commit 9a17b26

File tree

1 file changed

+65
-60
lines changed

1 file changed

+65
-60
lines changed

public/js/AoE4_Overlay.js

Lines changed: 65 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ function getCommonImages() {
155155
'knight2': 'unit_cavalry/knight-2.png',
156156
'knight3': 'unit_cavalry/knight-2.png',
157157
'knight4': 'unit_cavalry/knight-2.png',
158+
'lancer3': 'unit_cavalry/lancer-4.png',
158159
'lancer_3': 'unit_cavalry/lancer-4.png',
159160
'lancer_4': 'unit_cavalry/lancer-4.png',
160161
'scout': 'unit_cavalry/scout.png',
@@ -543,8 +544,8 @@ function convertNotesIllustrations(civ_name, input) {
543544
function resourceValue(input) {
544545
if ((input != '') && (input != ' ')) {
545546

546-
if ((typeof input == "string") && !isNaN(input) && !isNaN(parseFloat(input))) {
547-
return input;
547+
if ((typeof input == 'string') && !isNaN(input) && !isNaN(parseInt(input))) {
548+
return parseInt(input);
548549
} else {
549550
return -1;
550551
}
@@ -617,77 +618,81 @@ function focusToAuthorName(input) {
617618

618619
// Copy to clipboard for Illustrated Build Order format
619620
function copyForIllustratedOverlay() {
620-
var rows = document.getElementById('buildTable').rows;
621-
var str = ''; // output string
622-
var newline = '\r\n';
623-
624-
str += '{' + newline;
625621

626622
// selected civilization
627-
var civ_name = civToOverlayName(document.getElementById('civilizationName').innerHTML);
628-
str += ' "civilization": "' + civ_name + '",' + newline
629-
// name, author, source
630-
str += ' "name": "' + focusToBuildOrderName(document.getElementById("civilizationFocus").innerHTML) + '",' + newline
631-
str += ' "author": "' + focusToAuthorName(document.getElementById("civilizationFocus").innerHTML) + '",' + newline
632-
str += ' "source": "unknown",' + newline
633-
634-
// build order
635-
str += ' "build_order": [' + newline
636-
637-
for (let row_id = 1; row_id < rows.length; row_id++) { // loop on the steps of the build order (one row per step)
638-
639-
// population and age undefined
640-
str += ' {' + newline
641-
str += ' "population_count": -1,' + newline
642-
str += ' "villager_count": -1,' + newline
643-
str += ' "age": -1,' + newline
644-
645-
// time option
646-
var time_target = rows[row_id].cells[0].innerHTML;
647-
if (time_target != "" && time_target != " ") {
648-
str += ' "time": ' + time_target + ',' + newline
623+
var civName = civToOverlayName(document.getElementById("civilizationName").innerHTML);
624+
625+
// start JSON Obj
626+
var jsonObj = {
627+
civilization: civName,
628+
name: focusToBuildOrderName(document.getElementById("civilizationFocus").innerHTML),
629+
author: focusToAuthorName(document.getElementById("civilizationFocus").innerHTML),
630+
source: "unknown",
631+
build_order: []
632+
};
633+
634+
// loop on the rows of the build table
635+
var firstRow = true;
636+
var rows = document.getElementById("buildTable").rows;
637+
638+
for (var currentLine of rows) {
639+
// skip the first row
640+
if (firstRow) {
641+
firstRow = false;
642+
continue;
649643
}
650644

651-
// resources
652-
str += ' "resources": {' + newline
653-
str += ' "food": ' + resourceValue(rows[row_id].cells[1].innerHTML) + ',' + newline
654-
str += ' "wood": ' + resourceValue(rows[row_id].cells[2].innerHTML) + ',' + newline
655-
str += ' "gold": ' + resourceValue(rows[row_id].cells[4].innerHTML) + ',' + newline
656-
str += ' "stone": ' + resourceValue(rows[row_id].cells[3].innerHTML) + newline
657-
str += ' },' + newline
645+
// new step element for the JSON format
646+
var newStepJson = {
647+
age: -1, // unknown
648+
population_count: -1 // unknown
649+
};
658650

659-
// notes in a single line
660-
var single_line_notes = convertNotesIllustrations(civ_name, rows[row_id].cells[5].innerHTML);
661-
662-
// split the single line to multiple ones, using the '. ' pattern
663-
array_notes = single_line_notes.split('. ');
664-
665-
str += ' "notes": [' + newline;
666-
for (var array_id = 0; array_id < array_notes.length; array_id++) {
667-
str += ' "' + array_notes[array_id];
668-
if (array_id < array_notes.length - 1) {
669-
str += '.",' + newline;
670-
} else {
671-
str += '"' + newline;
672-
}
651+
// add time if indicated
652+
var timeTarget = currentLine.cells[0].innerHTML;
653+
if (timeTarget != "" && timeTarget != " ") {
654+
newStepJson["time"] = timeTarget;
673655
}
674-
str += ' ]' + newline;
675656

676-
if (row_id < rows.length - 1) {
677-
str += ' },' + newline
678-
} else {
679-
str += ' }' + newline
657+
// villagers per resource
658+
var foodValue = resourceValue(currentLine.cells[1].innerHTML);
659+
var woodValue = resourceValue(currentLine.cells[2].innerHTML);
660+
var goldValue = resourceValue(currentLine.cells[4].innerHTML);
661+
var stoneValue = resourceValue(currentLine.cells[3].innerHTML);
662+
663+
// set villager count to all of the villagers on resouces combined
664+
if ((foodValue >= 0 && woodValue >= 0 && goldValue >= 0 && stoneValue >= 0)) {
665+
newStepJson["villager_count"] = foodValue + woodValue + goldValue + stoneValue;
666+
} else { // at least one resource not specified
667+
newStepJson["villager_count"] = -1;
680668
}
681-
}
682669

683-
// finish build order and JSON
684-
str += ' ]' + newline
685-
str += '}'
670+
// resources
671+
newStepJson["resources"] = {
672+
food: foodValue,
673+
wood: woodValue,
674+
gold: goldValue,
675+
stone: stoneValue
676+
};
677+
678+
// notes in a single line
679+
var single_line_notes = convertNotesIllustrations(civName, currentLine.cells[5].innerHTML);
680+
681+
// split the single line to multiple ones, using the ". " pattern
682+
var notes = single_line_notes.split(". ");
683+
for (let i = 0; i < notes.length - 1; i++) {
684+
notes[i] += '.'; // add dot removed by split
685+
}
686+
newStepJson["notes"] = notes;
686687

688+
// add new step
689+
jsonObj["build_order"].push(newStepJson);
690+
}
691+
var str = JSON.stringify(jsonObj, null, 4); // JSON to output string
687692
//console.log(str);
688693
navigator.clipboard.writeText(htmlDecode(str)).then(function() {
689694
//console.log('Async: Copying to clipboard was successful!');
690695
}, function(err) {
691-
console.error('Async: Could not copy text: ', err);
696+
console.error("Async: Could not copy text: ", err);
692697
});
693698
}

0 commit comments

Comments
 (0)