QTfrontend/newnetclient.cpp
author displacer
Wed, 10 Jan 2007 23:24:55 +0000
changeset 315 73003488240b
child 319 7f3bd9e31f18
permissions -rw-r--r--
client/server net pre-alpha

/*
 * Hedgewars, a worms-like game
 * Copyright (c) 2006 Ulyanov Igor <iulyanov@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <QUuid>
#include <QMessageBox>
#include <QDebug>

#include "newnetclient.h"
#include "proto.h"
#include "gameuiconfig.h"
#include "game.h"

char delimeter='\t';

HWNewNet::HWNewNet(GameUIConfig * config) :
  config(config)
{
  connect(&NetSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
  connect(&NetSocket, SIGNAL(connected()), this, SLOT(OnConnect()));
  connect(&NetSocket, SIGNAL(disconnected()), this, SLOT(OnDisconnect()));
  connect(&NetSocket, SIGNAL(error(QAbstractSocket::SocketError)), this,
	  SLOT(displayError(QAbstractSocket::SocketError)));
}

void HWNewNet::Connect(const QString & hostName, quint16 port, const QString & nick)
{
  qDebug() << hostName << ":" << port;
  NetSocket.connectToHost(hostName, port);
  mynick = nick;
}

void HWNewNet::Disconnect()
{
  NetSocket.disconnect();
}

void HWNewNet::JoinGame(const QString & game)
{
  RawSendNet(QString("JOIN %1").arg(game));
}

void HWNewNet::AddTeam(const HWTeam & team)
{
  RawSendNet(QString("ADDTEAM:") + delimeter +
	     team.TeamName + delimeter + team.HHName[0] + delimeter + team.HHName[1] + delimeter +
	     team.HHName[2] + delimeter + team.HHName[3] + delimeter + team.HHName[4] + delimeter +
	     team.HHName[5] + delimeter + team.HHName[6] + delimeter + team.HHName[7]);
}

void HWNewNet::StartGame()
{
  seed = QUuid::createUuid().toString();
  RawSendNet(QString("START:") + delimeter + seed);
}

void HWNewNet::SendNet(const QByteArray & buf)
{
  QString msg = QString(buf.toBase64());

  //NetBuffer += buf;
  //RawSendNet(QString("PRIVMSG %1 :"MAGIC_CHAR MAGIC_CHAR"%2").arg(channel, msg));
}

void HWNewNet::RawSendNet(const QString & str)
{
  RawSendNet(str.toUtf8());
}

void HWNewNet::RawSendNet(const QByteArray & buf)
{
  if (buf.size() > 510) return;
  NetSocket.write(buf);
  NetSocket.write("\n", 1);
}

void HWNewNet::ClientRead()
{
  while (NetSocket.canReadLine()) {
    ParseLine(NetSocket.readLine().trimmed());
  }
}

void HWNewNet::OnConnect()
{
  RawSendNet(QString("USER") + delimeter + "hwgame 1 2 Hedgewars game");
  RawSendNet(QString("NICK%1%2").arg(delimeter).arg(mynick));
}

void HWNewNet::OnDisconnect()
{
  emit ChangeInTeams(QStringList());
  emit Disconnected();
}

void HWNewNet::displayError(QAbstractSocket::SocketError socketError)
{
  switch (socketError) {
  case QAbstractSocket::RemoteHostClosedError:
    break;
  case QAbstractSocket::HostNotFoundError:
    QMessageBox::information(0, tr("Error"),
			     tr("The host was not found. Please check the host name and port settings."));
    break;
  case QAbstractSocket::ConnectionRefusedError:
    QMessageBox::information(0, tr("Error"),
			     tr("Connection refused"));
    break;
  default:
    QMessageBox::information(0, tr("Error"),
			     NetSocket.errorString());
  }
}

void HWNewNet::ParseLine(const QByteArray & line)
{
  QString msg = QString::fromUtf8 (line.data(), line.size());
  
  qDebug() << "line " << msg << " received";

  QStringList lst = msg.split(delimeter);
  if (lst[0] == "ERRONEUSNICKNAME") {
    QMessageBox::information(0, 0, "Your net nickname is in use or cannot be used");
    return;
  }

  if (lst[0] == "CONNECTED") {
    emit Connected();
    emit EnteredGame();
    return;
  }

  if (lst[0] == "TEAMCHANGED") {
    lst.pop_front();
    emit ChangeInTeams(lst);
    return;
  }

  if (lst[0] == "CONFIGASKED") {
    ConfigAsked();
    return;
  }
  
  if (lst[0] == "CONFIGURED") {
    lst.pop_front();
    RunGame();
    qDebug() << lst[0];
    QByteArray ar=QByteArray::fromBase64(lst[0].toAscii());
    emit FromNet(ar);
    
    lst.pop_front();
    QByteArray cache;
    emit FromNet(HWProto::addStringListToBuffer(cache, lst));
    return;
  }

  QByteArray em = QByteArray::fromBase64(msg.toAscii());
  emit FromNet(em);
}


void HWNewNet::ConfigAsked()
{
  QByteArray cache;
  HWProto::addStringToBuffer(cache, "eseed " + seed);
  HWProto::addStringToBuffer(cache, "e$gmflags 0");
  HWProto::addStringToBuffer(cache, QString("etheme %1").arg(config->GetRandomTheme()));
  QString _msg = QString("CONFIGANSWER") + delimeter + QString(cache.toBase64());
  RawSendNet(_msg);
}

void HWNewNet::RunGame()
{
  HWGame * game = new HWGame(config, 0);
  connect(game, SIGNAL(SendNet(const QByteArray &)), this, SLOT(SendNet(const QByteArray &)));
  connect(this, SIGNAL(FromNet(const QByteArray &)), game, SLOT(FromNet(const QByteArray &)));
  connect(this, SIGNAL(LocalCFG(const QString &)), game, SLOT(LocalCFG(const QString &)));
  game->StartNet();
}