rsj001 / Bonus Calculator

// ==UserScript==
// @name Bonus Calculator
// @version 2.5
// @include */torrents.php*
// @license GPL-3.0-or-later
// @downloadURL https://openuserjs.org/install/rsj001/Bonus_Calculator.user.js
// @updateURL https://openuserjs.org/meta/rsj001/Bonus_Calculator.meta.js
// @require http://code.jquery.com/jquery-1.11.0.min.js
// @copyright 2021, rsj001 (https://openuserjs.org/users/rsj001)
// @description  A script of calulating bonus
// @author rsj001
// @grant XMLHttpRequest
// ==/UserScript==

(function () {
    'use strict';
    var T0, N0, L, B0;
    function init(){
        var req = new XMLHttpRequest();
        req.open("GET","/mybonus.php",0);
        req.send(null);
        var text = req.responseText;
        var p1=new RegExp("T0</b> = ([0-9]+)");
        var p2=new RegExp("N0</b> = ([0-9]+)");
        var p3=new RegExp("B0</b> = ([0-9]+)");
        var p4=new RegExp("L</b> = ([0-9]+)");
        T0 = (p1.exec(text))[1];
        N0 = (p2.exec(text))[1];
        B0 = (p3.exec(text))[1];
        L = (p4.exec(text))[1];
    }
    function calc(T, S, N) {
        var std = (1 - 1 / Math.pow(10, T / T0)) * (1 + Math.sqrt(2) / Math.pow(10, (N - 1) / (N0 - 1)));
        var A = std * S;
        var B = B0 * 2 / Math.PI * Math.atan(A / L);
        return std;
    }
    function convertsize(size) {
        if (size.indexOf("TB") != -1) return size.substring(0, size.indexOf("TB")) * 1024;
        if (size.indexOf("GB") != -1) return size.substring(0, size.indexOf("GB"));
        if (size.indexOf("MB") != -1) return size.substring(0, size.indexOf("MB")) / 1024;
        if (size.indexOf("KB") != -1) return size.substring(0, size.indexOf("KB")) / 1024 / 1024;
        if (size.indexOf("B") != -1) return size.substring(0, size.indexOf("B")) / 1024 / 1024 / 1024;
        return 0;
    }
    function convertdate(date) {
        var time = Date.parse(new Date());
        var curtime = Date.parse(new Date(date));
        var day = (time - curtime) / (1000 * 60 * 60 * 24 * 7);
        return day;
    }
    init();
    console.log("(T0, N0, L, B0)=("+T0+","+N0+","+L+","+B0+")");
    var torrents = $(".torrents > tbody > tr"), area = $(".torrents");
    var q = [], src = [];
    var head = torrents.eq(0);
    var hrows = head.find("> td");

    var index = Object();
    for (var i = 0; i < hrows.length; i++) {
        var cur = hrows.eq(i);
        if(cur.find(".time").length) index.time = i;
        else if(cur.find(".size").length) index.size = i;
        else if(cur.find(".seeders").length) index.seeders = i;
    }
    for (i = 1; i < torrents.length; i++) {
        var torrent = torrents.eq(i);
        var mt_official = torrent.find("img[alt='+20%']").length > 0;
        var rows = torrent.find("> td");
        var date = convertdate(rows.eq(index.time).find("span").attr("title"));
        var size = convertsize(rows.eq(index.size).text());
        var seeders = rows.eq(index.seeders).find("a").text().replace(",","");
        var bonus_rate = calc(date, size, seeders) * (mt_official? 1.20:1.00);
        rows.eq(2).after("<td class=\"rowfollow\">" + (bonus_rate * size).toFixed(4) + "</td>");
        var c = {
            content: torrent,
            number: bonus_rate
        };
        q.push(c);
        src.push(c);
    }
    torrents.eq(0).find("> td").eq(2).after("<td id=\"delta_A\" class=\"colhead\"><a href='javascript:void(0)'>ΔA</a></td>");
    var rh = $("#delta_A");
    var sorted = 0;
    q.sort(function (b, a) {
        return a.number - b.number;
    });
    rh.get(0).addEventListener("click", function () {
        area.empty();
        area.append(head);
        if (sorted) {
            for (i = 0; i < src.length; i++) area.append(src[i].content);
        }
        else {
            for (i = 0; i < q.length; i++) area.append(q[i].content);
        }
        sorted = 1 - sorted;
    });
})();