Pendoria - Chance to die

Implements some extra information per finished fight in the battle view on pendoria.

目前为 2018-08-12 提交的版本。查看 最新版本

// ==UserScript==
// @name         Pendoria - Chance to die
// @description  Implements some extra information per finished fight in the battle view on pendoria.
// @namespace    http://pendoria.net/
// @version      0.0.3
// @author       Anders Morgan Larsen (Xortrox)
// @match        http://pendoria.net/game
// @match        https://pendoria.net/game
// @match        http://www.pendoria.net/game
// @match        https://www.pendoria.net/game
// @grant        none
// ==/UserScript==


(function () {
    function showChanceToDie() {

        let enemyChanceToHit = $('.mchancetohit')[0];

        if (!enemyChanceToHit) {
            return console.log('enemyChanceToHit element was not found.');
        }

        let playerHealth = $('#php-value')[0];

        if (!playerHealth) {
            return console.log('playerHealth element was not found.');
        }

        let enemyAverageDamage = $('.mavgdamage')[0];

        if (!enemyAverageDamage) {
            return console.log('enemyAverageDamage element was not found.');
        }

        let fightDiv = $('#fight')[0];

        if (!fightDiv) {
            return console.log('fightDiv element was not found.');
        }

        function createInfoDivIfNotExists() {
            let ctdInfoDiv = $('#ctdInfoDiv')[0];

            if (!ctdInfoDiv) {
                let infoDiv = document.createElement('div');
                infoDiv.id = 'ctdInfoDiv';
                fightDiv.parentElement.appendChild(infoDiv);
            }

            return ctdInfoDiv;
        }

        let playerChanceToHit = $('.pchancetohit')[0];

        if (!playerChanceToHit) {
            return console.log('playerChanceToHit element was not found.');
        }

        let enemyHealth = $('#mhp-value')[0];

        if (!enemyHealth) {
            return console.log('enemyHealth element was not found.');
        }

        let enemyChance = enemyChanceToHit.innerText;
        enemyChance = enemyChance.substr(0, enemyChance.length - 1);
        enemyChance = parseFloat(enemyChance);
        let chanceFactor = enemyChance/100.0;

        let playerMaxHealth = playerHealth.innerText;
        playerMaxHealth = playerMaxHealth.substr(playerMaxHealth.indexOf('/') + 1);
        playerMaxHealth = parseInt(playerMaxHealth);

        let averageDamage = enemyAverageDamage.innerText;
        averageDamage = parseInt(averageDamage);

        let hitsToKill = Math.ceil(playerMaxHealth / averageDamage);

        let chanceFactorDeath = chanceFactor ** hitsToKill;

        //console.log('hitsToKill:', hitsToKill);
        //console.log('averageDamage:', averageDamage);
        //console.log('playerMaxHealth:', playerMaxHealth);
        //console.log('chanceFactor:', chanceFactor);
        //console.log('chanceFactorDeath:', chanceFactorDeath);

        //console.log('Chance to die:', (chanceFactorDeath * 100.0) + '%');
        //console.log('Chance to die (fraction):', getFractionString(chanceFactorDeath));

        let playerChance = playerChanceToHit.innerText;
        playerChance = playerChance.substr(0, playerChance.length - 1);
        playerChance = parseFloat(playerChance);
        let playerHitChanceFactor = playerChance/100.0;

        let playerAverageDamage = $('.mavgdamage')[0];

        if (!playerAverageDamage) {
            return console.log('playerAverageDamage element was not found.');
        }

        playerAverageDamage = playerAverageDamage.innerText;
        playerAverageDamage = parseInt(playerAverageDamage);

        let enemyMaxHealth = enemyHealth.innerText;
        enemyMaxHealth = enemyMaxHealth.substr(enemyMaxHealth.indexOf('/') + 1);
        enemyMaxHealth = parseInt(enemyMaxHealth);

        let enemyMaxHealthWithChanceFactor = (enemyMaxHealth/playerHitChanceFactor);
        let playerHitsToKillEnemy = Math.ceil(enemyMaxHealthWithChanceFactor / playerAverageDamage);

        let ctdInfoDiv = createInfoDivIfNotExists();

        ctdInfoDiv.innerHTML =
            '[CTD]: Chance to die: ' + (chanceFactorDeath * 100.0).toPrecision(3) + '%~<br>' +
            '[CTD]: It would take ' + hitsToKill + ' successful hits to kill you with an average damage of ' + averageDamage + '<br>' +
            '[CTD]: Given your hit chance of ' + playerChance + ', it would take roughly ' + playerHitsToKillEnemy + ' hits to kill the enemy with an average damage of ' + playerAverageDamage + '<br>' +
            '[CTD]: This was calculated by increasing the enemy\'s hp by ' + ((1 - playerHitChanceFactor) * 100).toFixed(2) + '% from ' + enemyMaxHealth + ' to ' + enemyMaxHealthWithChanceFactor.toFixed() + ' in order to make up for your hit chance.';
    }

    $(function () {
        function load() {
            /**
             * Can be 0, so we explicitly check for undefined.
             */
            if (window.ctdInterval) {
                clearInterval(window.ctdInterval);
                window.ctdInterval = setInterval(showChanceToDie, 1000);
            } else {
                window.ctdInterval = setInterval(showChanceToDie, 1000);
            }
        }

        function checkIfLoaded(){
            if($('#fight') && $('#fight')[0]) {
                load();
            } else {
                setTimeout(() => {
                    checkIfLoaded();
                }, 500);
            }
        }

        checkIfLoaded();
    });
}());