QTfrontend/netserver.cpp
author unc0rr
Tue, 23 Jan 2007 18:54:09 +0000
changeset 359 59fbfc65fbda
parent 354 60e4af0a4375
child 382 e7220e48ead1
permissions -rw-r--r--
- New land templates - Template editor
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
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    35
  IPCServer = new QTcpServer(this);
347
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
    36
  if (!IPCServer->listen(QHostAddress::Any, ds_port)) {
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    37
    QMessageBox::critical(0, tr("Error"),
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    38
			  tr("Unable to start the server: %1.")
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    39
			  .arg(IPCServer->errorString()));
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    40
  }
326
c6718bfdd642 Small fixes
unc0rr
parents: 319
diff changeset
    41
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    42
  connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    43
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    44
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    45
void HWNetServer::StopServer()
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    46
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    47
  IPCServer->close();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    48
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    49
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    50
void HWNetServer::NewConnection()
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    51
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    52
  QTcpSocket* client = IPCServer->nextPendingConnection();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    53
  if(!client) return;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    54
  connclients.push_back(new HWConnectedClient(this, client));
326
c6718bfdd642 Small fixes
unc0rr
parents: 319
diff changeset
    55
  connect(connclients.back(), SIGNAL(HWClientDisconnected(HWConnectedClient*)),
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    56
	  this, SLOT(ClientDisconnect(HWConnectedClient*)));
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    57
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    58
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    59
void HWNetServer::ClientDisconnect(HWConnectedClient* client)
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    60
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    61
  QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client);
350
c3ccec3834e8 more correct net disconnection
displacer
parents: 349
diff changeset
    62
  for(QList<QStringList>::iterator tmIt=(*it)->m_teamsCfg.begin(); tmIt!=(*it)->m_teamsCfg.end(); ++tmIt) {
c3ccec3834e8 more correct net disconnection
displacer
parents: 349
diff changeset
    63
    sendOthers(*it, QString("REMOVETEAM:")+delimeter+*(tmIt->begin()));
c3ccec3834e8 more correct net disconnection
displacer
parents: 349
diff changeset
    64
  }
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    65
  connclients.erase(it);
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
    66
  //teamChanged();
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    67
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    68
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    69
QString HWNetServer::getRunningHostName() const
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    70
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    71
  return IPCServer->serverAddress().toString();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    72
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    73
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    74
quint16 HWNetServer::getRunningPort() const
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    75
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    76
  return ds_port;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    77
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    78
334
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    79
HWConnectedClient* HWNetServer::getChiefClient() const
317
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
    80
{
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
    81
  for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
334
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    82
    // watch for first fully connected client (with confirmed nick)
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    83
    if((*it)->getClientNick()!="") return *it;
317
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
    84
  }
334
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    85
  return 0;
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    86
}
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    87
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    88
bool HWNetServer::isChiefClient(HWConnectedClient* cl) const
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    89
{
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    90
  return getChiefClient()==cl;
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    91
}
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    92
354
60e4af0a4375 network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents: 352
diff changeset
    93
QMap<QString, QStringList> HWNetServer::getGameCfg() const
334
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    94
{
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    95
  for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
335
751348947fce CONFIG_PARAM for storing config
displacer
parents: 334
diff changeset
    96
    if(isChiefClient(*it)) {
751348947fce CONFIG_PARAM for storing config
displacer
parents: 334
diff changeset
    97
      return (*it)->m_gameCfg;
751348947fce CONFIG_PARAM for storing config
displacer
parents: 334
diff changeset
    98
    }
334
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
    99
  }
85eacbd9827f protocol changes
displacer
parents: 332
diff changeset
   100
  // 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
   101
  return QMap<QString, QStringList>();
317
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   102
}
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   103
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   104
bool HWNetServer::haveNick(const QString& nick) const
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   105
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   106
  for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   107
    if((*it)->getClientNick()==nick) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   108
      return true;
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
  return false;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   112
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   113
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   114
QList<QStringList> HWNetServer::getTeamsConfig() const
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   115
{
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   116
  QList<QStringList> lst;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   117
  for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   118
    try {
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   119
      lst+=(*it)->getTeamNames();
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   120
    } catch(HWConnectedClient::NoTeamNameException& e) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   121
    }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   122
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   123
  return lst;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   124
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   125
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   126
bool HWNetServer::shouldStart(HWConnectedClient* client)
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   127
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   128
  QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client);
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   129
  if(it==connclients.end() || *it!=client) return false;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   130
  for(it=connclients.begin(); it!=connclients.end(); ++it) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   131
    if(!(*it)->isReady()) return false;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   132
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   133
  return true;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   134
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   135
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   136
QString HWNetServer::prepareConfig(QStringList lst)
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   137
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   138
  QString msg=lst.join((QString)delimeter)+delimeter;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   139
  for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   140
    if(!(*it)->isReady()) continue;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   141
    msg+=(*it)->getHedgehogsDescription()+delimeter;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   142
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   143
  qDebug() << msg;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   144
  return msg;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   145
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   146
319
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   147
void HWNetServer::sendAll(QString gameCfg)
317
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   148
{
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   149
  for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) {
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   150
    (*it)->RawSendNet(gameCfg);
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   151
  }
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   152
}
e95340dbfc1d start all clients
displacer
parents: 315
diff changeset
   153
319
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   154
void HWNetServer::sendOthers(HWConnectedClient* this_cl, QString gameCfg)
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   155
{
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   156
  for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) {
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   157
    if(*it==this_cl) continue;
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   158
    (*it)->RawSendNet(gameCfg);
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   159
  }
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   160
}
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   161
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   162
HWConnectedClient::HWConnectedClient(HWNetServer* hwserver, QTcpSocket* client) :
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   163
  readyToStart(false),
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   164
  m_hwserver(hwserver),
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   165
  m_client(client)
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   166
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   167
  connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   168
  connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
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
HWConnectedClient::~HWConnectedClient()
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   172
{
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
void HWConnectedClient::ClientDisconnect()
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   176
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   177
  emit(HWClientDisconnected(this));
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   178
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   179
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   180
void HWConnectedClient::ClientRead()
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   181
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   182
  try {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   183
    while (m_client->canReadLine()) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   184
      ParseLine(m_client->readLine().trimmed());
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   185
    }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   186
  } catch(ShouldDisconnectException& e) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   187
    m_client->close();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   188
  }
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
void HWConnectedClient::ParseLine(const QByteArray & line)
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   192
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   193
  QString msg = QString::fromUtf8 (line.data(), line.size());
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   194
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   195
  qDebug() << "line " << msg << " received";
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   196
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   197
  QStringList lst = msg.split(delimeter);
319
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   198
  if(!lst.size()) return;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   199
  if (lst[0] == "NICK") {
319
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   200
    if(lst.size()<2) return;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   201
    if(m_hwserver->haveNick(lst[1])) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   202
      RawSendNet(QString("ERRONEUSNICKNAME"));
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   203
      throw ShouldDisconnectException();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   204
    }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   205
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   206
    client_nick=lst[1];
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   207
    qDebug() << "send connected";
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   208
    RawSendNet(QString("CONNECTED"));
326
c6718bfdd642 Small fixes
unc0rr
parents: 319
diff changeset
   209
    if(m_hwserver->isChiefClient(this)) RawSendNet(QString("CONFIGASKED"));
335
751348947fce CONFIG_PARAM for storing config
displacer
parents: 334
diff changeset
   210
    else {
349
5b37d6a39829 disable widgets if we are slave network client
displacer
parents: 347
diff changeset
   211
      RawSendNet(QString("SLAVE"));
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   212
      // send teams
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   213
      QList<QStringList> team_conf=m_hwserver->getTeamsConfig();
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   214
      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
   215
	RawSendNet(QString("ADDTEAM:")+delimeter+tmit->join(QString(delimeter)));
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   216
      }
354
60e4af0a4375 network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents: 352
diff changeset
   217
      // send config
60e4af0a4375 network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents: 352
diff changeset
   218
      QMap<QString, QStringList> conf=m_hwserver->getGameCfg();
60e4af0a4375 network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents: 352
diff changeset
   219
      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
   220
	RawSendNet(QString("CONFIG_PARAM")+delimeter+it.key()+delimeter+it.value().join(QString(delimeter)));
60e4af0a4375 network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents: 352
diff changeset
   221
      }
335
751348947fce CONFIG_PARAM for storing config
displacer
parents: 334
diff changeset
   222
    }
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   223
    return;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   224
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   225
  if(client_nick=="") return;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   226
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   227
  if (lst[0]=="START:") {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   228
    readyToStart=true;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   229
    if(m_hwserver->shouldStart(this)) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   230
      // start
339
7535ab6c3820 Run game message added, team and config info provided for net game
displacer
parents: 338
diff changeset
   231
      m_hwserver->sendAll("RUNGAME");
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   232
    }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   233
    return;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   234
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   235
335
751348947fce CONFIG_PARAM for storing config
displacer
parents: 334
diff changeset
   236
  if(lst[0]=="CONFIG_PARAM") {
751348947fce CONFIG_PARAM for storing config
displacer
parents: 334
diff changeset
   237
    if(!m_hwserver->isChiefClient(this) || lst.size()<3) return; // error or permission denied :)
354
60e4af0a4375 network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents: 352
diff changeset
   238
    else m_gameCfg[lst[1]]=lst.mid(2);
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   239
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   240
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   241
  if(lst[0]=="ADDTEAM:") {
319
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   242
    if(lst.size()<10) return;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   243
    lst.pop_front();
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   244
    // add team ID
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   245
    static unsigned int netTeamID=1;
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   246
    lst.insert(1, QString::number(netTeamID++));
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   247
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   248
    m_teamsCfg.push_back(lst);
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   249
    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
   250
    RawSendNet(QString("TEAM_ACCEPTED%1%2%1%3").arg(delimeter).arg(lst[0]).arg(lst[1]));
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   251
    return;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   252
  }
326
c6718bfdd642 Small fixes
unc0rr
parents: 319
diff changeset
   253
347
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   254
  if(lst[0]=="REMOVETEAM:") {
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   255
    if(lst.size()<2) return;
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   256
    unsigned int netID=removeTeam(lst[1]);
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   257
    m_hwserver->sendOthers(this, QString("REMOVETEAM:")+delimeter+lst[1]+delimeter+QString::number(netID));
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   258
    return;
347
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   259
  }
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   260
319
7f3bd9e31f18 some changes
displacer
parents: 317
diff changeset
   261
  m_hwserver->sendOthers(this, msg);
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   262
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   263
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   264
unsigned int HWConnectedClient::removeTeam(const QString& tname)
347
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   265
{
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   266
  unsigned int netID=0;
347
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   267
  for(QList<QStringList>::iterator it=m_teamsCfg.begin(); it!=m_teamsCfg.end(); ++it) {
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   268
    if((*it)[0]==tname) {
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   269
      netID=(*it)[1].toUInt();
347
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   270
      m_teamsCfg.erase(it);
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   271
      break;
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   272
    }
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   273
  }
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 350
diff changeset
   274
  return netID;
347
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   275
}
6521e1b2cd40 more correct remove net teams
displacer
parents: 339
diff changeset
   276
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   277
QList<QStringList> HWConnectedClient::getTeamNames() const
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   278
{
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   279
  return m_teamsCfg;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   280
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   281
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   282
void HWConnectedClient::RawSendNet(const QString & str)
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   283
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   284
  RawSendNet(str.toUtf8());
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   285
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   286
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   287
void HWConnectedClient::RawSendNet(const QByteArray & buf)
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   288
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   289
  m_client->write(buf);
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   290
  m_client->write("\n", 1);
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   291
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   292
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   293
QString HWConnectedClient::getClientNick() const
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   294
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   295
  return client_nick;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   296
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   297
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   298
bool HWConnectedClient::isReady() const
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   299
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   300
  return readyToStart;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   301
}
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   302
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   303
QString HWConnectedClient::getHedgehogsDescription() const
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   304
{
338
d1e75dcd285f multiple teams now available per host (still alpha)
displacer
parents: 335
diff changeset
   305
  return QString();//pclent_team->TeamGameConfig(65535, 4, 100, true).join((QString)delimeter);
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   306
}