Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
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
You can’t perform that action at this time.