// ==UserScript==
// @name Agma.io stats 📈
// @namespace http://tampermonkey.net/
// @version 0.1.4
// @description See stats about your game in the death screen!
// @author firebone & fishy
// @match https://agma.io/*
// @grant none
// @downloadURL none
// ==/UserScript==
//Type "/s help" in the agma chat to see all chat commands!
(function() {
const ad = document.getElementById("zoomItem");
const statsDiv = document.createElement("div");
statsDiv.innerHTML =`
Gameplay Stats
Highest Mass |
Top Leaderboard |
/ |
/ |
XP Gained |
Coins Gained |
0 | 0% |
0 |
Time Alive |
Powerups Used |
0h 0min 0s |
0 |
`
ad.parentNode.replaceChild(statsDiv, ad);
document.getElementById("advertDialog1").style.opacity = "0";
document.getElementById("advertDialog2").style.height = "250px";
document.getElementById("advertContinue").parentElement.style.marginTop = "4px";
var plrAlive = false,
level = 0,
deathTime = 0,
spawnedFirstTime = false,
highestMassEl = document.getElementById('highestMass'),
topLeaderEl = document.getElementById('topLeader'),
xpGainEl = document.getElementById('xpGain'),
coinGainEl = document.getElementById('coinGain'),
timeAliveEl = document.getElementById('timeAlive'),
powsUsedEl = document.getElementById('powsUsed');
function stats(){
trackPowerCount();
deathTime = Date.now();
plrAlive = false;
document.getElementById("agmaAdHref").removeAttribute("href");
document.getElementById("agmaAdHref").parentElement.removeAttribute("onclick");
let gainedXp = Math.floor(calcXp() - xp) > 0 ? Math.floor(calcXp() - xp) : 0;
let gainedPercent = Math.round(gainedXp / (level / 10)) / 100;
//golden colors if value is great
highestMassEl.style.color = topMass > 1e5 ? '#FFD700': '#BFBFC0'; //100k+ mass
topLeaderEl.style.color = topLeaderboard == 1 ? '#FFD700': '#BFBFC0'; //lb pos 1
xpGainEl.style.color = gainedXp > 25000 ? '#FFD700': '#BFBFC0'; //25k xp
coinGainEl.style.color = getCoins() - coins > 25000 ? '#FFD700': '#BFBFC0'; //25k coins
timeAliveEl.style.color = deathTime - timeJoin > 12e5 ? '#FFD700': '#BFBFC0'; //20 minutes
powsUsedEl.style.color = usedPowers > 30 ? '#FFD700': '#BFBFC0'; //30 powers
gainedXp = gainedXp > 1e5 ? Math.round(gainedXp/100)/10 + 'k ' : gainedXp;
//insert values
highestMassEl.innerText = topMass;
topLeaderEl.innerText = topLeaderboard == 1e5 ? "/" : topLeaderboard;
xpGainEl.innerText = gainedXp + 'XP | ' + (gainedPercent ? gainedPercent : 0) + '%';
coinGainEl.innerText = getCoins() - coins > 0 ? getCoins() - coins : "0";
timeAliveEl.innerText = getTimeAlive();
powsUsedEl.innerText = usedPowers;
}
//Track if player died
var adv = document.getElementById('advert');
setInterval(check => {
if(adv.style.display != 'none' && plrAlive){
stats();
}
}, 300)
//Track if player spawns
let playFunction = window.setNick;
window.setNick = function(nick, respawn){
playFunction(nick, respawn);
if(!plrAlive){
plrAlive = true; deathTime = false; spawnedFirstTime = true;
resetStats();
} else {
respawn && setTimeout(resetStats, 1000); // otherwise stats wont show when dying by respawn
};
}
//Track if plr switches server (if so reset stats)
let setserverFunction = window.setserver;
window.setserver = function(sv, sn){
setserverFunction(sv, sn);
resetStats();
}
//Track stats
var topMass, topLeaderboard, timeJoin, xp, coins, usedPowers, powerCount;
function trackPowerCount(){
let oldPowerCount = powerCount;
powerCount = 0;
let inv = [...document.getElementById('inventory1').children, ...document.getElementById('inventory2').children]; //get all items in one array
for(let i = 0; i < inv.length; i++){
let amnt = parseInt(inv[i].innerText);
if(isNaN(amnt)){
amnt = inv[i].style.display == 'none' ? 0 : 1;
}
powerCount += amnt;
}
if(oldPowerCount - powerCount > 0){
usedPowers += oldPowerCount - powerCount;
}
//checking if user logged out/in...
let currlvl = parseInt(document.getElementById('level').innerText);
if(level > currlvl || level + 1 < currlvl){
xp = calcXp();
coins = getCoins();
}
}
function calcXp(){
level = parseInt(document.getElementById('level').innerText);
var progress = parseFloat(document.getElementsByClassName('progress-bar')[0].style.width);
return (level ** 2 * 0.5 - 0.5 * level) * 1000 + (level * 1000 * (progress / 100));
}
function getCoins(){
return parseInt(document.getElementById("coinsDash").innerText.replaceAll(' ', ''));
}
function getTimeAlive(){
let msAlive = (plrAlive ? Date.now() : deathTime) - timeJoin;
let [hh,mm,ss] = new Date(msAlive).toISOString().substr(11, 8).split(':');
return parseInt(hh) + 'h ' + parseInt(mm) + 'min ' + parseInt(ss) + 's';
}
function resetStats(){
topMass = 0;
topLeaderboard = 1e5;
timeJoin = Date.now();
xp = calcXp();
usedPowers = 0;
coins = getCoins();
}
const _fillText = CanvasRenderingContext2D.prototype.fillText;
CanvasRenderingContext2D.prototype.fillText = function(text) {
const match = text.toString().match(/^Mass: (\d+)$/);
if(match){
var mass = parseInt(match[1]);
if(mass > topMass) topMass = mass;
}
if(this.fillStyle == '#ffaaaa'){
if(parseInt(text) < topLeaderboard) topLeaderboard = parseInt(text);
}
_fillText.apply(this, arguments);
}
setInterval(trackPowerCount, 1e3)
//Chat commands
function replaceChat(msg){
if(spawnedFirstTime){
$('#chtbox').val(msg)
} else {
$('#chtbox').val('')
$('#curser').text('You need to spawn one time to be able to use the chat commands!')
$('#curser').css('color', '#FF0000')
$('#curser').fadeIn(400)
setTimeout(() => $('#curser').fadeOut(400), 2000)
}
}
$('#chtbox').keydown(function (event) {
if (event.keyCode === 13) {
let [prefix, cmd] = $('#chtbox').val().split(' ')
if(prefix == '/s' || prefix == '/stats'){
switch(cmd){
case 'mass':
replaceChat('Highest mass in this game: ' + topMass)
break;
case 'leaderboard': case 'lb':
replaceChat('Highest leaderboard position in this game: ' + topLeaderboard)
break;
case 'xp':
var xpGain = Math.floor(calcXp() - xp);
var gainedPercent = Math.round(xpGain / (level / 10)) / 100;
replaceChat('XP gained in this game: ' + (xpGain > 0 ? xpGain : 0) + 'xp | ' + (gainedPercent ? gainedPercent : 0) + '%')
break;
case 'coins':
replaceChat('Coins gained in this game: ' + (getCoins() - coins > 0 ? getCoins() - coins : "0"))
break;
case 'alive': case 'time':
replaceChat('Time alive: ' + getTimeAlive())
break;
case 'powerups': case 'pws':
replaceChat('Used Powerups in this game: ' + usedPowers + (usedPowers > 419 ? ' 😳' : ''))
break;
case 'help':
swal({
title: `Stats Commands
`,
text: `
All of the stats reset when dying/respawing!
/s mass - Your highest mass
/s lb - Your highest leaderboard position
/s xp - XP you have gained
/s coins - Coins you have gained
/s alive - Time you have been alive for
/s pws - Powerups used
`,
type: '',
customClass: 'swal-title-gold',
html: true
})
$('#chtbox').val('')
break;
}
}
}
});
})();