From 51afa44cff35ab0a535606075ebd8825151e881a Mon Sep 17 00:00:00 2001
From: Benedikt Magnus <magnus@magnuscraft.de>
Date: Sat, 13 Nov 2021 22:44:06 +0100
Subject: [PATCH] Added "run assignment" moderation command.

---
 locale/de-DE.commands.json                    |  8 ++++++
 locale/de-DE.texts.json                       |  2 ++
 scripts/utility/localisation.ts               |  3 +++
 .../wichtelbot/message/handlingDefinition.ts  | 27 +++++++++++++++++--
 scripts/wichtelbot/message/messageHandler.ts  | 10 ++++++-
 5 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/locale/de-DE.commands.json b/locale/de-DE.commands.json
index f5db414..b258925 100644
--- a/locale/de-DE.commands.json
+++ b/locale/de-DE.commands.json
@@ -92,6 +92,14 @@
     "maybe": {
         "commands": ["vielleicht", "vllt", "eventuell", "evtl"]
     },
+    "moddingRunAssignment":
+    {
+        "commands": [
+            "Auslosen",
+            "ZiehungAusführen"
+        ],
+        "info": "Führt die Auslosung der Wichtel aus."
+    },
     "moddingStatus":
     {
         "commands": [
diff --git a/locale/de-DE.texts.json b/locale/de-DE.texts.json
index 0401117..b654f9c 100644
--- a/locale/de-DE.texts.json
+++ b/locale/de-DE.texts.json
@@ -1,4 +1,6 @@
 {
+    "assignmentError": "Die Auslosung ist fehlgeschlagen.",
+    "assignmentSuccessful": "Die Auslosung war erfolgreich.",
     "becameMember": "Herzlichen Glückwunsch, du hast dich erfolgreich zum Wichteln „{var.currentEventName}“ registriert.\nToll!\n\nWas nun? Nun wird gewartet, bis der Anmeldeschluss kommt. Dann wird gelost und du bekommst hier alle Informationen zu deinem Wichtel.\n\nDu hast einen Fehler gemacht oder etwas vergessen? Kein Problem, mit „ändern“ kannst du deine Angaben bis zum Anmeldeschluss jederzeit überarbeiten. (Achte allerdings darauf, dass alle Änderungen bis Registrierungsschluss abgeschlossen sein müssen, sonst bist du nicht registriert!)\n\nFrohes Hibbeln bis zum Auslosetag!",
     "changedInformation": "Angaben erfolgreich geändert. Du kannst jederzeit mittels „ändern“ die Angaben erneut anpassen.",
     "commandInfo": "`{command.name}`: {command.info}",
diff --git a/scripts/utility/localisation.ts b/scripts/utility/localisation.ts
index 6a5c812..746a216 100644
--- a/scripts/utility/localisation.ts
+++ b/scripts/utility/localisation.ts
@@ -27,6 +27,7 @@ interface Commands
     informationBothAnalogueAndDigital: CommandInfo;
     informationDigital: CommandInfo;
     maybe: CommandInfo;
+    moddingRunAssignment: CommandInfo;
     moddingStatus: CommandInfo;
     no: CommandInfo;
     registration: CommandInfo;
@@ -39,6 +40,8 @@ interface Commands
 // TODO: Documentation
 interface Texts
 {
+    assignmentError: TokenString;
+    assignmentSuccessful: TokenString;
     becameMember: TokenString;
     changedInformation: TokenString;
     commandInfo: TokenString;
diff --git a/scripts/wichtelbot/message/handlingDefinition.ts b/scripts/wichtelbot/message/handlingDefinition.ts
index 76b857e..401bd82 100644
--- a/scripts/wichtelbot/message/handlingDefinition.ts
+++ b/scripts/wichtelbot/message/handlingDefinition.ts
@@ -1,5 +1,6 @@
 import { CommandHandlerFunction, StateCommandHandlerFunction } from './handlingTools/handlerFunctions';
 import Localisation, { CommandInfo } from '../../utility/localisation';
+import { AssignmentModule } from './modules/assignmentModule';
 import { ComponentBuilder } from './handlingTools/componentBuilder';
 import Config from '../../utility/config';
 import GeneralModule from './modules/generalModule';
@@ -51,17 +52,23 @@ export default class HandlingDefinition
     protected generalModule: GeneralModule;
     protected informationModule: InformationModule;
     protected moderationModule: ModerationModule;
+    protected assignmentModule: AssignmentModule;
 
     private get maxShortMessageLength (): number
     {
         return Math.floor(Config.main.maxMessageLength / 2);
     }
 
-    constructor (generalModule: GeneralModule, informationModule: InformationModule, moderationModule: ModerationModule)
-    {
+    constructor (
+        generalModule: GeneralModule,
+        informationModule: InformationModule,
+        moderationModule: ModerationModule,
+        assignmentModule: AssignmentModule
+    ) {
         this.generalModule = generalModule;
         this.informationModule = informationModule;
         this.moderationModule = moderationModule;
+        this.assignmentModule = assignmentModule;
     }
 
     public stateCommands: StateCommandDefinition[] = [
@@ -490,6 +497,22 @@ export default class HandlingDefinition
     ];
 
     public moderatorCommands: CommandDefinition[] = [
+        {
+            commandInfo: Localisation.commands.moddingRunAssignment,
+            handlerFunction: async (message: Message): Promise<void> =>
+            {
+                const asssignmentResult = this.assignmentModule.runAssignment();
+
+                if (asssignmentResult)
+                {
+                    await this.generalModule.reply(message, Localisation.texts.assignmentSuccessful);
+                }
+                else
+                {
+                    await this.generalModule.reply(message, Localisation.texts.assignmentError);
+                }
+            }
+        },
         {
             commandInfo: Localisation.commands.moddingStatus,
             handlerFunction: async (message: Message): Promise<void> => this.moderationModule.sendStatus(message)
diff --git a/scripts/wichtelbot/message/messageHandler.ts b/scripts/wichtelbot/message/messageHandler.ts
index 4157447..fccb3af 100644
--- a/scripts/wichtelbot/message/messageHandler.ts
+++ b/scripts/wichtelbot/message/messageHandler.ts
@@ -1,5 +1,6 @@
 import { ChannelType, Message, State } from '../endpoint/definitions';
 import Localisation, { CommandInfo } from '../../utility/localisation';
+import { AssignmentModule } from './modules/assignmentModule';
 import { CommandHandlerFunction } from './handlingTools/handlerFunctions';
 import Config from '../../utility/config';
 import Database from '../database/database';
@@ -31,6 +32,7 @@ export default class MessageHandler
     protected generalModule: GeneralModule;
     protected informationModule: InformationModule;
     protected moderationModule: ModerationModule;
+    protected assignmentModule: AssignmentModule;
 
     // In private messages:
     protected stateCommands = new StateCommandMap();
@@ -59,8 +61,14 @@ export default class MessageHandler
         this.generalModule = new GeneralModule(database);
         this.informationModule = new InformationModule(database);
         this.moderationModule = new ModerationModule(database);
+        this.assignmentModule = new AssignmentModule(database);
 
-        this.handlingDefinition = new HandlingDefinition(this.generalModule, this.informationModule, this.moderationModule);
+        this.handlingDefinition = new HandlingDefinition(
+            this.generalModule,
+            this.informationModule,
+            this.moderationModule,
+            this.assignmentModule
+        );
 
         this.applyHandlingDefinition();
     }
-- 
GitLab