ensl_gathers/lib/gather/controller.js

163 lines
4 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");
2015-09-14 16:50:00 +00:00
var Gather = require("./gather_singleton");
2015-08-28 13:18:09 +00:00
var _ = require("lodash");
2015-09-14 21:40:49 +00:00
var winston = require("winston");
2015-07-27 11:55:36 +00:00
module.exports = function (namespace) {
2015-08-28 13:18:09 +00:00
var refreshGather = _.debounce(function () {
2015-07-24 17:05:12 +00:00
namespace.sockets.forEach(function (socket) {
socket.emit("gather:refresh", {
2015-09-14 16:50:00 +00:00
gather: Gather.current.toJson(),
currentGatherer: Gather.current.getGatherer(socket._user),
2015-07-29 13:50:39 +00:00
maps: Map.list,
servers: Server.list,
2015-09-14 16:50:00 +00:00
previousGather: Gather.previous
2015-07-24 17:05:12 +00:00
});
2015-07-22 23:30:14 +00:00
});
2015-08-28 13:18:09 +00:00
}, 200, {
leading: true,
trailing: true
});
2015-07-22 23:30:14 +00:00
2015-09-14 16:50:00 +00:00
Gather.registerCallback('onDone', refreshGather);
Gather.registerCallback('onEvent', refreshGather);
Gather.restart();
2015-08-08 17:13:17 +00:00
// ***** Generate Test Users *****
if (process.env.POPULATE_GATHER) {
2015-09-14 16:50:00 +00:00
let helper = require("./helper");
helper.createTestUsers({ gather: Gather.current }, refreshGather);
}
2015-08-08 17:13:17 +00:00
namespace.on("connection", function (socket) {
socket.on("gather:join", function (data) {
2015-09-14 16:50:00 +00:00
let gather = Gather.current;
2015-08-08 17:13:17 +00:00
if (gather.can("addGatherer")) gather.addGatherer(socket._user);
2015-09-14 21:40:49 +00:00
winston.info("Gather Joiner", JSON.stringify(socket._user));
2015-08-09 14:27:46 +00:00
refreshGather();
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", {
2015-09-14 16:50:00 +00:00
gather: Gather.current.toJson(),
currentGatherer: Gather.current.getGatherer(socket._user)
2015-07-28 23:32:50 +00:00
});
});
2015-09-17 12:36:18 +00:00
let removeGatherer = user => {
2015-09-14 16:50:00 +00:00
let gather = Gather.current;
2015-09-17 12:36:18 +00:00
if (gather.can("removeGatherer")) gather.removeGatherer(user);
winston.info("Gather Leaver", JSON.stringify(user));
2015-08-09 14:27:46 +00:00
refreshGather();
2015-09-17 12:36:18 +00:00
}
socket.on("gather:leave", function (data) {
if (data.gatherer) {
// Remove gatherer defined by ID (admins only)
if (!socket._user.admin) return;
removeGatherer({ id: data.gatherer });
} else {
// Remove gatherer attached to socket
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) {
2015-09-14 16:50:00 +00:00
let gather = Gather.current;
let playerId = data.player;
2015-07-28 23:32:50 +00:00
// Check team & leader
2015-09-14 16:50:00 +00:00
let gatherLeader = gather.getGatherer(socket._user);
2015-07-28 23:32:50 +00:00
// Cancel if not gatherer or leader
if (gatherLeader === null || gatherLeader.leader === false) {
return null;
}
// Cancel if id belongs to a leader
2015-09-14 16:50:00 +00:00
let selectedPlayer = gather.getGatherer({id: playerId});
2015-07-28 23:32:50 +00:00
if (selectedPlayer === null || selectedPlayer.leader) {
return null;
}
2015-09-14 16:50:00 +00:00
let team = gatherLeader.team;
2015-07-28 23:32:50 +00:00
2015-09-14 16:50:00 +00:00
let method = (team === 'alien') ? gather.moveToAlien : gather.moveToMarine;
2015-09-16 15:16:00 +00:00
method.call(gather, selectedPlayer.user, socket._user);
// Check if last player and add to last team
if (gather.lobby().length === 1) {
let assignLast = (gather.marines().length === 6) ?
gather.moveToAlien : gather.moveToMarine;
assignLast.call(gather, gather.lobby().pop());
}
2015-07-28 23:32:50 +00:00
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
2015-09-14 21:40:49 +00:00
winston.info("Selection Data",
JSON.stringify(socket._user),
JSON.stringify(data));
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-09-14 16:50:00 +00:00
let gather = Gather.current;
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-09-14 21:40:49 +00:00
winston.info("Vote Data",
JSON.stringify(socket._user),
JSON.stringify(data));
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) {
2015-09-14 16:50:00 +00:00
Gather.reset();
2015-07-29 14:35:58 +00:00
refreshGather();
}
});
2015-07-24 15:01:56 +00:00
refreshGather();
2015-07-22 23:30:14 +00:00
});
};