2020-08-20 15:21:03 -05:00
|
|
|
import { SquadLayerFilter } from 'core/squad-layers';
|
|
|
|
import { COPYRIGHT_MESSAGE } from 'core/constants';
|
2020-09-09 03:31:48 -05:00
|
|
|
import { NEW_GAME, CHAT_MESSAGE } from 'squad-server/events';
|
2020-06-13 09:33:44 -05:00
|
|
|
|
|
|
|
import MapVote from './mapvote.js';
|
|
|
|
|
2020-08-20 15:21:03 -05:00
|
|
|
export default {
|
|
|
|
name: 'mapvote-123',
|
2020-08-22 08:32:39 -05:00
|
|
|
description:
|
2020-09-10 08:17:27 -05:00
|
|
|
'The <code>mapvote-123</code> plugin provides map voting functionality. This variant of map voting allows admins to specify ' +
|
2020-08-22 08:32:39 -05:00
|
|
|
'a small number of maps which are numbered and announced in admin broadcasts. Players can then vote for the map ' +
|
|
|
|
'their choice by typing the corresponding map number into chat.' +
|
|
|
|
'\n\n' +
|
|
|
|
'Player Commands:\n' +
|
2020-09-10 08:17:27 -05:00
|
|
|
' * <code>!mapvote help</code> - Show other commands players can use.\n' +
|
|
|
|
' * <code>!mapvote results</code> - Show the results of the current map vote.\n' +
|
|
|
|
' * <code><layer number></code> - Vote for a layer using the layer number.\n' +
|
2020-08-22 08:32:39 -05:00
|
|
|
'\n\n' +
|
|
|
|
'Admin Commands (Admin Chat Only):\n' +
|
2020-09-10 08:17:27 -05:00
|
|
|
' * <code>!mapvote start <layer name 1>, <layer name 2>, ...</code> - Start a new map vote with the specified maps.\n' +
|
|
|
|
' * <code>!mapvote restart</code> - Restarts the map vote with the same layers.\n' +
|
|
|
|
' * <code>!mapvote end</code> - End the map vote and announce the winner.\n' +
|
|
|
|
' * <code>!mapvote destroy</code> - End the map vote without announcing the winner.\n',
|
2020-08-20 15:21:03 -05:00
|
|
|
|
2020-08-22 10:54:05 -05:00
|
|
|
defaultEnabled: false,
|
2020-08-20 15:21:03 -05:00
|
|
|
optionsSpec: {
|
|
|
|
minVoteCount: {
|
|
|
|
required: false,
|
2020-09-10 08:17:27 -05:00
|
|
|
description: 'The minimum number of votes required for the vote to succeed.',
|
2020-08-20 15:21:03 -05:00
|
|
|
default: null,
|
2020-09-10 08:17:27 -05:00
|
|
|
example: 3
|
2020-06-13 09:33:44 -05:00
|
|
|
}
|
2020-08-20 15:21:03 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
init: async (server, options) => {
|
|
|
|
let mapvote = null;
|
|
|
|
|
2020-09-09 03:31:48 -05:00
|
|
|
server.on(NEW_GAME, () => {
|
2020-08-20 15:21:03 -05:00
|
|
|
mapvote = null;
|
|
|
|
});
|
|
|
|
|
2020-09-09 03:31:48 -05:00
|
|
|
server.on(CHAT_MESSAGE, async (info) => {
|
2020-08-20 15:21:03 -05:00
|
|
|
const voteMatch = info.message.match(/^([0-9])/);
|
|
|
|
if (voteMatch) {
|
|
|
|
if (!mapvote) return;
|
|
|
|
try {
|
|
|
|
const layerName = await mapvote.makeVoteByNumber(info.steamID, parseInt(voteMatch[1]));
|
|
|
|
await server.rcon.warn(info.steamID, `You voted for ${layerName}.`);
|
|
|
|
} catch (err) {
|
|
|
|
await server.rcon.warn(info.steamID, err.message);
|
|
|
|
}
|
|
|
|
await server.rcon.warn(info.steamID, `Powered by: ${COPYRIGHT_MESSAGE}`);
|
|
|
|
}
|
2020-06-13 09:33:44 -05:00
|
|
|
|
2020-08-20 15:21:03 -05:00
|
|
|
const commandMatch = info.message.match(/^!mapvote ?(.*)/);
|
|
|
|
if (commandMatch) {
|
|
|
|
if (commandMatch[1].startsWith('start')) {
|
|
|
|
if (info.chat !== 'ChatAdmin') return;
|
|
|
|
|
|
|
|
if (mapvote) {
|
|
|
|
await server.rcon.warn(info.steamID, 'A mapvote has already begun.');
|
|
|
|
} else {
|
|
|
|
mapvote = new MapVote(
|
|
|
|
server,
|
|
|
|
SquadLayerFilter.buildFromDidYouMeanList(
|
|
|
|
commandMatch[1].replace('start ', '').split(', ')
|
|
|
|
),
|
|
|
|
{ minVoteCount: options.minVoteCount }
|
|
|
|
);
|
|
|
|
|
|
|
|
mapvote.on('NEW_WINNER', async (results) => {
|
|
|
|
await server.rcon.broadcast(
|
|
|
|
`New Map Vote Winner: ${results[0].layer.layer}. Participate in the map vote by typing "!mapvote help" in chat.`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
await server.rcon.broadcast(
|
|
|
|
`A new map vote has started. Participate in the map vote by typing "!mapvote help" in chat. Map options to follow...`
|
|
|
|
);
|
|
|
|
await server.rcon.broadcast(
|
|
|
|
mapvote.squadLayerFilter
|
|
|
|
.getLayerNames()
|
|
|
|
.map((layerName, key) => `${key + 1} - ${layerName}`)
|
|
|
|
.join(', ')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mapvote) {
|
|
|
|
await server.rcon.warn(info.steamID, 'A map vote has not begun.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (commandMatch[1] === 'restart') {
|
|
|
|
if (info.chat !== 'ChatAdmin') return;
|
2020-06-13 09:33:44 -05:00
|
|
|
|
2020-08-20 15:21:03 -05:00
|
|
|
mapvote = new MapVote(server, mapvote.squadLayerFilter, {
|
|
|
|
minVoteCount: options.minVoteCount
|
|
|
|
});
|
|
|
|
|
|
|
|
mapvote.on('NEW_WINNER', async (results) => {
|
2020-06-13 09:33:44 -05:00
|
|
|
await server.rcon.broadcast(
|
2020-08-20 15:21:03 -05:00
|
|
|
`New Map Vote Winner: ${results[0].layer}. Participate in the map vote by typing "!mapvote help" in chat.`
|
2020-06-13 09:33:44 -05:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
await server.rcon.broadcast(
|
2020-07-14 13:45:00 -05:00
|
|
|
`A new map vote has started. Participate in the map vote by typing "!mapvote help" in chat. Map options to follow...`
|
2020-06-13 09:33:44 -05:00
|
|
|
);
|
2020-08-01 05:40:16 -05:00
|
|
|
await server.rcon.broadcast(
|
|
|
|
mapvote.squadLayerFilter
|
|
|
|
.getLayerNames()
|
|
|
|
.map((layerName, key) => `${key + 1} - ${layerName}`)
|
|
|
|
.join(', ')
|
|
|
|
);
|
2020-08-20 15:21:03 -05:00
|
|
|
return;
|
2020-06-13 09:33:44 -05:00
|
|
|
}
|
|
|
|
|
2020-08-20 15:21:03 -05:00
|
|
|
if (commandMatch[1] === 'end') {
|
|
|
|
if (info.chat !== 'ChatAdmin') return;
|
2020-06-13 09:33:44 -05:00
|
|
|
|
2020-08-20 15:21:03 -05:00
|
|
|
const results = mapvote.getResults();
|
2020-06-13 09:33:44 -05:00
|
|
|
|
2020-08-20 15:21:03 -05:00
|
|
|
if (results.length === 0)
|
|
|
|
await server.rcon.broadcast(`No layer gained enough votes to win.`);
|
|
|
|
else
|
|
|
|
await server.rcon.broadcast(`${mapvote.getResults()[0].layer.layer} won the mapvote!`);
|
2020-06-13 09:33:44 -05:00
|
|
|
|
2020-08-20 15:21:03 -05:00
|
|
|
mapvote = null;
|
|
|
|
return;
|
|
|
|
}
|
2020-06-13 09:33:44 -05:00
|
|
|
|
2020-08-20 15:21:03 -05:00
|
|
|
if (commandMatch[1] === 'destroy') {
|
|
|
|
if (info.chat !== 'ChatAdmin') return;
|
|
|
|
mapvote = null;
|
|
|
|
return;
|
|
|
|
}
|
2020-06-13 09:33:44 -05:00
|
|
|
|
2020-08-20 15:21:03 -05:00
|
|
|
if (commandMatch[1] === 'help') {
|
|
|
|
await server.rcon.warn(info.steamID, 'To vote type the layer number into chat:');
|
|
|
|
for (const layer of mapvote.squadLayerFilter.getLayers()) {
|
|
|
|
await server.rcon.warn(info.steamID, `${layer.layerNumber} - ${layer.layer}`);
|
|
|
|
}
|
2020-06-13 09:33:44 -05:00
|
|
|
|
2020-08-20 15:21:03 -05:00
|
|
|
if (options.minVoteCount !== null)
|
|
|
|
await server.rcon.warn(
|
|
|
|
info.steamID,
|
|
|
|
`${options.minVoteCount} votes need to be made for a winner to be selected.`
|
|
|
|
);
|
2020-06-13 09:33:44 -05:00
|
|
|
|
|
|
|
await server.rcon.warn(
|
|
|
|
info.steamID,
|
2020-08-20 15:21:03 -05:00
|
|
|
'To see current results type into chat: !mapvote results'
|
2020-06-13 09:33:44 -05:00
|
|
|
);
|
2020-08-20 15:21:03 -05:00
|
|
|
}
|
2020-06-13 09:33:44 -05:00
|
|
|
|
2020-08-20 15:21:03 -05:00
|
|
|
if (commandMatch[1] === 'results') {
|
|
|
|
const results = mapvote.getResults();
|
|
|
|
|
|
|
|
if (results.length === 0) {
|
|
|
|
await server.rcon.warn(info.steamID, 'No one has voted yet.');
|
|
|
|
} else {
|
|
|
|
await server.rcon.warn(info.steamID, 'The current vote counts are as follows:');
|
|
|
|
for (const result of results) {
|
|
|
|
await server.rcon.warn(
|
|
|
|
info.steamID,
|
|
|
|
`${result.layer.layerNumber} - ${result.layer.layer} (${result.votes} vote${
|
|
|
|
result.votes > 1 ? 's' : ''
|
|
|
|
})`
|
|
|
|
);
|
|
|
|
}
|
2020-06-13 09:33:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-20 15:21:03 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|