lobby_moc.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include "StdInc.h"
  2. #include "lobby_moc.h"
  3. #include "ui_lobby_moc.h"
  4. #include "lobbyroomrequest_moc.h"
  5. #include "../mainwindow_moc.h"
  6. #include "../../lib/CConfigHandler.h"
  7. Lobby::Lobby(QWidget *parent) :
  8. QWidget(parent),
  9. ui(new Ui::Lobby)
  10. {
  11. ui->setupUi(this);
  12. ui->buttonReady->setEnabled(false);
  13. connect(&socketLobby, SIGNAL(text(QString)), this, SLOT(chatMessage(QString)));
  14. connect(&socketLobby, SIGNAL(receive(QString)), this, SLOT(dispatchMessage(QString)));
  15. connect(&socketLobby, SIGNAL(disconnect()), this, SLOT(onDisconnected()));
  16. ui->hostEdit->setText(QString::fromStdString(settings["launcher"]["lobbyUrl"].String()));
  17. ui->portEdit->setText(QString::number(settings["launcher"]["lobbyPort"].Integer()));
  18. ui->userEdit->setText(QString::fromStdString(settings["launcher"]["lobbyUsername"].String()));
  19. }
  20. Lobby::~Lobby()
  21. {
  22. delete ui;
  23. }
  24. void Lobby::serverCommand(const ServerCommand & command) try
  25. {
  26. //initialize variables outside of switch block
  27. const QString statusPlaceholder("%1 %2\n");
  28. QString resText;
  29. const auto & args = command.arguments;
  30. int amount, tagPoint;
  31. QString joinStr;
  32. switch(command.command)
  33. {
  34. case SRVERROR:
  35. protocolAssert(args.size());
  36. chatMessage("System error:" + args[0]);
  37. break;
  38. case CREATED:
  39. protocolAssert(args.size());
  40. hostSession = args[0];
  41. session = args[0];
  42. chatMessage("System: new session started");
  43. ui->buttonReady->setEnabled(true);
  44. break;
  45. case SESSIONS:
  46. protocolAssert(args.size());
  47. amount = args[0].toInt();
  48. protocolAssert(amount * 4 == (args.size() - 1));
  49. ui->sessionsTable->setRowCount(amount);
  50. tagPoint = 1;
  51. for(int i = 0; i < amount; ++i)
  52. {
  53. QTableWidgetItem * sessionNameItem = new QTableWidgetItem(args[tagPoint++]);
  54. ui->sessionsTable->setItem(i, 0, sessionNameItem);
  55. int playersJoined = args[tagPoint++].toInt();
  56. int playersTotal = args[tagPoint++].toInt();
  57. QTableWidgetItem * sessionPlayerItem = new QTableWidgetItem(QString("%1/%2").arg(playersJoined).arg(playersTotal));
  58. ui->sessionsTable->setItem(i, 1, sessionPlayerItem);
  59. QTableWidgetItem * sessionProtectedItem = new QTableWidgetItem(args[tagPoint++]);
  60. ui->sessionsTable->setItem(i, 2, sessionProtectedItem);
  61. }
  62. break;
  63. case JOINED:
  64. case KICKED:
  65. protocolAssert(args.size() == 2);
  66. joinStr = (command.command == JOINED ? "System: %1 joined to the session %2" : "System: %1 left session %2");
  67. if(args[1] == username)
  68. {
  69. ui->chat->clear(); //cleanup the chat
  70. chatMessage(joinStr.arg("you", args[0]));
  71. session = args[0];
  72. ui->stackedWidget->setCurrentWidget(command.command == JOINED ? ui->roomPage : ui->sessionsPage);
  73. if(command.command == KICKED)
  74. ui->buttonReady->setEnabled(false);
  75. }
  76. else
  77. {
  78. chatMessage(joinStr.arg(args[1], args[0]));
  79. }
  80. break;
  81. case STATUS:
  82. protocolAssert(args.size() > 0);
  83. amount = args[0].toInt();
  84. protocolAssert(amount * 2 == (args.size() - 1));
  85. tagPoint = 1;
  86. ui->roomChat->clear();
  87. resText.clear();
  88. for(int i = 0; i < amount; ++i, tagPoint += 2)
  89. {
  90. resText += statusPlaceholder.arg(args[tagPoint], args[tagPoint + 1] == "True" ? "ready" : "");
  91. }
  92. ui->roomChat->setPlainText(resText);
  93. break;
  94. case START: {
  95. protocolAssert(args.size() == 1);
  96. //actually start game
  97. gameArgs << "--lobby";
  98. gameArgs << "--lobby-address" << ui->hostEdit->text();
  99. gameArgs << "--lobby-port" << ui->portEdit->text();
  100. gameArgs << "--uuid" << args[0];
  101. startGame(gameArgs);
  102. break;
  103. }
  104. case HOST: {
  105. protocolAssert(args.size() == 2);
  106. gameArgs << "--lobby-host";
  107. gameArgs << "--lobby-uuid" << args[0];
  108. gameArgs << "--lobby-connections" << args[1];
  109. break;
  110. }
  111. case CHAT:
  112. protocolAssert(args.size() > 1);
  113. QString msg;
  114. for(int i = 1; i < args.size(); ++i)
  115. msg += args[i];
  116. chatMessage(QString("%1: %2").arg(args[0], msg));
  117. break;
  118. }
  119. }
  120. catch(const ProtocolError & e)
  121. {
  122. chatMessage(QString("System error: %1").arg(e.what()));
  123. }
  124. void Lobby::dispatchMessage(QString txt) try
  125. {
  126. if(txt.isEmpty())
  127. return;
  128. QStringList parseTags = txt.split(":>>");
  129. protocolAssert(parseTags.size() > 1 && parseTags[0].isEmpty() && !parseTags[1].isEmpty());
  130. for(int c = 1; c < parseTags.size(); ++c)
  131. {
  132. QStringList parseArgs = parseTags[c].split(":");
  133. protocolAssert(parseArgs.size() > 1);
  134. auto ctype = ProtocolStrings.key(parseArgs[0]);
  135. parseArgs.pop_front();
  136. ServerCommand cmd(ctype, parseArgs);
  137. serverCommand(cmd);
  138. }
  139. }
  140. catch(const ProtocolError & e)
  141. {
  142. chatMessage(QString("System error: %1").arg(e.what()));
  143. }
  144. void Lobby::onDisconnected()
  145. {
  146. ui->stackedWidget->setCurrentWidget(ui->sessionsPage);
  147. ui->connectButton->setChecked(false);
  148. }
  149. void Lobby::chatMessage(QString txt)
  150. {
  151. QTextCursor curs(ui->chat->document());
  152. curs.movePosition(QTextCursor::End);
  153. curs.insertText(txt + "\n");
  154. }
  155. void Lobby::protocolAssert(bool expr)
  156. {
  157. if(!expr)
  158. throw ProtocolError("Protocol error");
  159. }
  160. void Lobby::on_messageEdit_returnPressed()
  161. {
  162. socketLobby.send(ProtocolStrings[MESSAGE].arg(ui->messageEdit->text()));
  163. ui->messageEdit->clear();
  164. }
  165. void Lobby::on_connectButton_toggled(bool checked)
  166. {
  167. if(checked)
  168. {
  169. username = ui->userEdit->text();
  170. Settings node = settings.write["launcher"];
  171. node["lobbyUrl"].String() = ui->hostEdit->text().toStdString();
  172. node["lobbyPort"].Integer() = ui->portEdit->text().toInt();
  173. node["lobbyUsername"].String() = username.toStdString();
  174. socketLobby.connectServer(ui->hostEdit->text(), ui->portEdit->text().toInt(), username);
  175. }
  176. else
  177. {
  178. socketLobby.disconnectServer();
  179. }
  180. }
  181. void Lobby::on_newButton_clicked()
  182. {
  183. new LobbyRoomRequest(socketLobby, "", this);
  184. }
  185. void Lobby::on_joinButton_clicked()
  186. {
  187. auto * item = ui->sessionsTable->item(ui->sessionsTable->currentRow(), 0);
  188. if(item)
  189. new LobbyRoomRequest(socketLobby, item->text(), this);
  190. }
  191. void Lobby::on_buttonLeave_clicked()
  192. {
  193. socketLobby.requestLeaveSession(session);
  194. }
  195. void Lobby::on_buttonReady_clicked()
  196. {
  197. socketLobby.requestReadySession(session);
  198. }