ensl_gathers/lib/gather/controller.js

133 lines
2.9 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-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
*
* 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-29 13:50:39 +00:00
* gather:leave - Leave gather
* gather:select - Select player for team
* gather:refresh - Refresh gather for client
*
2015-07-29 14:35:58 +00:00
* gather:reset - Resets gather (ADMIN)
*
*/
2015-07-29 13:50:39 +00:00
var Map = require("./map");
var Server = require("./server");
var Gather = require("./gather");
2015-07-31 15:54:08 +00:00
var gather;
2015-08-08 17:13:17 +00:00
var previousGathers = [];
2015-07-27 11:55:36 +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-29 13:50:39 +00:00
currentGatherer: gather.getGatherer(socket._user),
maps: Map.list,
servers: Server.list
2015-07-24 17:05:12 +00:00
});
2015-07-22 23:30:14 +00:00
});
};
2015-08-08 17:13:17 +00:00
var newGather = function () {
if (gather) previousGathers.push(gather);
gather = Gather({
onEvent: function () {
2015-07-24 15:01:56 +00:00
refreshGather();
}
2015-08-08 17:13:17 +00:00
});
}
newGather();
// ***** Generate Test Users *****
var helper = require("./helper");
helper.createTestUsers({ gather: gather });
namespace.on("connection", function (socket) {
socket.on("gather:join", function (data) {
if (gather.can("addGatherer")) gather.addGatherer(socket._user);
2015-07-24 15:01:56 +00:00
});
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) {
2015-08-08 17:13:17 +00:00
if (gather.can("removeGatherer")) gather.removeGatherer(socket._user);
2015-07-24 15:01:56 +00:00
});
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);
if (gather.can("confirmSelection")) {
2015-07-29 14:54:45 +00:00
gather.confirmSelection(socket._user);
2015-07-28 23:32:50 +00:00
}
2015-07-29 14:35:58 +00:00
refreshGather();
2015-07-28 23:32:50 +00:00
});
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);
}
2015-07-29 13:50:39 +00:00
if (data.map) {
gather.voteForMap(socket._user, data.map.id);
}
if (data.server) {
gather.voteForServer(socket._user, data.server.id);
}
2015-07-27 11:55:36 +00:00
refreshGather();
2015-07-22 23:30:14 +00:00
});
2015-07-24 15:01:56 +00:00
2015-07-29 14:35:58 +00:00
socket.on("gather:reset", function () {
if (socket._user.admin) {
gather = Gather();
refreshGather();
}
});
2015-07-24 15:01:56 +00:00
refreshGather();
2015-07-22 23:30:14 +00:00
});
};