diff --git a/db/models/event.js b/db/models/event.js index b1069e0..6966542 100644 --- a/db/models/event.js +++ b/db/models/event.js @@ -7,7 +7,7 @@ const pubsub = require(path.join(__dirname, "../../lib/event/pubsub.js")); const winston = require("winston"); const eventSchema = new Schema({ - type: { type: String, required: true }, + eventType: { type: String, required: true }, description: { type: String }, meta: { type: Schema.Types.Mixed }, public: { type: Boolean, default: false }, @@ -15,7 +15,7 @@ const eventSchema = new Schema({ }); eventSchema.index({ createdAt: -1 }); -eventSchema.index({ type: 1 }); +eventSchema.index({ eventType: 1 }); eventSchema.post("save", event => { pubsub.emit("newEvent", event); @@ -24,7 +24,7 @@ eventSchema.post("save", event => { eventSchema.statics.joiner = function (user) { winston.info("Gather Joiner", JSON.stringify(user)); this.create({ - type: "gather:joiner", + eventType: "gather:joiner", description: `${user.username} joined the gather`, public: true }); @@ -33,10 +33,69 @@ eventSchema.statics.joiner = function (user) { eventSchema.statics.leaver = function (user) { winston.info("Gather Leaver", JSON.stringify(user)); this.create({ - type: "gather:leaver", + eventType: "gather:leaver", description: `${user.username} left the gather`, public: true }); }; +eventSchema.statics.playerSelected = function (user, data, gather) { + winston.info("Selection Data", JSON.stringify(user), JSON.stringify(data)); + const gatherer = gather.getGatherer({id: data.player}); + const description = `${user.username} selected ${gatherer.user.username} into ${gatherer.team} team`; + this.create({ + eventType: "gather:select", + description: description, + public: true + }); +}; + +eventSchema.statics.leaderVote = function (user, data, gather) { + winston.info("Vote Data", JSON.stringify(user), JSON.stringify(data)); + const gatherer = gather.getGatherer({ id: data.leader.candidate }); + this.create({ + eventType: "gather:vote:leader", + description: `${user.username} voted for ${gatherer.user.username}`, + public: true + }); +}; + +eventSchema.statics.adminRegather = function (user) { + this.create({ + eventType: "gather:reset", + description: `${user.username} reset the gather`, + public: true + }); +}; + +eventSchema.statics.mapVote = function (user, data, gather, maps) { + const gatherer = gather.getGatherer(user); + if (gatherer.mapVote.some(mapId => mapId === data.map.id)) { + let map = maps.reduce((prev, curr) => { + if (curr.id === data.map.id) return curr; + return prev; + }); + this.create({ + eventType: "gather:vote:map", + description: `${user.username} voted for map ${map.name}`, + public: true + }); + } +}; + +eventSchema.statics.serverVote = function (user, data, gather, servers) { + const gatherer = gather.getGatherer(user); + if (gatherer.serverVote.some(serverId => serverId === data.server.id)) { + let server = servers.reduce((prev, curr) => { + if (curr.id === data.server.id) return curr; + return prev; + }); + this.create({ + eventType: "gather:vote:server", + description: `${user.username} voted for server ${server.name || server.description}`, + public: true + }); + } +}; + module.exports = mongoose.model('Event', eventSchema); diff --git a/lib/gather/controller.js b/lib/gather/controller.js index 6102338..3b41e50 100644 --- a/lib/gather/controller.js +++ b/lib/gather/controller.js @@ -149,9 +149,7 @@ module.exports = function (namespace) { gather.confirmSelection(socket._user); } - winston.info("Selection Data", - JSON.stringify(socket._user), - JSON.stringify(data)); + Event.playerSelected(socket._user, data, gather); refreshGather(); }); @@ -164,24 +162,23 @@ module.exports = function (namespace) { let gather = Gather.current; if (data.leader) { gather.selectLeader(socket._user, data.leader.candidate); + Event.leaderVote(socket._user, data, gather); } if (data.map) { gather.toggleMapVote(socket._user, data.map.id); + Event.mapVote(socket._user, data, gather, Map.list); } if (data.server) { - gather.toggleServerVote(socket._user, data.server.id); + gather.toggleServerVote(socket._user, data.server.id); + Event.serverVote(socket._user, data, gather, Server.list); } if (typeof data.regather === 'boolean' && gather.can("regather")) { gather.regather(socket._user, data.regather); } - winston.info("Vote Data", - JSON.stringify(socket._user), - JSON.stringify(data)); - refreshGather(); }); @@ -189,6 +186,7 @@ module.exports = function (namespace) { if (socket._user.isGatherAdmin()) { Gather.reset(); refreshGather(); + Event.adminRegather(socket._user); } }); diff --git a/lib/gather/gather.js b/lib/gather/gather.js index b0ddf5c..2b87889 100644 --- a/lib/gather/gather.js +++ b/lib/gather/gather.js @@ -11,8 +11,8 @@ * */ -var Gatherer = require("./gatherer"); -var StateMachine = require("javascript-state-machine"); +const Gatherer = require("./gatherer"); +const StateMachine = require("javascript-state-machine"); function Gather (options) { if (!(this instanceof Gather)) { diff --git a/lib/react/event.jsx b/lib/react/event.jsx index 3205ff1..fa06ff3 100644 --- a/lib/react/event.jsx +++ b/lib/react/event.jsx @@ -11,28 +11,16 @@ const Events = React.createClass({ let events; if (this.props.events.length) { events = this.props.events.map(event => { - return ( -
+ {events} +); } }); diff --git a/lib/react/main.jsx b/lib/react/main.jsx index 026cee3..7237492 100644 --- a/lib/react/main.jsx +++ b/lib/react/main.jsx @@ -3,14 +3,20 @@ var App = React.createClass({ getInitialState() { let updateTitle = true; + let showEventsPanel = true; - if (storageAvailable('localStorage') && - localStorage.getItem("updateTitle") !== null) { - updateTitle = JSON.parse(localStorage.getItem("updateTitle")); + if (storageAvailable('localStorage')) { + if (localStorage.getItem("updateTitle") !== null) { + updateTitle = JSON.parse(localStorage.getItem("updateTitle")); + } + if (localStorage.getItem("showEventsPanel") !== null) { + showEventsPanel = JSON.parse(localStorage.getItem("showEventsPanel")); + } } return { updateTitle: updateTitle, + showEventsPanel: showEventsPanel, events: [] }; }, @@ -40,11 +46,17 @@ var App = React.createClass({ document.title = "NSL Gathers"; }, + toggleEventsPanel(event) { + let newState = event.target.checked; + this.setState({ showEventsPanel: newState }); + if (storageAvailable('localStorage')) { + localStorage.setItem("showEventsPanel", newState) + } + }, + toggleUpdateTitle(event) { let newState = event.target.checked; - this.setState({ - updateTitle: newState - }); + this.setState({ updateTitle: newState }); if (storageAvailable('localStorage')) { localStorage.setItem("updateTitle", newState) } @@ -85,7 +97,6 @@ var App = React.createClass({ }); socket.on('event:append', data => { - console.log(data) let events = self.state.events; events.unshift(data); self.setState({ @@ -138,6 +149,11 @@ var App = React.createClass({ }, render() { + let eventsPanel; + if (this.state.showEventsPanel) { + eventsPanel =