// ==UserScript== // @name GrepoTweaks-SentinelButton // @namespace sentinelbutton // @author Sau1707 // @description Add a button in the Context Menu for easly send a sentinel // @version 1.1.1 // @match http://*.grepolis.com/game/* // @match https://*.grepolis.com/game/* // @match https://sau1707.github.io/Grepolis/ // @require https://github.com/Sau1707/Grepolis/raw/main/update.js // ==/UserScript== (function () { 'use strict'; const uw = unsafeWindow ? unsafeWindow : window; /* return true if polis has own troops inside else otherwise or if it's player polis */ function hasSentinel(id) { const towns = Object.keys(uw.ITowns.towns); for (let town of towns) { if (town == id) return true; const models = uw.ITowns.all_supporting_units.fragments[town].models; for (let model of models) if (model.attributes.current_town_id == id) return true; } return false; } /* Retrun true if polis is in current active polis island */ function isTownInIsland(x, y) { const currentTown = uw.ITowns.getCurrentTown(); const cx = currentTown.getIslandCoordinateX(); const cy = currentTown.getIslandCoordinateY(); if (cx == x && cy == y) return true; return false; } /* Make ajax request */ function sendSentinel(unit, target_id) { let data = { id: target_id, type: 'support' }; data[unit] = 3; uw.gpAjax.ajaxPost('town_info', 'send_units', data); } /* Return what unit of current polis can be used */ function selectUnit() { let units = uw.ITowns.getCurrentTown().getLandUnits(); if (units.sword > 3) return 'sword'; if (units.archer > 3) return 'archer'; if (units.hoplite > 3) return 'hoplite'; return null; } /* Return true if the polis already have a support incoming */ function hasIncomingSupport(target_id) { let movments = uw.MM.getModels().MovementsUnits; for (let m in movments) if (movments[m].attributes.target_town_id == target_id) return true; return false; } /* Handle the clic of the sentinel button in context menu*/ function handleButtonClick(target) { let unit = selectUnit(); if (!unit) { uw.HumanMessage.error('No troops available'); return; } sendSentinel(unit, target.id); } /* When user click the polis set current polis */ uw.$.Observer(uw.GameEvents.map.town.click).subscribe((e, data) => { if (!isTownInIsland(data.x, data.y)) return; if (hasSentinel(data.id)) return; if (hasIncomingSupport(data.id)) return; let menu = uw.$('#context_menu'); if (!menu) return; let div = document.createElement('div'); div.id = 'sentinel_button'; div.style.width = '50px'; div.style.height = '50px'; div.style.position = 'absolute'; div.style.background = `url(https://i.ibb.co/9wwBNfD/sentinel.png)`; div.style.zIndex = 'auto'; div.style.cursor = 'pointer'; div.innerHTML = ` `; menu.append(div); uw.$('#sentinel_button').animate({ top: '-100px' }, 120); uw.$('#sentinel_button').hover( () => { uw.$('#sentinel_description').css('display', 'block'); }, () => { uw.$('#sentinel_description').css('display', 'none'); }, ); uw.$('#sentinel_button').click(() => { handleButtonClick(data); menu.remove(); }); }); console.log('[GrepoTweaks-SentinelButton] Loaded'); })();