105 lines
3.0 KiB
C++
105 lines
3.0 KiB
C++
#include "fourasmodule.h"
|
|
#include "message.h"
|
|
#include "riddles.h"
|
|
#include <time.h>
|
|
#include <QHash>
|
|
#include <ctime>
|
|
|
|
FourasModule::FourasModule() : current(-1)
|
|
{
|
|
riddles = new Riddles("res/fouras.txt", "res/fourasAnswers.txt");
|
|
std::srand(time(NULL));
|
|
}
|
|
|
|
bool FourasModule::messageHandler(Message msg)
|
|
{
|
|
if(msg.command.compare(QString("PRIVMSG"), Qt::CaseInsensitive) != 0 || !msg.target.contains(getChan()))
|
|
return false;
|
|
|
|
if(msg.args.compare("!fouras") == 0 && riddles->getNbRiddles() != 0)
|
|
{
|
|
nbClues = 0;
|
|
current = rand() % riddles->getNbRiddles();
|
|
QStringList riddle = riddles->getRiddle(current);
|
|
answer = "";
|
|
for(QString line : riddle)
|
|
answer.append(say(line));
|
|
return true;
|
|
}
|
|
else if(msg.args.compare("!help fouras") == 0)
|
|
{
|
|
answer = privateSay("fouras commands : !fouras, !answer", msg.nick);
|
|
return true;
|
|
}
|
|
else if(msg.args.compare("!answer") == 0)
|
|
{
|
|
answer = say(QString("%1 - Perdu ! La réponse était : %2").arg(current).arg(riddles->getAnswer(current)));
|
|
current = -1;
|
|
return true;
|
|
}
|
|
else if(msg.args.compare("!repeat") == 0)
|
|
{
|
|
if(current != -1)
|
|
{
|
|
QStringList riddle = riddles->getRiddle(current);
|
|
answer = "";
|
|
for(QString line : riddle)
|
|
answer.append(say(line));
|
|
return true;
|
|
}
|
|
}
|
|
else if(msg.args.compare("!clue") == 0)
|
|
{
|
|
if(current != -1)
|
|
{
|
|
QString str = riddles->getAnswer(current);
|
|
|
|
// prepare the clue string
|
|
QString finalString = "_";
|
|
for(int i=0; i<str.size() - 1; ++i)
|
|
finalString.append(" _");
|
|
|
|
// initialize the rand function with the hash of the answer so the rand() function always reveal the characters in the same order
|
|
std::srand(qHash(str));
|
|
int nbRevealed = 0;
|
|
for(int i=0; i<nbClues; ++i)
|
|
{
|
|
// find a character to reveal
|
|
int id = std::rand()%str.size();
|
|
while(finalString[id*2] != QChar('_'))
|
|
id = std::rand()%str.size();
|
|
|
|
// reveal it
|
|
++nbRevealed;
|
|
finalString[id*2] = str[id];
|
|
|
|
// if too many letters are revealed, the players have lost the game
|
|
if(nbRevealed == str.size())
|
|
{
|
|
answer = say(QString("%1 - Perdu ! La réponse était : %2").arg(current).arg(str));
|
|
current = -1;
|
|
return true;
|
|
}
|
|
}
|
|
++nbClues;
|
|
answer = say(QString("Indice : ").append(finalString));
|
|
return true;
|
|
}
|
|
}
|
|
|
|
if(current != -1 && msg.args.contains(riddles->getAnswer(current), Qt::CaseInsensitive))
|
|
{
|
|
answer = say(QString("Bravo %1 ! la réponse était bien : %2").arg(msg.nick).arg(riddles->getAnswer(current)));
|
|
current = -1;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
QString FourasModule::getName()
|
|
{
|
|
return "fouras";
|
|
}
|
|
|