UNPKG

pockybot

Version:

Spark bot that handles team recognition

58 lines (57 loc) 2.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DefaultPegService = void 0; class DefaultPegService { constructor(config, utilities, dbLocation) { this.config = config; this.utilities = utilities; this.dbLocation = dbLocation; } async getPegs(results) { const requireKeywords = this.config.getConfig('requireValues'); const keywords = this.config.getStringConfig('keyword'); const penaltyKeywords = this.config.getStringConfig('penaltyKeyword'); const locations = await this.dbLocation.getAllUserLocations(); return results.map(peg => { const categories = this.getKeywords(peg.comment, keywords); const isValid = this.utilities.pegValid(peg.comment, requireKeywords, keywords, penaltyKeywords); const receiverlocationIndex = locations.findIndex(item => item.userid === peg.receiverid); const receiverLocation = receiverlocationIndex === -1 ? null : locations[receiverlocationIndex].location; const senderLocationIndex = locations.findIndex(item => item.userid === peg.senderid); const senderLocation = senderLocationIndex === -1 ? null : locations[senderLocationIndex].location; const pegWeighting = !isValid ? 0 : this.getPegWeighting(senderLocation, receiverLocation); return { receiverId: peg.receiverid, receiverName: peg.receiver, receiverLocation, senderId: peg.senderid, senderName: peg.sender, senderLocation, comment: peg.comment, categories, isValid, pegWeighting }; }); } getKeywords(comment, keywords) { return keywords.filter(keyword => comment.toLowerCase().includes(keyword.toLowerCase())); } getPegWeighting(senderLocation, receiverLocation) { if (senderLocation == null || receiverLocation == null || senderLocation === receiverLocation) { return 1; } else { const senderToReceiver = `locationWeight${senderLocation}to${receiverLocation}`.toLowerCase(); const receiverToSender = `locationWeight${receiverLocation}to${senderLocation}`.toLowerCase(); const allConfig = this.config.getAllConfig(); const configIndex = allConfig.findIndex(item => item.name.toLowerCase() === senderToReceiver || item.name.toLowerCase() === receiverToSender); if (configIndex === -1) { const defaultConfig = this.config.getConfig('remoteLocationWeightingDefault'); return defaultConfig ? defaultConfig : 2; } return allConfig[configIndex].value; } } } exports.DefaultPegService = DefaultPegService;