From a828d1dc09a6af54304b061d315af87a41959fdc Mon Sep 17 00:00:00 2001 From: Sayak Mukhopadhyay Date: Fri, 14 Jul 2017 00:38:26 +0530 Subject: [PATCH] Completed BGS report --- src/modules/discord/client.ts | 3 +- src/modules/discord/commands/bgsReport.ts | 56 +++++++++++-------- .../{factionstatus.ts => factionStatus.ts} | 0 src/modules/discord/commands/index.ts | 5 +- .../{systemstatus.ts => systemStatus.ts} | 0 5 files changed, 38 insertions(+), 26 deletions(-) rename src/modules/discord/commands/{factionstatus.ts => factionStatus.ts} (100%) rename src/modules/discord/commands/{systemstatus.ts => systemStatus.ts} (100%) diff --git a/src/modules/discord/client.ts b/src/modules/discord/client.ts index 2dacbb8..0bcaac4 100644 --- a/src/modules/discord/client.ts +++ b/src/modules/discord/client.ts @@ -17,7 +17,7 @@ import * as discord from 'discord.js'; import { DiscordSecrets } from '../../secrets'; import { Responses } from './responseDict'; -import { Hi, Help, MyGuild, BGSRole, AdminRoles, ForbiddenRoles, BGSChannel, MonitorSystems, MonitorFactions, SystemStatus, FactionStatus } from './commands'; +import { Hi, Help, MyGuild, BGSRole, AdminRoles, ForbiddenRoles, BGSChannel, MonitorSystems, MonitorFactions, SystemStatus, FactionStatus, BGSReport } from './commands'; export class DiscordClient { public client: discord.Client; @@ -71,5 +71,6 @@ export class DiscordClient { this.commandsMap.set("monitorfactions", MonitorFactions); this.commandsMap.set("systemstatus", SystemStatus); this.commandsMap.set("factionstatus", FactionStatus); + this.commandsMap.set("bgsreport", BGSReport); } } diff --git a/src/modules/discord/commands/bgsReport.ts b/src/modules/discord/commands/bgsReport.ts index 81fad74..6645e31 100644 --- a/src/modules/discord/commands/bgsReport.ts +++ b/src/modules/discord/commands/bgsReport.ts @@ -20,7 +20,7 @@ import { Responses } from '../responseDict'; import { DB } from '../../../db/index'; import { Access } from './../access'; -export class SystemStatus { +export class BGSReport { db: DB; constructor() { this.db = App.db; @@ -51,15 +51,21 @@ export class SystemStatus { .then(guild => { if (guild) { let primaryFactions = []; + let secondaryFactions = []; guild.monitor_factions.forEach(faction => { if (faction.primary) { primaryFactions.push(faction.faction_name); + } else { + secondaryFactions.push(faction.faction_name); } }); let primarySystems = []; + let secondarySystems = [] guild.monitor_systems.forEach(system => { if (system.primary) { primarySystems.push(system.system_name); + } else { + secondarySystems.push(system.system_name); } }); this.db.model.faction.find({ faction_name: { $in: primaryFactions } }) @@ -69,11 +75,12 @@ export class SystemStatus { embed.setTitle("BGS REPORT"); embed.setColor([255, 100, 255]); factions.forEach(faction => { + let factionAcronym = this.acronym(faction.faction_name); embed.addField(faction.faction_name, "------------", false); faction.faction_presence.forEach((faction) => { - let factionDetail = ""; - factionDetail += `State : ${faction.state}\n`; - factionDetail += `Influence : ${(faction.influence * 100).toFixed(1)}%\n`; + let systemReport = ""; + systemReport += `Current ${factionAcronym} Influence : ${(faction.influence * 100).toFixed(1)}\n`; + systemReport += `Current ${factionAcronym} State : ${faction.state}\n`; let pendingStates: string = ""; if (faction.pending_states.length === 0) { pendingStates = "None"; @@ -86,25 +93,22 @@ export class SystemStatus { } }); } - factionDetail += `Pending States : ${pendingStates}\n`; - let recoveringStates: string = ""; - if (faction.recovering_states.length === 0) { - recoveringStates = "None"; - } else { - faction.recovering_states.forEach((recoveringState, index, factionRecoveringState) => { - let trend = this.getTrendIcon(recoveringState.trend); - recoveringStates = `${recoveringStates}${recoveringState.state}${trend}`; - if (index !== factionRecoveringState.length - 1) { - recoveringStates = `${recoveringStates}, ` - } - }) - } - factionDetail += `Recovering States : ${recoveringStates}`; - let systemName = faction.system_name; - // if (system.system_faction === factionName) { - // factionName += "* CONTROLLING FACTION"; - // } - embed.addField(systemName, factionDetail, false) + systemReport += `Pending ${factionAcronym} States : ${pendingStates}\n`; + secondaryFactions.forEach(otherFaction => { + this.db.model.faction.findOne({ faction_name: otherFaction }) + .then(otherFaction => { + if (otherFaction) { + let systems = otherFaction.faction_presence; + let indexOfSystem = systems.findIndex(x => x.system_name === faction.system_name); + if (indexOfSystem !== -1) { + let factionAcronym = this.acronym(otherFaction.faction_name); + let otherSystem = otherFaction.faction_presence[indexOfSystem]; + systemReport += `Current ${factionAcronym} Influence : ${(otherSystem.influence * 100).toFixed(1)} (Currently in ${otherSystem.state})\n`; + } + } + }); + }); + embed.addField(faction.system_name, systemReport); }); }); embed.setTimestamp(new Date()); @@ -218,4 +222,10 @@ export class SystemStatus { return "↔️"; } } + + private acronym(text) { + return text + .split(/\s/) + .reduce((accumulator, word) => accumulator + word.charAt(0), ''); + } } diff --git a/src/modules/discord/commands/factionstatus.ts b/src/modules/discord/commands/factionStatus.ts similarity index 100% rename from src/modules/discord/commands/factionstatus.ts rename to src/modules/discord/commands/factionStatus.ts diff --git a/src/modules/discord/commands/index.ts b/src/modules/discord/commands/index.ts index 30237c3..ff4feb9 100644 --- a/src/modules/discord/commands/index.ts +++ b/src/modules/discord/commands/index.ts @@ -23,5 +23,6 @@ export * from './forbiddenRoles'; export * from './bgsChannel'; export * from './monitorSystems'; export * from './monitorFactions'; -export * from './systemstatus'; -export * from './factionstatus'; +export * from './systemStatus'; +export * from './factionStatus'; +export * from './bgsReport'; diff --git a/src/modules/discord/commands/systemstatus.ts b/src/modules/discord/commands/systemStatus.ts similarity index 100% rename from src/modules/discord/commands/systemstatus.ts rename to src/modules/discord/commands/systemStatus.ts