ensl_gathers/lib/gather/gather_pool.mjs
Absurdon ddb6c460ff Fixes
* use ES Modules
* Fix some bugs
* add Novice Gather
2023-03-05 17:18:43 +00:00

126 lines
3 KiB
JavaScript

/*
* Implements a pool of concurrent gathers
* (no longer a singleton class, should rename)
*
*/
import _ from "lodash";
import winston from "winston"
import mongoose from "mongoose";
import Gather from "./gather.mjs";
import InvitationalGather from "./invitational_gather.mjs";
const ArchivedGather = mongoose.model("ArchivedGather");
let gatherCallbacks = {};
let archiveUpdatedCallback = () => { };
const GatherPool = new Map();
const GATHER_CONFIGS = [
{
type: "classic",
icon: '/normalGather.png',
name: "Classic Gather",
description: "No Requirements",
serverMembershipTest: function (server) {
return server.name.toLowerCase().indexOf("promod") === -1;
}
},
{
type: "novice",
icon: "/normalGather.png",
name: "Novice Gather",
description: "No Requirements",
serverMembershipTest: function (server) {
return server.name.toLowerCase().indexOf("promod") === -1;
},
},
{
type: "invitational",
icon: "/inviteGather.png",
name: "Invitational Gather",
description: "Join on ensl.org/teams/949",
// Grant invite if on a particular nsl team
membershipTest: function (user) {
return InvitationalGather.list.some(m => m.id === user.id);
},
serverMembershipTest: function (server) {
return server.name.toLowerCase().indexOf("promod") === -1;
},
},
];
GATHER_CONFIGS.forEach(config => {
const gatherManager = {
type: config.type,
icon: config.icon,
name: config.name,
registerCallback: function (type, method) {
if (this.gatherCallbacks[type]) {
this.gatherCallbacks[type].push(method);
} else {
this.gatherCallbacks[type] = [method];
}
},
onArchiveUpdate: function (callback) {
archiveUpdatedCallback = callback;
},
restart: function () {
this.previousGather = undefined;
this.current = undefined;
return newGather();
},
reset: function () {
return newGather();
},
current: new Gather(),
previous: undefined,
gatherCallbacks: {}
};
gatherManager.gatherCallbacks['onDone'] = [function () {
rotateGather();
}];
const newGather = () => {
const newGatherConfig = _.clone(config);
newGatherConfig.onEvent = function () {
gatherManager.gatherCallbacks['onEvent'].forEach(cb => {
cb.apply(this, [].slice.call(arguments))
});
};
newGatherConfig.onDone = function () {
gatherManager.gatherCallbacks['onDone'].forEach(cb => {
cb.apply(this, [].slice.call(arguments))
});
};
return gatherManager.current = new Gather(newGatherConfig);
};
const archiveGather = gather => {
ArchivedGather.archive(gather, (error, result) => {
if (error) return winston.error(error);
if (archiveUpdatedCallback
&& typeof archiveUpdatedCallback === 'function') {
archiveUpdatedCallback();
}
});
};
const rotateGather = () => {
if (gatherManager.current) {
gatherManager.previous = gatherManager.current;
archiveGather(gatherManager.previous);
}
return newGather();
};
GatherPool.set(config.type, gatherManager)
});
// Register initial callback to reset gather when state is `done`
export default GatherPool;