diff --git a/app/app.pro b/app/app.pro index 0f6a704..5958ecc 100644 --- a/app/app.pro +++ b/app/app.pro @@ -11,8 +11,10 @@ INCLUDEPATH += ../ircbot SOURCES = main.cpp \ regismodule.cpp \ - sparrowmodule.cpp + sparrowmodule.cpp \ + vocab.cpp HEADERS += \ regismodule.h \ - sparrowmodule.h + sparrowmodule.h \ + vocab.h diff --git a/app/regismodule.cpp b/app/regismodule.cpp index 65b6a07..8ba1a20 100644 --- a/app/regismodule.cpp +++ b/app/regismodule.cpp @@ -1,47 +1,16 @@ #include "regismodule.h" #include "message.h" -#include -RegisModule::RegisModule() : regisEnable(false) -{ - std::srand(time(NULL)); - noms.push_back("Le chat"); - noms.push_back("La passoire"); - noms.push_back("Le canapé"); - noms.push_back("L'ivrogne"); - noms.push_back("Robert"); - noms.push_back("Le policier"); - noms.push_back("une pastèque"); - noms.push_back("un ordinateur"); - noms.push_back("une voiture"); - noms.push_back("le chien"); - verbes.push_back("troue"); - verbes.push_back("aime"); - verbes.push_back("jette"); - verbes.push_back("saute au dessus de"); - verbes.push_back("tourne autour de"); - verbes.push_back("s'assied sur"); - verbes.push_back("pense à"); - verbes.push_back("vient de trouver"); - verbes.push_back("se frotte à"); - adjectifs.push_back("noir"); - adjectifs.push_back("ivre"); - adjectifs.push_back("en mauvais état"); - adjectifs.push_back("blanc"); - adjectifs.push_back("durci"); - adjectifs.push_back("en fer"); - adjectifs.push_back("rugueux"); -} +RegisModule::RegisModule() : voc("../res/vocab.txt"), regisEnable(false) {} bool RegisModule::messageHandler(Message msg) { if(msg.args.contains("régis ?")) { - int nom1 = std::rand() % noms.size(); - int nom2 = std::rand() % noms.size(); - int adjectif = std::rand() % adjectifs.size(); - int verbe = std::rand() % verbes.size(); - answer = say(noms[nom1] + " " + adjectifs[adjectif] + " " + verbes[verbe] + " " + noms[nom2] + "."); + answer = say(voc.getRandNom() + " " + + voc.getRandAdjectif() + " " + + voc.getRandVerbe() + " " + + voc.getRandNom() + "."); return true; } else if(msg.args.compare("!initrégis") == 0) diff --git a/app/regismodule.h b/app/regismodule.h index 1f78655..105a020 100644 --- a/app/regismodule.h +++ b/app/regismodule.h @@ -4,13 +4,12 @@ #include "module.h" #include #include +#include "vocab.h" class RegisModule : public Module { bool regisEnable; - std::vector noms; - std::vector verbes; - std::vector adjectifs; + Vocab voc; public: RegisModule(); virtual bool messageHandler(Message msg); diff --git a/app/vocab.cpp b/app/vocab.cpp new file mode 100644 index 0000000..6c0d313 --- /dev/null +++ b/app/vocab.cpp @@ -0,0 +1,115 @@ +#include "vocab.h" +#include +#include + +Vocab::Vocab() +{ + std::srand(time(NULL)); +} + +Vocab::Vocab(QString filename) +{ + std::srand(time(NULL)); + load(filename); +} + +void Vocab::load(QString filename) +{ + QFile file = QFile(filename); + if(file.isReadable()) + file.open(QIODevice::ReadOnly); + if(file.isOpen()) + { + char[512] buf; + int len; + while(len = file.readLine(buf, 512)) + { + if(len > 4) + { + buf[len-1] = 0; + switch(buf[0]) + { + case 'n': + addNom(QString(buf+2)); + break; + case 'v': + addVerbe(QString(buf+2)); + break; + case 'a': + addAdjectif(QString(buf+2)); + break; + } + } + } + file.close(); + } +} + +void Vocab::save(QString filename) +{ + QFile file = QFile(filename); + if(file.isWritable()) + file.open(QIODevice::WriteOnly); + if(file.isOpen()) + { + file.write(QString("#NOMS :\n")); + for(QString n : noms) + file.write(QString("n:%1\n").arg(n)); + file.write(QString("\n#VERBES :\n")); + for(QString v : verbes) + file.write(QString("v:%1\n").arg(v)); + file.write(QString("\n#ADJECTIFS :\n")); + for(QString a : adjectifs) + file.write(QString("a:%1\n").arg(a)); + file.close(); + } +} + +void Vocab::append(Vocab& voc) +{ + for(QString n : voc.noms) + addNom(n); + for(QString v : voc.verbes) + addVerbe(v); + for(QString a : voc.adjectifs) + addAdjectif(a); +} + +void Vocab::addNom(QString nom) +{ + noms.push_back(nom); +} + +void Vocab::addAdjectif(QString adj) +{ + adjectifs.push_back(adj); +} + +void Vocab::addVerbe(QString ver) +{ + verbes.push_back(ver); +} + +QString Vocab::getRandNom() +{ + if(noms.size()) + return noms[std::rand()%noms.size()]; + else + return ""; +} + +QString Vocab::getRandAdjectif() +{ + if(adjectifs.size()) + return adjectifs[std::rand()%adjectifs.size()]; + else + return ""; +} + +QString Vocab::getRandVerbe() +{ + if(verbes.size()) + return verbes[std::rand()%verbes.size()]; + else + return ""; +} diff --git a/app/vocab.h b/app/vocab.h new file mode 100644 index 0000000..caab934 --- /dev/null +++ b/app/vocab.h @@ -0,0 +1,28 @@ +#ifndef VOCAB_H +#define VOCAB_H + +#include +#include + +class Vocab +{ + std::vector noms; + std::vector verbes; + std::vector adjectifs; +public: + Vocab(); + Vocab(QString filename); + void load(QString filename); + void save(QString filename); + void merge(Vocab& voc); + + void addNom(QString nom); + void addAdjectif(QString adj); + void addVerbe(QString ver); + + QString getRandNom(); + QString getRandAdjectif(); + QString getRandVerbe(); +}; + +#endif // VOCAB_H diff --git a/res/vocab.txt b/res/vocab.txt new file mode 100644 index 0000000..d373d25 --- /dev/null +++ b/res/vocab.txt @@ -0,0 +1,28 @@ +n:Le chat +n:La passoire +n:Le canapé +n:L'ivrogne +n:Robert +n:Le policier +n:une pastèque +n:un ordinateur +n:une voiture +n:le chien + +v:troue +v:aime +v:jette +v:saute au dessus de +v:tourne autour de +v:s'assied sur +v:pense à +v:vient de trouver +v:se frotte à + +a:noir +a:ivre +a:en mauvais état +a:blanc +a:durci +a:en fer +a:rugueux \ No newline at end of file