Warcraft Logs Enhancement

Some Enhancement Scripts of Warcraft Logs

目前為 2018-05-18 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Warcraft Logs Enhancement
// @namespace    https://github.com/icyblade/warcraftlogs_enhancement
// @version      1.0.4
// @description  Some Enhancement Scripts of Warcraft Logs
// @author       swqsldz, kingofpowers, icyblade
// @match        http://*.warcraftlogs.com/*
// @match        https://*.warcraftlogs.com/*
// @run-at       document-idle
// ==/UserScript==
const PRESTIGE_SPELL_ID = 239042; // Concordance of the Legionfall
const columnNames = ['Artifact', 'Prestige', 'MainStat', 'Crit', 'Haste', 'Mastery', 'Versatility'];
const attributes = ['Prestige', 'MainStat', 'Crit', 'Haste', 'Mastery', 'Versatility'];
const regex = /\/reports\/([\S\s]+?)#fight=([0-9]+)/;
var loadListFight = new Array();
var loadListSummary = new Array();
var loadVersion = 0;

const attrToPercent = {
    0: {
        'Crit': 5,
        'perCrit': 400,
        'Haste': 0,
        'perHaste': 375,
        'Mastery': 8,
        'perMastery': 712,
        'Versatility': 0,
        'perVersatility': 475
    },
    63: {
        'Crit': 20,
        'Mastery': 6,
        'perMastery': 533.1
    }
};

const HOST = 'https://' + window.location.hostname;


var PlayerList = new Array();

function initialize() {
    'use strict';

    // initialize attribute columns
    for (let i = 0; i < columnNames.length; i++) {
        $('<th class="sorting ui-state-default">' + columnNames[i] + '</th>').insertBefore('th.zmdi.zmdi-flag.sorting.ui-state-default');
    }
    for (let i = 0; i < columnNames.length; i++) {
        $('<td class="' + columnNames[i] + '"></td>').insertBefore('td.zmdi.zmdi-flag');
    }

    // extract fights from ranking page
    $('td.unique-gear').parent().each(function() {
        var player = new Object();

        player.rowID = $(this).attr('id');
        player.name = $(this).find('.players-table-name .main-table-player').text();

        var href = $(this).find('.players-table-name .main-table-player').attr('href');
        if (typeof(href) == 'undefined') {
            return;
        }
        player.logID = href.match(regex)[1];
        player.fightID = href.match(regex)[2];

        PlayerList.push(player);
    });
}

function loadPlayerSummary(index, currentVersion) {
    if (currentVersion != loadVersion) {
        return;
    }
    loadListSummary[index] = $.ajax({
        type: 'GET',
        url: HOST + '/reports/summary/' + PlayerList[index].logID + '/' + PlayerList[index].fightID + '/' + PlayerList[index].timestamp + '/' + (PlayerList[index].timestamp + 3000) + '/' + PlayerList[index].sourceID + '/0/Any/0/-1.0.-1/0',
        dataType: 'text',
        success: function(data) {
            callback_playerSummary(data, index);
        }
    });
}

function callback_playerSummary(data, index) {
    var summary = new Array();

    // handle main and enhancement attributes
    var regex_attr = /<span class="composition-entry">([a-zA-Z]+): <span class=estimate><span class=estimate>([0-9\,]+)<\/span>/g;

    while ((stat = regex_attr.exec(data)) != null) {
        switch (stat[1]) {
            case 'Strength':
            case 'Agility':
            case 'Intellect':
                summary['MainStat'] = stat[2];
                break;
            case 'Crit':
                summary['Crit'] = stat[2];
                break;
            case 'Haste':
                summary['Haste'] = stat[2];
                break;
            case 'Mastery':
                summary['Mastery'] = stat[2];
                break;
            case 'Versatility':
                summary['Versatility'] = stat[2];
                break;
            case 'Stamina':
                break;
            default:
                break;
        }
    }

    // handle artifact
    var regex_trait = /<a target="_new" href="\/\/www.wowhead.com\/spell=([0-9]+)\?rank=[0-9]+"[^<]+<img src="([^"]+)"[^<]+<span[^<]+<\/span[^<]+<\/a[^<]+<td class="primary rank">([0-9]+)<\/td[^<]+<\/tr>/g;

    var relicnum = 0;
    while ((trait = regex_trait.exec(data)) != null) {
        if (trait[3] > 4 && trait[1] != PRESTIGE_SPELL_ID) {
            var t = trait[3];
            var spid = {
                'spellid': trait[1],
                'img': trait[2]
            };
            if (typeof(summary['relic']) == 'undefined') {
                summary['relic'] = new Array();
            }
            while (t > 4) {
                t--;
                relicnum++;
                summary['relic'].push(spid);
            }
        }
        if (trait[1] == PRESTIGE_SPELL_ID) {
            summary['Prestige'] = trait[3];
        }
    }

    // handle items
    var regex_item = /<td class="primary rank">([0-9]+)<\/td[^<]+<td nowrap class="num">(Trinket|Weapon)<td [^<]+<a target="_new" href="\/\/legion.wowhead.com\/item=([0-9]+)" rel="(?:(?:[^"]+|)bonus=([0-9:]+);|)"/g;

    var trinketnum = 0;
    while ((item = regex_item.exec(data)) != null) {
        if (item[2] == 'Trinket') {
            trinketnum++;
            summary['Trinket' + trinketnum] = {
                'id': item[3],
                'level': item[1],
                'bonus': item[4] || ""
            };
        } else {
            summary['WeaponLevel'] = item[1];
        }
    }
    PlayerList[index].summary = summary;
    updateRowSummary(index);
}

function updateRowSummary(index) {

    try {
        relics = '';
        if (typeof(PlayerList[index].summary['relic']) != 'undefined') {
            $.each(PlayerList[index].summary['relic'], function(i, relic) {
                relics += '<a target="_new" href="//www.wowhead.com/spell=' + relic['spellid'] + '"><img src="' + relic['img'] + '" class="tiny-icon"></a>';
            });
        }
        $('#' + PlayerList[index].rowID + ' .Artifact').html('<div style="align-items: center; display: flex;">' + PlayerList[index].summary['WeaponLevel'] + ':' + relics + '</div>');
        for (let i = 0; i < attributes.length; i++) {
            $('#' + PlayerList[index].rowID + ' .' + attributes[i]).html(PlayerList[index].summary[attributes[i]]);
        }

        //Trinket
        var regex_tditem = /wowhead.com\/item=([0-9]+)/;
        for (let trinketid = 1; trinketid <= 2; trinketid++) {
            if (typeof(PlayerList[index].summary['Trinket' + trinketid]) != 'undefined') {
                $('#' + PlayerList[index].rowID + '>.unique-gear>div>a[href$="item=' + PlayerList[index].summary['Trinket' + trinketid]['id'] + '"]').each(
                    function(i, a) {
                        $(a).html($(a).html() + '<span style="position: absolute; bottom: -6px; text-shadow: -2px 0 black, 0 2px black, 2px 0 black, 0 -2px black;">' + PlayerList[index].summary['Trinket' + trinketid]['level'] + '</span>');
                        $(a).attr('href', a.href + '&bonus=' + PlayerList[index].summary['Trinket' + trinketid]['bonus']);
                        $(a).css('position', 'relative');
                    }
                );
            }
        }
        $('td.Artifact').css('width', '1%');
    } catch (e) {
        console.error(e);
        console.error(PlayerList[index]);
    }
}

function loadFights(index, currentVersion) {
    if (currentVersion != loadVersion) {
        return;
    }
    loadListFight[index] = $.ajax({
        type: 'GET',
        url: HOST + '/reports/fights_and_participants/' + PlayerList[index].logID + '/0',
        dataType: 'json',
        success: function(data) {
            callback_fights(data, index, currentVersion);
        }
    });
}

function callback_stats(data, rowID, logID, fightID, timestamp, sourceID) {
    for (var key in columnNames) {
        try {
            $('#' + rowID + ' .attr-' + columnNames[key]).html(data.events[0][columnNames[key]]);
        } catch (e) {
            console.error(e);
            console.error(rowID);
            console.error(data);
        }
    }
}

function callback_fights(data, idx, currentVersion) {
    'use strict';
    PlayerList[idx].fight = data;

    for (let j in PlayerList[idx].fight.friendlies) {
        if (PlayerList[idx].fight.friendlies[j].name == PlayerList[idx].name) {
            PlayerList[idx].sourceID = PlayerList[idx].fight.friendlies[j].id;
            break;
        }
    }

    for (let j in PlayerList[idx].fight.fights) {
        if (PlayerList[idx].fight.fights[j].id == PlayerList[idx].fightID) {
            PlayerList[idx].timestamp = PlayerList[idx].fight.fights[j].start_time;
            break;
        }
    }

    loadPlayerSummary(idx, currentVersion);
    idx++;

    if (idx >= PlayerList.length) {
        return;
    }

    loadFights(idx, currentVersion);
}

function clearLoad() {
    $.each(loadListFight, function(i, _ajax) {
        _ajax.abort();
    });
    $.each(loadListSummary, function(i, _ajax) {
        _ajax.abort();
    });
}

function loadAttributes(currentVersion) {
    initialize();
    loadFights(0, currentVersion);
}

function delayLoadAttributes() {
    if ($('.ranking-table tr:eq(1)').length !== 0 && $('.ranking-table tr:eq(1) >.MainStat').length === 0) {
        loadVersion++;
        clearLoad();
        PlayerList = new Array();
        loadAttributes(loadVersion);
        setTimeout(delayLoadAttributes, 10000);
    } else {
        setTimeout(delayLoadAttributes, 1000);
    }
}

$('.responsive #content').attr('style', 'max-width: 10000px !important;');
delayLoadAttributes();