TF2 Connection Problems Persist

Team Fortress 2 has recently gone free to play, which I am guessing has resulted in a big spike in players (it is currently the most played game on Steam, with a peak of nearly 100 000 players today – no doubt it was busier at the weekend). As a result, my old post on fixing the “lost connection to server” issue has been very popular in the last few days.

I find it surprising this problem has persisted for over three years without apparent resolution. The fix that worked for me on my old BeBox was to forward to following ports to my PC:

TCP 27030 – 27039
UDP 1200
UDP 27000 – 27015

If this doesn’t fix the problem, there may be complicating factors. I’ve seen suggestions that the TCP timeout on a router may be set too low. I have a feeling that this may be what is causing connection problems a friend of mine is having, but since it does not affect me I haven’t investigated further. Wireshark would probably be useful if you wanted to probe further.

You can also take a look at my old post if you want more information.

Sort a List By Length and Alphabetically Using jQuery

A fairly common problem in jQuery is to sort a set of elements eg a list of <li>, and most examples I came across seemed to offer pretty decent solutions – provided you want to sort alphabetically. If you want to do this in a number of ways, then tinysort is a great way of doing it.

I wanted to sort a list of links by length for aesthetic purposes, eg

sort by length | sort alphabetically

It turns out there isn’t an iron-cast way of doing this. If you go by length of the text of each <li>, you potentially run into problems with variable-width text, where links with more letters appear shorter than links with fewer but wider letters. I then tried the width of the <li> elements, but being block elements (in my case) they all had the same width. The solution that eventually worked for me was to compare the widths of the <a> child element of each <li>. This took a little bit of experimentation with the selectors and iterator functions as I’m not that familiar with either jQuery or javascript. I based my code off code for sorting a list by Dan Sargeant of one more take, so thanks Dan!

I packaged my code up into a function (well two, as I used the alphabetical sort too) which takes one argument – the selector object representing the parent <ul> or <ol> that is to be sorted. Please adapt the code to your own needs, as it may well not work as is! (ie works for me, YMMV, etc)

function sortbylength($elements) {
        var listitems = $elements.children('li').get();
        listitems.sort(function(a, b) {
                var compA = $(a).children('a').width();
                var compB = $(b).children('a').width();
                return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
        })
        $.each(listitems, function(idx, itm) { $elements.append(itm); });
}

function sortbyalpha($elements) {
        var listitems = $elements.children('li').get();
        listitems.sort(function(a, b) {
                var compA = $(a).text().toUpperCase();
                var compB = $(b).text().toUpperCase();
                return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
        })
        $.each(listitems, function(idx, itm) { $elements.append(itm); });
}