OnlineligaLineupInfo

Zusatzinfos zur Aufstellung bei www.onlineliga.de (OFA)

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/424584/919064/OnlineligaLineupInfo.js

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/* jshint esversion: 6, multistr: true */
/* globals waitForKeyElements, OLCore */

// ==UserScript==
// @name           OnlineligaLineupInfo
// @namespace      https://greasyfork.org/de/users/577453
// @version        0.1.0
// @license        LGPLv3
// @description    Zusatzinfos zur Aufstellung bei www.onlineliga.de (OFA)
// @author         KnutEdelbert
// @match          https://www.onlineliga.de
// @require        https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
// @require        https://greasyfork.org/scripts/424598-olcore/code/OLCore.js?version=919044
// ==/UserScript==

/*********************************************
 * 0.1.0 10.04.2021 Release
 *********************************************/
(function() {
    'use strict';

    var $ = unsafeWindow.jQuery;

    async function showInfo(){

        if ($("div.ol-team-settings-pitch-wrapper").length === 0){
            return;
        }

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

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

        function parseTeamData(teamData){
            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 playerData = {};
            const playerArray = [];
            teamRows.each(function(){
                const row = $(this);
                const playerSpan = row.find("span.ol-player-name");
                if (playerSpan.length > 0){
                    const playerId = parseInt(playerSpan.attr("onclick").match(/\s*'?playerId'?\s*:\s*(\d+)\s*}/)[1], 10);
                    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;
                    if (playerId > 0){
                        playerData[playerId] = playerValue;
                        playerArray.push({"id":playerId, "value": playerValue});
                    }
                }
            });
            const top11value = playerArray
            .sort((a,b) => b.value - a.value)
            .slice(0,11)
            .map(a => a.value)
            .reduce((pv, cv) => pv + cv, 0);
            const teamValueInt = parseInt(teamValue.replace(/\./g,''),10);
            const averageTeam = Math.round(teamValueInt/teamSize);
            return {
                "teamVal" : teamValueInt,
                "teamAvg" : averageTeam,
                "top11" : top11value,
                "playerObj": playerData,
                "playerArr": playerArray,
                "lineupVal": 0
            }
        }

        const userIdCont = $("div#matchdayresult > div.ol-page-content > div.row.ol-paragraph-2 > div > a[onclick]");
        const userId0 = $(userIdCont[0]).attr("onclick").match(/\s*'?userId'?\s*:\s*(\d+)\s*}/)[1];
        const userId1 = $(userIdCont[1]).attr("onclick").match(/\s*'?userId'?\s*:\s*(\d+)\s*}/)[1];

        const teamData0 = parseTeamData(await getData(`/team/overview/squad?userId=${userId0}`));
        const teamData1 = parseTeamData(await getData(`/team/overview/squad?userId=${userId1}`));

        const pitches = $("div#matchContent div.ol-team-settings-pitch-position-wrapper");
        const pitch0 = pitches[0];
        const pitch1 = pitches[1];

        $(pitch0).find("div.ol-pitch-position").each(function(i,e){
            const playerId = parseInt(e.parentNode.localName === "a" && e.parentNode.hasAttribute("onclick") ? $(e.parentNode).attr("onclick").match(/\s*'?playerId'?\s*:\s*(\d+)\s*}/)[1] : $(e).find("div[data-player-id]").attr("data-player-id"),10);
            if (playerId > 0){
                teamData0.lineupVal += teamData0.playerObj[playerId] || 0;
            }
        });
        $(pitch1).find("div.ol-pitch-position").each(function(i,e){
            const playerId = parseInt(e.parentNode.localName === "a" && e.parentNode.hasAttribute("onclick") ? $(e.parentNode).attr("onclick").match(/\s*'?playerId'?\s*:\s*(\d+)\s*}/)[1] : $(e).find("div[data-player-id]").attr("data-player-id"),10);
            if (playerId > 0){
                teamData1.lineupVal += teamData1.playerObj[playerId] || 0;
            }
        });

        teamData0.lineupPercent11 = Math.round(teamData0.lineupVal*100/teamData0.top11,0);
        teamData0.lineupPercentAll = Math.round(teamData0.lineupVal*100/teamData0.teamVal,0);
        teamData1.lineupPercent11 = Math.round(teamData1.lineupVal*100/teamData1.top11,0);
        teamData1.lineupPercentAll = Math.round(teamData1.lineupVal*100/teamData1.teamVal,0);

        $(pitch0).append(`<div class="lineupinfo_percentLineup" style="top: 0%" title="% v. Top 11 (MW)">${teamData0.lineupPercent11}%</div>`);
        $(pitch0).append(`<div class="lineupinfo_percentLineup" style="top: 5%" title="% v. gesamt Team">${teamData0.lineupPercentAll}%</div>`);
        $(pitch0).append(`<div class="lineupinfo_percentLineup" style="top: 0%; right:0%;" title="MW Aufstellung">${euroValue(teamData0.lineupVal)}</div>`);
        $(pitch0).append(`<div class="lineupinfo_percentLineup" style="top: 5%; right:0%;" title="MW Team">${euroValue(teamData0.teamVal)}</div>`);
        $(pitch1).append(`<div class="lineupinfo_percentLineup" style="top: 0%" title="% v. Top 11 (MW)">${teamData1.lineupPercent11}%</div>`);
        $(pitch1).append(`<div class="lineupinfo_percentLineup" style="top: 5%" title="% v. gesamtes Team">${teamData1.lineupPercentAll}%</div>`);
        $(pitch1).append(`<div class="lineupinfo_percentLineup" style="top: 0%; right:0%;" title="MW Aufstellung">${euroValue(teamData1.lineupVal)}</div>`);
        $(pitch1).append(`<div class="lineupinfo_percentLineup" style="top: 5%; right:0%;" title="MW Team">${euroValue(teamData1.teamVal)}</div>`);

        console.log('team0', teamData0);
        console.log('team1', teamData1);

    };

    function init(){
        OLCore.addStyle("div.lineupinfo_percentLineup {position:absolute;}","OLI_LineupDiv");
        showInfo();
    };

    waitForKeyElements(
        "div.row > div.statistics-lineup-wrapper:first-child",
        init
    );

})();