author | unc0rr |
Wed, 08 Oct 2008 15:53:23 +0000 | |
changeset 1318 | 18da1c5e960d |
parent 1316 | 50514e45d0b5 |
child 1319 | fedaae857a50 |
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(); |
1316 | 280 |
if (isChief) |
281 |
ConfigAsked(); |
|
1313
f4c54e9e1b8c
- Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents:
1311
diff
changeset
|
282 |
} |
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
283 |
emit nickAdded(lst[i]); |
1311 | 284 |
} |
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
285 |
return; |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
286 |
} |
455 | 287 |
|
1310 | 288 |
if(lst[0]=="LEFT") { |
289 |
if(lst.size() < 2) |
|
290 |
{ |
|
291 |
qWarning("Net: Bad LEFT message"); |
|
292 |
return; |
|
293 |
} |
|
294 |
emit nickRemoved(lst[1]); |
|
295 |
return; |
|
296 |
} |
|
461 | 297 |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
338
diff
changeset
|
298 |
if (lst[0] == "RUNGAME") { |
396 | 299 |
RunGame(); |
300 |
return; |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
338
diff
changeset
|
301 |
} |
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
338
diff
changeset
|
302 |
|
315 | 303 |
if (lst[0] == "CONFIGURED") { |
1082 | 304 |
QStringList tmp = lst; |
305 |
tmp.removeFirst(); |
|
306 |
if(tmp.size() < 6) |
|
573 | 307 |
{ |
308 |
qWarning("Net: Bad CONFIGURED message"); |
|
309 |
return; |
|
310 |
} |
|
1082 | 311 |
emit seedChanged(tmp[0]); |
312 |
emit mapChanged(tmp[1]); |
|
313 |
emit themeChanged(tmp[2]); |
|
314 |
emit initHealthChanged(tmp[3].toUInt()); |
|
315 |
emit turnTimeChanged(tmp[4].toUInt()); |
|
316 |
emit fortsModeChanged(tmp[5].toInt() != 0); |
|
315 | 317 |
return; |
318 |
} |
|
319 |
||
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
320 |
if(lst[0]=="TEAM_ACCEPTED") { |
573 | 321 |
if(lst.size() < 3) |
322 |
{ |
|
323 |
qWarning("Net: Bad TEAM_ACCEPTED message"); |
|
324 |
return; |
|
325 |
} |
|
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
326 |
m_networkToLocalteams.insert(lst[2].toUInt(), lst[1]); |
373 | 327 |
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
|
328 |
return; |
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
329 |
} |
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
330 |
|
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
331 |
if (lst[0] == "CONFIG_PARAM") { |
573 | 332 |
if(lst.size() < 3) |
333 |
{ |
|
334 |
qWarning("Net: Bad CONFIG_PARAM message"); |
|
335 |
return; |
|
336 |
} |
|
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
337 |
if (lst[1] == "SEED") { |
332 | 338 |
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
|
339 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
340 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
341 |
if (lst[1] == "MAP") { |
332 | 342 |
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
|
343 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
344 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
345 |
if (lst[1] == "THEME") { |
332 | 346 |
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
|
347 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
348 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
349 |
if (lst[1] == "HEALTH") { |
332 | 350 |
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
|
351 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
352 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
353 |
if (lst[1] == "TURNTIME") { |
332 | 354 |
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
|
355 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
356 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
357 |
if (lst[1] == "FORTSMODE") { |
332 | 358 |
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
|
359 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
360 |
} |
697 | 361 |
if (lst[1] == "AMMO") { |
703 | 362 |
if(lst.size() < 4) return; |
363 |
emit ammoChanged(lst[3], lst[2]); |
|
697 | 364 |
return; |
365 |
} |
|
401 | 366 |
QStringList hhTmpList=lst[1].split('+'); |
367 |
if (hhTmpList[0] == "TEAM_COLOR") { |
|
368 |
HWTeam tmptm(hhTmpList[1], hhTmpList[2].toUInt()); |
|
369 |
if(m_networkToLocalteams.find(hhTmpList[2].toUInt())!=m_networkToLocalteams.end()) { |
|
370 |
tmptm=HWTeam(hhTmpList[1]); // local team should be changed |
|
372 | 371 |
} |
401 | 372 |
tmptm.teamColor=QColor(lst[2]); |
372 | 373 |
emit teamColorChanged(tmptm); |
374 |
return; |
|
375 |
} |
|
401 | 376 |
if (hhTmpList[0] == "HHNUM") { |
399 | 377 |
HWTeam tmptm(hhTmpList[1], hhTmpList[2].toUInt()); |
378 |
if(m_networkToLocalteams.find(hhTmpList[2].toUInt())!=m_networkToLocalteams.end()) { |
|
379 |
tmptm=HWTeam(hhTmpList[1]); // local team should be changed |
|
380 |
} |
|
381 |
tmptm.numHedgehogs=lst[2].toUInt(); |
|
382 |
emit hhnumChanged(tmptm); |
|
383 |
return; |
|
384 |
} |
|
1082 | 385 |
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
|
386 |
return; |
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 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
390 |
// 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
|
391 |
// "CONNECTED" at round phase, etc. |
1310 | 392 |
if (lst[0] == "GAMEMSG") { |
573 | 393 |
if(lst.size() < 2) |
394 |
{ |
|
1310 | 395 |
qWarning("Net: Bad GAMEMSG message"); |
573 | 396 |
return; |
397 |
} |
|
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
398 |
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
|
399 |
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
|
400 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
401 |
} |
573 | 402 |
|
1082 | 403 |
qWarning() << "Net: Unknown message:" << lst; |
315 | 404 |
} |
405 |
||
406 |
||
407 |
void HWNewNet::ConfigAsked() |
|
408 |
{ |
|
1310 | 409 |
QString map = m_pGameCFGWidget->getCurrentMap(); |
410 |
if (map.size()) |
|
411 |
onMapChanged(map); |
|
574 | 412 |
|
1310 | 413 |
onSeedChanged(m_pGameCFGWidget->getCurrentSeed()); |
414 |
onThemeChanged(m_pGameCFGWidget->getCurrentTheme()); |
|
415 |
onInitHealthChanged(m_pGameCFGWidget->getInitHealth()); |
|
416 |
onTurnTimeChanged(m_pGameCFGWidget->getTurnTime()); |
|
417 |
onFortsModeChanged(m_pGameCFGWidget->getGameFlags() & 0x1); |
|
418 |
// always initialize with default ammo (also avoiding complicated cross-class dependencies) |
|
419 |
onWeaponsNameChanged("Default", cDefaultAmmoStore->mid(10)); |
|
315 | 420 |
} |
421 |
||
422 |
void HWNewNet::RunGame() |
|
423 |
{ |
|
1310 | 424 |
emit AskForRunGame(); |
431 | 425 |
} |
426 |
||
352 | 427 |
void HWNewNet::onHedgehogsNumChanged(const HWTeam& team) |
428 |
{ |
|
455 | 429 |
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
|
430 |
.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
|
431 |
.arg(team.numHedgehogs)); |
352 | 432 |
} |
433 |
||
372 | 434 |
void HWNewNet::onTeamColorChanged(const HWTeam& team) |
435 |
{ |
|
401 | 436 |
RawSendNet(QString("CONFIG_PARAM%1TEAM_COLOR+%2+%3%1%4").arg(delimeter).arg(team.TeamName)\ |
372 | 437 |
.arg(team.getNetID() ? team.getNetID() : m_networkToLocalteams.key(team.TeamName))\ |
438 |
.arg(team.teamColor.name())); |
|
439 |
} |
|
440 |
||
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
441 |
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
|
442 |
{ |
1318 | 443 |
if (isChief) RawSendNet(QString("CONFIG_PARAM%1SEED%1%2").arg(delimeter).arg(seed)); |
329
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 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
446 |
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
|
447 |
{ |
1318 | 448 |
if (isChief) RawSendNet(QString("CONFIG_PARAM%1MAP%1%2").arg(delimeter).arg(map)); |
329
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 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
451 |
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
|
452 |
{ |
1318 | 453 |
if (isChief) RawSendNet(QString("CONFIG_PARAM%1THEME%1%2").arg(delimeter).arg(theme)); |
329
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 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
456 |
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
|
457 |
{ |
1318 | 458 |
if (isChief) RawSendNet(QString("CONFIG_PARAM%1HEALTH%1%2").arg(delimeter).arg(health)); |
329
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 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
461 |
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
|
462 |
{ |
1318 | 463 |
if (isChief) RawSendNet(QString("CONFIG_PARAM%1TURNTIME%1%2").arg(delimeter).arg(time)); |
329
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 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
466 |
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
|
467 |
{ |
1318 | 468 |
if (isChief) RawSendNet(QString("CONFIG_PARAM%1FORTSMODE%1%2").arg(delimeter).arg(value)); |
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
469 |
} |
453 | 470 |
|
703 | 471 |
void HWNewNet::onWeaponsNameChanged(const QString& name, const QString& ammo) |
697 | 472 |
{ |
1318 | 473 |
if (isChief) RawSendNet(QString("CONFIG_PARAM%1AMMO%1%2%1%3").arg(delimeter).arg(ammo).arg(name)); |
697 | 474 |
} |
475 |
||
453 | 476 |
void HWNewNet::chatLineToNet(const QString& str) |
477 |
{ |
|
478 |
if(str!="") { |
|
479 |
RawSendNet(QString("CHAT_STRING")+delimeter+mynick+delimeter+str); |
|
480 |
emit(chatStringFromNet(QStringList(mynick) << str)); |
|
481 |
} |
|
482 |
} |
|
1315 | 483 |
|
484 |
void HWNewNet::askRoomsList() |
|
485 |
{ |
|
486 |
if(netClientState != 2) |
|
487 |
{ |
|
488 |
qDebug("Illegal try to get rooms list!"); |
|
489 |
return; |
|
490 |
} |
|
491 |
RawSendNet(QString("LIST")); |
|
492 |
} |