Add automatic error reporting
parent
998ecc3bd4
commit
ade2d45708
|
@ -2,6 +2,15 @@ import assets from '../assets.js';
|
||||||
import ModUtils from '../modUtils.js';
|
import ModUtils from '../modUtils.js';
|
||||||
|
|
||||||
export default (/** @type {ModUtils} */ modUtils) => {
|
export default (/** @type {ModUtils} */ modUtils) => {
|
||||||
|
|
||||||
|
// Disable built-in Territorial.io error reporting
|
||||||
|
modUtils.insertCode(
|
||||||
|
`window.removeEventListener("error", err);
|
||||||
|
msg = e.lineno + " " + e.colno + "|" + getStack(e); /* here */`,
|
||||||
|
`__fx.utils.reportError(e, msg);
|
||||||
|
return alert("Error:\\n" + e.filename + " " + e.lineno + " " + e.colno + " " + e.message);`
|
||||||
|
)
|
||||||
|
|
||||||
modUtils.waitForMinification(() => applyPatches(modUtils))
|
modUtils.waitForMinification(() => applyPatches(modUtils))
|
||||||
}
|
}
|
||||||
//export const requiredVariables = ["game", "playerId", "playerData", "rawPlayerNames", "gIsSingleplayer", "playerTerritories"];
|
//export const requiredVariables = ["game", "playerId", "playerData", "rawPlayerNames", "gIsSingleplayer", "playerTerritories"];
|
||||||
|
@ -301,10 +310,6 @@ canvas.font=aY.g0.g1(1,fontSize),canvas.fillStyle="rgba("+gR+","+tD+","+hj+",0.6
|
||||||
replaceRawCode(`,this.hostnameIsValid=0<=window.location.hostname.toLowerCase().indexOf("territorial.io"),`,
|
replaceRawCode(`,this.hostnameIsValid=0<=window.location.hostname.toLowerCase().indexOf("territorial.io"),`,
|
||||||
`,this.hostnameIsValid=true,`)
|
`,this.hostnameIsValid=true,`)
|
||||||
|
|
||||||
// Disable built-in Territorial.io error reporting
|
|
||||||
replaceOne(/window\.addEventListener\("error",function (\w+)\((\w+)\){/g,
|
|
||||||
'$& window.removeEventListener("error", $1); return alert("Error:\\n" + $2.filename + " " + $2.lineno + " " + $2.colno + " " + $2.message);');
|
|
||||||
|
|
||||||
console.log('Removing ads...');
|
console.log('Removing ads...');
|
||||||
// Remove ads
|
// Remove ads
|
||||||
replace('//api.adinplay.com/libs/aiptag/pub/TRT/territorial.io/tag.min.js', '');
|
replace('//api.adinplay.com/libs/aiptag/pub/TRT/territorial.io/tag.min.js', '');
|
||||||
|
|
|
@ -1,23 +1,43 @@
|
||||||
import { getSettings } from "./settings.js";
|
import { getSettings } from "./settings.js";
|
||||||
import { getVar } from "./gameInterface.js";
|
import { getVar } from "./gameInterface.js";
|
||||||
|
|
||||||
const utils = new (function() {
|
// Example usage from game script: __fx.utils.getMaxTroops(...)
|
||||||
this.getMaxTroops = function(playerTerritories, playerID) { return (playerTerritories[playerID]*150).toString(); };
|
|
||||||
this.getDensity = function(playerID, playerBalances = getVar("playerBalances"), playerTerritories = getVar("playerTerritories")) {
|
function getMaxTroops(playerTerritories, playerID) {
|
||||||
if (getSettings().densityDisplayStyle === "percentage") return (((playerBalances[playerID] / ((playerTerritories[playerID] === 0 ? 1 : playerTerritories[playerID]) * 150)) * 100).toFixed(1) + "%");
|
return (playerTerritories[playerID] * 150).toString();
|
||||||
else return (playerBalances[playerID] / (playerTerritories[playerID] === 0 ? 1 : playerTerritories[playerID])).toFixed(1);
|
};
|
||||||
};
|
function getDensity(playerID, playerBalances = getVar("playerBalances"), playerTerritories = getVar("playerTerritories")) {
|
||||||
this.isPointInRectangle = function(x, y, rectangleStartX, rectangleStartY, width, height) {
|
if (getSettings().densityDisplayStyle === "percentage") return (((playerBalances[playerID] / ((playerTerritories[playerID] === 0 ? 1 : playerTerritories[playerID]) * 150)) * 100).toFixed(1) + "%");
|
||||||
return x >= rectangleStartX && x <= rectangleStartX + width && y >= rectangleStartY && y <= rectangleStartY + height;
|
else return (playerBalances[playerID] / (playerTerritories[playerID] === 0 ? 1 : playerTerritories[playerID])).toFixed(1);
|
||||||
};
|
};
|
||||||
/** @param {CanvasRenderingContext2D} canvas @param {string} text */
|
function isPointInRectangle(x, y, rectangleStartX, rectangleStartY, width, height) {
|
||||||
this.fillTextMultiline = function(canvas, text, x, y, maxWidth) {
|
return x >= rectangleStartX && x <= rectangleStartX + width && y >= rectangleStartY && y <= rectangleStartY + height;
|
||||||
const lineHeight = parseInt(canvas.font.split(" ").find(part => part.endsWith("px")).slice(0, -2));
|
};
|
||||||
text.split("\n").forEach((line, index) => canvas.fillText(line, x, y + index * lineHeight, maxWidth));
|
/** @param {CanvasRenderingContext2D} canvas @param {string} text */
|
||||||
}
|
function fillTextMultiline(canvas, text, x, y, maxWidth) {
|
||||||
this.textStyleBasedOnDensity = function(playerID) {
|
const lineHeight = parseInt(canvas.font.split(" ").find(part => part.endsWith("px")).slice(0, -2));
|
||||||
const playerBalances = getVar("playerBalances"), playerTerritories = getVar("playerTerritories");
|
text.split("\n").forEach((line, index) => canvas.fillText(line, x, y + index * lineHeight, maxWidth));
|
||||||
return `hsl(${playerBalances[playerID] / (playerTerritories[playerID] * 1.5)}, 100%, 50%, 1)`;
|
}
|
||||||
}
|
function textStyleBasedOnDensity(playerID) {
|
||||||
});
|
const playerBalances = getVar("playerBalances"), playerTerritories = getVar("playerTerritories");
|
||||||
export default utils
|
return `hsl(${playerBalances[playerID] / (playerTerritories[playerID] * 1.5)}, 100%, 50%, 1)`;
|
||||||
|
}
|
||||||
|
function reportError(e, message) {
|
||||||
|
message = e.filename + " " + e.lineno + " " + e.colno + " " + e.message + "\n" + message;
|
||||||
|
fetch("https://fx.peshomir.workers.dev/stats/errors", {
|
||||||
|
body: JSON.stringify({
|
||||||
|
message,
|
||||||
|
context: {
|
||||||
|
swState: navigator.serviceWorker?.controller?.state,
|
||||||
|
location: window.location.toString(),
|
||||||
|
userAgent: navigator.userAgent,
|
||||||
|
dictionary,
|
||||||
|
buildTimestamp,
|
||||||
|
scripts: Array.from(document.scripts).map(s => s.src)
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
method: "POST"
|
||||||
|
}).catch(e => alert("Failed to report error: " + e));
|
||||||
|
}
|
||||||
|
|
||||||
|
export default { getMaxTroops, getDensity, isPointInRectangle, fillTextMultiline, textStyleBasedOnDensity, reportError }
|
Loading…
Reference in New Issue