Update static/js/app.js
This commit is contained in:
@@ -1,4 +1,21 @@
|
|||||||
document.addEventListener("DOMContentLoaded", () => {
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
|
let buildingNames = {};
|
||||||
|
let unitNames = {};
|
||||||
|
|
||||||
|
// Fetch building names first
|
||||||
|
fetch("/api/buildings")
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(data => {
|
||||||
|
buildingNames = data;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Fetch unit names
|
||||||
|
fetch("/api/units")
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(data => {
|
||||||
|
unitNames = data;
|
||||||
|
});
|
||||||
|
|
||||||
fetch("/api/data")
|
fetch("/api/data")
|
||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
@@ -51,29 +68,80 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
item.addEventListener("click", () => {
|
item.addEventListener("click", () => {
|
||||||
const townId = parseInt(item.dataset.townid);
|
const townId = parseInt(item.dataset.townid);
|
||||||
const selectedTown = player.towns.find(t => t.town_id === townId);
|
const selectedTown = player.towns.find(t => t.town_id === townId);
|
||||||
showTownDetails(selectedTown);
|
showTownMenu(selectedTown);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 2: Show town data
|
// Step 2: Show town menu
|
||||||
function showTownDetails(town) {
|
function showTownMenu(town) {
|
||||||
let html = `<h4>${town.town_name}</h4>`;
|
let html = `<h4>${town.town_name}</h4>`;
|
||||||
html += `<p><strong>Points:</strong> ${town.points} | <strong>Population:</strong> ${town.population}</p>`;
|
html += `<p>Select info to view:</p>`;
|
||||||
html += `<h5>Resources</h5>
|
html += `
|
||||||
<ul>
|
<select id="town-info-select" class="form-select mb-2">
|
||||||
|
<option value="main">Main Info</option>
|
||||||
|
<option value="buildingOrder">Building Order</option>
|
||||||
|
<option value="researches">Researches</option>
|
||||||
|
<option value="units">Units</option>
|
||||||
|
</select>
|
||||||
|
<div id="town-info-display"></div>
|
||||||
|
`;
|
||||||
|
playerDetails.innerHTML = html;
|
||||||
|
|
||||||
|
const infoSelect = document.getElementById("town-info-select");
|
||||||
|
const infoDisplay = document.getElementById("town-info-display");
|
||||||
|
|
||||||
|
function renderInfo(option) {
|
||||||
|
let displayHTML = "";
|
||||||
|
if (option === "main") {
|
||||||
|
displayHTML += `<h5>Main Info</h5>`;
|
||||||
|
displayHTML += `<ul>
|
||||||
|
<li>Sea: ${town.sea}</li>
|
||||||
<li>Wood: ${town.wood}</li>
|
<li>Wood: ${town.wood}</li>
|
||||||
<li>Stone: ${town.stone}</li>
|
<li>Stone: ${town.stone}</li>
|
||||||
<li>Iron: ${town.iron}</li>
|
<li>Iron: ${town.iron}</li>
|
||||||
</ul>`;
|
<li>Population: ${town.population}</li>
|
||||||
html += `<h5>Buildings</h5><ul>`;
|
<li>Points: ${town.points}</li>
|
||||||
Object.entries(town.buildings).forEach(([b, lvl]) => {
|
</ul>`;
|
||||||
if (b !== "id") {
|
displayHTML += `<h5>Buildings</h5><ul>`;
|
||||||
html += `<li>${b}: ${lvl}</li>`;
|
Object.entries(town.buildings).forEach(([b, lvl]) => {
|
||||||
|
if (b !== "id") {
|
||||||
|
const name = buildingNames[b]?.name || b;
|
||||||
|
displayHTML += `<li>${name}: ${lvl}</li>`;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
displayHTML += `</ul>`;
|
||||||
|
} else if (option === "buildingOrder") {
|
||||||
|
displayHTML += `<h5>Building Order</h5><ul>`;
|
||||||
|
town.buildingOrder.forEach(b => {
|
||||||
|
const name = buildingNames[b.building_type]?.name || b.building_type;
|
||||||
|
displayHTML += `<li>${name} (ID: ${b.id}) - To complete at: ${b.to_be_completed_at}</li>`;
|
||||||
|
});
|
||||||
|
displayHTML += `</ul>`;
|
||||||
|
} else if (option === "researches") {
|
||||||
|
displayHTML += `<h5>Researches</h5><ul>`;
|
||||||
|
Object.entries(town.researches).forEach(([r, val]) => {
|
||||||
|
if (r !== "id" && val) displayHTML += `<li>${r}</li>`;
|
||||||
|
});
|
||||||
|
displayHTML += `</ul>`;
|
||||||
|
} else if (option === "units") {
|
||||||
|
displayHTML += `<h5>Units</h5><ul>`;
|
||||||
|
Object.entries(town.units).forEach(([u, val]) => {
|
||||||
|
const name = unitNames[u]?.name || u; // <-- Use Greek name mapping
|
||||||
|
displayHTML += `<li>${name}: ${val}</li>`;
|
||||||
|
});
|
||||||
|
displayHTML += `</ul>`;
|
||||||
}
|
}
|
||||||
|
infoDisplay.innerHTML = displayHTML;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initial render
|
||||||
|
renderInfo(infoSelect.value);
|
||||||
|
|
||||||
|
// Update on selection change
|
||||||
|
infoSelect.addEventListener("change", () => {
|
||||||
|
renderInfo(infoSelect.value);
|
||||||
});
|
});
|
||||||
html += `</ul>`;
|
|
||||||
playerDetails.innerHTML = html;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user