new market

This commit is contained in:
2026-04-25 16:01:15 +03:00
parent 8a64a7b4fc
commit a143345831
6 changed files with 282 additions and 8 deletions

View File

@@ -786,6 +786,80 @@
await sleep(500);
return { ok: true, msg: `Market offer posted: ${offer} ${offer_type} => ${demand} ${demand_type}` };
}
// ----------------------------------------------------------------
// Execute: Scan Market (On-Demand)
// ----------------------------------------------------------------
async function executeScanMarket(cmd) {
const { town_id } = cmd;
const reactionMs = randInt(1500, 3000);
log(`Waiting ${reactionMs}ms before scanning market...`);
await sleep(reactionMs);
if (paused) return { ok: false, msg: 'Aborted due to pause/captcha' };
return new Promise((resolve) => {
uw.gpAjax.ajaxPost('frontend_bridge', 'execute', {
model_url: 'BuildingMarket',
action_name: 'getData',
arguments: {
limit: 20,
offset: 0,
demand_type: 'all_but_gold',
offer_type: 'all_but_gold',
max_ratio: 3,
max_delivery_time: 172800,
visibility: 2,
order_by: 'ratio',
order_direction: 'desc'
},
town_id: town_id,
nl_init: true
}, false, {
success: async function(resp) {
try {
// Send the data back to our backend
await fetch(`${BASE_URL}/api/market_data?player_id=${uw.Game.player_id}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(resp)
});
resolve({ ok: true, msg: 'Market scanned and data uploaded' });
} catch (e) {
resolve({ ok: false, msg: 'Failed to upload market data: ' + e });
}
},
error: function() {
resolve({ ok: false, msg: 'Failed to fetch market data from game' });
}
});
});
}
// ----------------------------------------------------------------
// Execute: Accept Market Offer
// ----------------------------------------------------------------
async function executeAcceptMarketOffer(cmd) {
const { town_id, payload } = cmd;
const { offer_id, amount } = payload;
const reactionMs = randInt(800, 2000);
log(`Waiting ${reactionMs}ms before accepting offer...`);
await sleep(reactionMs);
if (paused) return { ok: false, msg: 'Aborted due to pause/captcha' };
uw.gpAjax.ajaxPost('frontend_bridge', 'execute', {
model_url: 'BuildingMarket',
action_name: 'acceptOffer',
arguments: { offer_id, amount },
town_id: town_id,
nl_init: true
});
await sleep(500);
return { ok: true, msg: `Accepted market offer ${offer_id} for amount ${amount}` };
}
// ----------------------------------------------------------------
// Execute: Research (Academy)
@@ -836,6 +910,8 @@
// Build queue, Recruit queue and Market queue are independent
const buildCmd = cmdData.build;
const recruitCmd = cmdData.recruit;
const scanMarketCmd = cmdData.scan_market;
const acceptMarketCmd = cmdData.accept_market_offer;
const marketCmd = cmdData.market;
const researchCmd = cmdData.research;
const farmCmd = cmdData.farm;
@@ -851,17 +927,24 @@
const execute = async (cmd) => {
if (!cmd) return;
log(`Executing command #${cmd.id} — type:${cmd.type} town:${cmd.town_id}`);
if (paused) {
log(`[Paused] Ignoring command #${cmd.id}`);
return;
}
let result;
try {
if (cmd.type === 'build') result = await executeBuild(cmd);
else if (cmd.type === 'recruit') result = await executeRecruit(cmd);
else if (cmd.type === 'market_offer') result = await executeMarketOffer(cmd);
else if (cmd.type === 'scan_market') result = await executeScanMarket(cmd);
else if (cmd.type === 'accept_market_offer') result = await executeAcceptMarketOffer(cmd);
else if (cmd.type === 'research') result = await executeResearch(cmd);
else if (cmd.type === 'farm_loot') result = await executeFarmLoot(cmd);
else if (cmd.type === 'farm_upgrade') result = await executeFarmUpgrade(cmd);
else result = { ok: false, msg: `Unknown type: ${cmd.type}` };
} catch (e) {
result = { ok: false, msg: `Exception: ${e.message}` };
result = { ok: false, msg: `Exception: ${e}` };
}
const finalStatus = result.requeue ? 'pending' : (result.ok ? 'done' : 'failed');
log(`Command #${cmd.id}: ${finalStatus === 'done' ? '✅' : finalStatus === 'pending' ? '⏳' : '❌'} ${result.msg}`);
@@ -872,6 +955,8 @@
await execute(buildCmd);
await execute(recruitCmd);
await execute(marketCmd);
await execute(scanMarketCmd);
await execute(acceptMarketCmd);
await execute(researchCmd);
await execute(farmCmd);
await execute(farmUpgradeCmd);