184
|
1 |
/*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU General Public License as published by
|
|
7 |
* the Free Software Foundation; version 2 of the License
|
|
8 |
*
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
* GNU General Public License for more details.
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
15 |
* along with this program; if not, write to the Free Software
|
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
17 |
*/
|
|
18 |
|
|
19 |
#ifndef NET_H
|
|
20 |
#define NET_H
|
|
21 |
|
|
22 |
#include <QObject>
|
|
23 |
#include <QTcpSocket>
|
|
24 |
#include <QRegExp>
|
|
25 |
#include <QStringList>
|
|
26 |
#include <QTimer>
|
|
27 |
#include "team.h"
|
|
28 |
|
|
29 |
#define MAGIC_CHAR "\x2f"
|
|
30 |
|
|
31 |
struct netTeam
|
|
32 |
{
|
|
33 |
QString nick;
|
|
34 |
QStringList hhs;
|
|
35 |
};
|
|
36 |
|
|
37 |
class GameUIConfig;
|
|
38 |
|
|
39 |
class HWNet : public QObject
|
|
40 |
{
|
|
41 |
Q_OBJECT
|
|
42 |
|
|
43 |
public:
|
|
44 |
HWNet(GameUIConfig * config);
|
|
45 |
void Connect(const QString & hostName, quint16 port, const QString & nick);
|
|
46 |
void Disconnect();
|
|
47 |
void JoinGame(const QString & game);
|
|
48 |
void AddTeam(const HWTeam & team);
|
|
49 |
void StartGame();
|
|
50 |
|
|
51 |
signals:
|
|
52 |
void Connected();
|
|
53 |
void AddGame(const QString & chan);
|
|
54 |
void EnteredGame();
|
|
55 |
void FromNet(const QByteArray & buf);
|
|
56 |
void LocalCFG(const QString & team);
|
|
57 |
void ChangeInTeams(const QStringList & teams);
|
|
58 |
|
|
59 |
public slots:
|
|
60 |
void SendNet(const QByteArray & buf);
|
|
61 |
|
|
62 |
private:
|
|
63 |
enum NetState {
|
|
64 |
nsDisconnected = 0,
|
|
65 |
nsConnecting = 1,
|
|
66 |
nsConnected = 3,
|
|
67 |
nsJoining = 4,
|
|
68 |
nsJoined = 5,
|
|
69 |
nsStarting = 6,
|
|
70 |
nsGaming = 7,
|
|
71 |
nsQuitting = 8
|
|
72 |
};
|
|
73 |
|
|
74 |
QTcpSocket NetSocket;
|
|
75 |
NetState state;
|
|
76 |
QRegExp * IRCmsg_cmd_text;
|
|
77 |
QRegExp * IRCmsg_number_param;
|
|
78 |
QRegExp * IRCmsg_who_cmd_target;
|
|
79 |
QRegExp * IRCmsg_who_cmd_target_text;
|
|
80 |
QRegExp * IRCmsg_who_cmd_text;
|
|
81 |
QString mynick;
|
|
82 |
QString opnick;
|
|
83 |
QString channel;
|
|
84 |
QString seed;
|
|
85 |
bool isOp;
|
|
86 |
quint32 opCount;
|
|
87 |
netTeam teams[5];
|
|
88 |
quint8 teamsCount;
|
|
89 |
int playerscnt;
|
|
90 |
int configasks;
|
|
91 |
QByteArray NetBuffer;
|
|
92 |
QTimer * TimerFlusher;
|
|
93 |
GameUIConfig * config;
|
|
94 |
|
|
95 |
void RawSendNet(const QString & buf);
|
|
96 |
void RawSendNet(const QByteArray & buf);
|
|
97 |
|
|
98 |
void ParseLine(const QByteArray & line);
|
|
99 |
void msgcmd_textHandler(const QString & msg);
|
|
100 |
void msgnumber_paramHandler(const QString & msg);
|
|
101 |
void msgwho_cmd_targetHandler(const QString & msg);
|
|
102 |
void msgwho_cmd_textHandler(const QString & msg);
|
|
103 |
void msgwho_cmd_target_textHandler(const QString & msg);
|
|
104 |
|
|
105 |
void hwp_opmsg(const QString & who, const QString & msg);
|
|
106 |
void hwp_chanmsg(const QString & who, const QString & msg);
|
|
107 |
void ConfigAsked();
|
|
108 |
void NetTeamAdded(const QString & msg);
|
|
109 |
|
|
110 |
void RunGame();
|
|
111 |
|
|
112 |
|
|
113 |
private slots:
|
|
114 |
void ClientRead();
|
|
115 |
void OnConnect();
|
|
116 |
void OnDisconnect();
|
|
117 |
void Perform();
|
|
118 |
void displayError(QAbstractSocket::SocketError socketError);
|
|
119 |
void FlushNetBuf();
|
|
120 |
};
|
|
121 |
|
|
122 |
#define SENDCFGSTRNET(a) {\
|
|
123 |
QByteArray strmsg; \
|
|
124 |
strmsg.append(a); \
|
|
125 |
quint8 sz = strmsg.size(); \
|
|
126 |
QByteArray enginemsg = QByteArray((char *)&sz, 1) + strmsg; \
|
|
127 |
QString _msg = MAGIC_CHAR MAGIC_CHAR + QString(enginemsg.toBase64()); \
|
|
128 |
hwp_chanmsg(mynick, _msg); \
|
|
129 |
RawSendNet(QString("PRIVMSG %1 :").arg(channel) + _msg); \
|
|
130 |
}
|
|
131 |
|
|
132 |
#define SENDCFGSTRLOC(a) {\
|
|
133 |
QByteArray strmsg; \
|
|
134 |
strmsg.append(QString(a).toUtf8()); \
|
|
135 |
quint8 sz = strmsg.size(); \
|
|
136 |
QByteArray enginemsg = QByteArray((char *)&sz, 1) + strmsg; \
|
|
137 |
emit FromNet(enginemsg); \
|
|
138 |
}
|
|
139 |
|
|
140 |
#endif
|