/*jshint multistr: true */
// ==UserScript==
// @name Screeps alliance overlay
// @namespace https://screeps.com/
// @version 0.2.9
// @author James Cook
// @include https://screeps.com/a/
// @run-at document-ready
// @downloadUrl https://raw.githubusercontent.com/LeagueOfAutomatedNations/loan-browser-ext/master/dist/alliance-overlay.user.js
// @grant GM_xmlhttpRequest
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require http://www.leagueofautomatednations.com/static/js/vendor/randomColor.js
// @require https://github.com/Esryok/screeps-browser-ext/raw/master/screeps-browser-core.js
// @connect www.leagueofautomatednations.com
// ==/UserScript==
const loanBaseUrl = "http://www.leagueofautomatednations.com";
let allianceData;
let userAlliance;
function getAllianceLogo(allianceKey) {
let data = allianceData[allianceKey];
if (data) {
return loanBaseUrl + "/obj/" + data.logo;
}
}
let colorMap = {};
function getAllianceColor(allianceKey) {
if (!colorMap[allianceKey]) {
let seed = allianceData[allianceKey].name;
let [hue,sat,lum] = randomColor({
hue: "random",
luminosity: "light",
seed,
format: "hslArray"
});
// the canvas opacity means the light color set has bad costrast: reduce luminosity to improve
colorMap[allianceKey] = `hsl(${hue},${sat}%,${lum/2}%)`;
}
return colorMap[allianceKey];
}
// query for alliance data from the LOAN site
function ensureAllianceData(callback) {
if (allianceData) {
if (callback) callback();
return;
}
GM_xmlhttpRequest({
method: "GET",
url: (loanBaseUrl + "/alliances.js"),
onload: function(response) {
allianceData = JSON.parse(response.responseText);
userAlliance = {};
for (let allianceKey in allianceData) {
let alliance = allianceData[allianceKey];
for (let userIndex in alliance.members) {
let userName = alliance.members[userIndex];
userAlliance[userName] = allianceKey;
}
}
console.log("Alliance data loaded from LOAN.");
if (callback) callback();
}
});
}
// Stuff references to the alliance data in the world map object. Not clear whether this is actually doing useful things.
function exposeAllianceDataForAngular() {
let app = angular.element(document.body);
let $timeout = angular.element('body').injector().get('$timeout');
$timeout(()=>{
let worldMapElem = angular.element($('.world-map'));
let worldMap = worldMapElem.scope().WorldMap;
worldMap.allianceData = allianceData;
worldMap.userAlliance = userAlliance;
recalculateAllianceOverlay();
});
for (let allianceKey in allianceData) {
addStyle(".alliance-" + allianceKey + " { background-color: " + getAllianceColor(allianceKey) + " }");
addStyle(".alliance-logo-3.alliance-" + allianceKey + " { background-image: url('" + getAllianceLogo(allianceKey) + "') }");
}
}
// inject a new CSS style
function addStyle(css) {
let head = document.head;
if (!head) return;
let style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
head.appendChild(style);
}
function generateCompiledElement(parent, content) {
let $scope = parent.scope();
let $compile = parent.injector().get("$compile");
return $compile(content)($scope);
}
// Bind the WorldMap alliance display option to the localStorage value
function bindAllianceSetting() {
let alliancesEnabled = localStorage.getItem("alliancesEnabled") !== "false";
let worldMapElem = angular.element($('.world-map'));
let worldMap = worldMapElem.scope().WorldMap;
worldMap.displayOptions.alliances = alliancesEnabled;
worldMap.toggleAlliances = function () {
worldMap.displayOptions.alliances = !worldMap.displayOptions.alliances;
localStorage.setItem("alliancesEnabled", worldMap.displayOptions.alliances);
if (worldMap.displayOptions.alliances && !worldMap.userAlliances) {
ensureAllianceData(exposeAllianceDataForAngular);
} else {
$('.alliance-logo').remove();
}
};
worldMap.getAllianceName = function (userId) {
if (!worldMap.userAlliance) return "Loading...";
let userName = this.roomUsers[userId].username;
let allianceKey = worldMap.userAlliance[userName];
if (!allianceKey) return "None";
return this.allianceData[allianceKey].name;
};
if (alliancesEnabled) {
ensureAllianceData(exposeAllianceDataForAngular);
recalculateAllianceOverlay();
}
}
// insert the alliance toggle into the map container layer
function addAllianceToggle() {
let content = "\