author | unc0rr |
Sun, 14 Jan 2007 21:51:17 +0000 | |
changeset 337 | a46abb65eb0c |
parent 335 | 751348947fce |
child 338 | d1e75dcd285f |
permissions | -rw-r--r-- |
315 | 1 |
/* |
2 |
* Hedgewars, a worms-like game |
|
3 |
* Copyright (c) 2006 Ulyanov Igor <iulyanov@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 |
#include <QMessageBox> |
|
20 |
#include <QDebug> |
|
21 |
||
22 |
#include "newnetclient.h" |
|
23 |
#include "proto.h" |
|
24 |
#include "gameuiconfig.h" |
|
25 |
#include "game.h" |
|
334 | 26 |
#include "gamecfgwidget.h" |
315 | 27 |
|
28 |
char delimeter='\t'; |
|
29 |
||
334 | 30 |
HWNewNet::HWNewNet(GameUIConfig * config, GameCFGWidget* pGameCFGWidget) : |
31 |
config(config), |
|
32 |
m_pGameCFGWidget(pGameCFGWidget), |
|
33 |
isChief(false) |
|
315 | 34 |
{ |
35 |
connect(&NetSocket, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
|
36 |
connect(&NetSocket, SIGNAL(connected()), this, SLOT(OnConnect())); |
|
37 |
connect(&NetSocket, SIGNAL(disconnected()), this, SLOT(OnDisconnect())); |
|
38 |
connect(&NetSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, |
|
39 |
SLOT(displayError(QAbstractSocket::SocketError))); |
|
40 |
} |
|
41 |
||
42 |
void HWNewNet::Connect(const QString & hostName, quint16 port, const QString & nick) |
|
43 |
{ |
|
44 |
qDebug() << hostName << ":" << port; |
|
45 |
NetSocket.connectToHost(hostName, port); |
|
46 |
mynick = nick; |
|
47 |
} |
|
48 |
||
49 |
void HWNewNet::Disconnect() |
|
50 |
{ |
|
51 |
NetSocket.disconnect(); |
|
52 |
} |
|
53 |
||
54 |
void HWNewNet::JoinGame(const QString & game) |
|
55 |
{ |
|
56 |
RawSendNet(QString("JOIN %1").arg(game)); |
|
57 |
} |
|
58 |
||
59 |
void HWNewNet::AddTeam(const HWTeam & team) |
|
60 |
{ |
|
61 |
RawSendNet(QString("ADDTEAM:") + delimeter + |
|
62 |
team.TeamName + delimeter + team.HHName[0] + delimeter + team.HHName[1] + delimeter + |
|
63 |
team.HHName[2] + delimeter + team.HHName[3] + delimeter + team.HHName[4] + delimeter + |
|
64 |
team.HHName[5] + delimeter + team.HHName[6] + delimeter + team.HHName[7]); |
|
65 |
} |
|
66 |
||
67 |
void HWNewNet::StartGame() |
|
68 |
{ |
|
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
69 |
RawSendNet(QString("START:")); |
315 | 70 |
} |
71 |
||
72 |
void HWNewNet::SendNet(const QByteArray & buf) |
|
73 |
{ |
|
74 |
QString msg = QString(buf.toBase64()); |
|
322 | 75 |
qDebug() << "to net:" << buf << ":" << msg; |
315 | 76 |
|
77 |
//NetBuffer += buf; |
|
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
78 |
RawSendNet(QString("GAMEMSG:%1%2").arg(delimeter).arg(msg)); |
315 | 79 |
} |
80 |
||
81 |
void HWNewNet::RawSendNet(const QString & str) |
|
82 |
{ |
|
83 |
RawSendNet(str.toUtf8()); |
|
84 |
} |
|
85 |
||
86 |
void HWNewNet::RawSendNet(const QByteArray & buf) |
|
87 |
{ |
|
88 |
NetSocket.write(buf); |
|
89 |
NetSocket.write("\n", 1); |
|
90 |
} |
|
91 |
||
92 |
void HWNewNet::ClientRead() |
|
93 |
{ |
|
94 |
while (NetSocket.canReadLine()) { |
|
95 |
ParseLine(NetSocket.readLine().trimmed()); |
|
96 |
} |
|
97 |
} |
|
98 |
||
99 |
void HWNewNet::OnConnect() |
|
100 |
{ |
|
101 |
RawSendNet(QString("USER") + delimeter + "hwgame 1 2 Hedgewars game"); |
|
102 |
RawSendNet(QString("NICK%1%2").arg(delimeter).arg(mynick)); |
|
103 |
} |
|
104 |
||
105 |
void HWNewNet::OnDisconnect() |
|
106 |
{ |
|
107 |
emit ChangeInTeams(QStringList()); |
|
108 |
emit Disconnected(); |
|
109 |
} |
|
110 |
||
111 |
void HWNewNet::displayError(QAbstractSocket::SocketError socketError) |
|
112 |
{ |
|
113 |
switch (socketError) { |
|
114 |
case QAbstractSocket::RemoteHostClosedError: |
|
115 |
break; |
|
116 |
case QAbstractSocket::HostNotFoundError: |
|
117 |
QMessageBox::information(0, tr("Error"), |
|
118 |
tr("The host was not found. Please check the host name and port settings.")); |
|
119 |
break; |
|
120 |
case QAbstractSocket::ConnectionRefusedError: |
|
121 |
QMessageBox::information(0, tr("Error"), |
|
122 |
tr("Connection refused")); |
|
123 |
break; |
|
124 |
default: |
|
125 |
QMessageBox::information(0, tr("Error"), |
|
126 |
NetSocket.errorString()); |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
void HWNewNet::ParseLine(const QByteArray & line) |
|
131 |
{ |
|
132 |
QString msg = QString::fromUtf8 (line.data(), line.size()); |
|
320 | 133 |
|
315 | 134 |
qDebug() << "line " << msg << " received"; |
135 |
||
136 |
QStringList lst = msg.split(delimeter); |
|
137 |
if (lst[0] == "ERRONEUSNICKNAME") { |
|
138 |
QMessageBox::information(0, 0, "Your net nickname is in use or cannot be used"); |
|
139 |
return; |
|
140 |
} |
|
141 |
||
142 |
if (lst[0] == "CONNECTED") { |
|
143 |
emit Connected(); |
|
144 |
emit EnteredGame(); |
|
145 |
return; |
|
146 |
} |
|
147 |
||
148 |
if (lst[0] == "TEAMCHANGED") { |
|
149 |
lst.pop_front(); |
|
150 |
emit ChangeInTeams(lst); |
|
151 |
return; |
|
152 |
} |
|
153 |
||
154 |
if (lst[0] == "CONFIGASKED") { |
|
334 | 155 |
isChief=true; |
315 | 156 |
ConfigAsked(); |
157 |
return; |
|
158 |
} |
|
320 | 159 |
|
315 | 160 |
if (lst[0] == "CONFIGURED") { |
161 |
lst.pop_front(); |
|
334 | 162 |
if(lst.size()<5) return; |
163 |
qDebug() << lst; |
|
164 |
emit seedChanged(lst[0]); |
|
165 |
emit mapChanged(lst[1]); |
|
166 |
emit themeChanged(lst[2]); |
|
167 |
emit initHealthChanged(lst[3].toUInt()); |
|
168 |
emit turnTimeChanged(lst[4].toUInt()); |
|
169 |
//emit fortsModeChanged(lst[5].toInt() != 0); // FIXME: add a getFortsMode in ConfigAsked |
|
315 | 170 |
return; |
171 |
} |
|
172 |
||
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
173 |
if (lst[0] == "CONFIG_PARAM") { |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
174 |
if (lst[1] == "SEED") { |
332 | 175 |
emit seedChanged(lst[2]); |
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
176 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
177 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
178 |
if (lst[1] == "MAP") { |
332 | 179 |
emit mapChanged(lst[2]); |
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
180 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
181 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
182 |
if (lst[1] == "THEME") { |
332 | 183 |
emit themeChanged(lst[2]); |
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
184 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
185 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
186 |
if (lst[1] == "HEALTH") { |
332 | 187 |
emit initHealthChanged(lst[2].toUInt()); |
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
188 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
189 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
190 |
if (lst[1] == "TURNTIME") { |
332 | 191 |
emit turnTimeChanged(lst[2].toUInt()); |
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
192 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
193 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
194 |
if (lst[1] == "FORTSMODE") { |
332 | 195 |
emit fortsModeChanged(lst[2].toInt() != 0); |
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
196 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
197 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
198 |
qDebug() << "unknow config param: " << lst[1]; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
199 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
200 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
201 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
202 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
203 |
// should be kinda game states, which don't allow "GAMEMSG:" at configure step, |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
204 |
// "CONNECTED" at round phase, etc. |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
205 |
if (lst[0] == "GAMEMSG:") { |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
206 |
QByteArray em = QByteArray::fromBase64(lst[1].toAscii()); |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
207 |
qDebug() << "to engine:" << em; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
208 |
emit FromNet(em); |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
209 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
210 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
211 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
212 |
qDebug() << "unknown net command: " << msg; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
213 |
|
315 | 214 |
} |
215 |
||
216 |
||
217 |
void HWNewNet::ConfigAsked() |
|
218 |
{ |
|
335 | 219 |
onSeedChanged(m_pGameCFGWidget->getCurrentSeed()); |
220 |
onMapChanged(m_pGameCFGWidget->getCurrentMap()); |
|
221 |
onThemeChanged(m_pGameCFGWidget->getCurrentTheme()); |
|
222 |
onInitHealthChanged(m_pGameCFGWidget->getInitHealth()); |
|
223 |
onTurnTimeChanged(m_pGameCFGWidget->getTurnTime()); |
|
315 | 224 |
} |
225 |
||
226 |
void HWNewNet::RunGame() |
|
227 |
{ |
|
228 |
HWGame * game = new HWGame(config, 0); |
|
229 |
connect(game, SIGNAL(SendNet(const QByteArray &)), this, SLOT(SendNet(const QByteArray &))); |
|
230 |
connect(this, SIGNAL(FromNet(const QByteArray &)), game, SLOT(FromNet(const QByteArray &))); |
|
231 |
connect(this, SIGNAL(LocalCFG(const QString &)), game, SLOT(LocalCFG(const QString &))); |
|
232 |
game->StartNet(); |
|
233 |
} |
|
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
234 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
235 |
void HWNewNet::onSeedChanged(const QString & seed) |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
236 |
{ |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
237 |
RawSendNet(QString("CONFIG_PARAM%1SEED%1%2").arg(delimeter).arg(seed)); |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
238 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
239 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
240 |
void HWNewNet::onMapChanged(const QString & map) |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
241 |
{ |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
242 |
RawSendNet(QString("CONFIG_PARAM%1MAP%1%2").arg(delimeter).arg(map)); |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
243 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
244 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
245 |
void HWNewNet::onThemeChanged(const QString & theme) |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
246 |
{ |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
247 |
RawSendNet(QString("CONFIG_PARAM%1THEME%1%2").arg(delimeter).arg(theme)); |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
248 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
249 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
250 |
void HWNewNet::onInitHealthChanged(quint32 health) |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
251 |
{ |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
252 |
RawSendNet(QString("CONFIG_PARAM%1HEALTH%1%2").arg(delimeter).arg(health)); |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
253 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
254 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
255 |
void HWNewNet::onTurnTimeChanged(quint32 time) |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
256 |
{ |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
257 |
RawSendNet(QString("CONFIG_PARAM%1TURNTIME%1%2").arg(delimeter).arg(time)); |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
258 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
259 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
260 |
void HWNewNet::onFortsModeChanged(bool value) |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
261 |
{ |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
262 |
RawSendNet(QString("CONFIG_PARAM%1FORTSMODE%1%2").arg(delimeter).arg(value)); |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
263 |
} |