2015-07-22 16:28:15 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Gather Controller
|
|
|
|
*
|
|
|
|
* Server API
|
2015-07-22 23:30:14 +00:00
|
|
|
* gather:refresh - Refreshes active gather
|
2015-07-28 23:32:50 +00:00
|
|
|
* gather:select - Selects a player for team
|
2015-07-24 15:01:56 +00:00
|
|
|
* gather:notification - Creates a notification
|
2015-07-22 23:30:14 +00:00
|
|
|
*
|
2015-07-22 16:28:15 +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
|
2015-07-22 16:28:15 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
var Gather = require("./gather");
|
2015-07-24 15:01:56 +00:00
|
|
|
var gather = new Gather();
|
2015-07-22 16:28:15 +00:00
|
|
|
|
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);
|
2015-07-28 23:32:50 +00:00
|
|
|
console.log(body.username, body.id)
|
2015-07-27 11:55:36 +00:00
|
|
|
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");
|
2015-07-28 15:54:29 +00:00
|
|
|
gather.gatherers.forEach(function (gatherer, index, array) {
|
|
|
|
var candidate = Math.floor(Math.random() * array.length);
|
|
|
|
array[index].leaderVote = array[candidate].id;
|
|
|
|
});
|
|
|
|
console.log("Assigned vote for each gatherer");
|
2015-07-27 11:55:36 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// ***** Temporary code to test voting *****
|
|
|
|
|
|
|
|
|
2015-07-22 16:28:15 +00:00
|
|
|
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-28 15:54:29 +00:00
|
|
|
currentGatherer: gather.getGatherer(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-28 23:32:50 +00:00
|
|
|
socket.on("gather:refresh", function () {
|
|
|
|
socket.emit("gather:refresh", {
|
|
|
|
gather: gather.toJson(),
|
|
|
|
currentGatherer: gather.getGatherer(socket._user)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-07-24 15:01:56 +00:00
|
|
|
socket.on("gather:leave", function (data) {
|
|
|
|
if (gather.can("removeGatherer")) {
|
|
|
|
gather.removeGatherer(socket._user);
|
|
|
|
refreshGather();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-07-28 23:32:50 +00:00
|
|
|
socket.on("gather:select", function (data) {
|
|
|
|
var playerId = data.player;
|
|
|
|
// Check team & leader
|
|
|
|
var gatherLeader = gather.getGatherer(socket._user);
|
|
|
|
|
|
|
|
// Cancel if not gatherer or leader
|
|
|
|
if (gatherLeader === null || gatherLeader.leader === false) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel if id belongs to a leader
|
|
|
|
var selectedPlayer = gather.getGatherer({id: playerId});
|
|
|
|
|
|
|
|
if (selectedPlayer === null || selectedPlayer.leader) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var team = gatherLeader.team;
|
|
|
|
|
|
|
|
var method = (team === 'alien') ? gather.moveToAlien : gather.moveToMarine;
|
|
|
|
method.call(gather, selectedPlayer.user);
|
|
|
|
|
|
|
|
refreshGather();
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("gather:select:confirm", function () {
|
|
|
|
var user = socket._user;
|
|
|
|
if (gather.can("confirmSelection")) {
|
|
|
|
gather.confirmSelection(user);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-07-24 15:01:56 +00:00
|
|
|
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
|
|
|
});
|
2015-07-22 16:28:15 +00:00
|
|
|
};
|