Added singleton for gather

This commit is contained in:
Chris Blanchard 2015-09-14 17:50:00 +01:00
parent 6525b44714
commit 861fc57757
6 changed files with 85 additions and 43 deletions

2
.nvmrc
View file

@ -1 +1 @@
0.12.7
4.0.0

View file

@ -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();
}
});

View file

@ -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;

View file

@ -451,7 +451,7 @@ var ServerVoting = React.createClass({
onClick={self.voteHandler(server.id)}
key={server.id}>
<span className="badge">{votes}</span>
{server.description || server.dns}
{server.name || server.description || server.dns}
</a>
);
}
@ -598,7 +598,7 @@ var Gather = React.createClass({
}
return (
<div>
<h2 className="headline">Current Gather</h2>
<h3 className="headline">Current Gather</h3>
<GatherProgress {...this.props} />
<Gatherers {...this.props} />
{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 (
<div className="panel panel-default">
<div className="panel-heading">
<div>
<h4 className="headline previous-headline">
<strong>Previous Gather</strong>
</div>
</h4>
<GatherTeams gather={this.props.gather} />
<GatherVotingResults gather={this.props.gather} maps={this.props.maps} servers={this.props.servers}/>
</div>

View file

@ -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;
};

View file

@ -1 +0,0 @@
--harmony