~ubuntu-branches/ubuntu/quantal/psi/quantal

« back to all changes in this revision

Viewing changes to src/plugins/generic/noughtsandcrosses/noughtsandcrossesplugin.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * noughtsandcrossesplugin.cpp - Psi plugin to play noughts and crosses
 
3
 * Copyright (C) 2006  Kevin Smith
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
19
 *
 
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
 
23
 *
 
24
 */
 
25
 
 
26
#include <QtCore>
 
27
#include <QDebug>
 
28
 
 
29
#include "psiplugin.h"
 
30
#include "tictac.h"
 
31
 
 
32
class NoughtsAndCrossesPlugin : public PsiPlugin
 
33
{
 
34
        Q_OBJECT
 
35
        Q_INTERFACES(PsiPlugin)
 
36
 
 
37
public:
 
38
        NoughtsAndCrossesPlugin();
 
39
        virtual QString name() const; 
 
40
        virtual void message( const PsiAccount* account, const QString& message, const QString& fromJid, const QString& fromDisplay); 
 
41
        virtual QString shortName() const;
 
42
        virtual QString version() const;
 
43
 
 
44
private slots:
 
45
        void stopGame();
 
46
        void myTurn(int space);
 
47
        void theirTurn(int space);
 
48
        void gameOver(TicTacGameBoard::State state);
 
49
        
 
50
private:
 
51
        void startGame(QString jid, int size, bool meFirst, const PsiAccount* account);
 
52
        
 
53
 
 
54
        
 
55
        TicTacToe* game;
 
56
        QString playingWith;
 
57
        PsiAccount* account_;
 
58
};
 
59
 
 
60
Q_EXPORT_PLUGIN(NoughtsAndCrossesPlugin);
 
61
 
 
62
NoughtsAndCrossesPlugin::NoughtsAndCrossesPlugin() : PsiPlugin()
 
63
{
 
64
        game = NULL;
 
65
}
 
66
 
 
67
QString NoughtsAndCrossesPlugin::name() const
 
68
{
 
69
        return "NoughtsAndCrosses Plugin";
 
70
}
 
71
 
 
72
QString NoughtsAndCrossesPlugin::shortName() const
 
73
{
 
74
        return "noughtsandcrosses";
 
75
}
 
76
 
 
77
QString NoughtsAndCrossesPlugin::version() const
 
78
{
 
79
        return "0.0.1";
 
80
}
 
81
 
 
82
void NoughtsAndCrossesPlugin::message( const PsiAccount* account, const QString& message, const QString& fromJid, const QString& fromDisplay)
 
83
{
 
84
        QString reply;
 
85
        qDebug("naughtsandcrosses message");
 
86
        if (!message.startsWith("noughtsandcrosses"))
 
87
                return;
 
88
        qDebug("message for us in noughtsandcrosses");
 
89
        if (game && fromJid != playingWith)
 
90
        {
 
91
                reply=QString("<message to=\"%1\" type=\"chat\"><body>already playing with %2, sorry</body></message>").arg(fromJid).arg(playingWith);
 
92
                emit sendStanza(account, reply);
 
93
                return;
 
94
        }
 
95
        QString command = QString(message);
 
96
        command.remove(0,18);
 
97
        qDebug() << (qPrintable(QString("noughtsandcrosses command string %1").arg(command)));
 
98
        if (command == QString("start"))
 
99
        {
 
100
                if (game)
 
101
                        return;
 
102
                qWarning(qPrintable(QString("Received message '%1', launching nac with %2").arg(message).arg(fromDisplay)));
 
103
                QString reply;
 
104
                reply=QString("<message to=\"%1\" type=\"chat\"><body>noughtsandcrosses starting</body></message>").arg(fromJid);
 
105
                emit sendStanza(account, reply);
 
106
                startGame(fromJid, 3, false, account);
 
107
        }
 
108
        else if (command == QString("starting"))
 
109
        {
 
110
                if (game)
 
111
                        return;
 
112
                qWarning(qPrintable(QString("Received message '%1', launching nac with %2").arg(message).arg(fromDisplay)));
 
113
                QString reply;
 
114
                reply=QString("<message to=\"%1\" type=\"chat\"><body>starting noughts and crosses, I go first :)</body></message>").arg(fromJid);
 
115
                emit sendStanza(account, reply);
 
116
                startGame(fromJid, 3, true, account);
 
117
        }
 
118
        else if (!game)
 
119
        {
 
120
                return;
 
121
        }
 
122
        else if (command.startsWith("move"))
 
123
        {
 
124
                command.remove(0,5);
 
125
                
 
126
                int space=command.toInt();
 
127
                qDebug() << (qPrintable(QString("noughtsandcrosses move to space %1").arg(space)));
 
128
                theirTurn(space);
 
129
        }
 
130
}       
 
131
 
 
132
void NoughtsAndCrossesPlugin::startGame(QString jid, int size, bool meFirst, const PsiAccount* account)
 
133
{
 
134
        game = new TicTacToe( meFirst, size );
 
135
        game->setCaption(QString("Noughts and Crosses with %1").arg(jid));
 
136
        playingWith=jid;
 
137
    game->show();
 
138
        account_=(PsiAccount*)account;
 
139
        connect(game, SIGNAL(closing()), this, SLOT(stopGame()));
 
140
        connect(game, SIGNAL(myMove(int)), this, SLOT(myTurn(int)));
 
141
        connect(game, SIGNAL(gameOverSignal(TicTacGameBoard::State)), this, SLOT(gameOver(TicTacGameBoard::State)));
 
142
}
 
143
 
 
144
void NoughtsAndCrossesPlugin::stopGame()
 
145
{
 
146
        delete game;
 
147
        game=NULL;
 
148
}
 
149
 
 
150
void NoughtsAndCrossesPlugin::gameOver(TicTacGameBoard::State state)
 
151
{
 
152
        QString reply;
 
153
        QString winner;
 
154
        switch (state)
 
155
        {
 
156
                case TicTacGameBoard::HumanWon:
 
157
                        winner="I";
 
158
                        break;
 
159
                case TicTacGameBoard::ComputerWon:
 
160
                        winner="You";
 
161
                        break;
 
162
                case TicTacGameBoard::NobodyWon:
 
163
                        winner="It was a draw, no-one";
 
164
                        break;
 
165
                default:
 
166
                        winner="ERROR!!!";
 
167
        }
 
168
        reply=QString("<message to=\"%1\" type=\"chat\"><body>%2 won. Good game.</body></message>").arg(playingWith).arg(winner);
 
169
        emit sendStanza(account_, reply);
 
170
}
 
171
 
 
172
void NoughtsAndCrossesPlugin::myTurn(int space)
 
173
{
 
174
        qDebug() << (qPrintable(QString("my turn: %1").arg(space)));
 
175
        if (!game)
 
176
                return;
 
177
        QString reply;
 
178
        reply=QString("<message to=\"%1\" type=\"chat\"><body>noughtsandcrosses move %2</body></message>").arg(playingWith).arg(space);
 
179
        emit sendStanza(account_, reply);
 
180
}
 
181
 
 
182
void NoughtsAndCrossesPlugin::theirTurn(int space)
 
183
{
 
184
        qDebug() << (qPrintable(QString("their turn: %1").arg(space)));
 
185
        if (!game)
 
186
                return;
 
187
        game->theirMove(space);
 
188
}
 
189
 
 
190
 
 
191
#include "noughtsandcrossesplugin.moc"