ensl_gathers/lib/gather/controller.js

102 lines
2.1 KiB
JavaScript
Raw Normal View History

"use strict";
/*
* Gather Controller
*
* Server API
2015-07-22 23:30:14 +00:00
* gather:refresh - Refreshes active gather
2015-07-24 15:01:56 +00:00
* gather:notification - Creates a notification
2015-07-22 23:30:14 +00:00
*
* Client API
2015-07-22 23:30:14 +00:00
* gather:join - Assigns user to gather
2015-07-24 13:34:02 +00:00
* gather:vote - Assigns vote to map, leader or server
*
*/
var Gather = require("./gather");
2015-07-24 15:01:56 +00:00
var gather = new Gather();
2015-07-27 11:55:36 +00:00
// ***** Temporary code to test voting *****
var User = require("../user/user");
var client = require("../ensl/client")();
var async = require("async");
var getRandomUser = function (callback) {
var id = Math.floor(Math.random() * 5000) + 1;
console.log(id);
client.getUserById({
id: id
}, function (error, response, body) {
if (response.statusCode !== 200) return getRandomUser(callback);
return callback(error, response, body);
});
};
var instructions = [];
for (var i = 0; i < 11; i++) {
instructions.push(function (callback) {
getRandomUser(function (error, response, body) {
if (error) return callback(error);
if (gather.can("addGatherer")) {
gather.addGatherer(new User(body));
}
callback();
});
});
};
async.parallel(instructions, function (error) {
if (error) {
console.log("Error while adding gatherers", error);
} else {
console.log("Loaded gatherers");
}
});
// ***** Temporary code to test voting *****
module.exports = function (namespace) {
2015-07-22 23:30:14 +00:00
var refreshGather = function () {
2015-07-24 17:05:12 +00:00
namespace.sockets.forEach(function (socket) {
socket.emit("gather:refresh", {
gather: gather.toJson(),
2015-07-27 11:55:36 +00:00
currentUser: socket._user
2015-07-24 17:05:12 +00:00
});
2015-07-22 23:30:14 +00:00
});
};
namespace.on("connection", function (socket) {
socket.on("gather:join", function (data) {
2015-07-24 15:01:56 +00:00
if (gather.can("addGatherer")) {
gather.addGatherer(socket._user);
refreshGather();
}
});
2015-07-22 23:30:14 +00:00
2015-07-24 15:01:56 +00:00
socket.on("gather:leave", function (data) {
if (gather.can("removeGatherer")) {
gather.removeGatherer(socket._user);
refreshGather();
}
});
socket.on("disconnect", function () {
2015-07-22 23:30:14 +00:00
});
socket.on("gather:vote", function (data) {
2015-07-27 11:55:36 +00:00
if (data.leader) {
gather.selectLeader(socket._user, data.leader.candidate);
}
refreshGather();
2015-07-22 23:30:14 +00:00
});
2015-07-24 15:01:56 +00:00
refreshGather();
2015-07-22 23:30:14 +00:00
});
};