QTfrontend/netserver.cpp
author displacer
Wed, 07 Feb 2007 22:54:24 +0000
changeset 412 2ddcc3e3e644
parent 404 64a62b679b04
child 420 6cdfc07dceed
permissions -rw-r--r--
udp server detecting experimental version
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 "netserver.h"
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    20
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    21
#include <QTcpServer>
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    22
#include <QTcpSocket>
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    23
#include <QMessageBox>
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    24
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    25
#include <algorithm>
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    26
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    27
#include <QDebug>
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    28
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    29
const quint16 HWNetServer::ds_port=46631;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    30
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    31
extern char delimeter;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    32
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    33
void HWNetServer::StartServer()
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    34
{
399
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
    35
  hhnum=0;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    36
  IPCServer = new QTcpServer(this);
347
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
    37
  if (!IPCServer->listen(QHostAddress::Any, ds_port)) {
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    38
    QMessageBox::critical(0, tr("Error"),
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    39
			  tr("Unable to start the server: %1.")
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    40
			  .arg(IPCServer->errorString()));
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    41
  }
326
c6718bfdd642 Small fixes
unc0rr
parents: 319
diff changeset
    42
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    43
  connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    44
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    45
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    46
void HWNetServer::StopServer()
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    47
{
383
09a8795105a4 some disconnect and remove teams bugfixes
displacer
parents: 382
diff changeset
    48
  QList<HWConnectedClient*>::iterator it;
09a8795105a4 some disconnect and remove teams bugfixes
displacer
parents: 382
diff changeset
    49
  for(it=connclients.begin(); it!=connclients.end(); ++it) {
09a8795105a4 some disconnect and remove teams bugfixes
displacer
parents: 382
diff changeset
    50
    ClientDisconnect(*it);
09a8795105a4 some disconnect and remove teams bugfixes
displacer
parents: 382
diff changeset
    51
  }
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    52
  IPCServer->close();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    53
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    54
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    55
void HWNetServer::NewConnection()
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    56
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    57
  QTcpSocket* client = IPCServer->nextPendingConnection();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    58
  if(!client) return;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    59
  connclients.push_back(new HWConnectedClient(this, client));
326
c6718bfdd642 Small fixes
unc0rr
parents: 319
diff changeset
    60
  connect(connclients.back(), SIGNAL(HWClientDisconnected(HWConnectedClient*)),
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    61
	  this, SLOT(ClientDisconnect(HWConnectedClient*)));
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    62
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    63
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    64
void HWNetServer::ClientDisconnect(HWConnectedClient* client)
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    65
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    66
  QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client);
383
09a8795105a4 some disconnect and remove teams bugfixes
displacer
parents: 382
diff changeset
    67
  if(it==connclients.end()) return;
350
c3ccec3834e8 more correct net disconnection
displacer
parents: 349
diff changeset
    68
  for(QList<QStringList>::iterator tmIt=(*it)->m_teamsCfg.begin(); tmIt!=(*it)->m_teamsCfg.end(); ++tmIt) {
383
09a8795105a4 some disconnect and remove teams bugfixes
displacer
parents: 382
diff changeset
    69
    sendOthers(*it, QString("REMOVETEAM:")+delimeter+*(tmIt->begin()) + delimeter + *(tmIt->begin()+1));
350
c3ccec3834e8 more correct net disconnection
displacer
parents: 349
diff changeset
    70
  }
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    71
  connclients.erase(it);
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
    72
  //teamChanged();
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    73
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    74
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    75
QString HWNetServer::getRunningHostName() const
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    76
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    77
  return IPCServer->serverAddress().toString();
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
quint16 HWNetServer::getRunningPort() const
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    81
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    82
  return ds_port;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    83
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    84
334
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    85
HWConnectedClient* HWNetServer::getChiefClient() const
317
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
    86
{
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
    87
  for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
334
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    88
    // watch for first fully connected client (with confirmed nick)
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    89
    if((*it)->getClientNick()!="") return *it;
317
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
    90
  }
334
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    91
  return 0;
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    92
}
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    93
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    94
bool HWNetServer::isChiefClient(HWConnectedClient* cl) const
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    95
{
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    96
  return getChiefClient()==cl;
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    97
}
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    98
354
60e4af0a4375 network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents: 352
diff changeset
    99
QMap<QString, QStringList> HWNetServer::getGameCfg() const
334
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
   100
{
404
64a62b679b04 final bugfixes :)
displacer
parents: 401
diff changeset
   101
  return m_gameCfg;
64a62b679b04 final bugfixes :)
displacer
parents: 401
diff changeset
   102
  /*
334
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
   103
  for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
335
751348947fce CONFIG_PARAM for storing config
displacer
parents: 334
diff changeset
   104
    if(isChiefClient(*it)) {
751348947fce CONFIG_PARAM for storing config
displacer
parents: 334
diff changeset
   105
      return (*it)->m_gameCfg;
751348947fce CONFIG_PARAM for storing config
displacer
parents: 334
diff changeset
   106
    }
334
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
   107
  }
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
   108
  // error happened if we are here
354
60e4af0a4375 network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents: 352
diff changeset
   109
  return QMap<QString, QStringList>();
404
64a62b679b04 final bugfixes :)
displacer
parents: 401
diff changeset
   110
  */
317
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   111
}
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   112
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   113
bool HWNetServer::haveNick(const QString& nick) const
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   114
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   115
  for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   116
    if((*it)->getClientNick()==nick) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   117
      return true;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   118
    }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   119
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   120
  return false;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   121
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   122
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   123
QList<QStringList> HWNetServer::getTeamsConfig() const
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   124
{
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   125
  QList<QStringList> lst;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   126
  for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   127
    try {
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   128
      lst+=(*it)->getTeamNames();
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   129
    } catch(HWConnectedClient::NoTeamNameException& e) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   130
    }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   131
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   132
  return lst;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   133
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   134
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   135
bool HWNetServer::shouldStart(HWConnectedClient* client)
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   136
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   137
  QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client);
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   138
  if(it==connclients.end() || *it!=client) return false;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   139
  for(it=connclients.begin(); it!=connclients.end(); ++it) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   140
    if(!(*it)->isReady()) return false;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   141
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   142
  return true;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   143
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   144
388
dcf5335940bd repeated start now allowed
displacer
parents: 383
diff changeset
   145
void HWNetServer::resetStart()
dcf5335940bd repeated start now allowed
displacer
parents: 383
diff changeset
   146
{
dcf5335940bd repeated start now allowed
displacer
parents: 383
diff changeset
   147
  QList<HWConnectedClient*>::iterator it;
dcf5335940bd repeated start now allowed
displacer
parents: 383
diff changeset
   148
  for(it=connclients.begin(); it!=connclients.end(); ++it) {
dcf5335940bd repeated start now allowed
displacer
parents: 383
diff changeset
   149
    (*it)->readyToStart=false;
dcf5335940bd repeated start now allowed
displacer
parents: 383
diff changeset
   150
  }
dcf5335940bd repeated start now allowed
displacer
parents: 383
diff changeset
   151
}
dcf5335940bd repeated start now allowed
displacer
parents: 383
diff changeset
   152
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   153
QString HWNetServer::prepareConfig(QStringList lst)
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   154
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   155
  QString msg=lst.join((QString)delimeter)+delimeter;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   156
  for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   157
    if(!(*it)->isReady()) continue;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   158
    msg+=(*it)->getHedgehogsDescription()+delimeter;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   159
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   160
  qDebug() << msg;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   161
  return msg;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   162
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   163
319
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   164
void HWNetServer::sendAll(QString gameCfg)
317
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   165
{
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   166
  for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) {
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   167
    (*it)->RawSendNet(gameCfg);
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   168
  }
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   169
}
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   170
319
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   171
void HWNetServer::sendOthers(HWConnectedClient* this_cl, QString gameCfg)
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   172
{
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   173
  for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) {
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   174
    if(*it==this_cl) continue;
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   175
    (*it)->RawSendNet(gameCfg);
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   176
  }
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   177
}
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   178
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   179
HWConnectedClient::HWConnectedClient(HWNetServer* hwserver, QTcpSocket* client) :
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   180
  readyToStart(false),
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   181
  m_hwserver(hwserver),
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   182
  m_client(client)
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   183
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   184
  connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   185
  connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   186
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   187
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   188
HWConnectedClient::~HWConnectedClient()
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   189
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   190
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   191
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   192
void HWConnectedClient::ClientDisconnect()
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   193
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   194
  emit(HWClientDisconnected(this));
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   195
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   196
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   197
void HWConnectedClient::ClientRead()
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   198
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   199
  try {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   200
    while (m_client->canReadLine()) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   201
      ParseLine(m_client->readLine().trimmed());
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   202
    }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   203
  } catch(ShouldDisconnectException& e) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   204
    m_client->close();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   205
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   206
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   207
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   208
void HWConnectedClient::ParseLine(const QByteArray & line)
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   209
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   210
  QString msg = QString::fromUtf8 (line.data(), line.size());
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   211
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   212
  QStringList lst = msg.split(delimeter);
319
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   213
  if(!lst.size()) return;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   214
  if (lst[0] == "NICK") {
319
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   215
    if(lst.size()<2) return;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   216
    if(m_hwserver->haveNick(lst[1])) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   217
      RawSendNet(QString("ERRONEUSNICKNAME"));
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   218
      throw ShouldDisconnectException();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   219
    }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   220
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   221
    client_nick=lst[1];
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   222
    qDebug() << "send connected";
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   223
    RawSendNet(QString("CONNECTED"));
326
c6718bfdd642 Small fixes
unc0rr
parents: 319
diff changeset
   224
    if(m_hwserver->isChiefClient(this)) RawSendNet(QString("CONFIGASKED"));
335
751348947fce CONFIG_PARAM for storing config
displacer
parents: 334
diff changeset
   225
    else {
349
5b37d6a39829 disable widgets if we are slave network client
displacer
parents: 347
diff changeset
   226
      RawSendNet(QString("SLAVE"));
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   227
      // send teams
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   228
      QList<QStringList> team_conf=m_hwserver->getTeamsConfig();
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   229
      for(QList<QStringList>::iterator tmit=team_conf.begin(); tmit!=team_conf.end(); ++tmit) {
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   230
	RawSendNet(QString("ADDTEAM:")+delimeter+tmit->join(QString(delimeter)));
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   231
      }
354
60e4af0a4375 network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents: 352
diff changeset
   232
      // send config
60e4af0a4375 network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents: 352
diff changeset
   233
      QMap<QString, QStringList> conf=m_hwserver->getGameCfg();
401
bcebe3921740 some colors and network protocol fixes
displacer
parents: 399
diff changeset
   234
      qDebug() << "Config:";
354
60e4af0a4375 network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents: 352
diff changeset
   235
      for(QMap<QString, QStringList>::iterator it=conf.begin(); it!=conf.end(); ++it) {
60e4af0a4375 network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents: 352
diff changeset
   236
	RawSendNet(QString("CONFIG_PARAM")+delimeter+it.key()+delimeter+it.value().join(QString(delimeter)));
401
bcebe3921740 some colors and network protocol fixes
displacer
parents: 399
diff changeset
   237
	qDebug() << QString("CONFIG_PARAM")+delimeter+it.key()+delimeter+it.value().join(QString(delimeter));
354
60e4af0a4375 network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents: 352
diff changeset
   238
      }
335
751348947fce CONFIG_PARAM for storing config
displacer
parents: 334
diff changeset
   239
    }
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   240
    return;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   241
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   242
  if(client_nick=="") return;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   243
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   244
  if (lst[0]=="START:") {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   245
    readyToStart=true;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   246
    if(m_hwserver->shouldStart(this)) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   247
      // start
339
7535ab6c3820 Run game message added, team and config info provided for net game
displacer
parents: 338
diff changeset
   248
      m_hwserver->sendAll("RUNGAME");
388
dcf5335940bd repeated start now allowed
displacer
parents: 383
diff changeset
   249
      m_hwserver->resetStart();
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   250
    }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   251
    return;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   252
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   253
335
751348947fce CONFIG_PARAM for storing config
displacer
parents: 334
diff changeset
   254
  if(lst[0]=="CONFIG_PARAM") {
751348947fce CONFIG_PARAM for storing config
displacer
parents: 334
diff changeset
   255
    if(!m_hwserver->isChiefClient(this) || lst.size()<3) return; // error or permission denied :)
404
64a62b679b04 final bugfixes :)
displacer
parents: 401
diff changeset
   256
    else m_hwserver->m_gameCfg[lst[1]]=lst.mid(2);
401
bcebe3921740 some colors and network protocol fixes
displacer
parents: 399
diff changeset
   257
    qDebug() << msg;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   258
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   259
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   260
  if(lst[0]=="ADDTEAM:") {
382
e7220e48ead1 colors changing config fully working (still need disabling in slaves)
displacer
parents: 354
diff changeset
   261
    if(lst.size()<11) return;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   262
    lst.pop_front();
382
e7220e48ead1 colors changing config fully working (still need disabling in slaves)
displacer
parents: 354
diff changeset
   263
    
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   264
    // add team ID
401
bcebe3921740 some colors and network protocol fixes
displacer
parents: 399
diff changeset
   265
    static unsigned int netTeamID=0;
bcebe3921740 some colors and network protocol fixes
displacer
parents: 399
diff changeset
   266
    lst.insert(1, QString::number(++netTeamID));
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   267
399
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   268
    // hedgehogs num count
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   269
    int maxAdd=18-m_hwserver->hhnum;
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   270
    if (maxAdd<=0) return; // reject command
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   271
    int toAdd=maxAdd<4 ? maxAdd : 4;
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   272
    m_hwserver->hhnum+=toAdd;
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   273
    // hedgehogs num config
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   274
    QString hhnumCfg=QString("CONFIG_PARAM%1HHNUM+%2+%3%1%4").arg(delimeter).arg(lst[0])\
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   275
      .arg(netTeamID)\
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   276
      .arg(toAdd);
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   277
    
382
e7220e48ead1 colors changing config fully working (still need disabling in slaves)
displacer
parents: 354
diff changeset
   278
    // creating color config for new team
401
bcebe3921740 some colors and network protocol fixes
displacer
parents: 399
diff changeset
   279
    QString colorCfg=QString("CONFIG_PARAM%1TEAM_COLOR+%2+%3%1%4").arg(delimeter).arg(lst[0])\
382
e7220e48ead1 colors changing config fully working (still need disabling in slaves)
displacer
parents: 354
diff changeset
   280
      .arg(netTeamID)\
e7220e48ead1 colors changing config fully working (still need disabling in slaves)
displacer
parents: 354
diff changeset
   281
      .arg(lst.takeAt(2));
e7220e48ead1 colors changing config fully working (still need disabling in slaves)
displacer
parents: 354
diff changeset
   282
    qDebug() << "color config:" << colorCfg;
e7220e48ead1 colors changing config fully working (still need disabling in slaves)
displacer
parents: 354
diff changeset
   283
404
64a62b679b04 final bugfixes :)
displacer
parents: 401
diff changeset
   284
    m_hwserver->m_gameCfg[colorCfg.split(delimeter)[1]]=colorCfg.split(delimeter).mid(2);
64a62b679b04 final bugfixes :)
displacer
parents: 401
diff changeset
   285
    m_hwserver->m_gameCfg[hhnumCfg.split(delimeter)[1]]=hhnumCfg.split(delimeter).mid(2);
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   286
    m_teamsCfg.push_back(lst);
382
e7220e48ead1 colors changing config fully working (still need disabling in slaves)
displacer
parents: 354
diff changeset
   287
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   288
    m_hwserver->sendOthers(this, QString("ADDTEAM:")+delimeter+lst.join(QString(delimeter)));
354
60e4af0a4375 network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents: 352
diff changeset
   289
    RawSendNet(QString("TEAM_ACCEPTED%1%2%1%3").arg(delimeter).arg(lst[0]).arg(lst[1]));
404
64a62b679b04 final bugfixes :)
displacer
parents: 401
diff changeset
   290
    m_hwserver->sendAll(colorCfg);
399
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   291
    m_hwserver->sendAll(hhnumCfg);
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   292
    return;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   293
  }
326
c6718bfdd642 Small fixes
unc0rr
parents: 319
diff changeset
   294
347
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   295
  if(lst[0]=="REMOVETEAM:") {
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   296
    if(lst.size()<2) return;
399
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   297
    
404
64a62b679b04 final bugfixes :)
displacer
parents: 401
diff changeset
   298
    for(QMap<QString, QStringList>::iterator it=m_hwserver->m_gameCfg.begin(); it!=m_hwserver->m_gameCfg.end(); ++it) {
399
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   299
      QStringList hhTmpList=it.key().split('+');
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   300
      if(hhTmpList[0] == "HHNUM") {
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   301
	qDebug() << "hhnum config found";
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   302
	if(hhTmpList[1]==lst[1]) {
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   303
	  qDebug() << "hhnum config team found with: " << lst[1] << ":" << it.value()[0].toUInt();
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   304
	  m_hwserver->hhnum-=it.value()[0].toUInt();
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   305
	  break;
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   306
	}
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   307
      }
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   308
    }
c7da1bd32b4e more correct network hhnum
displacer
parents: 391
diff changeset
   309
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   310
    unsigned int netID=removeTeam(lst[1]);
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   311
    m_hwserver->sendOthers(this, QString("REMOVETEAM:")+delimeter+lst[1]+delimeter+QString::number(netID));
401
bcebe3921740 some colors and network protocol fixes
displacer
parents: 399
diff changeset
   312
    qDebug() << QString("REMOVETEAM:")+delimeter+lst[1]+delimeter+QString::number(netID);
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   313
    return;
347
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   314
  }
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   315
319
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   316
  m_hwserver->sendOthers(this, msg);
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   317
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   318
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   319
unsigned int HWConnectedClient::removeTeam(const QString& tname)
347
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   320
{
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   321
  unsigned int netID=0;
347
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   322
  for(QList<QStringList>::iterator it=m_teamsCfg.begin(); it!=m_teamsCfg.end(); ++it) {
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   323
    if((*it)[0]==tname) {
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   324
      netID=(*it)[1].toUInt();
347
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   325
      m_teamsCfg.erase(it);
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   326
      break;
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   327
    }
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   328
  }
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   329
  return netID;
347
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   330
}
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   331
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   332
QList<QStringList> HWConnectedClient::getTeamNames() const
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   333
{
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   334
  return m_teamsCfg;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   335
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   336
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   337
void HWConnectedClient::RawSendNet(const QString & str)
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   338
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   339
  RawSendNet(str.toUtf8());
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   340
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   341
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   342
void HWConnectedClient::RawSendNet(const QByteArray & buf)
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   343
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   344
  m_client->write(buf);
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   345
  m_client->write("\n", 1);
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   346
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   347
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   348
QString HWConnectedClient::getClientNick() const
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   349
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   350
  return client_nick;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   351
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   352
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   353
bool HWConnectedClient::isReady() const
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   354
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   355
  return readyToStart;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   356
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   357
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   358
QString HWConnectedClient::getHedgehogsDescription() const
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   359
{
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   360
  return QString();//pclent_team->TeamGameConfig(65535, 4, 100, true).join((QString)delimeter);
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   361
}