lobby.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 = 5;
  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, SETCHANNEL,
  24. //server consts
  25. SESSIONS, CREATED, JOINED, KICKED, SRVERROR, CHAT, CHATCHANNEL, START, STATUS, HOST, MODS, CLIENTMODS, USERS, HEALTH, GAMEMODE, CHANNEL
  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. //set new chat channel
  74. //%1: channel name
  75. {SETCHANNEL, "<CHANNEL>%1"},
  76. //=== server commands ===
  77. //server commands are started from :>>, arguments are enumerated by : symbol
  78. //new session was created
  79. //arg[0]: room name
  80. {CREATED, "CREATED"},
  81. //list of existing sessions
  82. //arg[0]: amount of sessions, following arguments depending on it
  83. //arg[x]: session name
  84. //arg[x+1]: amount of players in the session
  85. //arg[x+2]: total amount of players allowed
  86. //arg[x+3]: True if session is protected by password
  87. {SESSIONS, "SESSIONS"},
  88. //user has joined to the session
  89. //arg[0]: session name
  90. //arg[1]: username (who was joined)
  91. {JOINED, "JOIN"},
  92. //user has left the session
  93. //arg[0]: session name
  94. //arg[1]: username (who has left)
  95. {KICKED, "KICK"},
  96. //session has been started
  97. //arg[0]: session name
  98. //arg[1]: uuid to be used for connection
  99. {START, "START"},
  100. //host ownership for the game session
  101. //arg[0]: uuid to be used by vcmiserver
  102. //arg[1]: amount of players (clients) to be connected
  103. {HOST, "HOST"},
  104. //room status
  105. //arg[0]: amount of players, following arguments depending on it
  106. //arg[x]: player username
  107. //arg[x+1]: True if player is ready
  108. {STATUS, "STATUS"}, //joined_players:player_name:is_ready
  109. //server error
  110. //arg[0]: error message
  111. {SRVERROR, "ERROR"},
  112. //mods used in the session by host player
  113. //arg[0]: amount of mods, following arguments depending on it
  114. //arg[x]: mod name
  115. //arg[x+1]: mod version
  116. {MODS, "MODS"},
  117. //mods used by user
  118. //arg[0]: username
  119. //arg[1]: amount of mods, following arguments depending on it
  120. //arg[x]: mod name
  121. //arg[x+1]: mod version
  122. {CLIENTMODS, "MODSOTHER"},
  123. //received chat message
  124. //arg[0]: sender username
  125. //arg[1]: channel
  126. //arg[2]: message text
  127. {CHAT, "MSG"},
  128. //received chat message to specific channel
  129. //arg[0]: sender username
  130. //arg[1]: channel
  131. //arg[2]: message text
  132. {CHATCHANNEL, "MSGCH"},
  133. //list of users currently in lobby
  134. //arg[0]: amount of players, following arguments depend on it
  135. //arg[x]: username
  136. //arg[x+1]: room (empty if not in the room)
  137. {USERS, "USERS"},
  138. //healthcheck from server
  139. {HEALTH, "HEALTH"},
  140. //game mode (new game or load game) set by host
  141. //arg[0]: game mode
  142. {GAMEMODE, "GAMEMODE"},
  143. //chat channel changed
  144. //arg[0]: channel name
  145. {CHANNEL, "CHANNEL"},
  146. };
  147. class ServerCommand
  148. {
  149. public:
  150. ServerCommand(ProtocolConsts, const QStringList & arguments);
  151. const ProtocolConsts command;
  152. const QStringList arguments;
  153. };
  154. class SocketLobby : public QObject
  155. {
  156. Q_OBJECT
  157. public:
  158. explicit SocketLobby(QObject *parent = 0);
  159. void connectServer(const QString & host, int port, const QString & username, int timeout);
  160. void disconnectServer();
  161. void requestNewSession(const QString & session, int totalPlayers, const QString & pswd, const QMap<QString, QString> & mods);
  162. void requestJoinSession(const QString & session, const QString & pswd, const QMap<QString, QString> & mods);
  163. void requestLeaveSession(const QString & session);
  164. void requestReadySession(const QString & session);
  165. void send(const QString &);
  166. signals:
  167. void text(QString);
  168. void receive(QString);
  169. void disconnect();
  170. public slots:
  171. void connected();
  172. void disconnected();
  173. void bytesWritten(qint64 bytes);
  174. void readyRead();
  175. private:
  176. QTcpSocket *socket;
  177. bool isConnected = false;
  178. QString username;
  179. };
  180. QString prepareModsClientString(const QMap<QString, QString> & mods);