Hi I have a game that I am creating with javascript. I has a leaderboard and every 10th person the sockets in the game make an extra room. So from person 11 a different room will be created and the leaderboard from room 1 doesn't show in room 2.
Now the problem: When the 11th person comes in the sort goes haywire. the code:
users.sort(function (a, b) {
return b.score - a.score;
});
eg: with 8 people showing names with alphabet when everyones score is 0
a, b, c, d, e, f, g, h
eg: with 11 people showing names with alphabet when everyones score is 0
b, f, c, d, e, a, g, h, i, j, k
and the sort moves around the 0,1 and 5 indexes every 2 seconds.
It ONLY happens when the 11th person joins. I have tried decreasing person per room to 5 and still the sort goes weird after the 11th person. If there is anyone that might be able to solve this problem please let me know.
Just for more info, when a person connects to a room, they are pushed to a users[] array and should always be at the bottom default with 0 score. If there is any other info you need please let me know.
EDIT: The name is associated with the score. I have written it so that the user is connecting in that alphabetical order. When user A connects the the sort should end with user A only. Then when B connects because he is pushed to the users array, the sort should be A then B in that order, and when C connects the same thing occurs respectively.
EDIT: User structure.
var currentPlayer = {
id: socket.id,
level: 1,
x: position.x.round(2),
y: position.y.round(2),
angle: 0,
score: 0,
type: type,
radius: radius,
facing: 0,
room: nameOfRoom,
lastChatSent: 0,
name: ""
};
Thanks