lobby.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * lobby.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include <QTcpSocket>
  12. #include <QAbstractSocket>
  13. const unsigned int ProtocolVersion = 4;
  14. const std::string ProtocolEncoding = "utf8";
  15. class ProtocolError: public std::runtime_error
  16. {
  17. public:
  18. ProtocolError(const char * w): std::runtime_error(w) {}
  19. };
  20. enum ProtocolConsts
  21. {
  22. //client consts
  23. GREETING, USERNAME, MESSAGE, VERSION, CREATE, JOIN, LEAVE, KICK, READY, FORCESTART, HERE, ALIVE, HOSTMODE,
  24. //server consts
  25. SESSIONS, CREATED, JOINED, KICKED, SRVERROR, CHAT, START, STATUS, HOST, MODS, CLIENTMODS, USERS, HEALTH, GAMEMODE
  26. };
  27. const QMap<ProtocolConsts, QString> ProtocolStrings
  28. {
  29. //=== client commands ===
  30. //handshaking with server
  31. //%1: first byte is protocol_version, then size of encoding string in bytes, then encoding string
  32. //%2: client name
  33. //%3: VCMI version
  34. {GREETING, "%1<GREETINGS>%2<VER>%3"},
  35. //[unsupported] autorization with username
  36. //%1: username
  37. {USERNAME, "<USER>%1"},
  38. //sending message to the chat
  39. //%1: message text
  40. {MESSAGE, "<MSG>%1"},
  41. //create new room
  42. //%1: room name
  43. //%2: password for the room
  44. //%3: max number of players
  45. //%4: mods used by host
  46. // each mod has a format modname&modversion, mods should be separated by ; symbol
  47. {CREATE, "<NEW>%1<PSWD>%2<COUNT>%3<MODS>%4"},
  48. //join to the room
  49. //%1: room name
  50. //%2: password for the room
  51. //%3: list of mods used by player
  52. // each mod has a format modname&modversion, mods should be separated by ; symbol
  53. {JOIN, "<JOIN>%1<PSWD>%2<MODS>%3"},
  54. //leave the room
  55. //%1: room name
  56. {LEAVE, "<LEAVE>%1"},
  57. //kick user from the current room
  58. //%1: player username
  59. {KICK, "<KICK>%1"},
  60. //signal that player is ready for game
  61. //%1: room name
  62. {READY, "<READY>%1"},
  63. //[unsupported] start session immediately
  64. //%1: room name
  65. {FORCESTART, "<FORCESTART>%1"},
  66. //request user list
  67. {HERE, "<HERE>"},
  68. //used as reponse to healcheck
  69. {ALIVE, "<ALIVE>"},
  70. //host sets game mode (new game or load game)
  71. //%1: game mode - 0 for new game, 1 for load game
  72. {HOSTMODE, "<HOSTMODE>%1"},
  73. //=== server commands ===
  74. //server commands are started from :>>, arguments are enumerated by : symbol
  75. //new session was created
  76. //arg[0]: room name
  77. {CREATED, "CREATED"},
  78. //list of existing sessions
  79. //arg[0]: amount of sessions, following arguments depending on it
  80. //arg[x]: session name
  81. //arg[x+1]: amount of players in the session
  82. //arg[x+2]: total amount of players allowed
  83. //arg[x+3]: True if session is protected by password
  84. {SESSIONS, "SESSIONS"},
  85. //user has joined to the session
  86. //arg[0]: session name
  87. //arg[1]: username (who was joined)
  88. {JOINED, "JOIN"},
  89. //user has left the session
  90. //arg[0]: session name
  91. //arg[1]: username (who has left)
  92. {KICKED, "KICK"},
  93. //session has been started
  94. //arg[0]: session name
  95. //arg[1]: uuid to be used for connection
  96. {START, "START"},
  97. //host ownership for the game session
  98. //arg[0]: uuid to be used by vcmiserver
  99. //arg[1]: amount of players (clients) to be connected
  100. {HOST, "HOST"},
  101. //room status
  102. //arg[0]: amount of players, following arguments depending on it
  103. //arg[x]: player username
  104. //arg[x+1]: True if player is ready
  105. {STATUS, "STATUS"}, //joined_players:player_name:is_ready
  106. //server error
  107. //arg[0]: error message
  108. {SRVERROR, "ERROR"},
  109. //mods used in the session by host player
  110. //arg[0]: amount of mods, following arguments depending on it
  111. //arg[x]: mod name
  112. //arg[x+1]: mod version
  113. {MODS, "MODS"},
  114. //mods used by user
  115. //arg[0]: username
  116. //arg[1]: amount of mods, following arguments depending on it
  117. //arg[x]: mod name
  118. //arg[x+1]: mod version
  119. {CLIENTMODS, "MODSOTHER"},
  120. //received chat message
  121. //arg[0]: sender username
  122. //arg[1]: message text
  123. {CHAT, "MSG"},
  124. //list of users currently in lobby
  125. //arg[0]: amount of players, following arguments depend on it
  126. //arg[x]: username
  127. //arg[x+1]: room (empty if not in the room)
  128. {USERS, "USERS"},
  129. //healthcheck from server
  130. {HEALTH, "HEALTH"},
  131. //game mode (new game or load game) set by host
  132. //arg[0]: game mode
  133. {GAMEMODE, "GAMEMODE"},
  134. };
  135. class ServerCommand
  136. {
  137. public:
  138. ServerCommand(ProtocolConsts, const QStringList & arguments);
  139. const ProtocolConsts command;
  140. const QStringList arguments;
  141. };
  142. class SocketLobby : public QObject
  143. {
  144. Q_OBJECT
  145. public:
  146. explicit SocketLobby(QObject *parent = 0);
  147. void connectServer(const QString & host, int port, const QString & username, int timeout);
  148. void disconnectServer();
  149. void requestNewSession(const QString & session, int totalPlayers, const QString & pswd, const QMap<QString, QString> & mods);
  150. void requestJoinSession(const QString & session, const QString & pswd, const QMap<QString, QString> & mods);
  151. void requestLeaveSession(const QString & session);
  152. void requestReadySession(const QString & session);
  153. void send(const QString &);
  154. signals:
  155. void text(QString);
  156. void receive(QString);
  157. void disconnect();
  158. public slots:
  159. void connected();
  160. void disconnected();
  161. void bytesWritten(qint64 bytes);
  162. void readyRead();
  163. private:
  164. QTcpSocket *socket;
  165. bool isConnected = false;
  166. QString username;
  167. };
  168. QString prepareModsClientString(const QMap<QString, QString> & mods);