151 lines
4.6 KiB
JavaScript
151 lines
4.6 KiB
JavaScript
// ================================================================
|
||
// API & Networking
|
||
// ================================================================
|
||
|
||
window.fetchTowns = async function() {
|
||
try {
|
||
const res = await fetch('/dashboard/towns');
|
||
window.towns = await res.json();
|
||
window.renderTowns();
|
||
window.updateServerStatus(true);
|
||
|
||
if (window.selectedTownId) {
|
||
window.renderBuildQueuePreview();
|
||
window.renderBuildingDropdown();
|
||
window.renderTownDetails();
|
||
}
|
||
} catch (e) {
|
||
window.updateServerStatus(false);
|
||
}
|
||
};
|
||
|
||
window.fetchClientStatus = async function() {
|
||
try {
|
||
const res = await fetch('/dashboard/client-status');
|
||
const data = await res.json();
|
||
const isOnline = data.online === true;
|
||
|
||
// Detect transition: online → offline
|
||
if (window.wasClientOnline === true && !isOnline) {
|
||
// Fail all queued commands immediately
|
||
await fetch('/dashboard/commands/fail-stale', { method: 'POST' });
|
||
window.fetchLog();
|
||
}
|
||
|
||
window.clientOnline = isOnline;
|
||
window.wasClientOnline = isOnline;
|
||
window.updateClientStatus(isOnline);
|
||
} catch (e) {
|
||
window.updateClientStatus(false);
|
||
}
|
||
};
|
||
|
||
window.fetchLog = async function() {
|
||
try {
|
||
const res = await fetch('/dashboard/commands');
|
||
const cmds = await res.json();
|
||
window.renderLog(cmds);
|
||
} catch (e) {}
|
||
};
|
||
|
||
window.updateServerStatus = function(online) {
|
||
const el = document.getElementById('server-status');
|
||
if (online) {
|
||
el.textContent = '● Server';
|
||
el.className = 'conn-badge online';
|
||
} else {
|
||
el.textContent = '● Server offline';
|
||
el.className = 'conn-badge offline';
|
||
}
|
||
};
|
||
|
||
window.updateClientStatus = function(online) {
|
||
const el = document.getElementById('client-status');
|
||
if (online) {
|
||
el.textContent = '● Script';
|
||
el.className = 'conn-badge online';
|
||
} else {
|
||
el.textContent = '● Script offline';
|
||
el.className = 'conn-badge offline';
|
||
}
|
||
// Enable/disable the Send button
|
||
const btn = document.querySelector('#command-form-wrap .btn-gold');
|
||
if (btn) {
|
||
btn.disabled = !online;
|
||
btn.title = online ? '' : 'Script is offline — cannot send commands';
|
||
btn.style.opacity = online ? '' : '0.4';
|
||
btn.style.cursor = online ? '' : 'not-allowed';
|
||
}
|
||
};
|
||
|
||
window.sendCommand = async function() {
|
||
if (!window.clientOnline) return alert('Το script είναι offline — δεν μπορείτε να στείλετε εντολές.');
|
||
const town = window.getSelectedTown();
|
||
if (!town) return alert('Select a town first.');
|
||
|
||
const type = document.getElementById('cmd-type').value;
|
||
let payload = {};
|
||
|
||
if (type === 'build') {
|
||
const building_id = document.getElementById('building-select').value;
|
||
const bData = town.build_data?.[building_id];
|
||
|
||
// UI Validation logic
|
||
if (bData) {
|
||
const missingDeps = bData.missing_dependencies || {};
|
||
const missingKeys = Object.keys(missingDeps);
|
||
|
||
// 1. Popup if dependencies are lacking
|
||
if (missingKeys.length > 0) {
|
||
let msg = '⚠ ΑΔΥΝΑΤΗ Η ΚΑΤΑΣΚΕΥΗ: Λείπουν προϋποθέσεις!\n\nΑπαιτείται:\n';
|
||
for (const k of missingKeys) {
|
||
msg += `- ${missingDeps[k].name} (Επίπεδο ${missingDeps[k].needed_level})\n`;
|
||
}
|
||
return alert(msg);
|
||
}
|
||
|
||
// 2. Confirmation if resources are lacking
|
||
if (bData.enough_resources === false) {
|
||
if (!confirm('❌ Δεν έχετε αρκετούς πόρους!\n\nΘέλετε να προστεθεί στην ουρά αναμονής; Το σύστημα θα το χτίσει αυτόματα μόλις συγκεντρωθούν οι πόροι.')) {
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
|
||
payload = { building_id };
|
||
} else {
|
||
payload = {
|
||
unit_id: document.getElementById('unit-select').value,
|
||
amount: parseInt(document.getElementById('recruit-amount').value) || 1,
|
||
};
|
||
}
|
||
|
||
try {
|
||
const res = await fetch('/dashboard/commands', {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({
|
||
town_id: town.town_id,
|
||
town_name: town.town_name,
|
||
type,
|
||
payload
|
||
})
|
||
});
|
||
const data = await res.json();
|
||
if (data.ok) {
|
||
window.fetchLog();
|
||
} else if (data.error === 'client_offline') {
|
||
alert(data.message || 'Το script είναι offline.');
|
||
} else {
|
||
alert('Error: ' + JSON.stringify(data));
|
||
}
|
||
} catch (e) {
|
||
alert('Failed to send command: ' + e);
|
||
}
|
||
};
|
||
|
||
window.cancelCommand = async function(id) {
|
||
await fetch(`/dashboard/commands/${id}`, { method: 'DELETE' });
|
||
window.fetchLog();
|
||
};
|