|
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2013 Jeffrey Lee.
|
|
|
|
|
** All rights reserved.
|
|
|
|
|
** Contact: vipjeffreylee@gmail.com
|
|
|
|
|
** Please keep the author contact information.
|
|
|
|
|
** 2013-10-10
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
#include "robot.h"
|
|
|
|
|
#include<QTime>
|
|
|
|
|
#include<QDebug>
|
|
|
|
|
#include<QDateTime>
|
|
|
|
|
#include <QtSql/QSqlDatabase>
|
|
|
|
|
#include <QtSql/QSqlQuery>
|
|
|
|
|
#include <QtSql/QSqlError>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
Robot::Robot(){
|
|
|
|
|
QTime time;
|
|
|
|
|
time= QTime::currentTime();
|
|
|
|
|
qsrand(time.msec()+time.second()*1000);
|
|
|
|
|
qDebug()<<"rebot start"<<endl;
|
|
|
|
|
if(loaddb()){
|
|
|
|
|
QSqlQuery query("select * from joke;");
|
|
|
|
|
while(query.next()){
|
|
|
|
|
jokelist<<query.value("content").toString();
|
|
|
|
|
//qDebug()<<jokelist<<endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Robot::~Robot(){
|
|
|
|
|
closedb();
|
|
|
|
|
qDebug()<<"rebot close"<<endl;
|
|
|
|
|
//db->close();
|
|
|
|
|
//delete db;
|
|
|
|
|
}
|
|
|
|
|
bool Robot::loaddb(){
|
|
|
|
|
QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
|
|
|
|
|
db.setDatabaseName(QString(".%1db%1robotjoke.db3").arg(QDir::separator()));
|
|
|
|
|
if(db.open()){
|
|
|
|
|
qDebug()<<"<EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD>"<<endl;
|
|
|
|
|
}else{
|
|
|
|
|
qDebug()<<"<EFBFBD><EFBFBD><EFBFBD>ݿⲻ<EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>"<<endl;
|
|
|
|
|
QDir::current().mkdir("db");
|
|
|
|
|
if(db.open()){
|
|
|
|
|
qDebug()<<"<EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD>½<EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD>"<<db.databaseName()<<endl;
|
|
|
|
|
QSqlQuery query(db);
|
|
|
|
|
if (query.exec("CREATE TABLE joke ( id INTEGER PRIMARY KEY,content TEXT,creator TEXT);")) {
|
|
|
|
|
qDebug() << "create table joke success.";
|
|
|
|
|
} else {
|
|
|
|
|
qDebug() << "create table joke failure" << query.lastError();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
qDebug()<<"<EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>"<<db.databaseName()<<endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
void Robot::closedb(){
|
|
|
|
|
QString name;
|
|
|
|
|
{
|
|
|
|
|
name = QSqlDatabase::database().connectionName();
|
|
|
|
|
}
|
|
|
|
|
QSqlDatabase::removeDatabase(name);
|
|
|
|
|
}
|
|
|
|
|
bool Robot::savejoke(QString joke,QString creator){
|
|
|
|
|
QSqlQuery query;
|
|
|
|
|
return query.exec(QString("INSERT INTO joke (content,creator) values('%1','%2');").arg(joke).arg(creator));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString Robot::name(){
|
|
|
|
|
|
|
|
|
|
return QString("<EFBFBD>Ұ<EFBFBD><EFBFBD><EFBFBD>Ц<EFBFBD><EFBFBD>");
|
|
|
|
|
}
|
|
|
|
|
ReplyMsg Robot::listenandsay(const MsgSender msgSender, const QString &message){
|
|
|
|
|
ReplyMsg replyMsg;
|
|
|
|
|
if(msgSender.groupTXUIN.isEmpty()){
|
|
|
|
|
replyMsg.recTXUIN=msgSender.friendTXUIN;
|
|
|
|
|
}else{
|
|
|
|
|
replyMsg.recTXUIN=msgSender.groupTXUIN;
|
|
|
|
|
}
|
|
|
|
|
if(message.indexOf("#help")>-1){
|
|
|
|
|
replyMsg.content="<EFBFBD>Ұ<EFBFBD><EFBFBD><EFBFBD>Ц<EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n #joke<EFBFBD><EFBFBD>#Ц<EFBFBD><EFBFBD>;#addjoke <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ц<EFBFBD><EFBFBD>";
|
|
|
|
|
|
|
|
|
|
}else if(message.indexOf("#joke")>-1||message.indexOf("#Ц<EFBFBD><EFBFBD>")>-1){
|
|
|
|
|
if(jokelist.isEmpty()){
|
|
|
|
|
replyMsg.content="<EFBFBD>Բ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>ռ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD>Ц<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>#addjoke<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ц<EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
|
|
|
|
|
}else{
|
|
|
|
|
replyMsg.content=jokelist.at(qrand()%(jokelist.length()));
|
|
|
|
|
}
|
|
|
|
|
}else if(message.left(8)=="#addjoke"){
|
|
|
|
|
jokelist<<message.mid(8);
|
|
|
|
|
replyMsg.content=QString("<EFBFBD><EFBFBD>Ц<EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD>ڹ<EFBFBD><EFBFBD>ռ<EFBFBD>Ц<EFBFBD><EFBFBD>%1<EFBFBD><EFBFBD><EFBFBD><EFBFBD>").arg(jokelist.length());
|
|
|
|
|
savejoke(jokelist.last(),msgSender.friendName);
|
|
|
|
|
}else if(message.indexOf("лл")>-1){
|
|
|
|
|
replyMsg.content= QString("%1 <EFBFBD><EFBFBD>̫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD><EFBFBD><EFBFBD>ʱΪ<EFBFBD><EFBFBD>Ч<EFBFBD>͡<EFBFBD>").arg(msgSender.friendName);
|
|
|
|
|
}
|
|
|
|
|
return replyMsg;
|
|
|
|
|
}
|