Ok, I became interested in finding some simple but effective way to sort data in a file, and I think I came up with something. It is assumed that the highest results will always be at the top of the list. The method is to find the closest result with a smaller number than the new number of visits of the user being checked. The script uses a loop from the first to the found row with user data, and constantly compares the number of visits in each subsequent row. If the number of visits of a user is greater than the number in the line being checked, then the record with this user will be overwritten/embedded in this line, and all subsequent records will be shifted below, after which the cycle will be stopped, otherwise the overwriting is performed in the same line in which the line with the user data was found.
I can't be completely sure that this is the best solution, since this can only be determined after long-term testing with a large number of users :-)
So, try using this script code:
on *:JOIN:#test:{
if ($read($ctfile,ntw,$nick *)) {
var %rn $readn, %count $calc($gettok($read($ctfile,nt,%rn),2,32) + 1)
var %i 1 | while (%i <= %rn) {
var %lc $gettok($read($ctfile,nt,%i),2,32)
if (%count > %lc) { write $+(-dl,%rn) $ctfile | write $+(-il,%i) $ctfile $nick %count | break }
if (%count == %lc) || (%i == %rn) { write $+(-l,%rn) $ctfile $nick %count }
inc %i
}
}
else { write -i $ctfile $nick 1 }
}
alias -l ctfile { return channeltracker.txt }
The contents of the file "channeltracker.txt" during my testing:
PlayerC 15
PlayerA 12
PlayerD 9
PlayerB 5