From 397bc53610d835e1694f675638f695a4e601d04b Mon Sep 17 00:00:00 2001
From: Adrien NUNES <anunes.etudiant@gmail.com>
Date: Fri, 24 Nov 2023 03:06:26 +0100
Subject: [PATCH] Aled

---
 commands/CommandPourquoi.js |  6 +--
 commands/CommandQui.js      |  6 +--
 commands/InformeMoi.js      | 42 ++-------------------
 database/RandomSentence.js  | 75 +++++++++++++++++++++++++++++++++++++
 4 files changed, 83 insertions(+), 46 deletions(-)
 create mode 100644 database/RandomSentence.js

diff --git a/commands/CommandPourquoi.js b/commands/CommandPourquoi.js
index 746d6e9..e9d493f 100644
--- a/commands/CommandPourquoi.js
+++ b/commands/CommandPourquoi.js
@@ -1,5 +1,5 @@
-const complotDB = require('../database/ComplotDB.js');
-const {Category} = require('../database/Category.js');
+const {getRandomReason} = require('../database/RandomSentence.js');
+
 
 class CommandPourquoi{
     constructor(client){
@@ -9,7 +9,7 @@ class CommandPourquoi{
     }
 
     getRandomPourquoi(){
-        const reason = complotDB.getRandom(Category.REASON);
+        const reason = getRandomReason();
         return this.sentences[Math.floor(Math.random()*this.sentences.length)].replace('<why>', reason);
     }
 
diff --git a/commands/CommandQui.js b/commands/CommandQui.js
index c17819b..c2d59e1 100644
--- a/commands/CommandQui.js
+++ b/commands/CommandQui.js
@@ -1,6 +1,5 @@
 
-const complotDB = require('../database/ComplotDB.js');
-const {Category} = require('../database/Category.js');
+const {getRandomActor} = require('../database/RandomSentence.js');
 
 class CommandQui{
     constructor(client){
@@ -10,8 +9,7 @@ class CommandQui{
     }
 
     getRandomQui(){
-        const actor = complotDB.getRandom(Category.ACTOR);
-
+        const actor = getRandomActor();
         return this.sentences[Math.floor(Math.random()*this.sentences.length)].replace('<who>', actor);
     }
 
diff --git a/commands/InformeMoi.js b/commands/InformeMoi.js
index 477bb8e..97249b4 100644
--- a/commands/InformeMoi.js
+++ b/commands/InformeMoi.js
@@ -1,58 +1,22 @@
 
-const complotDB = require('../database/ComplotDB.js');
+const {getRandomComplot, getRandomWelcome} = require('../database/RandomSentence.js');
 const channelsID = require('../channels_conf.json');
-const {Category} = require('../database/Category.js');
 
 
 class InformeMoi{
     constructor(client){
         this.client = client;
         this.channels = this.client.channels.cache;
-        this.actorRE = new RegExp('<actor>', 'gi'); 
-        this.wantRE = new RegExp('<want>', 'gi'); 
-        this.reasonRE = new RegExp('<reason>', 'gi'); 
-        this.sourceRE = new RegExp('<source>', 'gi'); 
-        this.actionRE = new RegExp('<action>', 'gi'); 
-    }
-
-    fillAllRandomObject(sentence, regularExpr, category){
-        const matches = sentence.matchAll(regularExpr);
-        let objectString = "";
-        let newSentence = sentence;
-        for(let match of matches){
-            objectString = complotDB.getRandom(category);
-            newSentence = newSentence.replace(match, objectString);
-        }
-        
-        return newSentence;
-    }
-
-    getRandomSentenceFilled(sentence){
-        sentence = this.fillAllRandomObject(sentence, this.actorRE, Category.ACTOR);
-        sentence = this.fillAllRandomObject(sentence, this.actionRE, Category.ACTION);
-        sentence = this.fillAllRandomObject(sentence, this.reasonRE, Category.REASON);
-        sentence = this.fillAllRandomObject(sentence, this.sourceRE, Category.SOURCE);
-        
-        sentence = sentence.replaceAll(this.wantRE, 'veut'); /**Todo, change */
-        return sentence;
-    }
-
-    getRandomComplot(){
-        return this.getRandomSentenceFilled(complotDB.getRandom(Category.SENTENCE));
-    }
-
-    getRandomWelcome(){
-        return this.getRandomSentenceFilled(complotDB.getRandom(Category.WELCOME));
     }
 
     sayWelcome(userID){
-        let message = this.getRandomWelcome().replace('<new_user>', `<@${userID}>`);
+        let message = getRandomWelcome().replace('<new_user>', `<@${userID}>`);
         this.client.channels.cache.get(channelsID.welcome).send(message);
     }
 
     parse(message){
         if(this.shouldRespond(message)){
-            message.reply(this.getRandomComplot());
+            message.reply(getRandomComplot());
         }
     }
 
diff --git a/database/RandomSentence.js b/database/RandomSentence.js
new file mode 100644
index 0000000..5ef7d4c
--- /dev/null
+++ b/database/RandomSentence.js
@@ -0,0 +1,75 @@
+
+const complotDB = require('./ComplotDB.js');
+const { Category } = require('./Category.js');
+
+const actorRE = new RegExp('<actor>', 'gi');
+const wantRE = new RegExp('<want>', 'gi');
+const reasonRE = new RegExp('<reason>', 'gi');
+const sourceRE = new RegExp('<source>', 'gi');
+const actionRE = new RegExp('<action>', 'gi');
+
+const MAX_ITERATIONS = 10;
+
+function fillAllRandomObject(sentence, regularExpr, category){
+    const matches = sentence.matchAll(regularExpr);
+    let objectString = "";
+    let newSentence = sentence;
+    for (let match of matches) {
+        objectString = complotDB.getRandom(category);
+        newSentence = newSentence.replace(match, objectString);
+    }
+
+    return newSentence;
+}
+
+function shouldBeFilled(sentence) {
+    return [actorRE, reasonRE, sourceRE, actionRE].some((re) =>{
+        const b = re.test(sentence)
+        if(b){
+            const matches = sentence.matchAll(re);
+            for(let match of matches) console.log(match);
+        }
+    });
+}
+
+function getRandomSentenceFilled(sentence){
+    let it = 0;
+    while(shouldBeFilled(sentence)){
+        sentence = fillAllRandomObject(sentence, actorRE, Category.ACTOR);
+        sentence = fillAllRandomObject(sentence, actionRE, Category.ACTION);
+        sentence = fillAllRandomObject(sentence, reasonRE, Category.REASON);
+        sentence = fillAllRandomObject(sentence, sourceRE, Category.SOURCE);
+
+        it++;
+        if(it >= MAX_ITERATIONS) break;
+    }
+    
+
+    sentence = sentence.replaceAll(wantRE, 'veut');
+    return sentence;
+}
+
+function getRandomComplot(){
+    return getRandomSentenceFilled(complotDB.getRandom(Category.SENTENCE));
+}
+
+function getRandomActor(){
+    return getRandomSentenceFilled(complotDB.getRandom(Category.ACTOR));
+}
+
+function getRandomReason(){
+    return getRandomSentenceFilled(complotDB.getRandom(Category.REASON));
+}
+
+function getRandomWelcome(){
+    return getRandomSentenceFilled(complotDB.getRandom(Category.WELCOME));
+}
+
+
+module.exports = {
+    getRandomSentenceFilled,
+    getRandomComplot,
+    getRandomActor,
+    getRandomReason,
+    getRandomWelcome
+}
\ No newline at end of file
-- 
GitLab