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