From 861fc577575a2b8969e36d0b1c0ce992064a7dcf Mon Sep 17 00:00:00 2001 From: Chris Blanchard Date: Mon, 14 Sep 2015 17:50:00 +0100 Subject: [PATCH] Added singleton for gather --- .nvmrc | 2 +- lib/gather/controller.js | 51 +++++++++++++------------------ lib/gather/gather_singleton.js | 55 ++++++++++++++++++++++++++++++++++ lib/react/gather.jsx | 14 ++++----- lib/steam/bot.js | 5 +--- mocha.opts | 1 - 6 files changed, 85 insertions(+), 43 deletions(-) create mode 100644 lib/gather/gather_singleton.js delete mode 100644 mocha.opts diff --git a/.nvmrc b/.nvmrc index cc96715..0c89fc9 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -0.12.7 \ No newline at end of file +4.0.0 \ No newline at end of file diff --git a/lib/gather/controller.js b/lib/gather/controller.js index 98f0ca7..1223850 100644 --- a/lib/gather/controller.js +++ b/lib/gather/controller.js @@ -21,20 +21,18 @@ var Map = require("./map"); var Server = require("./server"); -var Gather = require("./gather"); -var gather; -var previousGather; +var Gather = require("./gather_singleton"); var _ = require("lodash"); module.exports = function (namespace) { var refreshGather = _.debounce(function () { namespace.sockets.forEach(function (socket) { socket.emit("gather:refresh", { - gather: gather.toJson(), - currentGatherer: gather.getGatherer(socket._user), + gather: Gather.current.toJson(), + currentGatherer: Gather.current.getGatherer(socket._user), maps: Map.list, servers: Server.list, - previousGather: previousGather + previousGather: Gather.previous }); }); }, 200, { @@ -42,49 +40,41 @@ module.exports = function (namespace) { trailing: true }); - var newGather = function () { - if (gather) previousGather = gather; - gather = Gather({ - onEvent: function () { - refreshGather(); - }, - onDone:function () { - newGather(); - refreshGather(); - } - }); - } - - newGather(); + Gather.registerCallback('onDone', refreshGather); + Gather.registerCallback('onEvent', refreshGather); + Gather.restart(); // ***** Generate Test Users ***** if (process.env.POPULATE_GATHER) { - var helper = require("./helper"); - helper.createTestUsers({ gather: gather }, refreshGather); + let helper = require("./helper"); + helper.createTestUsers({ gather: Gather.current }, refreshGather); } namespace.on("connection", function (socket) { socket.on("gather:join", function (data) { + let gather = Gather.current; if (gather.can("addGatherer")) gather.addGatherer(socket._user); refreshGather(); }); socket.on("gather:refresh", function () { socket.emit("gather:refresh", { - gather: gather.toJson(), - currentGatherer: gather.getGatherer(socket._user) + gather: Gather.current.toJson(), + currentGatherer: Gather.current.getGatherer(socket._user) }); }); socket.on("gather:leave", function (data) { + let gather = Gather.current; if (gather.can("removeGatherer")) gather.removeGatherer(socket._user); refreshGather(); }); socket.on("gather:select", function (data) { - var playerId = data.player; + let gather = Gather.current; + let playerId = data.player; // Check team & leader - var gatherLeader = gather.getGatherer(socket._user); + let gatherLeader = gather.getGatherer(socket._user); // Cancel if not gatherer or leader if (gatherLeader === null || gatherLeader.leader === false) { @@ -92,15 +82,15 @@ module.exports = function (namespace) { } // Cancel if id belongs to a leader - var selectedPlayer = gather.getGatherer({id: playerId}); + let selectedPlayer = gather.getGatherer({id: playerId}); if (selectedPlayer === null || selectedPlayer.leader) { return null; } - var team = gatherLeader.team; + let team = gatherLeader.team; - var method = (team === 'alien') ? gather.moveToAlien : gather.moveToMarine; + let method = (team === 'alien') ? gather.moveToAlien : gather.moveToMarine; method.call(gather, selectedPlayer.user); if (gather.can("confirmSelection")) { @@ -115,6 +105,7 @@ module.exports = function (namespace) { }); socket.on("gather:vote", function (data) { + let gather = Gather.current; if (data.leader) { gather.selectLeader(socket._user, data.leader.candidate); } @@ -132,7 +123,7 @@ module.exports = function (namespace) { socket.on("gather:reset", function () { if (socket._user.admin) { - gather = Gather(); + Gather.reset(); refreshGather(); } }); diff --git a/lib/gather/gather_singleton.js b/lib/gather/gather_singleton.js new file mode 100644 index 0000000..b890257 --- /dev/null +++ b/lib/gather/gather_singleton.js @@ -0,0 +1,55 @@ +"use strict" + +let Gather = require("./gather"); +let gatherCallbacks = {}; + +// Register initial callback to reset gather when state is `done` +gatherCallbacks['onDone'] = [function () { + rotateGather(); +}]; + +let executeCallbacks = (context, type) => { + let cbs = gatherCallbacks[type]; + if (!cbs) return; + cbs.forEach(function (cb) { + cb.call(context); + }); +}; + +let newGather = () => { + return SingletonClass.current = Gather({ + onEvent: function () { + executeCallbacks(this, 'onEvent') + }, + onDone: function () { + executeCallbacks(this, 'onDone') + } + }); +}; + +let rotateGather = () => { + if (SingletonClass.current) SingletonClass.previous = SingletonClass.current; + return newGather(); +} + +let SingletonClass = { + registerCallback: function (type, method) { + if (gatherCallbacks[type]) { + gatherCallbacks[type].push(method); + } else { + gatherCallbacks[type] = [method]; + } + }, + restart: function () { + this.previousGather = undefined; + this.current = undefined; + return newGather(); + }, + reset: function () { + return newGather(); + }, + current: Gather(), + previous: undefined +}; + +module.exports = SingletonClass; diff --git a/lib/react/gather.jsx b/lib/react/gather.jsx index 7bff79e..e280fb6 100644 --- a/lib/react/gather.jsx +++ b/lib/react/gather.jsx @@ -451,7 +451,7 @@ var ServerVoting = React.createClass({ onClick={self.voteHandler(server.id)} key={server.id}> {votes} - {server.description || server.dns} + {server.name || server.description || server.dns} ); } @@ -598,7 +598,7 @@ var Gather = React.createClass({ } return (
-

Current Gather

+

Current Gather

{gatherTeams} @@ -635,10 +635,10 @@ var Gatherers = React.createClass({ var lifeform = (abilities.length) ? abilities.join(", ") : "None Specified"; var hiveStats = []; - if (gatherer.user.hive.skill) hiveStats.push("ELO: " + gatherer.user.hive.skill); + if (gatherer.user.hive.skill) hiveStats.push(`${gatherer.user.hive.skill} ELO`); if (gatherer.user.hive.playTime) { - hiveStats.push(Math.floor(gatherer.user.hive.playTime / 3600) + " Hours"); + hiveStats.push(`${Math.floor(gatherer.user.hive.playTime / 3600)} Hours`); } var hive = (hiveStats.length) ? hiveStats.join(", ") : "Not Available"; @@ -727,10 +727,10 @@ var Gatherers = React.createClass({ var CompletedGather = React.createClass({ render() { return ( -
-
+
+

Previous Gather -

+
diff --git a/lib/steam/bot.js b/lib/steam/bot.js index a793c33..ba57d63 100644 --- a/lib/steam/bot.js +++ b/lib/steam/bot.js @@ -83,9 +83,6 @@ var bot; module.exports = config => { if (bot) return bot; if (!config) throw new Error("No credentials provided for Steam Gather Bot"); - bot = new SteamBot({ - account_name: account_name, - password: password - }); + bot = new SteamBot(config); return bot; }; diff --git a/mocha.opts b/mocha.opts deleted file mode 100644 index 5d6484a..0000000 --- a/mocha.opts +++ /dev/null @@ -1 +0,0 @@ ---harmony \ No newline at end of file