315
|
1 |
/*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2006 Ulyanov Igor <iulyanov@gmail.com>
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU General Public License as published by
|
|
7 |
* the Free Software Foundation; version 2 of the License
|
|
8 |
*
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
* GNU General Public License for more details.
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
15 |
* along with this program; if not, write to the Free Software
|
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
17 |
*/
|
|
18 |
|
|
19 |
#include <QUuid>
|
|
20 |
#include <QMessageBox>
|
|
21 |
#include <QDebug>
|
|
22 |
|
|
23 |
#include "newnetclient.h"
|
|
24 |
#include "proto.h"
|
|
25 |
#include "gameuiconfig.h"
|
|
26 |
#include "game.h"
|
|
27 |
|
|
28 |
char delimeter='\t';
|
|
29 |
|
|
30 |
HWNewNet::HWNewNet(GameUIConfig * config) :
|
|
31 |
config(config)
|
|
32 |
{
|
|
33 |
connect(&NetSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
|
|
34 |
connect(&NetSocket, SIGNAL(connected()), this, SLOT(OnConnect()));
|
|
35 |
connect(&NetSocket, SIGNAL(disconnected()), this, SLOT(OnDisconnect()));
|
|
36 |
connect(&NetSocket, SIGNAL(error(QAbstractSocket::SocketError)), this,
|
|
37 |
SLOT(displayError(QAbstractSocket::SocketError)));
|
|
38 |
}
|
|
39 |
|
|
40 |
void HWNewNet::Connect(const QString & hostName, quint16 port, const QString & nick)
|
|
41 |
{
|
|
42 |
qDebug() << hostName << ":" << port;
|
|
43 |
NetSocket.connectToHost(hostName, port);
|
|
44 |
mynick = nick;
|
|
45 |
}
|
|
46 |
|
|
47 |
void HWNewNet::Disconnect()
|
|
48 |
{
|
|
49 |
NetSocket.disconnect();
|
|
50 |
}
|
|
51 |
|
|
52 |
void HWNewNet::JoinGame(const QString & game)
|
|
53 |
{
|
|
54 |
RawSendNet(QString("JOIN %1").arg(game));
|
|
55 |
}
|
|
56 |
|
|
57 |
void HWNewNet::AddTeam(const HWTeam & team)
|
|
58 |
{
|
|
59 |
RawSendNet(QString("ADDTEAM:") + delimeter +
|
|
60 |
team.TeamName + delimeter + team.HHName[0] + delimeter + team.HHName[1] + delimeter +
|
|
61 |
team.HHName[2] + delimeter + team.HHName[3] + delimeter + team.HHName[4] + delimeter +
|
|
62 |
team.HHName[5] + delimeter + team.HHName[6] + delimeter + team.HHName[7]);
|
|
63 |
}
|
|
64 |
|
|
65 |
void HWNewNet::StartGame()
|
|
66 |
{
|
|
67 |
seed = QUuid::createUuid().toString();
|
|
68 |
RawSendNet(QString("START:") + delimeter + seed);
|
|
69 |
}
|
|
70 |
|
|
71 |
void HWNewNet::SendNet(const QByteArray & buf)
|
|
72 |
{
|
319
|
73 |
qDebug() << "to net:" << buf;
|
315
|
74 |
QString msg = QString(buf.toBase64());
|
|
75 |
|
|
76 |
//NetBuffer += buf;
|
319
|
77 |
RawSendNet(QString(msg));
|
315
|
78 |
}
|
|
79 |
|
|
80 |
void HWNewNet::RawSendNet(const QString & str)
|
|
81 |
{
|
|
82 |
RawSendNet(str.toUtf8());
|
|
83 |
}
|
|
84 |
|
|
85 |
void HWNewNet::RawSendNet(const QByteArray & buf)
|
|
86 |
{
|
|
87 |
if (buf.size() > 510) return;
|
|
88 |
NetSocket.write(buf);
|
|
89 |
NetSocket.write("\n", 1);
|
|
90 |
}
|
|
91 |
|
|
92 |
void HWNewNet::ClientRead()
|
|
93 |
{
|
|
94 |
while (NetSocket.canReadLine()) {
|
|
95 |
ParseLine(NetSocket.readLine().trimmed());
|
|
96 |
}
|
|
97 |
}
|
|
98 |
|
|
99 |
void HWNewNet::OnConnect()
|
|
100 |
{
|
|
101 |
RawSendNet(QString("USER") + delimeter + "hwgame 1 2 Hedgewars game");
|
|
102 |
RawSendNet(QString("NICK%1%2").arg(delimeter).arg(mynick));
|
|
103 |
}
|
|
104 |
|
|
105 |
void HWNewNet::OnDisconnect()
|
|
106 |
{
|
|
107 |
emit ChangeInTeams(QStringList());
|
|
108 |
emit Disconnected();
|
|
109 |
}
|
|
110 |
|
|
111 |
void HWNewNet::displayError(QAbstractSocket::SocketError socketError)
|
|
112 |
{
|
|
113 |
switch (socketError) {
|
|
114 |
case QAbstractSocket::RemoteHostClosedError:
|
|
115 |
break;
|
|
116 |
case QAbstractSocket::HostNotFoundError:
|
|
117 |
QMessageBox::information(0, tr("Error"),
|
|
118 |
tr("The host was not found. Please check the host name and port settings."));
|
|
119 |
break;
|
|
120 |
case QAbstractSocket::ConnectionRefusedError:
|
|
121 |
QMessageBox::information(0, tr("Error"),
|
|
122 |
tr("Connection refused"));
|
|
123 |
break;
|
|
124 |
default:
|
|
125 |
QMessageBox::information(0, tr("Error"),
|
|
126 |
NetSocket.errorString());
|
|
127 |
}
|
|
128 |
}
|
|
129 |
|
|
130 |
void HWNewNet::ParseLine(const QByteArray & line)
|
|
131 |
{
|
|
132 |
QString msg = QString::fromUtf8 (line.data(), line.size());
|
320
|
133 |
|
315
|
134 |
qDebug() << "line " << msg << " received";
|
|
135 |
|
|
136 |
QStringList lst = msg.split(delimeter);
|
|
137 |
if (lst[0] == "ERRONEUSNICKNAME") {
|
|
138 |
QMessageBox::information(0, 0, "Your net nickname is in use or cannot be used");
|
|
139 |
return;
|
|
140 |
}
|
|
141 |
|
|
142 |
if (lst[0] == "CONNECTED") {
|
|
143 |
emit Connected();
|
|
144 |
emit EnteredGame();
|
|
145 |
return;
|
|
146 |
}
|
|
147 |
|
|
148 |
if (lst[0] == "TEAMCHANGED") {
|
|
149 |
lst.pop_front();
|
|
150 |
emit ChangeInTeams(lst);
|
|
151 |
return;
|
|
152 |
}
|
|
153 |
|
|
154 |
if (lst[0] == "CONFIGASKED") {
|
|
155 |
ConfigAsked();
|
|
156 |
return;
|
|
157 |
}
|
320
|
158 |
|
315
|
159 |
if (lst[0] == "CONFIGURED") {
|
|
160 |
lst.pop_front();
|
|
161 |
RunGame();
|
|
162 |
qDebug() << lst[0];
|
|
163 |
QByteArray ar=QByteArray::fromBase64(lst[0].toAscii());
|
|
164 |
emit FromNet(ar);
|
320
|
165 |
|
315
|
166 |
lst.pop_front();
|
|
167 |
QByteArray cache;
|
|
168 |
emit FromNet(HWProto::addStringListToBuffer(cache, lst));
|
|
169 |
return;
|
|
170 |
}
|
|
171 |
|
|
172 |
QByteArray em = QByteArray::fromBase64(msg.toAscii());
|
319
|
173 |
qDebug() << "to engine:" << em;
|
315
|
174 |
emit FromNet(em);
|
|
175 |
}
|
|
176 |
|
|
177 |
|
|
178 |
void HWNewNet::ConfigAsked()
|
|
179 |
{
|
|
180 |
QByteArray cache;
|
|
181 |
HWProto::addStringToBuffer(cache, "eseed " + seed);
|
319
|
182 |
HWProto::addStringToBuffer(cache, "TN");
|
315
|
183 |
HWProto::addStringToBuffer(cache, "e$gmflags 0");
|
320
|
184 |
HWProto::addStringToBuffer(cache, QString("etheme steel"));
|
315
|
185 |
QString _msg = QString("CONFIGANSWER") + delimeter + QString(cache.toBase64());
|
|
186 |
RawSendNet(_msg);
|
|
187 |
}
|
|
188 |
|
|
189 |
void HWNewNet::RunGame()
|
|
190 |
{
|
|
191 |
HWGame * game = new HWGame(config, 0);
|
|
192 |
connect(game, SIGNAL(SendNet(const QByteArray &)), this, SLOT(SendNet(const QByteArray &)));
|
|
193 |
connect(this, SIGNAL(FromNet(const QByteArray &)), game, SLOT(FromNet(const QByteArray &)));
|
|
194 |
connect(this, SIGNAL(LocalCFG(const QString &)), game, SLOT(LocalCFG(const QString &)));
|
|
195 |
game->StartNet();
|
|
196 |
}
|