80 lines
3.3 KiB
JavaScript
80 lines
3.3 KiB
JavaScript
// ================================================================
|
||
// 04d_execute_culture.js — Execute Αγορά celebration commands
|
||
//
|
||
// Handles commands of type 'culture' from the poll response.
|
||
// Supported celebration_type values:
|
||
// 'party' → Γιορτή πόλης (costs wood/stone/iron)
|
||
// 'triumph' → Παρέλαση θριάμβου (costs battle points)
|
||
//
|
||
// Uses the same fire-and-forget ajaxPost pattern as all other
|
||
// execute modules in this codebase (04a, 04b, 04c).
|
||
// ================================================================
|
||
|
||
async function executeCultureCommand(cmd) {
|
||
if (!cmd || !cmd.id || !cmd.payload) return;
|
||
|
||
// town_id lives on the command root (like build/recruit), payload has celebration_type
|
||
const town_id = cmd.town_id || cmd.payload.town_id;
|
||
const celebration_type = cmd.payload.celebration_type;
|
||
|
||
if (!celebration_type || !town_id) {
|
||
reportResult(cmd.id, 'failed', 'Invalid culture payload: missing celebration_type or town_id');
|
||
return;
|
||
}
|
||
|
||
if (!['party', 'triumph'].includes(celebration_type)) {
|
||
reportResult(cmd.id, 'failed', `Unknown celebration_type: ${celebration_type}`);
|
||
return;
|
||
}
|
||
|
||
const label = celebration_type === 'party' ? 'Γιορτή πόλης' : 'Παρέλαση θριάμβου';
|
||
log(`[αγορά] Εκτέλεση ${label} για πόλη ${town_id}`);
|
||
|
||
// Validate town exists in game memory
|
||
const town = uw.ITowns?.towns?.[town_id];
|
||
if (!town) {
|
||
reportResult(cmd.id, 'failed', `Η πόλη ${town_id} δεν βρέθηκε στη μνήμη του παιχνιδιού.`);
|
||
return;
|
||
}
|
||
|
||
// Double-check: is there already a celebration of this type running?
|
||
try {
|
||
const celebModels = uw.MM.getModels()?.Celebration;
|
||
if (celebModels) {
|
||
const nowTs = Math.floor(Date.now() / 1000);
|
||
for (const cel of Object.values(celebModels)) {
|
||
const a = cel.attributes;
|
||
if (String(a.town_id) === String(town_id)
|
||
&& a.celebration_type === celebration_type
|
||
&& (a.finished_at ?? 0) > nowTs) {
|
||
reportResult(cmd.id, 'failed',
|
||
`${label} ήδη ενεργή στην πόλη ${town_id}.`);
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
} catch (e) { /* model not loaded — proceed; server already validated */ }
|
||
|
||
// Human-like reaction delay — same as all other executors
|
||
const reactionMs = randInt(800, 2500);
|
||
log(`[αγορά] Waiting ${reactionMs}ms (reaction time)...`);
|
||
await sleep(reactionMs);
|
||
|
||
if (paused) {
|
||
reportResult(cmd.id, 'failed', 'Aborted due to pause/captcha');
|
||
return;
|
||
}
|
||
|
||
// Fire-and-forget — exact same 3-arg pattern used everywhere in this codebase.
|
||
// AutoFarm confirms: page=building_place, action=start_celebration,
|
||
// params={ celebration_type, town_id }
|
||
uw.gpAjax.ajaxPost('building_place', 'start_celebration', {
|
||
town_id: parseInt(town_id, 10),
|
||
celebration_type: celebration_type
|
||
});
|
||
|
||
await sleep(500);
|
||
log(`[αγορά] ✅ ${label} εστάλη (πόλη ${town_id})`);
|
||
reportResult(cmd.id, 'done', `${label} εστάλη επιτυχώς.`);
|
||
}
|