Broken Sorting of Correspondence Games (List View)

Those who started many correspondence games will have noticed, that the sort order of the games is not helpful. Those who know what JavaScript and GreaseMonkey add-on for Firefox is, will also know how
to use/improve this script, which works for me and sorts game according to time left for the move (see below).

PS: I could get the script production ready if you would supply time left for move in seconds
or milliseconds.

PPS: I am not a JavaScript developer nor Web developer. This script is result of an hour googling. Feel free to improve.

// ==UserScript==
// @name        sort_games
// @namespace   rklocal
// @include     https://online-go.com/overview
// @version     1
// ==/UserScript==

var day = /Days/;
var hour = /[0-9]+h/;

function select(e, class_name, index=0)
{
    return e.getElementsByClassName(class_name)[index].textContent;
}

function compare_time(a, b)
{
    // time in days
    if ( day.test(a) && day.test(b))  return a.localeCompare(b);
    if ( day.test(a)  ) return 1;
    if ( day.test(b)  ) return -1;

    // time in hours
    if ( hour.test(a) && hour.test(b))
    {
        hour_a = parseInt(hour.exec(a));
        hour_b = parseInt(hour.exec(b));
        return hour_a - hour_b;
    }

    if (hour.test(a)) return 1;
    if (hour.test(b)) return -1;

    return a.localeCompare(b);
}

var sort_funs = [
    function sort_by_move_number(a,b)
    {
        return parseInt(select(b, "move-number")) - parseInt(select(a, "move-number"));
    },

    function sort_by_name(a,b)
    {
        return select(a, "game-name").localeCompare(select(b, "game-name"));
    },

    function sort_by_player(a,b)
    {
        return select(a, "player").localeCompare(select(b,"player"));
    },

    function sort_by_time(a,b)
    {
        if (a.className !== b.className) return b.className.localeCompare(a.className);
        return compare_time(select(a, "clock plenty_of_time"), select(b, "clock plenty_of_time"));
    },

    function sort_by_oppent_time(a,b)
    {
        return compare_time(select(a, "clock plenty_of_time", 1), select(b, "clock plenty_of_time", 1));
    }];

function sort_fun(index)
{
    return function(){
        var game_list = document.getElementById("_game_list");
        [].slice.call(game_list.children)
            .sort(function(a,b){
                // assert that the header line stays at the pos 0
                if (a.className == "GobanLineSummaryContainerHeader") return -1;
                if (b.className == "GobanLineSummaryContainerHeader") return  1;
                return sort_funs[index](a,b);
            })
            .forEach(function(val,index){game_list.appendChild(val)});
    }
}

function checkMoveElement()
{
    if ( document.getElementById("_game_list") ) return;

    var move = document.getElementById("Overview").firstChild.firstChild;
    if (move && move.lastChild && (move.lastChild.className == "GameList GobanLineSummaryContainer")
        && move.lastChild.firstChild && (move.lastChild.firstChild.className == "GobanLineSummaryContainerHeader"))
    {
        var gh = move.lastChild.firstChild;
        move.lastChild.setAttribute("id", "_game_list");

        [].slice.call(gh.children).forEach(function(val,index){
            var button = document.createElement('button');
            button.appendChild(document.createTextNode(val.textContent));
            button.addEventListener( "click", sort_fun(index), false );
            val.replaceChild(button, val.childNodes[0]);
        });
    }
    else
    {
        setTimeout( checkMoveElement, 100);
    }
}

$(document).on('click', 'a[href="/overview"]', function(e){checkMoveElement(); });
$(document).ready(function(){ checkMoveElement();});
9 Likes

I have updated the script, it is now possiple to sort the games by move number, game name, player name, player left time, oppenent left time. Half of the complexity is due to the fact, that the script is not build in into the page, but patched over via GreaseMonkey. Have fun.

2 Likes

OGS interface just went open source, so perhaps you’d like to add these changes and submit a pull request? :slight_smile:

3 Likes