squad-js-map-vote/mapvote.js

710 lines
31 KiB
JavaScript
Raw Normal View History

2022-09-16 05:20:18 -05:00
//Plugin reworked by JetDave, original version by MaskedMonkeyMan
2022-03-19 18:11:24 -05:00
2022-09-16 18:10:05 -05:00
// import BasePlugin from "./base-plugin.js";
import DiscordBasePlugin from './discord-base-plugin.js';
2022-03-19 18:11:24 -05:00
import fs from "fs";
import { Layers } from "../layers/index.js"
2022-09-15 07:05:06 -05:00
import axios from "axios"
import { time } from 'console';
2022-03-19 18:11:24 -05:00
2022-09-16 18:10:05 -05:00
export default class MapVote extends DiscordBasePlugin {
2022-08-13 05:05:40 -05:00
static get description() {
2022-03-19 18:11:24 -05:00
return "Map Voting plugin";
}
2022-08-13 05:05:40 -05:00
static get defaultEnabled() {
2022-03-19 18:11:24 -05:00
return true;
}
2022-08-13 05:05:40 -05:00
static get optionsSpecification() {
2022-03-19 18:11:24 -05:00
return {
2022-09-16 18:10:05 -05:00
...DiscordBasePlugin.optionsSpecification,
2022-03-19 18:11:24 -05:00
commandPrefix:
{
required: false,
description: "command name to use in chat",
default: "!vote"
},
2022-09-15 07:05:06 -05:00
automaticVoteStart: {
required: false,
description: "a map vote will automatically start after a new match if set to true",
default: true
},
2022-08-13 05:05:40 -05:00
minPlayersForVote:
2022-03-19 18:11:24 -05:00
{
required: false,
description: 'number of players needed on the server for a vote to start',
default: 40
2022-03-19 18:11:24 -05:00
},
voteWaitTimeFromMatchStart:
{
required: false,
description: 'time in mins from the start of a round to the start of a new map vote',
default: 15
2022-03-19 18:11:24 -05:00
},
voteBroadcastInterval:
{
required: false,
description: 'broadcast interval for vote notification in mins',
default: 7
},
automaticSeedingMode:
{
required: false,
description: 'set a seeding layer if server has less than 20 players',
default: true
},
2022-08-31 04:24:12 -05:00
numberRecentMapsToExlude: {
required: false,
description: 'random layer list will not include the n. recent maps',
default: 4
},
gamemodeWhitelist: {
required: false,
description: 'random layer list will be generated with only selected gamemodes',
default: [ "AAS", "RAAS", "INVASION" ]
},
layerFilteringMode: {
required: false,
description: "Select Whitelist mode or Blacklist mode",
default: "blacklist"
},
layerLevelWhitelist: {
required: false,
2022-09-21 09:46:11 -05:00
description: 'random layer list will include only the whitelisted layers or levels. (acceptable formats: Gorodok/Gorodok_RAAS/Gorodok_AAS_v1)',
default: []
},
layerLevelBlacklist: {
required: false,
description: 'random layer list will not include the blacklisted layers or levels. (acceptable formats: Gorodok/Gorodok_RAAS/Gorodok_AAS_v1)',
default: []
},
2022-09-15 17:41:20 -05:00
hideVotesCount: {
required: false,
2022-09-15 18:07:54 -05:00
description: 'hides the number of votes a layer received in broadcast message',
2022-09-15 17:41:20 -05:00
default: false
},
showRerollOption: {
required: false,
2022-09-15 18:07:54 -05:00
description: 'vote option to restart the vote with random entries',
2022-09-15 17:41:20 -05:00
default: false
2022-09-16 15:41:55 -05:00
},
voteBroadcastMessage: {
required: false,
description: 'Message that is sent as broadcast to announce a vote',
2022-09-16 18:10:05 -05:00
default: "✯ MAPVOTE ✯\nVote for the next map by writing in chat the corresponding number!"
},
allowedMapDuplicates: {
required: false,
description: 'Allowed NUMBER of duplicate map entries in vote list',
default: 1
},
logToDiscord: {
required: false,
2022-09-16 16:36:48 -05:00
description: 'Enables/disables vote logging to Discord',
default: false
},
channelID: {
required: false,
description: 'The ID of the channel to log votes to.',
default: '',
2022-09-16 16:36:48 -05:00
example: '112233445566778899'
},
2022-09-20 16:21:05 -05:00
timezone: {
required: false,
description: "Timezone relative to UTC time. 0 for UTC, 2 for CEST (UTC+2), -1 (UTC-1) ",
default: 0
},
timeFrames: {
required: false,
description: 'Array of timeframes to override options',
default: []
2022-04-14 04:08:53 -05:00
}
2022-03-19 18:11:24 -05:00
};
}
2022-08-13 05:05:40 -05:00
constructor(server, options, connectors) {
2022-03-19 18:11:24 -05:00
super(server, options, connectors);
2022-08-13 05:05:40 -05:00
2022-09-20 14:29:46 -05:00
this.options.timeFrames.forEach((e, key, arr) => { arr[ key ].id = key + 1 });
2022-03-19 18:11:24 -05:00
this.voteRules = {}; //data object holding vote configs
this.nominations = []; //layer strings for the current vote choices
2022-08-13 05:05:40 -05:00
this.trackedVotes = {}; //player votes, keyed by steam id
2022-03-19 18:11:24 -05:00
this.tallies = []; //votes per layer, parellel with nominations
this.votingEnabled = false;
this.onConnectBound = false;
this.broadcastIntervalTask = null;
2022-08-13 05:05:40 -05:00
this.firstBroadcast = true;
2022-09-15 17:41:20 -05:00
this.newVoteTimeout = null;
2022-09-16 13:35:08 -05:00
this.newVoteOptions = {
steamid: null,
cmdLayers: [],
bypassRaasFilter: false
};
this.or_options = { ...this.options };
2022-09-28 15:42:53 -05:00
this.autovotestart = null;
this.lastMapUpdate = new Date();
2022-08-13 05:05:40 -05:00
2022-03-19 18:11:24 -05:00
this.onNewGame = this.onNewGame.bind(this);
this.onPlayerDisconnected = this.onPlayerDisconnected.bind(this);
this.onChatMessage = this.onChatMessage.bind(this);
2022-08-13 05:05:40 -05:00
this.broadcastNominations = this.broadcastNominations.bind(this);
2022-03-19 18:11:24 -05:00
this.beginVoting = this.beginVoting.bind(this);
2022-09-07 17:46:37 -05:00
this.setSeedingMode = this.setSeedingMode.bind(this);
2022-09-16 18:33:21 -05:00
this.logVoteToDiscord = this.logVoteToDiscord.bind(this);
this.timeframeOptionOverrider = this.timeframeOptionOverrider.bind(this);
2022-03-19 18:11:24 -05:00
2022-09-02 05:52:42 -05:00
this.broadcast = (msg) => { this.server.rcon.broadcast(msg); };
this.warn = (steamid, msg) => { this.server.rcon.warn(steamid, msg); };
2022-03-19 18:11:24 -05:00
}
2022-08-13 05:05:40 -05:00
async mount() {
this.server.on('NEW_GAME', this.onNewGame);
2022-03-19 18:11:24 -05:00
this.server.on('CHAT_MESSAGE', this.onChatMessage);
this.server.on('PLAYER_DISCONNECTED', this.onPlayerDisconnected);
2022-09-20 16:21:05 -05:00
setTimeout(() => {
this.verbose(1, 'Enabled late listeners.');
this.server.on('PLAYER_CONNECTED', this.setSeedingMode);
}, 10 * 1000) // wait 10 seconds to be sure to have an updated player list
2022-08-13 05:05:40 -05:00
this.verbose(1, 'Map vote was mounted.');
2022-09-06 16:21:33 -05:00
this.verbose(1, "Blacklisted Layers/Levels: " + this.options.layerLevelBlacklist.join(', '))
2022-09-15 17:41:20 -05:00
// await this.checkUpdates();
this.timeframeOptionOverrider();
2022-09-17 18:19:19 -05:00
setInterval(this.timeframeOptionOverrider, 1 * 60 * 1000)
2022-03-19 18:11:24 -05:00
}
2022-08-13 05:05:40 -05:00
async unmount() {
this.server.removeEventListener('NEW_GAME', this.onNewGame);
2022-03-19 18:11:24 -05:00
this.server.removeEventListener('CHAT_MESSAGE', this.onChatMessage);
this.server.removeEventListener('PLAYER_DISCONNECTED', this.onPlayerDisconnected);
clearInterval(this.broadcastIntervalTask);
2022-08-13 05:05:40 -05:00
this.verbose(1, 'Map vote was un-mounted.');
2022-03-19 18:11:24 -05:00
}
2022-08-13 05:05:40 -05:00
async onNewGame() {
2022-08-25 08:43:38 -05:00
setTimeout(async () => {
this.endVoting();
this.trackedVotes = {};
this.tallies = [];
this.nominations = [];
this.factionStrings = [];
2022-09-28 15:42:53 -05:00
if (this.options.automaticVoteStart) this.autovotestart = setTimeout(this.beginVoting, toMils(this.options.voteWaitTimeFromMatchStart));
2022-09-07 18:06:20 -05:00
setTimeout(() => this.setSeedingMode(true), 10000);
2022-08-25 08:43:38 -05:00
}, 10000)
2022-08-13 05:05:40 -05:00
}
2022-08-16 16:53:08 -05:00
2022-08-13 05:05:40 -05:00
async onPlayerDisconnected() {
if (!this.votingEnabled) return;
2022-08-13 05:05:40 -05:00
await this.server.updatePlayerList();
2022-03-19 18:11:24 -05:00
this.clearVote();
if (new Date() - this.lastMapUpdate > 5 * 1000) this.updateNextMap();
}
async timeframeOptionOverrider() {
2022-09-20 14:29:46 -05:00
const orOpt = { ...this.or_options };
2022-09-20 16:21:05 -05:00
const utcDelay = parseFloat(this.options.timezone);
let timeNow = new Date(0, 0, 0, new Date().getUTCHours() + utcDelay, new Date().getUTCMinutes());
timeNow = new Date(0, 0, 0, timeNow.getHours(), timeNow.getMinutes())
// console.log(timeNow, timeNow.toTimeString(), timeNow.toLocaleTimeString())
this.verbose(1, `Current time (UTC${(utcDelay >= 0 ? '+' : '') + utcDelay}) ${timeNow.toLocaleTimeString('en-GB').split(':').splice(0, 2).join(':')} `)
2022-09-20 14:29:46 -05:00
const activeTimeframes = orOpt.timeFrames.filter(tfFilter);
2022-09-17 18:19:19 -05:00
let logTimeframe = "Active Time Frames: ";
let activeTfIds = [];
2022-09-18 14:45:29 -05:00
this.options = { ...this.or_options };
2022-09-17 18:19:19 -05:00
for (let atfK in activeTimeframes) {
const atf = activeTimeframes[ atfK ];
activeTfIds.push(atf.name || atf.id);
2022-09-18 07:19:22 -05:00
for (let o in atf.overrides) {
this.options[ o ] = atf.overrides[ o ];
2022-09-17 18:19:19 -05:00
}
}
this.verbose(1, logTimeframe + activeTfIds.join(', '));
2022-09-20 16:21:05 -05:00
function tfFilter(tf, key, arr) {
const tfStartSplit = [ parseInt(tf.start.split(':')[ 0 ]), parseInt(tf.start.split(':')[ 1 ]) ];
const tfEndSplit = [ parseInt(tf.end.split(':')[ 0 ]), parseInt(tf.end.split(':')[ 1 ]) ];
const tfStart = new Date(0, 0, 0, ...tfStartSplit)
const tfStart2 = new Date(0, 0, 0, 0, 0)
const tfEnd = new Date(0, 0, 0, ...tfEndSplit)
const tfEnd2 = new Date(0, 0, 0, 24, 0)
// console.log(timeNow, tfStart, tfEnd, tfStart2 <= timeNow, timeNow < tfEnd)
2022-09-20 16:21:05 -05:00
return (tfStart <= timeNow && timeNow < tfEnd) || (tfStart > tfEnd && ((tfStart <= timeNow && timeNow < tfEnd2) || (tfStart2 <= timeNow && timeNow < tfEnd)))
}
}
2022-09-07 18:06:20 -05:00
setSeedingMode(isNewGameEvent = false) {
2022-08-25 08:43:38 -05:00
// setTimeout(()=>{this.msgDirect('76561198419229279',"MV\ntest\ntest")},1000)
// this.msgBroadcast("[MapVote] Seeding mode active")
2022-09-07 17:46:37 -05:00
const baseDataExist = this && this.options && this.server && this.server.players;
if (baseDataExist) {
this.verbose(1, "Checking seeding mode");
if (this.options.automaticSeedingMode) {
if (this.server.players.length >= 1 && this.server.players.length < 40) {
2022-09-10 18:09:28 -05:00
const seedingMaps = Layers.layers.filter((l) => l.layerid && l.gamemode.toUpperCase() == "SEED" && !this.options.layerLevelBlacklist.find((fl) => l.layerid.toLowerCase().startsWith(fl.toLowerCase())))
2022-09-15 07:05:06 -05:00
2022-09-16 09:12:48 -05:00
const rndMap = randomElement(seedingMaps);
2022-09-07 17:46:37 -05:00
if (this.server.currentLayer) {
2022-09-10 18:09:28 -05:00
if (this.server.currentLayer.gamemode.toLowerCase() != "seed") {
if (this.server.players.length <= 5) {
const newCurrentMap = rndMap.layerid;
this.verbose(1, 'Going into seeding mode.');
this.server.rcon.execute(`AdminChangeLayer ${newCurrentMap} `);
2022-09-10 18:09:28 -05:00
}
}
2022-09-15 06:20:09 -05:00
} else this.verbose(1, "Bad data (currentLayer). Seeding mode for current layer skipped to prevent errors.");
2022-09-15 07:05:06 -05:00
2022-09-15 06:20:09 -05:00
if (this.server.nextLayer) {
const nextMaps = seedingMaps.filter((l) => (!this.server.currentLayer || l.layerid != this.server.currentLayer.layerid))
let rndMap2;
do rndMap2 = randomElement(nextMaps);
while (rndMap2.layerid == rndMap.layerid)
if (isNewGameEvent && this.server.players.length < 20 && this.server.nextLayer.gamemode.toLowerCase() != "seed") {
const newNextMap = rndMap2.layerid;
this.server.rcon.execute(`AdminSetNextLayer ${newNextMap} `);
2022-09-15 06:20:09 -05:00
}
} else this.verbose(1, "Bad data (nextLayer). Seeding mode for next layer skipped to prevent errors.");
2022-09-10 18:09:28 -05:00
2022-09-15 06:20:09 -05:00
} else this.verbose(1, `Player count doesn't allow seeding mode (${this.server.players.length}/20)`);
2022-09-07 17:46:37 -05:00
} else this.verbose(1, "Seeding mode disabled in config");
} else console.log("[MapVote][1] Bad data (this/this.server/this.options). Seeding mode skipped to prevent errors.");
2022-03-19 18:11:24 -05:00
}
2022-08-13 05:05:40 -05:00
async onChatMessage(info) {
const { steamID, name: playerName } = info;
2022-03-19 18:11:24 -05:00
const message = info.message.toLowerCase();
//check to see if this message has a command prefix
2022-08-13 05:05:40 -05:00
if (!message.startsWith(this.options.commandPrefix) && isNaN(message))
2022-03-19 18:11:24 -05:00
return;
2022-08-13 05:05:40 -05:00
const commandSplit = (isNaN(message) ? message.substring(this.options.commandPrefix.length).trim().split(' ') : [ message ]);
let cmdLayers = commandSplit.slice(1);
for (let k in cmdLayers) cmdLayers[ k ] = cmdLayers[ k ].toLowerCase();
const subCommand = commandSplit[ 0 ];
if (!isNaN(subCommand)) // if this succeeds player is voting for a map
2022-03-19 18:11:24 -05:00
{
2022-08-13 05:05:40 -05:00
const mapNumber = parseInt(subCommand); //try to get a vote number
2022-09-15 18:04:04 -05:00
if (this.nominations[ mapNumber ]) {
2022-09-15 17:41:20 -05:00
if (!this.votingEnabled) {
await this.warn(steamID, "There is no vote running right now");
return;
}
await this.registerVote(steamID, mapNumber, playerName);
this.updateNextMap();
} else
await this.warn(steamID, "Please vote a valid option");
2022-03-19 18:11:24 -05:00
return;
}
2022-08-13 05:05:40 -05:00
2022-03-19 18:11:24 -05:00
const isAdmin = info.chat === "ChatAdmin";
2022-08-13 05:05:40 -05:00
switch (subCommand) // select the sub command
2022-03-19 18:11:24 -05:00
{
case "choices": //sends choices to player in the from of a warning
case "results": //sends player the results in a warning
2022-08-13 05:05:40 -05:00
if (!this.votingEnabled) {
2022-09-02 05:52:42 -05:00
await this.warn(steamID, "There is no vote running right now");
2022-03-19 18:11:24 -05:00
return;
}
this.directMsgNominations(steamID);
return;
2022-08-13 05:05:40 -05:00
case "start": //starts the vote again if it was canceled
if (!isAdmin) return;
if (this.votingEnabled) {
2022-09-02 05:52:42 -05:00
await this.warn(steamID, "Voting is already enabled");
2022-03-19 18:11:24 -05:00
return;
}
2022-08-13 05:05:40 -05:00
this.beginVoting(true, steamID, cmdLayers);
return;
case "restart": //starts the vote again if it was canceled
if (!isAdmin) return;
this.endVoting();
this.beginVoting(true, steamID, cmdLayers);
2022-03-19 18:11:24 -05:00
return;
case "cancel": //cancels the current vote and wont set next map to current winnner
2022-08-13 05:05:40 -05:00
if (!isAdmin) return;
if (!this.votingEnabled) {
2022-09-02 05:52:42 -05:00
await this.warn(steamID, "There is no vote running right now");
2022-03-19 18:11:24 -05:00
return;
}
this.endVoting();
2022-09-02 05:52:42 -05:00
await this.warn(steamID, "Ending current vote");
2022-03-19 18:11:24 -05:00
return;
2022-09-28 15:42:53 -05:00
case "cancelauto": //cancels the current vote and wont set next map to current winnner
if (!isAdmin) return;
if (!this.autovotestart) {
await this.warn(steamID, "There is no automatic vote start scheduled");
return;
}
clearTimeout(this.autovotestart);
this.autovotestart = null;
await this.warn(steamID, "Ending current vote");
return;
2022-08-13 05:05:40 -05:00
case "broadcast":
if (!this.votingEnabled) {
2022-09-02 05:52:42 -05:00
await this.warn(steamID, "There is no vote running right now");
2022-08-13 05:05:40 -05:00
return;
}
this.broadcastNominations();
2022-03-19 18:11:24 -05:00
return;
case "help": //displays available commands
2022-08-31 04:24:12 -05:00
let msg = "";
msg += (`!vote\n > choices\n > results\n`);
if (isAdmin) msg += (`\n Admin only:\n > start\n > restart\n > cancel\n > broadcast`);
2022-08-13 05:05:40 -05:00
2022-09-02 05:52:42 -05:00
await this.warn(steamID, msg + `\nMapVote SquadJS plugin built by JetDave`);
2022-03-19 18:11:24 -05:00
return;
default:
//give them an error
2022-09-02 05:52:42 -05:00
await this.warn(steamID, `Unknown vote subcommand: ${subCommand}`);
2022-03-19 18:11:24 -05:00
return;
}
2022-08-13 05:05:40 -05:00
2022-03-19 18:11:24 -05:00
}
2022-08-13 05:05:40 -05:00
2022-03-19 18:11:24 -05:00
updateNextMap() //sets next map to current mapvote winner, if there is a tie will pick at random
{
this.lastMapUpdate = new Date();
2022-09-15 17:41:20 -05:00
let cpyWinners = this.currentWinners;
let skipSetNextMap = false;
if (cpyWinners.find(e => e == this.nominations[ 0 ])) {
2022-09-15 18:16:04 -05:00
if (cpyWinners.length > 1) {
delete cpyWinners[ cpyWinners.indexOf(this.nominations[ 0 ]) ]
cpyWinners = cpyWinners.filter(e => e != null)
}
2022-09-15 17:41:20 -05:00
else {
skipSetNextMap = true;
if (this.newVoteTimeout == null) {
this.newVoteTimeout = setTimeout(() => {
if (this.currentWinners.find(e => e == this.nominations[ 0 ]) && this.currentWinners.length == 1) {
this.newVoteTimeout = null;
this.endVoting()
this.broadcast("The previous Map Vote has been canceled and a new one has been generated!")
2022-09-16 13:35:08 -05:00
this.beginVoting(true, this.newVoteOptions.steamid, this.newVoteOptions.cmdLayers)
2022-09-15 17:41:20 -05:00
}
2022-09-15 18:04:04 -05:00
}, 2 * 60 * 1000)
2022-09-16 13:35:08 -05:00
setTimeout(this.broadcastNominations, 1 * 60 * 1000)
2022-09-15 17:41:20 -05:00
}
}
}
if (!skipSetNextMap) {
const nextMap = randomElement(cpyWinners);
2022-10-03 05:28:50 -05:00
const baseDataExist = this && this.options && this.server && this.server.players && this.server.nextLayer && this.server.nextLayer.layerid;
if (!baseDataExist || this.server.nextLayer.layerid != nextMap)
this.server.rcon.execute(`AdminSetNextLayer ${nextMap}`);
2022-09-15 17:41:20 -05:00
}
2022-03-19 18:11:24 -05:00
}
2022-08-13 05:05:40 -05:00
matchLayers(builtString) {
return Layers.layers.filter(element => element.layerid.includes(builtString));
2022-04-14 04:08:53 -05:00
}
2022-08-13 05:05:40 -05:00
getMode(nomination, currentMode) {
const mapName = nomination.map;
let modes = nomination.modes;
2022-08-13 05:05:40 -05:00
let mode = modes[ 0 ];
2022-04-14 04:08:53 -05:00
if (mode === "Any")
modes = this.voteRules.modes;
2022-08-13 05:05:40 -05:00
if (this.voteRules.mode_repeat_blacklist.includes(currentMode)) {
2022-04-14 04:08:53 -05:00
modes = modes.filter(mode => !mode.includes(currentMode));
}
2022-08-13 05:05:40 -05:00
while (modes.length > 0) {
mode = randomElement(modes);
2022-04-14 04:08:53 -05:00
modes = modes.filter(elem => elem !== mode);
if (this.matchLayers(`${mapName}_${mode}`).length > 0)
2022-04-14 04:08:53 -05:00
break;
}
return mode;
}
2022-03-19 18:11:24 -05:00
//TODO: right now if version is set to "Any" no caf layers will be selected
2022-08-16 16:53:08 -05:00
populateNominations(steamid = null, cmdLayers = [], bypassRaasFilter = false) //gets nomination strings from layer options
2022-03-19 18:11:24 -05:00
{
this.options.gamemodeWhitelist.forEach((e, k, a) => a[ k ] = e.toUpperCase());
2022-08-13 05:05:40 -05:00
// this.nominations.push(builtLayerString);
// this.tallies.push(0);
const translations = {
'United States Army': "USA",
'United States Marine Corps': "USMC",
'Russian Ground Forces': "RUS",
'British Army': "GB",
'Canadian Army': "CAF",
'Australian Defence Force': "AUS",
'Irregular Militia Forces': "IRR",
'Middle Eastern Alliance': "MEA",
'Insurgent Forces': "INS",
}
2022-03-19 18:11:24 -05:00
this.nominations = [];
2022-08-13 05:05:40 -05:00
this.tallies = [];
this.factionStrings = [];
let rnd_layers = [];
// let rnd_layers = [];
const sanitizedLayers = Layers.layers.filter((l) => l.layerid && l.map);
const maxOptions = this.options.showRerollOption ? 5 : 6;
2022-08-25 08:43:38 -05:00
if (!cmdLayers || cmdLayers.length == 0) {
const recentlyPlayedMaps = this.objArrToValArr(this.server.layerHistory.splice(0, this.options.numberRecentMapsToExlude), "layer", "map", "name");
2022-09-08 04:49:08 -05:00
this.verbose(1, "Recently played maps: " + recentlyPlayedMaps.join(', '))
const all_layers = sanitizedLayers.filter((l) =>
this.options.gamemodeWhitelist.includes(l.gamemode.toUpperCase()) &&
![ this.server.currentLayer ? this.server.currentLayer.map.name : null, ...recentlyPlayedMaps ].includes(l.map.name) &&
(
(this.options.layerFilteringMode.toLowerCase() == "blacklist" && !this.options.layerLevelBlacklist.find((fl) => l.layerid.toLowerCase().startsWith(fl.toLowerCase()))) ||
(this.options.layerFilteringMode.toLowerCase() == "whitelist" && this.options.layerLevelWhitelist.find((fl) => l.layerid.toLowerCase().startsWith(fl.toLowerCase())))
)
);
for (let i = 1; i <= maxOptions; i++) {
2022-09-08 04:49:08 -05:00
let l, maxtries = 10;
2022-10-03 18:02:51 -05:00
do l = randomElement(all_layers); while ((rnd_layers.find(lf => lf.layerid == l.layerid) || rnd_layers.filter(lf => lf.map.name == l.map.name).length > (this.options.allowedMapDuplicates-1)) && --maxtries >= 0)
2022-09-08 04:49:08 -05:00
if (maxtries > 0) {
rnd_layers.push(l);
2022-09-15 17:41:20 -05:00
this.nominations[ i ] = l.layerid
this.tallies[ i ] = 0;
this.factionStrings[ i ] = getTranslation(l.teams[ 0 ]) + "-" + getTranslation(l.teams[ 1 ]);
2022-09-08 04:49:08 -05:00
}
2022-03-19 18:11:24 -05:00
}
if (!bypassRaasFilter && this.options.gamemodeWhitelist.includes("RAAS") && rnd_layers.filter((l) => l.gamemode === 'RAAS').length < Math.floor(maxOptions / 2)) this.populateNominations();
if (this.nominations.length == 0) {
this.populateNominations(steamid, cmdLayers, bypassRaasFilter);
return;
}
2022-08-13 05:05:40 -05:00
} else {
2022-09-15 18:04:04 -05:00
let singleGamemodeVote = false;
if (cmdLayers.length == 1 && cmdLayers[ 0 ].split('_')[ 0 ] == "*") {
singleGamemodeVote = true;
for (let i = 0; i < maxOptions; i++) cmdLayers.push(cmdLayers[ 0 ])
}
if (singleGamemodeVote || cmdLayers.length <= maxOptions) {
let i = 1;
2022-08-13 05:05:40 -05:00
for (let cl of cmdLayers) {
const cls = cl.split('_');
2022-09-15 17:41:20 -05:00
const fLayers = sanitizedLayers.filter((l) => ((cls[ 0 ] == "*" || l.layerid.toLowerCase().startsWith(cls[ 0 ])) && (l.gamemode.toLowerCase().startsWith(cls[ 1 ]) || (!cls[ 1 ] && [ 'RAAS', 'AAS', 'INVASION' ].includes(l.gamemode.toUpperCase()))) && (!cls[ 2 ] || l.version.toLowerCase().startsWith("v" + cls[ 2 ].replace(/v/gi, '')))));
2022-10-03 18:02:51 -05:00
let l, maxtries = 10;
do l = randomElement(fLayers); while ((rnd_layers.filter(lf => lf.map.name == l.map.name).length > (this.options.allowedMapDuplicates-1)) && --maxtries >= 0)
if (l) {
2022-09-01 17:33:30 -05:00
rnd_layers.push(l);
2022-09-15 17:41:20 -05:00
this.nominations[ i ] = l.layerid
this.tallies[ i ] = 0;
this.factionStrings[ i ] = getTranslation(l.teams[ 0 ]) + "-" + getTranslation(l.teams[ 1 ]);
2022-09-15 18:04:04 -05:00
i++;
2022-09-01 17:33:30 -05:00
}
2022-08-13 05:05:40 -05:00
}
2022-09-15 18:04:04 -05:00
}
else if (steamid) {
this.warn(steamid, "You cannot start a vote with more than " + maxOptions + " options");
return;
}
2022-03-19 18:11:24 -05:00
}
2022-04-14 04:08:53 -05:00
2022-09-15 17:41:20 -05:00
if (this.options.showRerollOption) {
2022-09-15 18:04:04 -05:00
if (this.nominations.length > 5) {
2022-09-16 13:35:08 -05:00
this.nominations.splice(6, 1);
this.tallies.splice(6, 1);
this.factionStrings.splice(6, 1);
2022-09-15 18:04:04 -05:00
}
2022-09-16 13:35:08 -05:00
this.newVoteOptions.steamid = steamid;
this.newVoteOptions.bypassRaasFilter = bypassRaasFilter;
this.newVoteOptions.cmdLayers = cmdLayers;
2022-09-15 18:04:04 -05:00
this.nominations[ 0 ] = "Reroll vote list with random options"
2022-09-15 17:41:20 -05:00
this.tallies[ 0 ] = 0;
this.factionStrings[ 0 ] = "";
2022-09-16 13:35:08 -05:00
2022-03-19 18:11:24 -05:00
}
2022-04-14 04:08:53 -05:00
2022-08-13 05:05:40 -05:00
function getTranslation(t) {
if (translations[ t.faction ]) return translations[ t.faction ]
else {
const f = t.faction.split(' ');
let fTag = "";
f.forEach((e) => { fTag += e[ 0 ] });
return fTag.toUpperCase();
2022-03-19 18:11:24 -05:00
}
}
}
//checks if there are enough players to start voting, if not binds itself to player connected
//when there are enough players it clears old votes, sets up new nominations, and starts broadcast
2022-09-08 04:51:26 -05:00
beginVoting(force = false, steamid = null, cmdLayers = []) {
2022-08-25 08:43:38 -05:00
this.verbose(1, "Starting vote")
2022-03-19 18:11:24 -05:00
const playerCount = this.server.players.length;
const minPlayers = this.options.minPlayersForVote;
if (this.votingEnabled) //voting has already started
return;
2022-08-13 05:05:40 -05:00
if (playerCount < minPlayers && !force) {
if (this.onConnectBound == false) {
setTimeout(() => { this.beginVoting(force, null, cmdLayers) }, 60 * 1000)
2022-03-19 18:11:24 -05:00
this.onConnectBound = true;
}
return;
}
this.onConnectBound = false;
2022-03-19 18:11:24 -05:00
// these need to be reset after reenabling voting
this.trackedVotes = {};
this.tallies = [];
2022-08-13 05:05:40 -05:00
this.populateNominations(steamid, cmdLayers);
2022-03-19 18:11:24 -05:00
this.votingEnabled = true;
2022-08-13 05:05:40 -05:00
this.firstBroadcast = true;
this.broadcastNominations();
2022-03-19 18:11:24 -05:00
this.broadcastIntervalTask = setInterval(this.broadcastNominations, toMils(this.options.voteBroadcastInterval));
}
2022-08-13 05:05:40 -05:00
endVoting() {
2022-03-19 18:11:24 -05:00
this.votingEnabled = false;
clearInterval(this.broadcastIntervalTask);
2022-09-15 18:04:04 -05:00
clearTimeout(this.newVoteTimeout);
this.newVoteTimeout = null;
2022-03-19 18:11:24 -05:00
this.broadcastIntervalTask = null;
}
objArrToValArr(arr, ...key) {
let vet = [];
for (let o of arr) {
let obj = o;
for (let k of key) {
if (obj[ k ])
obj = obj[ k ];
}
vet.push(obj);
}
return vet;
}
2022-03-19 18:11:24 -05:00
//sends a message about nominations through a broadcast
2022-08-13 05:05:40 -05:00
//NOTE: max squad broadcast message length appears to be 485 characters
2022-03-19 18:11:24 -05:00
//Note: broadcast strings with multi lines are very strange
2022-08-13 05:05:40 -05:00
async broadcastNominations() {
2022-08-25 08:43:38 -05:00
if (this.nominations.length > 0 && this.votingEnabled) {
2022-09-16 15:41:55 -05:00
await this.broadcast(this.options.voteBroadcastMessage);
2022-08-13 05:05:40 -05:00
let nominationStrings = [];
2022-09-15 17:41:20 -05:00
for (let choice = 1; choice < this.nominations.length; choice++) {
2022-08-13 05:05:40 -05:00
choice = Number(choice);
2022-09-15 17:41:20 -05:00
let vLayer = Layers.layers.find(e => e.layerid == this.nominations[ choice ]);
nominationStrings.push(formatChoice(choice, vLayer.map.name + ' ' + vLayer.gamemode + ' ' + this.factionStrings[ choice ], this.tallies[ choice ], (this.options.hideVotesCount || this.firstBroadcast)));
2022-08-13 05:05:40 -05:00
}
2022-09-15 17:41:20 -05:00
if (this.nominations[ 0 ]) nominationStrings.push(formatChoice(0, this.nominations[ 0 ], this.tallies[ 0 ], (this.options.hideVotesCount || this.firstBroadcast)))
2022-09-02 05:52:42 -05:00
await this.broadcast(nominationStrings.join("\n"));
2022-09-16 18:10:05 -05:00
if (this.firstBroadcast)
2022-09-16 18:10:05 -05:00
await this.logVoteToDiscord(nominationStrings.join("\n"))
2022-08-13 05:05:40 -05:00
this.firstBroadcast = false;
}
2022-03-19 18:11:24 -05:00
//const winners = this.currentWinners;
//await this.msgBroadcast(`Current winner${winners.length > 1 ? "s" : ""}: ${winners.join(", ")}`);
}
2022-08-13 05:05:40 -05:00
async directMsgNominations(steamID) {
2022-08-25 08:43:38 -05:00
let strMsg = "";
2022-08-13 05:05:40 -05:00
for (let choice in this.nominations) {
choice = Number(choice);
2022-08-25 08:43:38 -05:00
// await this.msgDirect(steamID, formatChoice(choice, this.nominations[ choice ], this.tallies[ choice ]));
strMsg += (steamID, formatChoice(choice, this.nominations[ choice ], this.tallies[ choice ])) + "\n";
2022-08-13 05:05:40 -05:00
}
2022-08-25 08:43:38 -05:00
strMsg.trim();
2022-09-10 18:01:43 -05:00
if (steamID) this.warn(steamID, strMsg)
2022-08-13 05:05:40 -05:00
2022-08-25 08:43:38 -05:00
// const winners = this.currentWinners;
// await this.msgDirect(steamID, `Current winner${winners.length > 1 ? "s" : ""}: ${winners.join(", ")}`);
2022-03-19 18:11:24 -05:00
}
//counts a vote from a player and adds it to tallies
2022-08-13 05:05:40 -05:00
async registerVote(steamID, nominationIndex, playerName) {
2022-09-15 17:41:20 -05:00
// nominationIndex -= 1; // shift indices from display range
2022-08-13 05:05:40 -05:00
if (nominationIndex < 0 || nominationIndex > this.nominations.length) {
2022-09-02 05:52:42 -05:00
await this.warn(steamID, `[Map Vote] ${playerName}: invalid map number, typ !vote results to see map numbers`);
2022-03-19 18:11:24 -05:00
return;
}
2022-08-13 05:05:40 -05:00
const previousVote = this.trackedVotes[ steamID ];
this.trackedVotes[ steamID ] = nominationIndex;
this.tallies[ nominationIndex ] += 1;
if (previousVote !== undefined)
this.tallies[ previousVote ] -= 1;
2022-09-15 18:04:04 -05:00
await this.warn(steamID, `Registered vote: ${this.nominations[ nominationIndex ].replace(/\_/gi, ' ').replace(/\sv\d{1,2}/gi, '')} ${this.factionStrings[ nominationIndex ]} ` + (this.options.hideVotesCount ? `` : `(${this.tallies[ nominationIndex ]} votes)`));
2022-08-13 05:05:40 -05:00
// await this.msgDirect(steamID, `Registered vote`);// ${this.nominations[ nominationIndex ]} ${this.factionStrings[ nominationIndex ]} (${this.tallies[ nominationIndex ]} votes)`);
// await this.msgDirect(steamID, `${this.nominations[ nominationIndex ]} (${this.tallies[ nominationIndex ]} votes)`);
// await this.msgDirect(steamID, `${this.factionStrings[ nominationIndex ]}`);
// await this.msgDirect(steamID, `${this.tallies[ nominationIndex ]} votes`);
2022-03-19 18:11:24 -05:00
}
2022-08-13 05:05:40 -05:00
2022-09-16 18:33:21 -05:00
async logVoteToDiscord(message) {
if (!this.options.logToDiscord) return
await this.sendDiscordMessage({
embed: {
title: 'Vote Started',
color: 16761867,
fields: [
{
name: 'Options:',
value: `${message}`
}
]
2022-09-16 18:39:55 -05:00
},
timestamp: (new Date()).toISOString()
2022-09-16 18:33:21 -05:00
});
}
2022-09-16 18:10:05 -05:00
2022-03-19 18:11:24 -05:00
//removes a players vote if they disconnect from the sever
2022-08-13 05:05:40 -05:00
clearVote() {
2022-03-19 18:11:24 -05:00
const currentPlayers = this.server.players.map((p) => p.steamID);
2022-08-13 05:05:40 -05:00
for (const steamID in this.trackedVotes) {
if (!(currentPlayers.includes(steamID))) {
const vote = this.trackedVotes[ steamID ];
this.tallies[ vote ] -= 1;
delete this.trackedVotes[ steamID ];
2022-03-19 18:11:24 -05:00
}
2022-08-13 05:05:40 -05:00
}
2022-03-19 18:11:24 -05:00
}
//calculates the current winner(s) of the vote and returns thier strings in an array
2022-08-13 05:05:40 -05:00
get currentWinners() {
2022-03-19 18:11:24 -05:00
const ties = [];
2022-08-13 05:05:40 -05:00
2022-03-19 18:11:24 -05:00
let highestScore = -Infinity;
2022-08-13 05:05:40 -05:00
for (let choice in this.tallies) {
const score = this.tallies[ choice ];
if (score < highestScore)
2022-03-19 18:11:24 -05:00
continue;
2022-08-13 05:05:40 -05:00
else if (score > highestScore) {
2022-03-19 18:11:24 -05:00
highestScore = score;
ties.length = 0;
ties.push(choice);
}
else // equal
ties.push(choice);
}
2022-08-13 05:05:40 -05:00
return ties.map(i => this.nominations[ i ]);
2022-03-19 18:11:24 -05:00
}
2022-09-16 18:10:05 -05:00
}
2022-09-16 18:10:05 -05:00
function randomElement(array) {
return array[ Math.floor(Math.random() * array.length) ];
2022-09-06 08:32:48 -05:00
}
2022-09-16 18:10:05 -05:00
function formatChoice(choiceIndex, mapString, currentVotes, firstBroadcast) {
return `${choiceIndex}${mapString} ` + (!firstBroadcast ? `(${currentVotes})` : "");
// return `${choiceIndex + 1}❱ ${mapString} (${currentVotes} votes)`
}
function toMils(min) {
return min * 60 * 1000;
}