This commit is contained in:
2026-04-26 01:51:22 +03:00
parent b577c95f7c
commit b688b66275

View File

@@ -799,32 +799,45 @@
let intercepted = false; let intercepted = false;
let timeoutId; let timeoutId;
// Wrap gpAjax to intercept ANY response that looks like market data
const origAjaxPost = uw.gpAjax.ajaxPost.bind(uw.gpAjax); const origAjaxPost = uw.gpAjax.ajaxPost.bind(uw.gpAjax);
uw.gpAjax.ajaxPost = function(controller, action, params, loader, callbacks) {
const wrappedCallbacks = callbacks && typeof callbacks === 'object' ? { // Handle BOTH Grepolis callback styles:
success: function(resp) { // Style A: ajaxPost(ctrl, action, params, loader, {success, error})
// Market data has an 'offers' array // Style B: ajaxPost(ctrl, action, params, loader, fnSuccess, fnError)
if (!intercepted && resp && resp.offers) { uw.gpAjax.ajaxPost = function(controller, action, params, loader, cbSuccessOrObj, cbError) {
intercepted = true; const handleResp = function(resp) {
clearTimeout(timeoutId); log(`[Market intercept] ctrl:${controller} action:${action} has_offers:${!!(resp && resp.offers)}`);
uw.gpAjax.ajaxPost = origAjaxPost; // restore if (!intercepted && resp && resp.offers) {
fetch(`${BASE_URL}/api/market_data?player_id=${uw.Game.player_id}`, { intercepted = true;
method: 'POST', clearTimeout(timeoutId);
headers: { 'Content-Type': 'application/json' }, uw.gpAjax.ajaxPost = origAjaxPost;
body: JSON.stringify(resp) fetch(`${BASE_URL}/api/market_data?player_id=${uw.Game.player_id}`, {
}) method: 'POST',
.then(() => resolve({ ok: true, msg: `Market scanned: ${resp.offers.length} offers` })) headers: { 'Content-Type': 'application/json' },
.catch(e => resolve({ ok: false, msg: 'Upload failed: ' + e })); body: JSON.stringify(resp)
} })
if (callbacks.success) callbacks.success(resp); .then(() => resolve({ ok: true, msg: `Market scanned: ${resp.offers.length} offers` }))
}, .catch(e => resolve({ ok: false, msg: 'Upload failed: ' + e }));
error: callbacks.error }
} : callbacks; };
return origAjaxPost(controller, action, params, loader, wrappedCallbacks);
if (cbSuccessOrObj && typeof cbSuccessOrObj === 'object') {
// Style A — object with success/error keys
const origSuccess = cbSuccessOrObj.success;
return origAjaxPost(controller, action, params, loader, {
success: function(resp) { handleResp(resp); if (origSuccess) origSuccess(resp); },
error: cbSuccessOrObj.error
});
} else {
// Style B — separate function args
return origAjaxPost(controller, action, params, loader,
function(resp) { handleResp(resp); if (typeof cbSuccessOrObj === 'function') cbSuccessOrObj(resp); },
cbError
);
}
}; };
// Trigger the getData // Trigger getData through frontend_bridge
origAjaxPost('frontend_bridge', 'execute', { origAjaxPost('frontend_bridge', 'execute', {
model_url: 'BuildingMarket', model_url: 'BuildingMarket',
action_name: 'getData', action_name: 'getData',
@@ -841,7 +854,7 @@
// Timeout after 15 seconds // Timeout after 15 seconds
timeoutId = setTimeout(() => { timeoutId = setTimeout(() => {
if (!intercepted) { if (!intercepted) {
uw.gpAjax.ajaxPost = origAjaxPost; // restore uw.gpAjax.ajaxPost = origAjaxPost;
resolve({ ok: false, msg: 'Market scan timed out' }); resolve({ ok: false, msg: 'Market scan timed out' });
} }
}, 15000); }, 15000);