lobby_moc.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #ifndef LOBBY_MOC_H
  2. #define LOBBY_MOC_H
  3. #include <QWidget>
  4. #include <QTcpSocket>
  5. #include <QAbstractSocket>
  6. const unsigned int ProtocolVersion = 1;
  7. #ifdef VCMI_WINDOWS
  8. const std::string ProtocolEncoding = "utf16";
  9. #else
  10. const std::string ProtocolEncoding = "utf8";
  11. #endif
  12. class ProtocolError: public std::runtime_error
  13. {
  14. public:
  15. ProtocolError(const char * w): std::runtime_error(w) {}
  16. };
  17. enum ProtocolConsts
  18. {
  19. //client consts
  20. GREETING, USERNAME, MESSAGE, VERSION, CREATE, JOIN, LEAVE, READY,
  21. //server consts
  22. SESSIONS, CREATED, JOINED, KICKED, SRVERROR, CHAT, START, STATUS, HOST
  23. };
  24. const QMap<ProtocolConsts, QString> ProtocolStrings
  25. {
  26. //client consts
  27. {GREETING, "%1<GREETINGS>%2<VER>%3"}, //protocol_version byte, encoding bytes, encoding, name, version
  28. {USERNAME, "<USER>%1"},
  29. {MESSAGE, "<MSG>%1"},
  30. {CREATE, "<NEW>%1<PSWD>%2<COUNT>%3"},
  31. {JOIN, "<JOIN>%1<PSWD>%2"},
  32. {LEAVE, "<LEAVE>%1"}, //session
  33. {READY, "<READY>%1"}, //session
  34. //server consts
  35. {CREATED, "CREATED"},
  36. {SESSIONS, "SESSIONS"}, //amount:session_name:joined_players:total_players:is_protected
  37. {JOINED, "JOIN"}, //session_name:username
  38. {KICKED, "KICK"}, //session_name:username
  39. {START, "START"}, //session_name:uuid
  40. {HOST, "HOST"}, //host_uuid:players_count
  41. {STATUS, "STATUS"}, //joined_players:player_name:is_ready
  42. {SRVERROR, "ERROR"},
  43. {CHAT, "MSG"} //username:message
  44. };
  45. class ServerCommand
  46. {
  47. public:
  48. ServerCommand(ProtocolConsts, const QStringList & arguments);
  49. const ProtocolConsts command;
  50. const QStringList arguments;
  51. };
  52. class SocketLobby : public QObject
  53. {
  54. Q_OBJECT
  55. public:
  56. explicit SocketLobby(QObject *parent = 0);
  57. void connectServer(const QString & host, int port, const QString & username);
  58. void disconnectServer();
  59. void requestNewSession(const QString & session, int totalPlayers, const QString & pswd);
  60. void requestJoinSession(const QString & session, const QString & pswd);
  61. void requestLeaveSession(const QString & session);
  62. void requestReadySession(const QString & session);
  63. void send(const QString &);
  64. signals:
  65. void text(QString);
  66. void receive(QString);
  67. void disconnect();
  68. public slots:
  69. void connected();
  70. void disconnected();
  71. void bytesWritten(qint64 bytes);
  72. void readyRead();
  73. private:
  74. QTcpSocket *socket;
  75. bool isConnected = false;
  76. QString username;
  77. };
  78. namespace Ui {
  79. class Lobby;
  80. }
  81. class Lobby : public QWidget
  82. {
  83. Q_OBJECT
  84. public:
  85. explicit Lobby(QWidget *parent = nullptr);
  86. ~Lobby();
  87. private slots:
  88. void on_messageEdit_returnPressed();
  89. void chatMessage(QString);
  90. void dispatchMessage(QString);
  91. void serverCommand(const ServerCommand &);
  92. void on_connectButton_toggled(bool checked);
  93. void on_newButton_clicked();
  94. void on_joinButton_clicked();
  95. void on_buttonLeave_clicked();
  96. void on_buttonReady_clicked();
  97. void onDisconnected();
  98. private:
  99. Ui::Lobby *ui;
  100. SocketLobby socketLobby;
  101. QString hostSession;
  102. QString session;
  103. QString username;
  104. bool startGame = false;
  105. private:
  106. void protocolAssert(bool);
  107. };
  108. #endif // LOBBY_MOC_H