// ================================================================
// Town Viewer Component (Left panel + Town detail view)
// ================================================================
window.renderTowns = function() {
const container = document.getElementById('town-list');
if (!window.towns.length) {
container.innerHTML = '
No towns received yet.
';
return;
}
const now = Date.now();
container.innerHTML = window.towns.map(t => {
const updatedMs = new Date(t.updated_at + 'Z').getTime();
const ageMin = Math.round((now - updatedMs) / 60000);
const stale = ageMin > 3;
const selected = t.town_id === window.selectedTownId ? 'selected' : '';
return `
${t.town_name}${t.has_premium ? ' ★' : ''}
${t.sea != null ? `🌊 Θ${t.sea} · ` : ''}${t.points} pts · ${t.god || 'No god'} · ${ageMin}m ago
${window.fmt(t.resources.wood)}
${window.fmt(t.resources.stone)}
${window.fmt(t.resources.iron)}
${window.fmt(t.resources.population)}
`;
}).join('');
};
window.selectTown = function(id) {
window.selectedTownId = id;
window.renderTowns();
document.getElementById('no-town-selected').style.display = 'none';
document.getElementById('command-form-wrap').style.display = 'block';
document.getElementById('town-details-panel').style.display = 'block';
window.renderBuildQueuePreview();
window.renderBuildingDropdown();
window.renderUnitDropdown();
window.renderTownDetails();
};
window.getSelectedTown = function() {
return window.towns.find(t => t.town_id === window.selectedTownId);
};
window.renderTownDetails = function() {
const t = window.getSelectedTown();
if(!t) return;
document.getElementById('td-name').textContent = t.town_name;
const cap = t.resources.storage || 1;
const getCol = (amt) => {
const pct = amt / cap;
if (pct >= 0.95) return 'color: #ff4a4a;'; // Red
if (pct >= 0.85) return 'color: #ffa500;'; // Orange
return 'color: #fff;';
};
const capFmt = (t.resources.storage != null && t.resources.storage !== '') ? window.fmt(t.resources.storage) : '?';
document.getElementById('td-resources').innerHTML = `
Χωρητικότητα: ${capFmt}
${window.RES_ICONS.wood} Ξύλο: ${window.fmt(t.resources.wood)}
${window.RES_ICONS.stone} Πέτρα: ${window.fmt(t.resources.stone)}
${window.RES_ICONS.iron} Ασήμι: ${window.fmt(t.resources.iron)}
${window.RES_ICONS.pop} Πληθυσμός: ${t.resources.population}
`;
const godName = t.god ? t.god.charAt(0).toUpperCase() + t.god.slice(1) : 'Κανένας';
const seaStr = t.sea != null ? `Θ${t.sea}` : '—';
const coordStr = (t.x != null && t.y != null) ? ` (${Math.round(t.x)}:${Math.round(t.y)})` : '';
document.getElementById('td-general').innerHTML = `
Πόντοι: ${t.points}${t.wonder_points ? ` / Θαύμα: ${t.wonder_points}` : ''}
Θεός: ${godName}
Θάλασσα: ${seaStr}${coordStr}
Παίκτης: ${t.player}${t.has_premium ? ' ★' : ''}
${t.alliance_id ? `Συμμαχία: ${t.alliance_id}
` : ''}
`;
// ---- Researches ----
const researchObj = t.researches || {};
const researchEntries = Object.entries(researchObj).filter(([k, v]) =>
k !== 'id' && k !== 'town_id' && (v === true || v === 1 || Number(v) > 0)
);
let resHtml = '';
if (researchEntries.length) {
resHtml = researchEntries.map(([k]) =>
`✓ ${k.replace(/_/g, ' ')}
`
).join('');
} else {
resHtml = 'Καμία έρευνα
';
}
document.getElementById('td-researches').innerHTML = resHtml;
const unitsObj = t.units || {};
let unitsHtml = '';
for(const [unitKey, count] of Object.entries(unitsObj)) {
if(count > 0 && unitKey !== 'militia') {
const name = window.UNIT_NAMES_GR[unitKey] || unitKey;
unitsHtml += `${name}: ${count}
`;
}
}
if(unitsHtml === '') unitsHtml = 'Κανένα στράτευμα
';
document.getElementById('td-units').innerHTML = unitsHtml;
};