From 1dc96a53ee65c92f1c09d53dccbf1212e7ceea81 Mon Sep 17 00:00:00 2001 From: haunter Date: Fri, 24 Apr 2026 22:07:00 +0300 Subject: [PATCH] fix 6 --- GrepolisRemoteControl.user.js | 76 +++++++++++++++-------------------- 1 file changed, 32 insertions(+), 44 deletions(-) diff --git a/GrepolisRemoteControl.user.js b/GrepolisRemoteControl.user.js index 0a7aaa3..cbe8475 100644 --- a/GrepolisRemoteControl.user.js +++ b/GrepolisRemoteControl.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name Grepolis Remote Control // @namespace http://tampermonkey.net/ -// @version 3.6.6 +// @version 3.6.7 // @description Polls grepo.haunter-pets.top for remote commands and executes them in-game (Multi-Player) // @author Dimitrios // @match https://*.grepolis.com/game/* @@ -818,60 +818,48 @@ if (farmSettings.bandit_camp_enabled) { try { // player_id already declared above in pollAndExecute scope - // Get any valid town_id — we just need one to make the request const currentTownId = uw.ITowns?.getCurrentTown?.()?.id || Object.keys(uw.ITowns?.towns || {})[0] || null; if (!player_id || !currentTownId) { log(`⚔️ Bandit Camp: Missing globals — player_id=${player_id} town_id=${currentTownId}`); } else { - // Build hash param — it's embedded in the page URL or game globals - const hash = uw.Game?.csrf_token || uw.Game?.h || uw.Game?.hash || ''; + // First try: MM collection (works once camp was opened in-game) + let spotData = uw.MM.getOnlyCollectionByName('PlayerAttackSpot')?.models?.[0]?.attributes || null; - // Direct fetch of the PlayerAttackSpot model state - const url = `/game/frontend_bridge?town_id=${currentTownId}&action=execute&h=${hash}`; - const body = new URLSearchParams({ - json: JSON.stringify({ - model_url: `PlayerAttackSpot/${player_id}`, - action_name: 'get_state', - captcha: null, - arguments: {}, - town_id: currentTownId, - nl_init: true - }) - }); - - const resp = await fetch(url, { - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', - 'X-Requested-With': 'XMLHttpRequest' - }, - body: body.toString() - }); - - let spotData = null; - if (resp.ok) { - try { - const raw = await resp.json(); - // Try to find the spot data in the response - spotData = raw?.data?.PlayerAttackSpot?.[player_id] - || raw?.data?.player_attack_spot - || raw?.PlayerAttackSpot - || (Array.isArray(raw?.data) ? raw.data[0] : null) - || raw; - if (spotData?.cooldown_at === undefined) spotData = null; - } catch(e) {} - } - - // Fallback: try the MM collection (works if camp was opened once) + // Second try: use gpAjax with a callback — it handles auth/hash internally if (!spotData) { - const mmModel = uw.MM.getOnlyCollectionByName('PlayerAttackSpot')?.models?.[0]; - if (mmModel) spotData = mmModel.attributes; + spotData = await new Promise((resolve) => { + try { + uw.gpAjax.ajaxPost('frontend_bridge', 'execute', { + model_url: `PlayerAttackSpot/${player_id}`, + action_name: 'get_own', + captcha: null, + arguments: {}, + town_id: currentTownId, + nl_init: true + }, false, { + success: function(data) { + // Response may contain the spot data directly + const d = data?.PlayerAttackSpot + || data?.data?.PlayerAttackSpot?.[player_id] + || data?.[player_id] + || data; + resolve((d && d.cooldown_at !== undefined) ? d : null); + }, + error: function() { resolve(null); } + }); + } catch(e) { resolve(null); } + }); + + // After the gpAjax call, MM might now have the model + if (!spotData) { + spotData = uw.MM.getOnlyCollectionByName('PlayerAttackSpot')?.models?.[0]?.attributes || null; + } } if (!spotData) { - log(`⚔️ Bandit Camp: Could not load spot data. hash=${hash ? 'found' : 'MISSING'}. Try opening the camp window in-game once.`); + log('⚔️ Bandit Camp: Could not load spot data. Open the Bandit Camp window in-game once to let the game load it.'); } else { const nowTs = Math.floor(Date.now() / 1000); const spotId = spotData.id || player_id;