author | unc0rr |
Wed, 08 Oct 2008 15:57:35 +0000 | |
changeset 1320 | bffc7262e25e |
parent 1319 | fedaae857a50 |
child 1321 | d7dc4e86201e |
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 |
|
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
304 |
if(lst[0]=="TEAM_ACCEPTED") { |
573 | 305 |
if(lst.size() < 3) |
306 |
{ |
|
307 |
qWarning("Net: Bad TEAM_ACCEPTED message"); |
|
308 |
return; |
|
309 |
} |
|
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
310 |
m_networkToLocalteams.insert(lst[2].toUInt(), lst[1]); |
373 | 311 |
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
|
312 |
return; |
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
313 |
} |
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
314 |
|
1319 | 315 |
if (lst[0] == "CONFIG_PARAM") { |
316 |
if(lst.size() < 3) |
|
317 |
{ |
|
318 |
qWarning("Net: Bad CONFIG_PARAM message"); |
|
319 |
return; |
|
320 |
} |
|
321 |
if (lst[1] == "SEED") { |
|
322 |
emit seedChanged(lst[2]); |
|
323 |
return; |
|
324 |
} |
|
325 |
if (lst[1] == "MAP") { |
|
326 |
emit mapChanged(lst[2]); |
|
327 |
return; |
|
328 |
} |
|
329 |
if (lst[1] == "THEME") { |
|
330 |
emit themeChanged(lst[2]); |
|
331 |
return; |
|
332 |
} |
|
333 |
if (lst[1] == "HEALTH") { |
|
334 |
emit initHealthChanged(lst[2].toUInt()); |
|
335 |
return; |
|
336 |
} |
|
337 |
if (lst[1] == "TURNTIME") { |
|
338 |
emit turnTimeChanged(lst[2].toUInt()); |
|
339 |
return; |
|
340 |
} |
|
341 |
if (lst[1] == "FORTSMODE") { |
|
342 |
emit fortsModeChanged(lst[2].toInt() != 0); |
|
343 |
return; |
|
344 |
} |
|
345 |
if (lst[1] == "AMMO") { |
|
346 |
if(lst.size() < 4) return; |
|
347 |
emit ammoChanged(lst[3], lst[2]); |
|
348 |
return; |
|
349 |
} |
|
350 |
QStringList hhTmpList=lst[1].split('+'); |
|
351 |
if (hhTmpList[0] == "TEAM_COLOR") { |
|
352 |
HWTeam tmptm(hhTmpList[1], hhTmpList[2].toUInt()); |
|
353 |
if(m_networkToLocalteams.find(hhTmpList[2].toUInt())!=m_networkToLocalteams.end()) { |
|
354 |
tmptm=HWTeam(hhTmpList[1]); // local team should be changed |
|
355 |
} |
|
356 |
tmptm.teamColor=QColor(lst[2]); |
|
357 |
emit teamColorChanged(tmptm); |
|
358 |
return; |
|
359 |
} |
|
360 |
if (hhTmpList[0] == "HHNUM") { |
|
361 |
HWTeam tmptm(hhTmpList[1], hhTmpList[2].toUInt()); |
|
362 |
if(m_networkToLocalteams.find(hhTmpList[2].toUInt())!=m_networkToLocalteams.end()) { |
|
363 |
tmptm=HWTeam(hhTmpList[1]); // local team should be changed |
|
364 |
} |
|
365 |
tmptm.numHedgehogs=lst[2].toUInt(); |
|
366 |
emit hhnumChanged(tmptm); |
|
367 |
return; |
|
368 |
} |
|
369 |
qWarning() << "Net: Unknown 'CONFIG_PARAM' message:" << lst; |
|
370 |
return; |
|
697 | 371 |
} |
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
372 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
373 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
374 |
// 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
|
375 |
// "CONNECTED" at round phase, etc. |
1310 | 376 |
if (lst[0] == "GAMEMSG") { |
573 | 377 |
if(lst.size() < 2) |
378 |
{ |
|
1310 | 379 |
qWarning("Net: Bad GAMEMSG message"); |
573 | 380 |
return; |
381 |
} |
|
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
382 |
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
|
383 |
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
|
384 |
return; |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
385 |
} |
573 | 386 |
|
1082 | 387 |
qWarning() << "Net: Unknown message:" << lst; |
315 | 388 |
} |
389 |
||
390 |
||
391 |
void HWNewNet::ConfigAsked() |
|
392 |
{ |
|
1310 | 393 |
QString map = m_pGameCFGWidget->getCurrentMap(); |
394 |
if (map.size()) |
|
395 |
onMapChanged(map); |
|
574 | 396 |
|
1310 | 397 |
onSeedChanged(m_pGameCFGWidget->getCurrentSeed()); |
398 |
onThemeChanged(m_pGameCFGWidget->getCurrentTheme()); |
|
399 |
onInitHealthChanged(m_pGameCFGWidget->getInitHealth()); |
|
400 |
onTurnTimeChanged(m_pGameCFGWidget->getTurnTime()); |
|
401 |
onFortsModeChanged(m_pGameCFGWidget->getGameFlags() & 0x1); |
|
402 |
// always initialize with default ammo (also avoiding complicated cross-class dependencies) |
|
403 |
onWeaponsNameChanged("Default", cDefaultAmmoStore->mid(10)); |
|
315 | 404 |
} |
405 |
||
406 |
void HWNewNet::RunGame() |
|
407 |
{ |
|
1310 | 408 |
emit AskForRunGame(); |
431 | 409 |
} |
410 |
||
352 | 411 |
void HWNewNet::onHedgehogsNumChanged(const HWTeam& team) |
412 |
{ |
|
455 | 413 |
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
|
414 |
.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
|
415 |
.arg(team.numHedgehogs)); |
352 | 416 |
} |
417 |
||
372 | 418 |
void HWNewNet::onTeamColorChanged(const HWTeam& team) |
419 |
{ |
|
401 | 420 |
RawSendNet(QString("CONFIG_PARAM%1TEAM_COLOR+%2+%3%1%4").arg(delimeter).arg(team.TeamName)\ |
372 | 421 |
.arg(team.getNetID() ? team.getNetID() : m_networkToLocalteams.key(team.TeamName))\ |
422 |
.arg(team.teamColor.name())); |
|
423 |
} |
|
424 |
||
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
425 |
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
|
426 |
{ |
1318 | 427 |
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
|
428 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
429 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
430 |
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
|
431 |
{ |
1318 | 432 |
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
|
433 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
434 |
|
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
435 |
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
|
436 |
{ |
1318 | 437 |
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
|
438 |
} |
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
322
diff
changeset
|
439 |
|
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::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
|
441 |
{ |
1318 | 442 |
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
|
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::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
|
446 |
{ |
1318 | 447 |
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
|
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::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
|
451 |
{ |
1318 | 452 |
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
|
453 |
} |
453 | 454 |
|
703 | 455 |
void HWNewNet::onWeaponsNameChanged(const QString& name, const QString& ammo) |
697 | 456 |
{ |
1318 | 457 |
if (isChief) RawSendNet(QString("CONFIG_PARAM%1AMMO%1%2%1%3").arg(delimeter).arg(ammo).arg(name)); |
697 | 458 |
} |
459 |
||
453 | 460 |
void HWNewNet::chatLineToNet(const QString& str) |
461 |
{ |
|
462 |
if(str!="") { |
|
463 |
RawSendNet(QString("CHAT_STRING")+delimeter+mynick+delimeter+str); |
|
464 |
emit(chatStringFromNet(QStringList(mynick) << str)); |
|
465 |
} |
|
466 |
} |
|
1315 | 467 |
|
468 |
void HWNewNet::askRoomsList() |
|
469 |
{ |
|
470 |
if(netClientState != 2) |
|
471 |
{ |
|
472 |
qDebug("Illegal try to get rooms list!"); |
|
473 |
return; |
|
474 |
} |
|
475 |
RawSendNet(QString("LIST")); |
|
476 |
} |