ensl_gathers/db/models/event.js

103 lines
2.9 KiB
JavaScript
Raw Normal View History

2015-12-29 16:25:23 +00:00
"use strict";
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const path = require("path");
const pubsub = require(path.join(__dirname, "../../lib/event/pubsub.js"));
const winston = require("winston");
const eventSchema = new Schema({
2016-01-02 21:32:26 +00:00
eventType: { type: String, required: true },
2015-12-29 16:25:23 +00:00
description: { type: String },
meta: { type: Schema.Types.Mixed },
public: { type: Boolean, default: false },
createdAt: { type: Date, default: Date.now, required: true }
});
eventSchema.index({ createdAt: -1 });
2016-01-02 21:32:26 +00:00
eventSchema.index({ eventType: 1 });
2015-12-29 16:25:23 +00:00
eventSchema.post("save", event => {
pubsub.emit("newEvent", event);
});
eventSchema.statics.joiner = function (user) {
winston.info("Gather Joiner", JSON.stringify(user));
this.create({
2016-01-02 21:32:26 +00:00
eventType: "gather:joiner",
2015-12-29 16:25:23 +00:00
description: `${user.username} joined the gather`,
public: true
});
};
eventSchema.statics.leaver = function (user) {
winston.info("Gather Leaver", JSON.stringify(user));
this.create({
2016-01-02 21:32:26 +00:00
eventType: "gather:leaver",
2015-12-29 16:25:23 +00:00
description: `${user.username} left the gather`,
public: true
});
};
2016-01-02 21:32:26 +00:00
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 });
if (gatherer === null) return;
2016-01-02 21:32:26 +00:00
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
});
}
};
2015-12-29 16:25:23 +00:00
module.exports = mongoose.model('Event', eventSchema);