UNPKG

pockybot

Version:

Spark bot that handles team recognition

142 lines (141 loc) 6.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const trigger_1 = require("../../models/trigger"); const database_1 = require("../../models/database"); const command_1 = require("../../models/command"); const constants_1 = require("../../constants"); const xmlMessageParser_1 = require("../parsers/xmlMessageParser"); const location_action_1 = require("../../models/location-action"); const logger_1 = require("../logger"); const tableHelper_1 = require("../parsers/tableHelper"); class LocationWeight extends trigger_1.default { constructor(dbLocation, config) { super(); this.dbLocation = dbLocation; this.config = config; } isToTriggerOn(message) { if (!(this.config.checkRole(message.personId, database_1.Role.Admin) || this.config.checkRole(message.personId, database_1.Role.Config))) { return false; } let parsedMessage = xmlMessageParser_1.default.parseNonPegMessage(message); return parsedMessage.botId === constants_1.default.botId && parsedMessage.command.toLowerCase().startsWith(command_1.Command.LocationWeight); } async createMessage(message) { let parsedMessage = xmlMessageParser_1.default.parseNonPegMessage(message); let words = parsedMessage.command.trim().split(' ').filter(x => x !== ''); let response; if (words.length < 2) { return { markdown: `Please specify a command. Possible values are ${Object.values(location_action_1.LocationAction).join(', ')}` }; } let locations; try { locations = await this.dbLocation.getLocations(); } catch (error) { logger_1.Logger.error(`[Location.createMessage] Error getting locations ${error.message}`); return { markdown: 'Error getting locations' }; } switch (words[1].toLowerCase()) { case location_action_1.LocationAction.Get: response = await this.getLocationWeightMessage(locations); break; case location_action_1.LocationAction.Set: response = await this.setLocationWeight(words, locations); break; case location_action_1.LocationAction.Delete: response = await this.deleteLocationWeight(words, locations); break; default: response = `Unknown command. Possible values are ${Object.values(location_action_1.LocationAction).join(', ')}`; break; } return { markdown: response }; } async getLocationWeightMessage(locations) { const allConfig = this.config.getAllConfig(); const locationWeights = []; for (let i = 0; i < locations.length; i++) { for (let j = i + 1; j < locations.length; j++) { const config = this.getLocationWeightConfig(locations[i], locations[j], allConfig); if (config) { locationWeights.push({ location1: locations[i], location2: locations[j], weight: config.value }); } } } if (locationWeights.length === 0) { return 'No location weights set'; } let columnWidths = tableHelper_1.default.getColumnWidths(locationWeights, [ (x) => x.location1, (x) => x.location2, (x) => x.weight.toString() ], ['Location 1', 'Location 2', 'Weight']); let message = 'Here are the current location weights:\n```\n'; message += tableHelper_1.default.padString('Location 1', columnWidths[0]) + ' | ' + tableHelper_1.default.padString('Location 2', columnWidths[1]) + ' | Weight\n'; message += ''.padEnd(columnWidths[0], '-') + '-+-' + ''.padEnd(columnWidths[1], '-') + '-+-' + ''.padEnd(columnWidths[2], '-') + '\n'; locationWeights.forEach((config) => { message += config.location1.padEnd(columnWidths[0]) + ' | ' + config.location2.padEnd(columnWidths[1]) + ' | ' + config.weight + '\n'; }); message += '```'; return message; } getLocationWeightConfig(location1, location2, allConfig) { const location1To2 = `locationWeight${location1}to${location2}`.toLowerCase(); const location2To1 = `locationWeight${location2}to${location1}`.toLowerCase(); const configIndex = allConfig.findIndex(item => item.name.toLowerCase() === location1To2 || item.name.toLowerCase() === location2To1); if (configIndex === -1) { return null; } return allConfig[configIndex]; } async setLocationWeight(words, locations) { if (words.length !== 5) { return 'Please specify two locations and a weight'; } if (!locations.map(x => x.toLowerCase()).includes(words[2].toLowerCase())) { return `Location value "${words[2]}" is invalid`; } if (!locations.map(x => x.toLowerCase()).includes(words[3].toLowerCase())) { return `Location value "${words[3]}" is invalid`; } const value = Number(words[4]); if (isNaN(value)) { return 'Weight must be set to a number'; } if (value < 0) { return 'Weight should be greater than or equal to 0.'; } let configName = `locationWeight${words[2]}to${words[3]}`; const existingConfig = this.getLocationWeightConfig(words[2], words[3], this.config.getAllConfig()); if (existingConfig) { configName = existingConfig.name; } await this.config.setConfig(configName, value); return 'Location weight has been set'; } async deleteLocationWeight(words, locations) { if (words.length !== 4) { return 'You must specify a two location names to delete the weighting for'; } if (!locations.map(x => x.toLowerCase()).includes(words[2].toLowerCase())) { return `Location value "${words[2]}" is invalid`; } if (!locations.map(x => x.toLowerCase()).includes(words[3].toLowerCase())) { return `Location value "${words[3]}" is invalid`; } const existingConfig = this.getLocationWeightConfig(words[2], words[3], this.config.getAllConfig()); if (!existingConfig) { return `No weighting found for locations ${words[2]} and ${words[3]}`; } await this.config.deleteConfig(existingConfig.name); return 'Location weight has been deleted'; } } exports.default = LocationWeight;