Skip to content

Instantly share code, notes, and snippets.

@asci
Created February 10, 2013 22:35
Show Gist options
  • Save asci/4751350 to your computer and use it in GitHub Desktop.
Save asci/4751350 to your computer and use it in GitHub Desktop.
var db = [];
var maxCount = 573;
var neibourghsCount = 50;
for (var i = 0; i < maxCount; i++) {
db[i] = {
name: 'user' + i,
id: i,
exp: Math.round(Math.random() * 10000),
points: Math.round(Math.random() * 100000)
}
}
var neibourghs = [];
var last = maxCount % neibourghsCount;
var groupCount = (maxCount - last) / neibourghsCount;
for (i = 0; i < groupCount; i++) {
neibourghs[i] = db.splice(0, neibourghsCount);
}
neibourghs[i - 1] = neibourghs[i - 1].concat(db);
var getUserGroupId = function (userId) {
for (var i = 0; i < neibourghs.length; i++) {
for (var j = 0; j < neibourghs[i].length; j++) {
if (neibourghs[i][j].id == userId) {
return i;
}
}
}
}
var getWinnersFromGroupByPoints = function (groupId) {
var first = 0, second = 0, third = 0;
for (var i = 0; i < neibourghs[groupId].length; i++) {
if (neibourghs[groupId][i].points >= neibourghs[groupId][first].points) {
third = second;
second = first;
first = i;
continue;
}
if (neibourghs[groupId][i].points >= neibourghs[groupId][second].points) {
third = second;
second = i;
continue;
}
if (neibourghs[groupId][i].points >= neibourghs[groupId][third].points) {
third = i;
}
}
return {
first: neibourghs[groupId][first],
second: neibourghs[groupId][second],
third: neibourghs[groupId][third]
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment