QTfrontend/newnetclient.h
author nemo
Sun, 12 Jun 2011 22:34:38 -0400
changeset 5239 f34f391a223b
parent 5126 50803f3403a5
child 5998 e8f44e9433f0
permissions -rw-r--r--
Bit more reusability
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
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4908
diff changeset
     3
 * Copyright (c) 2006-2008 Igor Ulyanov <iulyanov@gmail.com>
088d40d8aba2 Happy 2011 :)
koda
parents: 4908
diff changeset
     4
 * Copyright (c) 2008-2011 Andrey Korotaev <unC0Rr@gmail.com>
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
     5
 *
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
     8
 * the Free Software Foundation; version 2 of the License
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
     9
 *
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    13
 * GNU General Public License for more details.
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    14
 *
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    18
 */
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    19
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    20
#ifndef _NEW_NETCLIENT_INCLUDED
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    21
#define _NEW_NETCLIENT_INCLUDED
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    22
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    23
#include <QObject>
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    24
#include <QString>
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    25
#include <QTcpSocket>
354
60e4af0a4375 network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents: 352
diff changeset
    26
#include <QMap>
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    27
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    28
#include "team.h"
448
e968e19a464c waiting state for net Go button
displacer
parents: 431
diff changeset
    29
#include "game.h" // for GameState
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    30
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    31
class GameUIConfig;
334
85eacbd9827f protocol changes
displacer
parents: 329
diff changeset
    32
class GameCFGWidget;
339
7535ab6c3820 Run game message added, team and config info provided for net game
displacer
parents: 338
diff changeset
    33
class TeamSelWidget;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    34
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    35
extern char delimeter;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    36
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    37
class HWNewNet : public QObject
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    38
{
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    39
  Q_OBJECT
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    40
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    41
 public:
339
7535ab6c3820 Run game message added, team and config info provided for net game
displacer
parents: 338
diff changeset
    42
  HWNewNet(GameUIConfig * config, GameCFGWidget* pGameCFGWidget, TeamSelWidget* pTeamSelWidget);
1526
18e412dd7d50 Send QUIT message to server when netclient object is destroyed
unc0rr
parents: 1512
diff changeset
    43
  ~HWNewNet();
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    44
  void Connect(const QString & hostName, quint16 port, const QString & nick);
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    45
  void Disconnect();
1339
8a11157c6c81 Fix the bug
unc0rr
parents: 1324
diff changeset
    46
  bool isRoomChief();
1671
bb12cb688f75 Implement isInRoom in client
unc0rr
parents: 1648
diff changeset
    47
  bool isInRoom();
2821
67815ee123d7 Frontend:
smxx
parents: 2777
diff changeset
    48
  int getClientState();
67815ee123d7 Frontend:
smxx
parents: 2777
diff changeset
    49
  QString getNick();
67815ee123d7 Frontend:
smxx
parents: 2777
diff changeset
    50
  QString getRoom();
67815ee123d7 Frontend:
smxx
parents: 2777
diff changeset
    51
  QString getHost();
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3283
diff changeset
    52
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    53
 private:
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    54
  GameUIConfig* config;
334
85eacbd9827f protocol changes
displacer
parents: 329
diff changeset
    55
  GameCFGWidget* m_pGameCFGWidget;
339
7535ab6c3820 Run game message added, team and config info provided for net game
displacer
parents: 338
diff changeset
    56
  TeamSelWidget* m_pTeamSelWidget;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    57
334
85eacbd9827f protocol changes
displacer
parents: 329
diff changeset
    58
  bool isChief;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    59
  QString mynick;
2821
67815ee123d7 Frontend:
smxx
parents: 2777
diff changeset
    60
  QString myroom;
67815ee123d7 Frontend:
smxx
parents: 2777
diff changeset
    61
  QString myhost;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    62
  QTcpSocket NetSocket;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    63
  QString seed;
383
09a8795105a4 some disconnect and remove teams bugfixes
displacer
parents: 372
diff changeset
    64
  bool m_game_connected;
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    65
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    66
  template <typename T>
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    67
  void SendCfgStrNet(T a) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    68
    QByteArray strmsg;
328
95625db21bf8 Oops, I should be more careful
unc0rr
parents: 315
diff changeset
    69
    strmsg.append(a);
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    70
    quint8 sz = strmsg.size();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    71
    QByteArray enginemsg = QByteArray((char *)&sz, 1) + strmsg;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    72
    QString _msg = delimeter + QString(enginemsg.toBase64());
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    73
    RawSendNet(_msg);
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    74
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    75
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    76
  template <typename T>
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    77
  void SendCfgStrLoc(T a) {
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    78
    QByteArray strmsg;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    79
    strmsg.append(QString(a).toUtf8());
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    80
    quint8 sz = strmsg.size();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    81
    QByteArray enginemsg = QByteArray((char *)&sz, 1) + strmsg;
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    82
    emit FromNet(enginemsg);
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    83
  }
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    84
1082
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1066
diff changeset
    85
  QStringList cmdbuf;
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1066
diff changeset
    86
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    87
  void RawSendNet(const QString & buf);
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    88
  void RawSendNet(const QByteArray & buf);
1082
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1066
diff changeset
    89
  void ParseCmd(const QStringList & lst);
4879
55f939e2c19c Ask for another nickname on collision. Works only with new server.
unc0rr
parents: 3697
diff changeset
    90
  void handleNotice(int n);
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    91
1313
f4c54e9e1b8c - Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents: 1311
diff changeset
    92
  int loginStep;
f4c54e9e1b8c - Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents: 1311
diff changeset
    93
  int netClientState;
f4c54e9e1b8c - Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents: 1311
diff changeset
    94
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    95
 signals:
660
7c155e849602 - Simplify net game objects relationship
unc0rr
parents: 486
diff changeset
    96
  void AskForRunGame();
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    97
  void Connected();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    98
  void Disconnected();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
    99
  void EnteredGame();
1592
5ee77ee470a6 Start converting network protocol to no-disconnecting religion
unc0rr
parents: 1577
diff changeset
   100
  void LeftRoom();
2777
95a1a69d491c tweak. no chirp on room creation.
nemo
parents: 2776
diff changeset
   101
  void nickAdded(const QString& nick, bool notifyNick);
465
07eca0a2546c chatWidget with nicks ready
displacer
parents: 453
diff changeset
   102
  void nickRemoved(const QString& nick);
2777
95a1a69d491c tweak. no chirp on room creation.
nemo
parents: 2776
diff changeset
   103
  void nickAddedLobby(const QString& nick, bool notifyNick);
1566
6b63c75fdc68 Start work on lobby: add/remove nick from the list on join/quit
unc0rr
parents: 1530
diff changeset
   104
  void nickRemovedLobby(const QString& nick);
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   105
  void FromNet(const QByteArray & buf);
1856
e71dbf958c87 Enable admin button when have privilege. Button does nothing yet.
unc0rr
parents: 1797
diff changeset
   106
  void adminAccess(bool);
1860
ce140b2b928a Some work on players lists context menus
unc0rr
parents: 1856
diff changeset
   107
  void roomMaster(bool);
1856
e71dbf958c87 Enable admin button when have privilege. Button does nothing yet.
unc0rr
parents: 1797
diff changeset
   108
1899
5763f46d7486 Sync schemes config over net should work now (untested)
unc0rr
parents: 1875
diff changeset
   109
  void netSchemeConfig(QStringList &);
1873
815a3ff1fe4b Start refactoring some things. Frontend becomes temporarily unusable for network game
unc0rr
parents: 1860
diff changeset
   110
  void paramChanged(const QString & param, const QStringList & value);
1875
189370d394db - Send initial room config
unc0rr
parents: 1874
diff changeset
   111
  void configAsked();
189370d394db - Send initial room config
unc0rr
parents: 1874
diff changeset
   112
339
7535ab6c3820 Run game message added, team and config info provided for net game
displacer
parents: 338
diff changeset
   113
  void AddNetTeam(const HWTeam&);
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 347
diff changeset
   114
  void hhnumChanged(const HWTeam&);
372
4bac77f8cd38 network colors (alpha)
displacer
parents: 354
diff changeset
   115
  void teamColorChanged(const HWTeam&);
1568
15a446307993 Lobby chat should work now
unc0rr
parents: 1566
diff changeset
   116
  void chatStringLobby(const QString&);
4897
11598e7aa7e6 make names in chats clickable. still color adjustments needed; and testing
sheepluva
parents: 4879
diff changeset
   117
  void chatStringLobby(const QString&, const QString&);
1356
77ab8735401d Share chat between frontend and engine
unc0rr
parents: 1344
diff changeset
   118
  void chatStringFromNet(const QString&);
1360
d3e285281c26 Fix doubling chat message
unc0rr
parents: 1356
diff changeset
   119
  void chatStringFromMe(const QString&);
1568
15a446307993 Lobby chat should work now
unc0rr
parents: 1566
diff changeset
   120
  void chatStringFromMeLobby(const QString&);
1377
a9e768739345 - Customize QTextBrowser look
unc0rr
parents: 1360
diff changeset
   121
1313
f4c54e9e1b8c - Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents: 1311
diff changeset
   122
  void roomsList(const QStringList&);
1377
a9e768739345 - Customize QTextBrowser look
unc0rr
parents: 1360
diff changeset
   123
  void serverMessage(const QString &);
3283
18ee933a5864 Some stuff for game server administration task
unc0rr
parents: 2821
diff changeset
   124
  void serverMessageNew(const QString &);
18ee933a5864 Some stuff for game server administration task
unc0rr
parents: 2821
diff changeset
   125
  void serverMessageOld(const QString &);
18ee933a5864 Some stuff for game server administration task
unc0rr
parents: 2821
diff changeset
   126
  void latestProtocolVar(int);
329
4c3aad46baa5 Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents: 328
diff changeset
   127
1405
ee4b28a1861d Show players ready status in chat nick list
unc0rr
parents: 1404
diff changeset
   128
  void setReadyStatus(const QString & nick, bool isReady);
1648
32d99a3c7998 Draw ready status icon on ready button
unc0rr
parents: 1592
diff changeset
   129
  void setMyReadyStatus(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
   130
  void showMessage(const QString &);
1405
ee4b28a1861d Show players ready status in chat nick list
unc0rr
parents: 1404
diff changeset
   131
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   132
 public slots:
1404
2b6b6809c2e4 - Fix a bug in READY message handling
unc0rr
parents: 1391
diff changeset
   133
  void ToggleReady();
453
4b1236759402 simple net cha added
displacer
parents: 448
diff changeset
   134
  void chatLineToNet(const QString& str);
1568
15a446307993 Lobby chat should work now
unc0rr
parents: 1566
diff changeset
   135
  void chatLineToLobby(const QString& str);
2403
6c5d504af2ba - Proper /team command implementation
unc0rr
parents: 2377
diff changeset
   136
  void SendTeamMessage(const QString& str);
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   137
  void SendNet(const QByteArray & buf);
328
95625db21bf8 Oops, I should be more careful
unc0rr
parents: 315
diff changeset
   138
  void AddTeam(const HWTeam & team);
347
6521e1b2cd40 more correct remove net teams
displacer
parents: 341
diff changeset
   139
  void RemoveTeam(const HWTeam& team);
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 347
diff changeset
   140
  void onHedgehogsNumChanged(const HWTeam& team);
372
4bac77f8cd38 network colors (alpha)
displacer
parents: 354
diff changeset
   141
  void onTeamColorChanged(const HWTeam& team);
1873
815a3ff1fe4b Start refactoring some things. Frontend becomes temporarily unusable for network game
unc0rr
parents: 1860
diff changeset
   142
  void onParamChanged(const QString & param, const QStringList & value);
2377
f3fab2b09e0c And in frontend
nemo
parents: 2345
diff changeset
   143
3283
18ee933a5864 Some stuff for game server administration task
unc0rr
parents: 2821
diff changeset
   144
  void setServerMessageNew(const QString &);
18ee933a5864 Some stuff for game server administration task
unc0rr
parents: 2821
diff changeset
   145
  void setServerMessageOld(const QString &);
18ee933a5864 Some stuff for game server administration task
unc0rr
parents: 2821
diff changeset
   146
  void setLatestProtocolVar(int proto);
18ee933a5864 Some stuff for game server administration task
unc0rr
parents: 2821
diff changeset
   147
  void askServerVars();
1873
815a3ff1fe4b Start refactoring some things. Frontend becomes temporarily unusable for network game
unc0rr
parents: 1860
diff changeset
   148
1313
f4c54e9e1b8c - Introduce a bit of state miachine in client code (should be more robust and verbosive now)
unc0rr
parents: 1311
diff changeset
   149
  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
   150
  void CreateRoom(const QString & room);
5126
50803f3403a5 Frontend: room rename
claymore
parents: 4976
diff changeset
   151
  void updateRoomName(const QString &);
1315
c2f09811bb8c Implement rooms list refresh
unc0rr
parents: 1313
diff changeset
   152
  void askRoomsList();
4908
99d6797b7ff4 Frontend sends ROUNDFINISHED with information about whether the round was played till end (will be needed for stats)
unc0rr
parents: 4897
diff changeset
   153
  void gameFinished(bool correcly);
1860
ce140b2b928a Some work on players lists context menus
unc0rr
parents: 1856
diff changeset
   154
  void banPlayer(const QString &);
1391
735f6d43780b Implement kick
unc0rr
parents: 1378
diff changeset
   155
  void kickPlayer(const QString &);
1577
e3e37c730dc0 - Decrease font size in chat
unc0rr
parents: 1568
diff changeset
   156
  void infoPlayer(const QString &);
2706
935b7d618cf0 sheepluva's patch to add a "follow" command to server and frontend, in order to stalk people and join them in their rooms
koda
parents: 2405
diff changeset
   157
  void followPlayer(const QString &);
1410
eece43296890 Send appropriate messages to server on control menu actions
unc0rr
parents: 1405
diff changeset
   158
  void startGame();
eece43296890 Send appropriate messages to server on control menu actions
unc0rr
parents: 1405
diff changeset
   159
  void toggleRestrictJoins();
eece43296890 Send appropriate messages to server on control menu actions
unc0rr
parents: 1405
diff changeset
   160
  void toggleRestrictTeamAdds();
1592
5ee77ee470a6 Start converting network protocol to no-disconnecting religion
unc0rr
parents: 1577
diff changeset
   161
  void partRoom();
2155
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 1925
diff changeset
   162
  void clearAccountsCache();
315
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   163
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   164
 private slots:
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   165
  void ClientRead();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   166
  void OnConnect();
73003488240b client/server net pre-alpha
displacer
parents:
diff changeset
   167
  void OnDisconnect();
4879
55f939e2c19c Ask for another nickname on collision. Works only with new server.
unc0rr
parents: 3697
diff changeset
   168
  void displayError(QAbstractSocket::SocketError socketError); 
315
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
#endif // _NEW_NETCLIENT_INCLUDED