fix 5
This commit is contained in:
@@ -799,16 +799,18 @@
|
|||||||
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)
|
||||||
|
uw.gpAjax.ajaxPost = function(controller, action, params, loader, cbSuccessOrObj, cbError) {
|
||||||
|
const handleResp = function(resp) {
|
||||||
|
log(`[Market intercept] ctrl:${controller} action:${action} has_offers:${!!(resp && resp.offers)}`);
|
||||||
if (!intercepted && resp && resp.offers) {
|
if (!intercepted && resp && resp.offers) {
|
||||||
intercepted = true;
|
intercepted = true;
|
||||||
clearTimeout(timeoutId);
|
clearTimeout(timeoutId);
|
||||||
uw.gpAjax.ajaxPost = origAjaxPost; // restore
|
uw.gpAjax.ajaxPost = origAjaxPost;
|
||||||
fetch(`${BASE_URL}/api/market_data?player_id=${uw.Game.player_id}`, {
|
fetch(`${BASE_URL}/api/market_data?player_id=${uw.Game.player_id}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
@@ -817,14 +819,25 @@
|
|||||||
.then(() => resolve({ ok: true, msg: `Market scanned: ${resp.offers.length} offers` }))
|
.then(() => resolve({ ok: true, msg: `Market scanned: ${resp.offers.length} offers` }))
|
||||||
.catch(e => resolve({ ok: false, msg: 'Upload failed: ' + e }));
|
.catch(e => resolve({ ok: false, msg: 'Upload failed: ' + e }));
|
||||||
}
|
}
|
||||||
if (callbacks.success) callbacks.success(resp);
|
|
||||||
},
|
|
||||||
error: callbacks.error
|
|
||||||
} : callbacks;
|
|
||||||
return origAjaxPost(controller, action, params, loader, wrappedCallbacks);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Trigger the getData
|
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 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);
|
||||||
|
|||||||
Reference in New Issue
Block a user