author | unc0rr |
Wed, 08 Oct 2008 13:54:51 +0000 | |
changeset 1315 | c2f09811bb8c |
parent 1313 | f4c54e9e1b8c |
child 1316 | 50514e45d0b5 |
permissions | -rw-r--r-- |
315 | 1 |
/* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
883 | 3 |
* Copyright (c) 2006-2008 Ulyanov Igor <iulyanov@gmail.com> |
315 | 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> |
|
1189
f66cdbbfc4b6
Fix compile under Ubuntu (why it compiles everywhere else?)
unc0rr
parents:
1083
diff
changeset
|
20 |
#include <QDebug> |
315 | 21 |
|
697 | 22 |
#include "hwconsts.h" |
315 | 23 |
#include "newnetclient.h" |
24 |
#include "proto.h" |
|
25 |
#include "gameuiconfig.h" |
|
26 |
#include "game.h" |
|
334 | 27 |
#include "gamecfgwidget.h" |
347 | 28 |
#include "teamselect.h" |
315 | 29 |
|
1082 | 30 |
char delimeter='\n'; |
315 | 31 |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
338
diff
changeset
|
32 |
HWNewNet::HWNewNet(GameUIConfig * config, GameCFGWidget* pGameCFGWidget, TeamSelWidget* pTeamSelWidget) : |
334 | 33 |
config(config), |
34 |
m_pGameCFGWidget(pGameCFGWidget), |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
338
diff
changeset
|
35 |
m_pTeamSelWidget(pTeamSelWidget), |
383 | 36 |
isChief(false), |
1313
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
37 |
m_game_connected(false), |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
38 |
loginStep(0), |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
39 |
netClientState(0) |
315 | 40 |
{ |
41 |
connect(&NetSocket, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
|
42 |
connect(&NetSocket, SIGNAL(connected()), this, SLOT(OnConnect())); |
|
43 |
connect(&NetSocket, SIGNAL(disconnected()), this, SLOT(OnDisconnect())); |
|
44 |
connect(&NetSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, |
|
45 |
SLOT(displayError(QAbstractSocket::SocketError))); |
|
46 |
} |
|
47 |
||
48 |
void HWNewNet::Connect(const QString & hostName, quint16 port, const QString & nick) |
|
49 |
{ |
|
655
e58a77556878
- Temporary set delimiter to *, as \t seems to be endline now in Qt
unc0rr
parents:
574
diff
changeset
|
50 |
mynick = nick; |
315 | 51 |
NetSocket.connectToHost(hostName, port); |
52 |
} |
|
53 |
||
54 |
void HWNewNet::Disconnect() |
|
55 |
{ |
|
1311 | 56 |
m_game_connected = false; |
383 | 57 |
NetSocket.disconnectFromHost(); |
315 | 58 |
} |
59 |
||
1082 | 60 |
void HWNewNet::CreateRoom(const QString & room) |
315 | 61 |
{ |
1313
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
62 |
if(netClientState != 2) |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
63 |
{ |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
64 |
qDebug("Illegal try to create room!"); |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
65 |
return; |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
66 |
} |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
67 |
|
1082 | 68 |
RawSendNet(QString("CREATE%1%2").arg(delimeter).arg(room)); |
1313
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
69 |
isChief = true; |
1082 | 70 |
} |
71 |
||
72 |
void HWNewNet::JoinRoom(const QString & room) |
|
73 |
{ |
|
1313
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
74 |
if(netClientState != 2) |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
75 |
{ |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
76 |
qDebug("Illegal try to join room!"); |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
77 |
return; |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
78 |
} |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
79 |
|
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
80 |
loginStep++; |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
81 |
|
1082 | 82 |
RawSendNet(QString("JOIN%1%2").arg(delimeter).arg(room)); |
1313
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
83 |
m_pGameCFGWidget->setEnabled(false); |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
84 |
m_pTeamSelWidget->setNonInteractive(); |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
85 |
isChief = false; |
315 | 86 |
} |
87 |
||
88 |
void HWNewNet::AddTeam(const HWTeam & team) |
|
89 |
{ |
|
1311 | 90 |
QString cmd = QString("ADDTEAM") + delimeter + |
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
373
diff
changeset
|
91 |
team.TeamName + delimeter + |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
373
diff
changeset
|
92 |
team.teamColor.name() + delimeter + |
443
eec37eb7f5db
fort, grave and difficulty information for net team
displacer
parents:
431
diff
changeset
|
93 |
team.Grave + delimeter + |
eec37eb7f5db
fort, grave and difficulty information for net team
displacer
parents:
431
diff
changeset
|
94 |
team.Fort + delimeter + |
1245
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1189
diff
changeset
|
95 |
QString::number(team.difficulty); |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1189
diff
changeset
|
96 |
|
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1189
diff
changeset
|
97 |
for(int i = 0; i < 8; ++i) |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1189
diff
changeset
|
98 |
{ |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1189
diff
changeset
|
99 |
cmd.append(delimeter); |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1189
diff
changeset
|
100 |
cmd.append(team.HHName[i]); |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1189
diff
changeset
|
101 |
cmd.append(delimeter); |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1189
diff
changeset
|
102 |
cmd.append(team.HHHat[i]); |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1189
diff
changeset
|
103 |
} |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1189
diff
changeset
|
104 |
RawSendNet(cmd); |
315 | 105 |
} |
106 |
||
347 | 107 |
void HWNewNet::RemoveTeam(const HWTeam & team) |
108 |
{ |
|
1311 | 109 |
RawSendNet(QString("REMOVETEAM") + delimeter + team.TeamName); |
401 | 110 |
m_networkToLocalteams.remove(m_networkToLocalteams.key(team.TeamName)); |
347 | 111 |
} |
112 |
||
1311 | 113 |
void HWNewNet::Ready() |
315 | 114 |
{ |
1311 | 115 |
RawSendNet(QString("READY")); |
315 | 116 |
} |
117 |
||
118 |
void HWNewNet::SendNet(const QByteArray & buf) |
|
119 |
{ |
|
120 |
QString msg = QString(buf.toBase64()); |
|
121 |
||
1311 | 122 |
RawSendNet(QString("GAMEMSG%1%2").arg(delimeter).arg(msg)); |
315 | 123 |
} |
124 |
||
125 |
void HWNewNet::RawSendNet(const QString & str) |
|
126 |
{ |
|
127 |
RawSendNet(str.toUtf8()); |
|
128 |
} |
|
129 |
||
130 |
void HWNewNet::RawSendNet(const QByteArray & buf) |
|
131 |
{ |
|
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
132 |
qDebug() << "Client: " << QString(buf).split("\n"); |
315 | 133 |
NetSocket.write(buf); |
1082 | 134 |
NetSocket.write("\n\n", 2); |
315 | 135 |
} |
136 |
||
137 |
void HWNewNet::ClientRead() |
|
138 |
{ |
|
1082 | 139 |
while (NetSocket.canReadLine()) { |
140 |
QString s = QString::fromUtf8(NetSocket.readLine().trimmed()); |
|
141 |
||
142 |
if (s.size() == 0) { |
|
143 |
ParseCmd(cmdbuf); |
|
144 |
cmdbuf.clear(); |
|
145 |
} else |
|
146 |
cmdbuf << s; |
|
147 |
} |
|
315 | 148 |
} |
149 |
||
150 |
void HWNewNet::OnConnect() |
|
151 |
{ |
|
1310 | 152 |
RawSendNet(QString("NICK%1%2").arg(delimeter).arg(mynick)); |
153 |
RawSendNet(QString("PROTO%1%2").arg(delimeter).arg(*cProtoVer)); |
|
315 | 154 |
} |
155 |
||
156 |
void HWNewNet::OnDisconnect() |
|
157 |
{ |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
158 |
//emit ChangeInTeams(QStringList()); |
383 | 159 |
if(m_game_connected) emit Disconnected(); |
160 |
m_game_connected=false; |
|
315 | 161 |
} |
162 |
||
163 |
void HWNewNet::displayError(QAbstractSocket::SocketError socketError) |
|
164 |
{ |
|
1302 | 165 |
switch (socketError) { |
166 |
case QAbstractSocket::RemoteHostClosedError: |
|
167 |
break; |
|
168 |
case QAbstractSocket::HostNotFoundError: |
|
169 |
QMessageBox::information(0, tr("Error"), |
|
170 |
tr("The host was not found. Please check the host name and port settings.")); |
|
171 |
break; |
|
172 |
case QAbstractSocket::ConnectionRefusedError: |
|
173 |
QMessageBox::information(0, tr("Error"), |
|
174 |
tr("Connection refused")); |
|
175 |
break; |
|
176 |
default: |
|
177 |
QMessageBox::information(0, tr("Error"), |
|
178 |
NetSocket.errorString()); |
|
179 |
} |
|
315 | 180 |
} |
181 |
||
1082 | 182 |
void HWNewNet::ParseCmd(const QStringList & lst) |
315 | 183 |
{ |
1305 | 184 |
qDebug() << "Server: " << lst; |
185 |
||
186 |
if(!lst.size()) |
|
187 |
{ |
|
188 |
qWarning("Net client: Bad message"); |
|
189 |
return; |
|
190 |
} |
|
320 | 191 |
|
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
192 |
if ((lst[0] == "NICK") || (lst[0] == "PROTO")) |
1313
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
193 |
{ |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
194 |
loginStep++; |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
195 |
if (loginStep == 2) |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
196 |
{ |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
197 |
netClientState = 2; |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
198 |
RawSendNet(QString("LIST")); |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
199 |
} |
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
200 |
return ; |
1313
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
201 |
} |
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
202 |
|
1305 | 203 |
if (lst[0] == "ERROR") { |
204 |
if (lst.size() == 2) |
|
1307 | 205 |
QMessageBox::information(0, 0, "Error: " + lst[1]); |
1305 | 206 |
else |
207 |
QMessageBox::information(0, 0, "Unknown error"); |
|
208 |
return; |
|
209 |
} |
|
315 | 210 |
|
1307 | 211 |
if (lst[0] == "WARNING") { |
212 |
if (lst.size() == 2) |
|
213 |
QMessageBox::information(0, 0, "Warning: " + lst[1]); |
|
214 |
else |
|
215 |
QMessageBox::information(0, 0, "Unknown warning"); |
|
216 |
return; |
|
217 |
} |
|
218 |
||
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
219 |
if (lst[0] == "CONNECTED") { |
1313
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
220 |
netClientState = 1; |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
221 |
m_game_connected = true; |
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
222 |
emit Connected(); |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
223 |
return; |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
224 |
} |
315 | 225 |
|
1313
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
226 |
if (lst[0] == "ROOMS") { |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
227 |
QStringList tmp = lst; |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
228 |
tmp.removeFirst(); |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
229 |
emit roomsList(tmp); |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
230 |
return; |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
231 |
} |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
232 |
|
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
233 |
if (lst[0] == "CHAT_STRING") { |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
234 |
if(lst.size() < 3) |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
235 |
{ |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
236 |
qWarning("Net: Empty CHAT_STRING message"); |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
237 |
return; |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
238 |
} |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
239 |
QStringList tmp = lst; |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
240 |
tmp.removeFirst(); |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
241 |
emit chatStringFromNet(tmp); |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
242 |
return; |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
243 |
} |
453 | 244 |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
245 |
if (lst[0] == "ADDTEAM:") { |
1245
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1189
diff
changeset
|
246 |
if(lst.size() < 22) |
574 | 247 |
{ |
248 |
qWarning("Net: Too short ADDTEAM message"); |
|
249 |
return; |
|
250 |
} |
|
1082 | 251 |
QStringList tmp = lst; |
252 |
tmp.removeFirst(); |
|
253 |
emit AddNetTeam(tmp); |
|
315 | 254 |
return; |
255 |
} |
|
256 |
||
347 | 257 |
if (lst[0] == "REMOVETEAM:") { |
573 | 258 |
if(lst.size() < 3) |
259 |
{ |
|
260 |
qWarning("Net: Bad REMOVETEAM message"); |
|
261 |
return; |
|
262 |
} |
|
352 | 263 |
m_pTeamSelWidget->removeNetTeam(HWTeam(lst[1], lst[2].toUInt())); |
347 | 264 |
return; |
265 |
} |
|
266 |
||
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
267 |
if(lst[0]=="JOINED") { |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
268 |
if(lst.size() < 2) |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
269 |
{ |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
270 |
qWarning("Net: Bad JOINED message"); |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
271 |
return; |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
272 |
} |
1311 | 273 |
|
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
274 |
for(int i = 1; i < lst.size(); ++i) |
1311 | 275 |
{ |
1313
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
276 |
if (lst[i] == mynick) |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
277 |
{ |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
278 |
netClientState = 3; |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
279 |
emit EnteredGame(); |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
280 |
ConfigAsked(); |
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
281 |
} |
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
282 |
emit nickAdded(lst[i]); |
1311 | 283 |
} |
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
284 |
return; |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
285 |
} |
455 | 286 |
|
1310 | 287 |
if(lst[0]=="LEFT") { |
288 |
if(lst.size() < 2) |
|
289 |
{ |
|
290 |
qWarning("Net: Bad LEFT message"); |
|
291 |
return; |
|
292 |
} |
|
293 |
emit nickRemoved(lst[1]); |
|
294 |
return; |
|
295 |
} |
|
461 | 296 |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
338
diff
changeset
|
297 |
if (lst[0] == "RUNGAME") { |
396 | 298 |
RunGame(); |
299 |
return; |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
338
diff
changeset
|
300 |
} |
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
338
diff
changeset
|
301 |
|
315 | 302 |
if (lst[0] == "CONFIGURED") { |
1082 | 303 |
QStringList tmp = lst; |
304 |
tmp.removeFirst(); |
|
305 |
if(tmp.size() < 6) |
|
573 | 306 |
{ |
307 |
qWarning("Net: Bad CONFIGURED message"); |
|
308 |
return; |
|
309 |
} |
|
1082 | 310 |
emit seedChanged(tmp[0]); |
311 |
emit mapChanged(tmp[1]); |
|
312 |
emit themeChanged(tmp[2]); |
|
313 |
emit initHealthChanged(tmp[3].toUInt()); |
|
314 |
emit turnTimeChanged(tmp[4].toUInt()); |
|
315 |
emit fortsModeChanged(tmp[5].toInt() != 0); |
|
315 | 316 |
return; |
317 |
} |
|
318 |
||
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
319 |
if(lst[0]=="TEAM_ACCEPTED") { |
573 | 320 |
if(lst.size() < 3) |
321 |
{ |
|
322 |
qWarning("Net: Bad TEAM_ACCEPTED message"); |
|
323 |
return; |
|
324 |
} |
|
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
325 |
m_networkToLocalteams.insert(lst[2].toUInt(), lst[1]); |
373 | 326 |
m_pTeamSelWidget->changeTeamStatus(lst[1]); |
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
327 |
return; |
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
328 |
} |
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
329 |
|
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
330 |
if (lst[0] == "CONFIG_PARAM") { |
573 | 331 |
if(lst.size() < 3) |
332 |
{ |
|
333 |
qWarning("Net: Bad CONFIG_PARAM message"); |
|
334 |
return; |
|
335 |
} |
|
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
336 |
if (lst[1] == "SEED") { |
332 | 337 |
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
|
338 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
339 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
340 |
if (lst[1] == "MAP") { |
332 | 341 |
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
|
342 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
343 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
344 |
if (lst[1] == "THEME") { |
332 | 345 |
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
|
346 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
347 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
348 |
if (lst[1] == "HEALTH") { |
332 | 349 |
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
|
350 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
351 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
352 |
if (lst[1] == "TURNTIME") { |
332 | 353 |
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
|
354 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
355 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
356 |
if (lst[1] == "FORTSMODE") { |
332 | 357 |
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
|
358 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
359 |
} |
697 | 360 |
if (lst[1] == "AMMO") { |
703 | 361 |
if(lst.size() < 4) return; |
362 |
emit ammoChanged(lst[3], lst[2]); |
|
697 | 363 |
return; |
364 |
} |
|
401 | 365 |
QStringList hhTmpList=lst[1].split('+'); |
366 |
if (hhTmpList[0] == "TEAM_COLOR") { |
|
367 |
HWTeam tmptm(hhTmpList[1], hhTmpList[2].toUInt()); |
|
368 |
if(m_networkToLocalteams.find(hhTmpList[2].toUInt())!=m_networkToLocalteams.end()) { |
|
369 |
tmptm=HWTeam(hhTmpList[1]); // local team should be changed |
|
372 | 370 |
} |
401 | 371 |
tmptm.teamColor=QColor(lst[2]); |
372 | 372 |
emit teamColorChanged(tmptm); |
373 |
return; |
|
374 |
} |
|
401 | 375 |
if (hhTmpList[0] == "HHNUM") { |
399 | 376 |
HWTeam tmptm(hhTmpList[1], hhTmpList[2].toUInt()); |
377 |
if(m_networkToLocalteams.find(hhTmpList[2].toUInt())!=m_networkToLocalteams.end()) { |
|
378 |
tmptm=HWTeam(hhTmpList[1]); // local team should be changed |
|
379 |
} |
|
380 |
tmptm.numHedgehogs=lst[2].toUInt(); |
|
381 |
emit hhnumChanged(tmptm); |
|
382 |
return; |
|
383 |
} |
|
1082 | 384 |
qWarning() << "Net: Unknown 'CONFIG_PARAM' message:" << lst; |
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
385 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
386 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
387 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
388 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
389 |
// 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
|
390 |
// "CONNECTED" at round phase, etc. |
1310 | 391 |
if (lst[0] == "GAMEMSG") { |
573 | 392 |
if(lst.size() < 2) |
393 |
{ |
|
1310 | 394 |
qWarning("Net: Bad GAMEMSG message"); |
573 | 395 |
return; |
396 |
} |
|
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
397 |
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
|
398 |
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
|
399 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
400 |
} |
573 | 401 |
|
1082 | 402 |
qWarning() << "Net: Unknown message:" << lst; |
315 | 403 |
} |
404 |
||
405 |
||
406 |
void HWNewNet::ConfigAsked() |
|
407 |
{ |
|
1310 | 408 |
QString map = m_pGameCFGWidget->getCurrentMap(); |
409 |
if (map.size()) |
|
410 |
onMapChanged(map); |
|
574 | 411 |
|
1310 | 412 |
onSeedChanged(m_pGameCFGWidget->getCurrentSeed()); |
413 |
onThemeChanged(m_pGameCFGWidget->getCurrentTheme()); |
|
414 |
onInitHealthChanged(m_pGameCFGWidget->getInitHealth()); |
|
415 |
onTurnTimeChanged(m_pGameCFGWidget->getTurnTime()); |
|
416 |
onFortsModeChanged(m_pGameCFGWidget->getGameFlags() & 0x1); |
|
417 |
// always initialize with default ammo (also avoiding complicated cross-class dependencies) |
|
418 |
onWeaponsNameChanged("Default", cDefaultAmmoStore->mid(10)); |
|
315 | 419 |
} |
420 |
||
421 |
void HWNewNet::RunGame() |
|
422 |
{ |
|
1310 | 423 |
emit AskForRunGame(); |
431 | 424 |
} |
425 |
||
352 | 426 |
void HWNewNet::onHedgehogsNumChanged(const HWTeam& team) |
427 |
{ |
|
455 | 428 |
RawSendNet(QString("HHNUM%1%2%1%3%1%4").arg(delimeter).arg(team.TeamName)\ |
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
429 |
.arg(team.getNetID() ? team.getNetID() : m_networkToLocalteams.key(team.TeamName))\ |
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
430 |
.arg(team.numHedgehogs)); |
352 | 431 |
} |
432 |
||
372 | 433 |
void HWNewNet::onTeamColorChanged(const HWTeam& team) |
434 |
{ |
|
401 | 435 |
RawSendNet(QString("CONFIG_PARAM%1TEAM_COLOR+%2+%3%1%4").arg(delimeter).arg(team.TeamName)\ |
372 | 436 |
.arg(team.getNetID() ? team.getNetID() : m_networkToLocalteams.key(team.TeamName))\ |
437 |
.arg(team.teamColor.name())); |
|
438 |
} |
|
439 |
||
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
440 |
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
|
441 |
{ |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
442 |
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
|
443 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
444 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
445 |
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
|
446 |
{ |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
447 |
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
|
448 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
449 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
450 |
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
|
451 |
{ |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
452 |
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
|
453 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
454 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
455 |
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
|
456 |
{ |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
457 |
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
|
458 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
459 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
460 |
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
|
461 |
{ |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
462 |
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
|
463 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
464 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
465 |
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
|
466 |
{ |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
467 |
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
|
468 |
} |
453 | 469 |
|
703 | 470 |
void HWNewNet::onWeaponsNameChanged(const QString& name, const QString& ammo) |
697 | 471 |
{ |
703 | 472 |
RawSendNet(QString("CONFIG_PARAM%1AMMO%1%2%1%3").arg(delimeter).arg(ammo).arg(name)); |
697 | 473 |
} |
474 |
||
453 | 475 |
void HWNewNet::chatLineToNet(const QString& str) |
476 |
{ |
|
477 |
if(str!="") { |
|
478 |
RawSendNet(QString("CHAT_STRING")+delimeter+mynick+delimeter+str); |
|
479 |
emit(chatStringFromNet(QStringList(mynick) << str)); |
|
480 |
} |
|
481 |
} |
|
1315 | 482 |
|
483 |
void HWNewNet::askRoomsList() |
|
484 |
{ |
|
485 |
if(netClientState != 2) |
|
486 |
{ |
|
487 |
qDebug("Illegal try to get rooms list!"); |
|
488 |
return; |
|
489 |
} |
|
490 |
RawSendNet(QString("LIST")); |
|
491 |
} |