2
* chessplugin.cpp - Psi plugin to play chess
3
* Copyright (C) 2006 Kevin Smith
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
10
* You can also redistribute and/or modify this program under the
11
* terms of the Psi License, specified in the accompanied COPYING
12
* file, as published by the Psi Project; either dated January 1st,
13
* 2005, or (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this library; if not, write to the Free Software
22
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
#include "psiplugin.h"
30
#include "gameboard.h"
34
class ChessPlugin : public PsiPlugin
37
Q_INTERFACES(PsiPlugin)
41
virtual QString name() const;
42
virtual bool processEvent( const PsiAccount* account, QDomNode &event );
43
virtual void message( const PsiAccount* account, const QString& message, const QString& fromJid, const QString& fromDisplay);
44
virtual QString shortName() const;
45
virtual QString version() const;
48
void sendData(const QString& data);
49
void receiveData(const QString& data);
53
void startGame(const QString& jid, bool meWhite, const PsiAccount* account);
63
Q_EXPORT_PLUGIN(ChessPlugin);
65
ChessPlugin::ChessPlugin() : PsiPlugin()
70
QString ChessPlugin::name() const
72
return "Chess Plugin";
75
QString ChessPlugin::shortName() const
80
QString ChessPlugin::version() const
85
bool ChessPlugin::processEvent( const PsiAccount* account, QDomNode &event )
91
void ChessPlugin::message( const PsiAccount* account, const QString& message, const QString& fromJid, const QString& fromDisplay)
94
qDebug("chess message");
95
if (!message.startsWith("chess"))
97
qDebug("message for us in chess");
98
if (game_ && fromJid != playingWith_)
100
reply=QString("<message to=\"%1\" type=\"chat\"><body>already playing chess with %2, sorry</body></message>").arg(fromJid).arg(playingWith_);
101
emit sendStanza(account, reply);
104
QString command = QString(message);
106
qDebug() << (qPrintable(QString("chess command string %1").arg(command)));
107
if (command == QString("start"))
111
qWarning(qPrintable(QString("Received message '%1', launching chess with %2").arg(message).arg(fromDisplay)));
113
reply=QString("<message to=\"%1\" type=\"chat\"><body>chess starting</body></message>").arg(fromJid);
114
emit sendStanza(account, reply);
115
startGame(fromJid, false, account);
117
else if (command == QString("starting"))
121
qWarning(qPrintable(QString("Received message '%1', launching chess with %2").arg(message).arg(fromDisplay)));
123
reply=QString("<message to=\"%1\" type=\"chat\"><body>starting chess, I go first :)</body></message>").arg(fromJid);
124
emit sendStanza(account, reply);
125
startGame(fromJid, true, account);
131
else if (command.startsWith("command"))
135
qDebug() << (qPrintable(QString("chess command %1").arg(command)));
136
receiveData(command);
140
void ChessPlugin::startGame(const QString& jid, bool meFirst, const PsiAccount* account)
142
/* game = new TicTacToe( meFirst, size );
143
game->setCaption(QString("Noughts and Crosses with %1").arg(jid));
146
account_=(PsiAccount*)account;
147
connect(game, SIGNAL(closing()), this, SLOT(stopGame()));
148
connect(game, SIGNAL(myMove(int)), this, SLOT(myTurn(int)));
149
connect(game, SIGNAL(gameOverSignal(TicTacGameBoard::State)), this, SLOT(gameOver(TicTacGameBoard::State)));
152
account_=(PsiAccount*)account;
153
cw = QColor(0x8F, 0xDF, 0xF0);
154
cb = QColor(0x70, 0x9F, 0xDF);
157
GameBoard::GameType type=GameBoard::WHITE;
158
game_ = new GameBoard(type, jid, NULL);
160
game_ = new GameBoard(1);
163
//showStatus(game_->status());
164
//QObject::connect(brd, SIGNAL(showStatus(const QString&)), this, SLOT(showStatus(const QString&)));
165
connect(game_, SIGNAL(sendData(const QString&)), this, SLOT(sendData(const QString &)));
169
void ChessPlugin::stopGame()
175
/*void ChessPlugin::gameOver(TicTacGameBoard::State state)
181
case TicTacGameBoard::HumanWon:
184
case TicTacGameBoard::ComputerWon:
187
case TicTacGameBoard::NobodyWon:
188
winner="It was a draw, no-one";
193
reply=QString("<message to=\"%1\" type=\"chat\"><body>%2 won. Good game.</body></message>").arg(playingWith).arg(winner);
194
emit sendStanza(account_, reply);
197
void ChessPlugin::sendData(const QString& data)
199
qDebug() << (qPrintable(QString("sendingData turn: %1").arg(data)));
203
QString stanzaId="aaaa";
204
reply=QString("<message to=\"%1\" type=\"chat\" id=\"%2\"><body>chess command %3</body></message>").arg(playingWith_).arg(stanzaId).arg(data);
205
qDebug() << (qPrintable(QString("sendingData stanza: %1").arg(reply)));
206
emit sendStanza(account_, reply);
209
void ChessPlugin::receiveData(const QString& data)
211
qDebug() << (qPrintable(QString("received data: %1").arg(data)));
214
//game->theirMove(space);
215
game_->receiveData(data);
219
#include "chessplugin.moc"