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.20
// @author       Anders Morgan Larsen (Xortrox)
// @contributor  Tester: euphone
// @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 = averageDamage.replace(/,/g, '');
        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 = playerAverageDamage.replace(/,/g, '');
        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 likelinessPrediction = '';

        // TODO: Make this based on some amount of % within proximity of max healths and damages.
        if (playerHitsToKillEnemy < hitsToKill) {
            likelinessPrediction +=
                'It seems <b style="color:lightgreen">unlikely</b> that you will die as the enemy is required to hit you ' + hitsToKill + ' times, but you need to hit it roughly ' + playerHitsToKillEnemy + ' times.<br>';
        } else if (playerHitsToKillEnemy >= hitsToKill){
            likelinessPrediction +=
                'It seems <b style="color:red">likely</b> that you will die as the enemy needs to hit you ' + hitsToKill + ' times, but you need to hit it roughly ' + playerHitsToKillEnemy + ' times.<br>';
        }

        let ctdInfoDiv = createInfoDivIfNotExists();

        if (ctdInfoDiv){
            ctdInfoDiv.innerHTML =
                '<div style="border-bottom: 1px solid white; margin-bottom: 3px;">Chance to die</div>' +
                likelinessPrediction +
                'You\'ve got a ' + (chanceFactorDeath * 100.0).toPrecision(3) + '%~ chance to die, granted the enemy lands ' + hitsToKill + ' successful hits.<br>' +
                'Taken that the enemy\'s average damage is ' + averageDamage + '<br>' +
                'Given your hit chance of ' + playerChance + ', it would take roughly ' + playerHitsToKillEnemy + ' hits to kill the enemy with your average damage of ' + playerAverageDamage + '<br>' +
                '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();
    });
}());