Created
May 13, 2014 20:55
-
-
Save marcusbesjes/1504582c02bb7fc9834f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function makeStuff(n, uid) { | |
if (!uid) uid = 0; | |
var results = { | |
rooms: {}, | |
clients: {}, | |
users: {} | |
} | |
var room, client, user; | |
for (var i = 0; i < n; i++) { | |
room = { | |
id: 'room_' + uid, | |
ms: 'msid', | |
clients: {} | |
}; | |
// number of clients in the room | |
var rnd = Math.random(), | |
clients; | |
if (rnd < 0.7) { // 75% just watching no multiscreen | |
clients = 1 | |
} else if (rnd < 0.95) { // 20% 2 devices | |
clients = 2; | |
} else if (rnd < 0.99) { // 4% 3 devices | |
clients = 3; | |
} else if (rnd < 0.997) { // < 1% 4 or 5 devices | |
clients = 4; | |
} else { | |
clients = 5; | |
} | |
user = false; | |
for (var c = 0; c < clients; c++) { | |
client = { | |
id: 'client_' + uid + '_' + c | |
} | |
// connected user | |
rnd = Math.random(); | |
if (user) { | |
// have a logged in user > bigger chance to also be that user | |
if (rnd < 0.5) { | |
client.user = { | |
__ref: ['users', user.id] | |
}; | |
user.clients[client.id] = { | |
__ref: ['clients', client.id] | |
} | |
} | |
} | |
if (!client.user) { | |
if (clients >= 2) rnd *= 0.7; // bigger chance to be logged in when more clients | |
if (rnd < 0.3) { // 30% base login rate | |
user = { | |
id: 'user_' + uid + '_' + c, | |
content: { | |
contentid: { | |
position: 13242, | |
liked: true, | |
rated: 2.5, | |
watched: false | |
} | |
}, | |
clients: {} | |
}; | |
user.clients[client.id] = { | |
__ref: ['clients', client.id] | |
}; | |
results.users[user.id] = user; | |
client.user = { | |
__ref: ['users', user.id] | |
}; | |
} | |
} | |
results.clients[client.id] = client; | |
room.clients[client.id] = { | |
__ref: ['clients', client.id] | |
}; | |
} | |
results.rooms[room.id] = room; | |
uid++; | |
} | |
return results; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var makeStuff = require('./'); | |
var stuff = makeStuff(1000); | |
var room, clients, client, users, user; | |
for (var r in stuff.rooms) { | |
console.log('--------- room'); | |
room = stuff.rooms[r]; | |
users = 0; | |
clients = 0; | |
user = false | |
for (var c in room.clients) { | |
client = stuff.clients[room.clients[c].__ref[1]]; | |
clients++; | |
if (client.user) { | |
if (user && client.user.__ref[1] !== user.id) console.log('two different users in one room!', user.id); | |
user = stuff.users[client.user.__ref[1]]; | |
console.log('client has user', user.id); | |
} | |
if (client.user && client.user.__ref[1] === 's') console.log('>>>>>>>', client.user) | |
} | |
console.log('total clients:', clients); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment