diff options
Diffstat (limited to 'irc/botircdiceparser.cpp')
| -rw-r--r-- | irc/botircdiceparser.cpp | 207 |
1 files changed, 178 insertions, 29 deletions
diff --git a/irc/botircdiceparser.cpp b/irc/botircdiceparser.cpp index 39bac4e..87d47ff 100644 --- a/irc/botircdiceparser.cpp +++ b/irc/botircdiceparser.cpp @@ -1,14 +1,33 @@ -#include "mainwindow.h" -#include "ui_mainwindow.h" +/*************************************************************************** +* Copyright (C) 2014 by Renaud Guezennec * +* http://www.rolisteam.org/contact * +* * +* This file is part of DiceParser * +* * +* DiceParser is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License for more details. * +* * +* You should have received a copy of the GNU General Public License * +* along with this program; if not, write to the * +* Free Software Foundation, Inc., * +* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * +***************************************************************************/ +#include "botircdiceparser.h" #include <math.h> #include <QDebug> +#include <QString> -MainWindow::MainWindow(QWidget *parent) : - QMainWindow(parent), - ui(new Ui::MainWindow) +BotIrcDiceParser::BotIrcDiceParser(QObject *parent) : + QObject(parent) { - ui->setupUi(this); // Create socket m_socket = new QTcpSocket(this); @@ -19,27 +38,24 @@ MainWindow::MainWindow(QWidget *parent) : connect(m_socket, SIGNAL(readyRead()), this, SLOT(readData())); connect(m_socket,SIGNAL(connected()),this,SLOT(authentificationProcess())); connect(m_socket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(errorOccurs(QAbstractSocket::SocketError))); - connect(ui->m_connectButton, SIGNAL(clicked()), this, SLOT(connectToServer())); - connect(ui->m_disconnectButton, SIGNAL(clicked()), this, SLOT(disconnectFromServer())); - connect(ui->m_joinButton, SIGNAL(clicked()), this, SLOT(joinChannel())); } -MainWindow::~MainWindow() +BotIrcDiceParser::~BotIrcDiceParser() { - delete ui; + // delete ui; } -void MainWindow::connectToServer() +void BotIrcDiceParser::connectToServer() { qDebug() << "start connection"; m_socket->connectToHost(QString("irc.freenode.net"), 8001); } -void MainWindow::errorOccurs(QAbstractSocket::SocketError) +void BotIrcDiceParser::errorOccurs(QAbstractSocket::SocketError) { qDebug() << "ERROR" << m_socket->errorString(); } -void MainWindow::readData() +void BotIrcDiceParser::readData() { qDebug() << "Reply"; @@ -64,19 +80,24 @@ void MainWindow::readData() if(list.size()==2) { QString cmd = list[1]; - if(m_parser->parseLine(cmd.simplified())) + if(!cmd.isEmpty()) { - m_parser->Start(); - QString result = m_parser->displayResult(); - QString msg("PRIVMSG #Rolisteam :%1 \r\n"); - m_socket->write(msg.arg(result).toLatin1()); + + cmd = cmd.simplified(); + QString result = startDiceParsing(cmd,true); + if(!result.isEmpty()) + { + QString msg("PRIVMSG #RolisteamOfficial :%1 \r\n"); + m_socket->write(msg.arg(result).toLatin1()); + } } + } else { return; } - +// } else if(readLine.contains("PING :")) @@ -92,32 +113,160 @@ void MainWindow::readData() m_socket->write(resp.toLatin1()); } } - if(readLine.contains("Found your hostname")) + if(readLine.contains(QLatin1String("Found your hostname"))) { authentificationProcess(); } + if(readLine.contains(QLatin1String("msg NickServ identify"))) + { + setRegisterName(); + } // Add to ouput - ui->m_output->append(readLine.trimmed()); + //ui->m_output->append(readLine.trimmed()); + QMessageLogger().debug() << readLine.trimmed(); // Next data?? if(m_socket->canReadLine()) readData(); } +void BotIrcDiceParser::setRegisterName() +{ + m_socket->write(QLatin1String("msg NickServ identify \r\n").data()); + joinChannel(); +} -void MainWindow::disconnectFromServer() +void BotIrcDiceParser::disconnectFromServer() { // Disconnect from IRC server m_socket->write("QUIT Good bye \r\n"); // Good bye is optional message m_socket->flush(); m_socket->disconnect(); // Now we can try it :-) + } - void MainWindow::authentificationProcess() + void BotIrcDiceParser::authentificationProcess() { qDebug() << "authentification"; - m_socket->write("NICK rolisteamDice \r\n"); - m_socket->write("USER rolisteamDice rolisteamDice rolisteamDice :rolisteamDice BOT \r\n"); - m_socket->write("MSG NickServ identify \r\n"); + m_socket->write(QLatin1String("NICK rolisteamDice \r\n").data()); + m_socket->write(QLatin1String("USER rolisteamDice rolisteamDice rolisteamDice :rolisteamDice BOT \r\n").data()); + } -void MainWindow::joinChannel() +void BotIrcDiceParser::joinChannel() +{ + m_socket->write(QLatin1String("JOIN #RolisteamOfficial \r\n").data()); +} +QString BotIrcDiceParser::diceToText(ExportedDiceResult& dice,bool highlight,bool homogeneous) { - m_socket->write("JOIN #Rolisteam \r\n"); + QStringList resultGlobal; + foreach(int face, dice.keys()) + { + QStringList result; + ListDiceResult diceResult = dice.value(face); + //patternColor = patternColorarg(); + foreach (HighLightDice tmp, diceResult) + { + QStringList diceListStr; + QStringList diceListChildren; + + + for(int i =0; i < tmp.getResult().size(); ++i) + { + qint64 dievalue = tmp.getResult()[i]; + QString prefix("%1"); + + if((tmp.isHighlighted())&&(highlight)) + { + if(tmp.getColor().isEmpty()|| tmp.getColor()=="black") + { + prefix = "%1"; + } + if(tmp.getColor()=="white") + { + prefix = "%1"; + } + if(tmp.getColor()=="blue") + { + prefix = "%1"; + } + if(tmp.getColor()=="red") + { + prefix = "%1"; + } + } + + if(i==0) + { + diceListStr << prefix.arg(QString::number(dievalue)); + } + else + { + diceListChildren << prefix.arg(QString::number(dievalue)); + } + } + if(!diceListChildren.isEmpty()) + { + diceListStr << QString("[%1]").arg(diceListChildren.join(' ')); + } + + result << diceListStr.join(' '); + // qDebug() << result << tmp.first << tmp.second; + } + + if(dice.keys().size()>1) + { + resultGlobal << QString(" d%2:(%1)").arg(result.join(',')).arg(face); + } + else + { + resultGlobal << result; + } + } + return resultGlobal.join(' '); +} + +QString BotIrcDiceParser::startDiceParsing(QString& cmd,bool highlight) +{ + QString result; + QTextStream out(&result); + if(m_parser->parseLine(cmd)) + { + + m_parser->Start(); + if(!m_parser->getErrorMap().isEmpty()) + { + out << "Error" << m_parser->humanReadableError()<< "\n"; + return QString(); + } + + ExportedDiceResult list; + bool homogeneous = true; + m_parser->getLastDiceResult(list,homogeneous); + QString diceText = diceToText(list,highlight,homogeneous); + QString scalarText; + QString str; + + if(m_parser->hasIntegerResultNotInFirst()) + { + scalarText = QString("%1").arg(m_parser->getLastIntegerResult()); + } + else if(!list.isEmpty()) + { + scalarText = QString("%1").arg(m_parser->getSumOfDiceResult()); + } + if(highlight) + str = QString("Result: %1, details:[%3 (%2)]").arg(scalarText).arg(diceText).arg(m_parser->getDiceCommand()); + else + str = QString("Result: %1, details:[%3 (%2)]").arg(scalarText).arg(diceText).arg(m_parser->getDiceCommand()); + + if(m_parser->hasStringResult()) + { + str = m_parser->getStringResult(); + } + out << str << "\n"; + + } + else + { + out << m_parser->humanReadableError()<< "\n";; + } + + return result; } |