File: gamesessions.h

package info (click to toggle)
psi-plugins 1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,368 kB
  • sloc: cpp: 42,063; xml: 714; ansic: 84; makefile: 61; sh: 12
file content (156 lines) | stat: -rw-r--r-- 4,611 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * gamesessionlist.cpp - Battleship game plugin
 * Copyright (C) 2014  Aleksey Andreev
 *
 * This program 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#ifndef GAMESESSIONLIST_H
#define GAMESESSIONLIST_H

#include <QObject>
#include <QDomElement>
#include <QHash>
#include <QPointer>
#include <QDateTime>
#include <QTimer>

#include "pluginwindow.h"

class GameSession;

namespace XML {
	QString escapeString(const QString &str);
	QString iqErrorString(const QString &jid, const QString &id);
}

class GameSessionList : public QObject
{
Q_OBJECT

public:
	static GameSessionList *instance();
	static void reset();
	GameSession *createSession(int account, const QString &jid, bool first, const QString &gameId);
	void updateGameKey(GameSession *gs);
	void removeGame(GameSession *gs);
	GameSession *findGame(int account, const QString &jid, const QString &gameId);
	GameSession *findGameByStanzaId(int account, const QString &jid, const QString &stanzaId);
	bool processIncomingIqStanza(int account, const QDomElement &xml, const QString &accStatus, bool fromPrivate);
	void invite(int account, const QString &jid, const QStringList &resList);
	QString getStanzaId(bool bigOffset);
	void sendErrorIq(int account, const QString &jid, const QString &id);

private:
	GameSessionList(QObject *parent = 0);
	~GameSessionList();
	QString generateKey(int account, const QString &jid, const QString &gameId);
	static QString getErrorMessage(const QDomElement &xml);

private:
	static GameSessionList *instance_;
	QHash<QString, GameSession *> list_;
	int stanzaId_;

signals:
	void sendStanza(int, QString);
	void doPopup(QString);
	void playSound(QString);
	void doInviteEvent(int, QString, QString, QObject *, const char *);

};

class GameSession : public QObject
{
Q_OBJECT

public:
	enum GameStage { StageNone, StageInvitation, StageInitBoard, StageShooting, StageShowBoard, StageEnd };
	enum GameStatus
	{
		StatusNone, StatusError,
		StatusWaitInviteConfirmation, StatusWaitBoardVerification,
		StatusWaitShotConfirmation, StatusWaitOpponent
	};
	typedef QPointer<QTimer> Timer;
	typedef QPointer<QDialog> InviteDlg;
	typedef QPointer<PluginWindow> BoardWidget;
	void executeNextAction();
	void invite(const QStringList &resList);
	void initOpponentBoard(const QDomElement &xml);
	void checkOpponentBoard(const QDomElement &xml);
	void opponentTurn(const QDomElement &xml);
	bool handleTurnResult(const QDomElement &xml);

private:
	friend class GameSessionList;
	GameSession(GameSessionList *gsl, int account, const QString &jid, bool first, const QString &gameId);
	~GameSession();
	void processIncomingInvite();
	void appendInvitationEvent();
	void generateGameId();
	void sendIqResponse(const QString &id);
	void sendStanzaResult(const QString &id, const QString &body = QString());
	void initBoard();
	void setError();
	void startGame();
	bool checkEndGame();
	bool isMyNextTurn();
	void sendUncoveredBoard();
	void setTimer();

private:
	GameSessionList *gsl_;
	GameStage       stage_;
	GameStatus      status_;
	int             account_;
	QString         jid_;
	bool            first_;
	QString         gameId_;
	QString         stanzaId_;
	QDateTime       modifTime_;
	Timer           timer_;
	InviteDlg       inviteDlg_;
	BoardWidget     boardWid_;
	bool            myBoardChecked_;
	bool            opBoardChecked_;
	bool            resign_;
	//int             lastTurnPos_;
	//bool            lastTurnMy_;
	QString         lastTurnResult_;
	QString         lastTurnSeed_;
	QString         boardStatus_;

private slots:
	void sendInvite(QString jid, bool first);
	void acceptInvitation();
	void rejectInvitation();
	void endSession();
	void boardEvent(QString data);
	void timeout();

public slots:
	void showInvitationDialog();

signals:
	void sendStanza(int, QString);
	void doPopup(const QString);
	void playSound(const QString);
	void doInviteEvent(int, QString, QString, QObject *, const char *);

};

#endif // GAMESESSIONLIST_H