ensl_gathers/lib/gather/controller.js

192 lines
4.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
* gather:archive:refresh - Refreshes gather archive
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 mongoose = require("mongoose");
2015-09-14 16:50:00 +00:00
var Gather = require("./gather_singleton");
var ArchivedGather = mongoose.model("ArchivedGather");
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-10-02 14:53:11 +00:00
namespace.emit("gather:refresh", {
gather: Gather.current ? Gather.current.toJson() : null,
maps: Map.list,
servers: Server.list
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
var refreshArchive = () => {
ArchivedGather.recent((error, recentGathers) => {
if (error) return winston.error(error);
2015-09-27 11:55:00 +00:00
namespace.emit("gather:archive:refresh", {
archive: recentGathers,
maps: Map.list,
servers: Server.list
});
});
};
2015-09-14 16:50:00 +00:00
Gather.registerCallback('onDone', refreshGather);
Gather.registerCallback('onEvent', refreshGather);
2015-10-02 14:53:11 +00:00
Gather.registerCallback('onEvent', (event, from, to) => {
if (from === 'gathering' && to === 'election') {
namespace.emit("notification", {
sound: "gather_starting"
});
}
});
2015-09-27 11:55:00 +00:00
Gather.onArchiveUpdate(refreshArchive);
2015-09-14 16:50:00 +00:00
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) {
ArchivedGather.recent((error, recentGathers) => {
if (error) return winston.error(error);
socket.emit("gather:archive:refresh", {
2015-09-27 11:55:00 +00:00
archive: recentGathers,
maps: Map.list,
servers: Server.list
});
});
2015-08-08 17:13:17 +00:00
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);
2015-10-03 15:55:42 +00:00
if (user.cooldown) gather.applyCooldown(user);
2015-09-17 12:36:18 +00:00
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 && data.gatherer) {
2015-09-17 12:36:18 +00:00
// Remove gatherer defined by ID (admins only)
2015-10-20 22:40:40 +00:00
if (!socket._user.isGatherAdmin()) return;
2015-10-03 15:55:42 +00:00
removeGatherer({ id: data.gatherer, cooldown: true });
2015-09-17 12:36:18 +00:00
} 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-17 13:56:50 +00:00
if (typeof data.regather === 'boolean' && gather.can("regather")) {
gather.regather(socket._user, data.regather);
}
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 () {
2015-10-20 22:40:40 +00:00
if (socket._user.isGatherAdmin()) {
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
});
};