SquadJS/squad-server/plugins/seeding-mode.js

104 lines
2.9 KiB
JavaScript
Raw Normal View History

import BasePlugin from './base-plugin.js';
export default class SeedingMode extends BasePlugin {
static get description() {
return (
'The <code>SeedingMode</code> plugin broadcasts seeding rule messages to players at regular intervals ' +
'when the server is below a specified player count. It can also be configured to display "Live" messages when ' +
'the server goes live.'
);
}
static get defaultEnabled() {
return true;
}
static get optionsSpecification() {
return {
interval: {
required: false,
description: 'Frequency of seeding messages in milliseconds.',
default: 2.5 * 60 * 1000
},
seedingThreshold: {
required: false,
description: 'Player count required for server not to be in seeding mode.',
default: 50
},
seedingMessage: {
required: false,
description: 'Seeding message to display.',
default: 'Seeding Rules Active! Fight only over the middle flags! No FOB Hunting!'
},
liveEnabled: {
required: false,
description: 'Enable "Live" messages for when the server goes live.',
default: true
},
liveThreshold: {
required: false,
description: 'Player count required for "Live" messages to not bee displayed.',
default: 52
},
liveMessage: {
required: false,
description: '"Live" message to display.',
default: 'Live!'
2021-03-08 05:36:38 -06:00
},
waitOnNewGames: {
required: false,
description: 'Should the plugin wait to be executed on NEW_GAME event.',
default: true
},
waitTimeOnNewGame: {
required: false,
description: 'The time to wait before check player counts in seconds.',
default: 30
}
};
}
2020-12-03 08:16:07 -06:00
constructor(server, options, connectors) {
super(server, options, connectors);
this.stop = false;
2020-12-03 08:16:07 -06:00
this.broadcast = this.broadcast.bind(this);
2021-03-08 05:36:38 -06:00
this.onNewGame = this.onNewGame.bind(this);
2020-12-03 08:16:07 -06:00
}
2020-12-08 06:13:53 -06:00
async mount() {
2021-03-08 05:36:38 -06:00
if (this.options.waitOnNewGames) {
this.server.on('NEW_GAME', this.onNewGame);
}
2020-12-06 15:23:05 -06:00
this.interval = setInterval(this.broadcast, this.options.interval);
2020-12-03 08:16:07 -06:00
}
2020-12-08 06:13:53 -06:00
async unmount() {
2020-12-03 08:16:07 -06:00
clearInterval(this.interval);
2021-03-08 05:36:38 -06:00
this.server.removeEventListener('NEW_GAME', this.onNewGame);
}
onNewGame() {
this.stop = true;
setTimeout(() => {
this.stop = false;
}, 30 * 1000);
2020-12-03 08:16:07 -06:00
}
async broadcast() {
if (this.stop) return;
if (
this.server.a2sPlayerCount !== 0 &&
this.server.a2sPlayerCount < this.options.seedingThreshold
)
await this.server.rcon.broadcast(this.options.seedingMessage);
else if (
this.server.a2sPlayerCount !== 0 &&
this.options.liveEnabled &&
this.server.a2sPlayerCount < this.options.liveThreshold
)
await this.server.rcon.broadcast(this.options.liveMessage);
}
}