This commit is contained in:
2026-04-20 15:04:42 +03:00
parent 484ea9dc0f
commit eba29659d6
8 changed files with 587 additions and 588 deletions

View File

@@ -0,0 +1,45 @@
// ================================================================
// Command Form Component
// ================================================================
window.onCmdTypeChange = function() {
const type = document.getElementById('cmd-type').value;
document.getElementById('build-options').style.display = type === 'build' ? '' : 'none';
document.getElementById('recruit-options').style.display = type === 'recruit' ? '' : 'none';
document.getElementById('amount-group').style.display = type === 'recruit' ? '' : 'none';
};
window.renderBuildingDropdown = function() {
const town = window.getSelectedTown();
if (!town) return;
const bSelect = document.getElementById('building-select');
const bLevels = town.buildings || {};
const currentVal = bSelect.value;
bSelect.innerHTML = '';
for (const [key, nameGr] of Object.entries(window.BUILDING_NAMES_GR)) {
const level = bLevels[key] !== undefined ? bLevels[key] : "?";
const option = document.createElement('option');
option.value = key;
option.textContent = `${nameGr} [Επίπεδο ${level}]`;
bSelect.appendChild(option);
}
if (currentVal && Array.from(bSelect.options).some(o => o.value === currentVal)) {
bSelect.value = currentVal;
}
};
window.renderBuildQueuePreview = function() {
const town = window.getSelectedTown();
const el = document.getElementById('build-queue-preview');
if (!town || !town.build_queue || !town.build_queue.length) {
el.innerHTML = '<span style="color:#444">Build queue: empty</span>';
return;
}
const items = town.build_queue.map(o =>
`<span>${o.building_type || o.name || JSON.stringify(o)}</span>`
).join('');
el.innerHTML = `<div style="margin-top:6px;color:#888;font-size:0.72rem;text-transform:uppercase;letter-spacing:0.5px;margin-bottom:4px;">Current queue</div>${items}`;
};

View File

@@ -0,0 +1,36 @@
// ================================================================
// Command Log Component
// ================================================================
window.renderLog = function(cmds) {
const el = document.getElementById('log-content');
if (!cmds.length) {
el.innerHTML = '<p id="empty-log">No commands sent yet.</p>';
return;
}
const rows = cmds.map(cmd => {
const p = typeof cmd.payload === 'string' ? JSON.parse(cmd.payload) : cmd.payload;
const desc = cmd.type === 'build'
? `Build: ${p.building_id}`
: `Recruit: ${p.amount}x ${p.unit_id}`;
const statusClass = `status-${cmd.status}`;
const cancelBtn = `<button class="btn btn-danger btn-sm" onclick="cancelCommand(${cmd.id})">✕</button>`;
return `<tr>
<td style="color:#888;font-size:0.7rem">#${cmd.id}</td>
<td>${cmd.town_name || cmd.town_id}</td>
<td>${desc}</td>
<td><span class="status-badge ${statusClass}">${cmd.status}</span></td>
<td style="color:#666;font-size:0.72rem">${cmd.result_msg || ''}</td>
<td>${cancelBtn}</td>
</tr>`;
}).join('');
el.innerHTML = `<table>
<thead><tr>
<th>#</th><th>Town</th><th>Command</th><th>Status</th><th>Result</th><th></th>
</tr></thead>
<tbody>${rows}</tbody>
</table>`;
};

View File

@@ -0,0 +1,112 @@
// ================================================================
// Town Viewer Component (Left panel + Town detail view)
// ================================================================
window.renderTowns = function() {
const container = document.getElementById('town-list');
if (!window.towns.length) {
container.innerHTML = '<p style="color:#444;font-size:0.8rem;">No towns received yet.</p>';
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 `
<div class="town-card ${selected} ${stale ? 'town-stale' : ''}"
onclick="selectTown('${t.town_id}')">
<div class="town-name">${t.town_name}${t.has_premium ? ' <span style="color:#c8a44a;font-size:0.7rem;">★</span>' : ''}</div>
<div class="town-meta">${t.sea != null ? `🌊 Θ${t.sea} · ` : ''}${t.points} pts · ${t.god || 'No god'} · ${ageMin}m ago</div>
<div class="town-res">
<div class="res-item"><div class="res-icon res-wood"></div>${window.fmt(t.resources.wood)}</div>
<div class="res-item"><div class="res-icon res-stone"></div>${window.fmt(t.resources.stone)}</div>
<div class="res-item"><div class="res-icon res-iron"></div>${window.fmt(t.resources.iron)}</div>
<div class="res-item"><div class="res-icon res-pop"></div>${window.fmt(t.resources.population)}</div>
</div>
</div>`;
}).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.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 = `
<div style="font-size:0.75rem; color:#aaa; margin-bottom: 4px;">Χωρητικότητα: <strong style="color:#eee">${capFmt}</strong></div>
<div>${window.RES_ICONS.wood} Ξύλο: <strong style="${getCol(t.resources.wood)}">${window.fmt(t.resources.wood)}</strong></div>
<div>${window.RES_ICONS.stone} Πέτρα: <strong style="${getCol(t.resources.stone)}">${window.fmt(t.resources.stone)}</strong></div>
<div>${window.RES_ICONS.iron} Ασήμι: <strong style="${getCol(t.resources.iron)}">${window.fmt(t.resources.iron)}</strong></div>
<div style="margin-top: 6px;">${window.RES_ICONS.pop} Πληθυσμός: <strong style="color:#fff">${t.resources.population}</strong></div>
`;
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 = `
<div>Πόντοι: <strong>${t.points}</strong>${t.wonder_points ? ` / Θαύμα: <strong>${t.wonder_points}</strong>` : ''}</div>
<div>Θεός: <strong>${godName}</strong></div>
<div>Θάλασσα: <strong style="color:#6fcfcf">${seaStr}</strong><span style="color:#666;font-size:0.72rem">${coordStr}</span></div>
<div>Παίκτης: <strong style="color:#aaa">${t.player}</strong>${t.has_premium ? ' <span style="color:#c8a44a" title="Premium">★</span>' : ''}</div>
${t.alliance_id ? `<div>Συμμαχία: <strong style="color:#aaa">${t.alliance_id}</strong></div>` : ''}
`;
// ---- 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]) =>
`<div>✓ ${k.replace(/_/g, ' ')}</div>`
).join('');
} else {
resHtml = '<div style="color:#555">Καμία έρευνα</div>';
}
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 += `<div>${name}: <strong style="color:#fff">${count}</strong></div>`;
}
}
if(unitsHtml === '') unitsHtml = '<div style="color:#666">Κανένα στράτευμα</div>';
document.getElementById('td-units').innerHTML = unitsHtml;
};