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