stetson / Capture-The-Flag

Proof of concept for real-time applications on Node.js
Other
4 stars 0 forks source link

Sort players on the server #107

Closed thinkjson closed 13 years ago

ghost commented 13 years ago

Basically, we're sorting players in this order: Captures, Tags, .

Sorting Code (may not be completely awesome-ifyed).

// Constant: sets how many players we're trying to sort public final static int LIST_COUNT = 10;

// Stores the score items ArrayList myScoreTable = new ArrayList(LIST_COUNT);

playerLoop : for(int i = 0; i < game.getPlayerCount(); i++) {

// This is the player object that contains player data (uid, name, tags, caps, etc)
player = game.getPlayer(i);
boolean isAdded = false;

// This is an object we use for the score table, with a custom comparison function (obj = name, tags, caps, team)
ScoreItem score = new ScoreItem(player.getName(), player.getTags(), player.getCaptures(), player.getTeam());

// Empty list (if the list is empty, we just add the only score we have (this one)
if(myScoreTable.size() == 0) {
    myScoreTable.add(score);
    continue;
}

// If the list has atleast 1 item in it, find out if it is greater than
// or equal to anything on the list already. If it is, place it infront of
// the score that it is greater than.
for(int n = 0; n < myScoreTable.size(); r++) {
    ScoreItem current = myScoreTable.get(r);
    if(score.compare(current) >= 0) {
        myScoreTable.add(r, score);
        continue playerLoop;
    }
}

// Nothing? Is the list full yet? Ok, if the list isn't full, add this item to the end.
if(myScoreTable.size() < LIST_COUNT) {
    myScoreTable.add(score);
}

}

Comparator code (used by ScoreItem): { if(other.caps < this.caps) { return 1; } if(other.caps > this.caps) { return -1; } if(other.tags < this.tags) { return 1; } if(other.tags > this.tags) { return -1; } return 0; }