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 |
{
|
|
73 |
QString msg = QString(buf.toBase64());
|
|
74 |
|
|
75 |
//NetBuffer += buf;
|
|
76 |
//RawSendNet(QString("PRIVMSG %1 :"MAGIC_CHAR MAGIC_CHAR"%2").arg(channel, msg));
|
|
77 |
}
|
|
78 |
|
|
79 |
void HWNewNet::RawSendNet(const QString & str)
|
|
80 |
{
|
|
81 |
RawSendNet(str.toUtf8());
|
|
82 |
}
|
|
83 |
|
|
84 |
void HWNewNet::RawSendNet(const QByteArray & buf)
|
|
85 |
{
|
|
86 |
if (buf.size() > 510) return;
|
|
87 |
NetSocket.write(buf);
|
|
88 |
NetSocket.write("\n", 1);
|
|
89 |
}
|
|
90 |
|
|
91 |
void HWNewNet::ClientRead()
|
|
92 |
{
|
|
93 |
while (NetSocket.canReadLine()) {
|
|
94 |
ParseLine(NetSocket.readLine().trimmed());
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
|
98 |
void HWNewNet::OnConnect()
|
|
99 |
{
|
|
100 |
RawSendNet(QString("USER") + delimeter + "hwgame 1 2 Hedgewars game");
|
|
101 |
RawSendNet(QString("NICK%1%2").arg(delimeter).arg(mynick));
|
|
102 |
}
|
|
103 |
|
|
104 |
void HWNewNet::OnDisconnect()
|
|
105 |
{
|
|
106 |
emit ChangeInTeams(QStringList());
|
|
107 |
emit Disconnected();
|
|
108 |
}
|
|
109 |
|
|
110 |
void HWNewNet::displayError(QAbstractSocket::SocketError socketError)
|
|
111 |
{
|
|
112 |
switch (socketError) {
|
|
113 |
case QAbstractSocket::RemoteHostClosedError:
|
|
114 |
break;
|
|
115 |
case QAbstractSocket::HostNotFoundError:
|
|
116 |
QMessageBox::information(0, tr("Error"),
|
|
117 |
tr("The host was not found. Please check the host name and port settings."));
|
|
118 |
break;
|
|
119 |
case QAbstractSocket::ConnectionRefusedError:
|
|
120 |
QMessageBox::information(0, tr("Error"),
|
|
121 |
tr("Connection refused"));
|
|
122 |
break;
|
|
123 |
default:
|
|
124 |
QMessageBox::information(0, tr("Error"),
|
|
125 |
NetSocket.errorString());
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
void HWNewNet::ParseLine(const QByteArray & line)
|
|
130 |
{
|
|
131 |
QString msg = QString::fromUtf8 (line.data(), line.size());
|
|
132 |
|
|
133 |
qDebug() << "line " << msg << " received";
|
|
134 |
|
|
135 |
QStringList lst = msg.split(delimeter);
|
|
136 |
if (lst[0] == "ERRONEUSNICKNAME") {
|
|
137 |
QMessageBox::information(0, 0, "Your net nickname is in use or cannot be used");
|
|
138 |
return;
|
|
139 |
}
|
|
140 |
|
|
141 |
if (lst[0] == "CONNECTED") {
|
|
142 |
emit Connected();
|
|
143 |
emit EnteredGame();
|
|
144 |
return;
|
|
145 |
}
|
|
146 |
|
|
147 |
if (lst[0] == "TEAMCHANGED") {
|
|
148 |
lst.pop_front();
|
|
149 |
emit ChangeInTeams(lst);
|
|
150 |
return;
|
|
151 |
}
|
|
152 |
|
|
153 |
if (lst[0] == "CONFIGASKED") {
|
|
154 |
ConfigAsked();
|
|
155 |
return;
|
|
156 |
}
|
|
157 |
|
|
158 |
if (lst[0] == "CONFIGURED") {
|
|
159 |
lst.pop_front();
|
|
160 |
RunGame();
|
|
161 |
qDebug() << lst[0];
|
|
162 |
QByteArray ar=QByteArray::fromBase64(lst[0].toAscii());
|
|
163 |
emit FromNet(ar);
|
|
164 |
|
|
165 |
lst.pop_front();
|
|
166 |
QByteArray cache;
|
|
167 |
emit FromNet(HWProto::addStringListToBuffer(cache, lst));
|
|
168 |
return;
|
|
169 |
}
|
|
170 |
|
|
171 |
QByteArray em = QByteArray::fromBase64(msg.toAscii());
|
|
172 |
emit FromNet(em);
|
|
173 |
}
|
|
174 |
|
|
175 |
|
|
176 |
void HWNewNet::ConfigAsked()
|
|
177 |
{
|
|
178 |
QByteArray cache;
|
|
179 |
HWProto::addStringToBuffer(cache, "eseed " + seed);
|
|
180 |
HWProto::addStringToBuffer(cache, "e$gmflags 0");
|
|
181 |
HWProto::addStringToBuffer(cache, QString("etheme %1").arg(config->GetRandomTheme()));
|
|
182 |
QString _msg = QString("CONFIGANSWER") + delimeter + QString(cache.toBase64());
|
|
183 |
RawSendNet(_msg);
|
|
184 |
}
|
|
185 |
|
|
186 |
void HWNewNet::RunGame()
|
|
187 |
{
|
|
188 |
HWGame * game = new HWGame(config, 0);
|
|
189 |
connect(game, SIGNAL(SendNet(const QByteArray &)), this, SLOT(SendNet(const QByteArray &)));
|
|
190 |
connect(this, SIGNAL(FromNet(const QByteArray &)), game, SLOT(FromNet(const QByteArray &)));
|
|
191 |
connect(this, SIGNAL(LocalCFG(const QString &)), game, SLOT(LocalCFG(const QString &)));
|
|
192 |
game->StartNet();
|
|
193 |
}
|