Greasy Fork

Greasy Fork is available in English.

OnlineligaFriendlyInfo

Zusatzinfos für Friendlies für www.onlineliga.de (OFA)

当前为 2021-04-06 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/424583/918962/OnlineligaFriendlyInfo.js

/*jshint esversion: 6, multistr: true */

// ==UserScript==
// @name           OnlineligaFriendlyInfo
// @namespace      http://greasyfork.icu/de/users/577453
// @version        0.1.0
// @license        LGPLv3
// @description    Zusatzinfos für Friendlies für www.onlineliga.de (OFA)
// @author         KnutEdelbert
// @match          https://www.onlineliga.de
// ==/UserScript==


'use strict';

var $ = window.$ || unsafeWindow.jQuery;

const OnlineligaFriendlyInfo = {};

OnlineligaFriendlyInfo.showTeamInfo = async function(){
    let userId;
    const parentDiv = this.parentNode;
    const offerRow = parentDiv.parentNode.parentNode;
    const spanClick = $(parentDiv).find('span.ol-team-name').attr("onclick");
    const userIdMatch = spanClick.match(/{\s*'?userId'?\s*:\s*(\d+)\s*}/);
    let lineup11Value = 0;
    let lineupAllValue = 0;

    function euroValue(val){
        return `${new Intl.NumberFormat('de-DE').format(val)} €`;
    }

    function getData(url){ return $.get(url); }

    if (userIdMatch){
        userId = userIdMatch[1];
        const teamData = await getData(`/team/overview/squad?userId=${userId}`);
        const teamValue = $(teamData).find('span.bandarole-team-value > span.pull-right').text();
        const teamRows = $(teamData).filter('div.team-overview-squad-row');
        const teamSize = teamRows.length;
        const playerValues = [];
        teamRows.each(function(){
            const row = $(this);
            const playerValue = parseInt(row.find('div.col-md-3.col-lg-3.col-sm-6.col-xs-6.text-right').text().trim().replace(/\./g,''),10) || 0;
            playerValues.push(playerValue);
            const lineupState = row.find('span.ol-player-squad-display.pull-right');
            if (lineupState.length > 0){
                if (!$($(lineupState)[0]).hasClass('player-substitute-display')){
                    lineup11Value += playerValue;
                }
                lineupAllValue += playerValue;
            }
        });
        const friendly11Value = euroValue(lineup11Value);
        const friendlyAllValue = euroValue(lineupAllValue);
        const teamValueInt = parseInt(teamValue.replace(/\./g,''),10);
        const top11Value = playerValues.sort((a,b) => b - a).slice(0,11).reduce((a, b) => a + b, 0);
        const friendlyPercent = Math.round((lineup11Value/teamValueInt) * 100);

        const average11Value = Math.round(lineup11Value/11);
        const averageTeam = Math.round(teamValueInt/teamSize);

        //console.log(lineup11Value, average11Value, teamValueInt, averageTeam, teamSize);

        $(`<div style="font-size:12px;margin-left:20px;"> \
            <span style="white-space:nowrap;" title="Durchschnitt MW Aufstellung">${euroValue(average11Value)} /</span> \
            <span style="white-space:nowrap;" title="% v. Top 11 (MW)">${Math.round(lineup11Value/top11Value*100)}%</span> \
            <br/><span style="white-space:nowrap;" title="Durchschnitt MW Team">${euroValue(averageTeam)} /</span> \
            <span style="white-space:nowrap;" title="% v. MW Team">${Math.round(lineup11Value/teamValueInt*100)}%</span> \
            </div>`).appendTo($(offerRow).children().eq(1));

    }
};

OnlineligaFriendlyInfo.init = function(){
    $("div#olFriendlyRequestsContent").on('click','span.user-badge',OnlineligaFriendlyInfo.showTeamInfo);
};

/* only for testing
waitForKeyElements (
    "div#olFriendlyRequestsContent",
    OnlineligaFriendlyInfo.init
);
*/