QTfrontend/newnetclient.h
author unc0rr
Fri, 02 Jan 2009 14:22:55 +0000
changeset 1561 f0af4e5fe880
parent 1530 3b8d723661b2
child 1566 6b63c75fdc68
permissions -rw-r--r--
Clear round replay data on server side when round is finished
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
     1
/*
1066
1f1b3686a2b0 Update copyright headers a bit
unc0rr
parents: 883
diff changeset
     2
 * Hedgewars, a free turn based strategy game
883
07a568ba44e0 Update copyright info in source files headers
unc0rr
parents: 703
diff changeset
     3
 * Copyright (c) 2006-2008 Ulyanov Igor <iulyanov@gmail.com>
315
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
#ifndef _NEW_NETCLIENT_INCLUDED
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    20
#define _NEW_NETCLIENT_INCLUDED
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    21
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    22
#include <QObject>
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    23
#include <QString>
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    24
#include <QTcpSocket>
354
60e4af0a4375 network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents: 352
diff changeset
    25
#include <QMap>
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    26
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    27
#include "team.h"
448
e968e19a464c waiting state for net Go button
displacer
parents: 431
diff changeset
    28
#include "game.h" // for GameState
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    29
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    30
class GameUIConfig;
334
85eacbd9827f protocol changes
displacer
parents: 329
diff changeset
    31
class GameCFGWidget;
339
7535ab6c3820 Run game message added, team and config info provided for net game
displacer
parents: 338
diff changeset
    32
class TeamSelWidget;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    33
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    34
extern char delimeter;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    35
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    36
class HWNewNet : public QObject
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    37
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    38
  Q_OBJECT
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    39
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    40
 public:
339
7535ab6c3820 Run game message added, team and config info provided for net game
displacer
parents: 338
diff changeset
    41
  HWNewNet(GameUIConfig * config, GameCFGWidget* pGameCFGWidget, TeamSelWidget* pTeamSelWidget);
1526
18e412dd7d50 Send QUIT message to server when netclient object is destroyed
unc0rr
parents: 1512
diff changeset
    42
  ~HWNewNet();
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    43
  void Connect(const QString & hostName, quint16 port, const QString & nick);
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    44
  void Disconnect();
1339
8a11157c6c81 Fix the bug
unc0rr
parents: 1324
diff changeset
    45
  bool isRoomChief();
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    46
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    47
 private:
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    48
  GameUIConfig* config;
334
85eacbd9827f protocol changes
displacer
parents: 329
diff changeset
    49
  GameCFGWidget* m_pGameCFGWidget;
339
7535ab6c3820 Run game message added, team and config info provided for net game
displacer
parents: 338
diff changeset
    50
  TeamSelWidget* m_pTeamSelWidget;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    51
334
85eacbd9827f protocol changes
displacer
parents: 329
diff changeset
    52
  bool isChief;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    53
  QString mynick;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    54
  QTcpSocket NetSocket;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    55
  QString seed;
383
09a8795105a4 some disconnect and remove teams bugfixes
displacer
parents: 372
diff changeset
    56
  bool m_game_connected;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    57
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    58
  void ConfigAsked();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    59
  void RunGame();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    60
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    61
  template <typename T>
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    62
  void SendCfgStrNet(T a) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    63
    QByteArray strmsg;
328
95625db21bf8 Oops, I should be more careful
unc0rr
parents: 315
diff changeset
    64
    strmsg.append(a);
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    65
    quint8 sz = strmsg.size();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    66
    QByteArray enginemsg = QByteArray((char *)&sz, 1) + strmsg;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    67
    QString _msg = delimeter + QString(enginemsg.toBase64());
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    68
    RawSendNet(_msg);
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
  template <typename T>
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    72
  void SendCfgStrLoc(T a) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    73
    QByteArray strmsg;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    74
    strmsg.append(QString(a).toUtf8());
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    75
    quint8 sz = strmsg.size();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    76
    QByteArray enginemsg = QByteArray((char *)&sz, 1) + strmsg;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    77
    emit FromNet(enginemsg);
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    78
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    79
1082
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1066
diff changeset
    80
  QStringList cmdbuf;
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1066
diff changeset
    81
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    82
  void RawSendNet(const QString & buf);
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    83
  void RawSendNet(const QByteArray & buf);
1082
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1066
diff changeset
    84
  void ParseCmd(const QStringList & lst);
1378
1a391883261d Allow /me in chat
unc0rr
parents: 1377
diff changeset
    85
  QString formatChatMsg(const QString & nick, const QString & msg);
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    86
1313
f4c54e9e1b8c - Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents: 1311
diff changeset
    87
  int loginStep;
f4c54e9e1b8c - Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents: 1311
diff changeset
    88
  int netClientState;
f4c54e9e1b8c - Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents: 1311
diff changeset
    89
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    90
 signals:
660
7c155e849602 - Simplify net game objects relationship
unc0rr
parents: 486
diff changeset
    91
  void AskForRunGame();
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    92
  void Connected();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    93
  void Disconnected();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    94
  void EnteredGame();
465
07eca0a2546c chatWidget with nicks ready
displacer
parents: 453
diff changeset
    95
  void nickAdded(const QString& nick);
07eca0a2546c chatWidget with nicks ready
displacer
parents: 453
diff changeset
    96
  void nickRemoved(const QString& nick);
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    97
  void FromNet(const QByteArray & buf);
339
7535ab6c3820 Run game message added, team and config info provided for net game
displacer
parents: 338
diff changeset
    98
  void AddNetTeam(const HWTeam&);
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    99
329
4c3aad46baa5 Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents: 328
diff changeset
   100
  void seedChanged(const QString & seed);
4c3aad46baa5 Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents: 328
diff changeset
   101
  void mapChanged(const QString & map);
4c3aad46baa5 Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents: 328
diff changeset
   102
  void themeChanged(const QString & theme);
1427
b510f7a74ef6 Add 'Divide teams' option to frontend
unc0rr
parents: 1410
diff changeset
   103
  void initHealthChanged(int health);
b510f7a74ef6 Add 'Divide teams' option to frontend
unc0rr
parents: 1410
diff changeset
   104
  void turnTimeChanged(int time);
329
4c3aad46baa5 Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents: 328
diff changeset
   105
  void fortsModeChanged(bool value);
1427
b510f7a74ef6 Add 'Divide teams' option to frontend
unc0rr
parents: 1410
diff changeset
   106
  void teamsDivideChanged(bool value);
1530
3b8d723661b2 Implement Solid Land checkbox
unc0rr
parents: 1526
diff changeset
   107
  void solidChanged(bool value);
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 347
diff changeset
   108
  void hhnumChanged(const HWTeam&);
372
4bac77f8cd38 network colors (alpha)
displacer
parents: 354
diff changeset
   109
  void teamColorChanged(const HWTeam&);
1356
77ab8735401d Share chat between frontend and engine
unc0rr
parents: 1344
diff changeset
   110
  void chatStringFromNet(const QString&);
1360
d3e285281c26 Fix doubling chat message
unc0rr
parents: 1356
diff changeset
   111
  void chatStringFromMe(const QString&);
703
424297e3165a weapon names transmitted over network
displacer
parents: 697
diff changeset
   112
  void ammoChanged(const QString& name, const QString& ammo);
1377
a9e768739345 - Customize QTextBrowser look
unc0rr
parents: 1360
diff changeset
   113
1313
f4c54e9e1b8c - Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents: 1311
diff changeset
   114
  void roomsList(const QStringList&);
1377
a9e768739345 - Customize QTextBrowser look
unc0rr
parents: 1360
diff changeset
   115
  void serverMessage(const QString &);
329
4c3aad46baa5 Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents: 328
diff changeset
   116
1405
ee4b28a1861d Show players ready status in chat nick list
unc0rr
parents: 1404
diff changeset
   117
  void setReadyStatus(const QString & nick, bool isReady);
1512
43742041c211 - Don't send 'Bad param' msg, as the only reason of it is just some lag
unc0rr
parents: 1427
diff changeset
   118
  void showMessage(const QString &);
1405
ee4b28a1861d Show players ready status in chat nick list
unc0rr
parents: 1404
diff changeset
   119
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   120
 public slots:
1404
2b6b6809c2e4 - Fix a bug in READY message handling
unc0rr
parents: 1391
diff changeset
   121
  void ToggleReady();
453
4b1236759402 simple net cha added
displacer
parents: 448
diff changeset
   122
  void chatLineToNet(const QString& str);
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   123
  void SendNet(const QByteArray & buf);
328
95625db21bf8 Oops, I should be more careful
unc0rr
parents: 315
diff changeset
   124
  void AddTeam(const HWTeam & team);
347
6521e1b2cd40 more correct remove net teams
displacer
parents: 341
diff changeset
   125
  void RemoveTeam(const HWTeam& team);
329
4c3aad46baa5 Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents: 328
diff changeset
   126
  void onSeedChanged(const QString & seed);
4c3aad46baa5 Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents: 328
diff changeset
   127
  void onMapChanged(const QString & map);
4c3aad46baa5 Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents: 328
diff changeset
   128
  void onThemeChanged(const QString & theme);
1427
b510f7a74ef6 Add 'Divide teams' option to frontend
unc0rr
parents: 1410
diff changeset
   129
  void onInitHealthChanged(int health);
b510f7a74ef6 Add 'Divide teams' option to frontend
unc0rr
parents: 1410
diff changeset
   130
  void onTurnTimeChanged(int time);
329
4c3aad46baa5 Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents: 328
diff changeset
   131
  void onFortsModeChanged(bool value);
1427
b510f7a74ef6 Add 'Divide teams' option to frontend
unc0rr
parents: 1410
diff changeset
   132
  void onTeamsDivideChanged(bool value);
1530
3b8d723661b2 Implement Solid Land checkbox
unc0rr
parents: 1526
diff changeset
   133
  void onSolidChanged(bool value);
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 347
diff changeset
   134
  void onHedgehogsNumChanged(const HWTeam& team);
372
4bac77f8cd38 network colors (alpha)
displacer
parents: 354
diff changeset
   135
  void onTeamColorChanged(const HWTeam& team);
703
424297e3165a weapon names transmitted over network
displacer
parents: 697
diff changeset
   136
  void onWeaponsNameChanged(const QString& name, const QString& ammo);
1313
f4c54e9e1b8c - Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents: 1311
diff changeset
   137
  void JoinRoom(const QString & room);
f4c54e9e1b8c - Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents: 1311
diff changeset
   138
  void CreateRoom(const QString & room);
1315
c2f09811bb8c Implement rooms list refresh
unc0rr
parents: 1313
diff changeset
   139
  void askRoomsList();
1344
4004e597f1bf Clients send roundfinished message to server when the round is over
unc0rr
parents: 1339
diff changeset
   140
  void gameFinished();
1391
735f6d43780b Implement kick
unc0rr
parents: 1378
diff changeset
   141
  void kickPlayer(const QString &);
1410
eece43296890 Send appropriate messages to server on control menu actions
unc0rr
parents: 1405
diff changeset
   142
  void startGame();
eece43296890 Send appropriate messages to server on control menu actions
unc0rr
parents: 1405
diff changeset
   143
  void toggleRestrictJoins();
eece43296890 Send appropriate messages to server on control menu actions
unc0rr
parents: 1405
diff changeset
   144
  void toggleRestrictTeamAdds();
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   145
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   146
 private slots:
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   147
  void ClientRead();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   148
  void OnConnect();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   149
  void OnDisconnect();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   150
  void displayError(QAbstractSocket::SocketError socketError);
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
#endif // _NEW_NETCLIENT_INCLUDED