diff --git a/client/globalLobby/GlobalLobbyWindow.cpp b/client/globalLobby/GlobalLobbyWindow.cpp index a4a9a67c3..8aa54378e 100644 --- a/client/globalLobby/GlobalLobbyWindow.cpp +++ b/client/globalLobby/GlobalLobbyWindow.cpp @@ -51,23 +51,20 @@ void GlobalLobbyWindow::doSendChatMessage() void GlobalLobbyWindow::doCreateGameRoom() { GH.windows().createAndPushWindow(); - // TODO: - // start local server and supply our UUID / client credentials to it - // server logs into lobby ( uuid = client, mode = server ). This creates 'room' in mode 'empty' - // server starts accepting connections from players (including host) - // client connects to local server - // client sends createGameRoom query to lobby with own / server UUID and mode 'direct' (non-proxy) - // client requests to change room status to private or public } void GlobalLobbyWindow::doInviteAccount(const std::string & accountID) { - + assert(0); // TODO } void GlobalLobbyWindow::doJoinRoom(const std::string & roomID) { + JsonNode toSend; + toSend["type"].String() = "joinGameRoom"; + toSend["gameRoomID"].String() = roomID; + CSH->getGlobalLobby().sendMessage(toSend); } void GlobalLobbyWindow::onGameChatMessage(const std::string & sender, const std::string & message, const std::string & when) diff --git a/lobby/LobbyDatabase.cpp b/lobby/LobbyDatabase.cpp index 44a747a68..e06b95ddc 100644 --- a/lobby/LobbyDatabase.cpp +++ b/lobby/LobbyDatabase.cpp @@ -176,12 +176,18 @@ void LobbyDatabase::prepareStatements() WHERE status = 1 )"; - static const std::string countAccountsInRoomText = R"( + static const std::string countRoomUsedSlotsText = R"( SELECT COUNT(accountID) FROM gameRoomPlayers WHERE roomID = ? )"; + static const std::string countRoomTotalSlotsText = R"( + SELECT playerLimit + FROM gameRooms + WHERE roomID = ? + )"; + static const std::string getAccountDisplayNameText = R"( SELECT displayName FROM accounts @@ -245,7 +251,8 @@ void LobbyDatabase::prepareStatements() getActiveAccountsStatement = database->prepare(getActiveAccountsText); getActiveGameRoomsStatement = database->prepare(getActiveGameRoomsText); getAccountDisplayNameStatement = database->prepare(getAccountDisplayNameText); - countAccountsInRoomStatement = database->prepare(countAccountsInRoomText); + countRoomUsedSlotsStatement = database->prepare(countRoomUsedSlotsText); + countRoomTotalSlotsStatement = database->prepare(countRoomTotalSlotsText); isAccountCookieValidStatement = database->prepare(isAccountCookieValidText); isPlayerInGameRoomStatement = database->prepare(isPlayerInGameRoomText); @@ -404,6 +411,22 @@ LobbyRoomState LobbyDatabase::getGameRoomStatus(const std::string & roomID) uint32_t LobbyDatabase::getGameRoomFreeSlots(const std::string & roomID) { + uint32_t usedSlots = 0; + uint32_t totalSlots = 0; + + countRoomUsedSlotsStatement->setBinds(roomID); + if(countRoomUsedSlotsStatement->execute()) + countRoomUsedSlotsStatement->getColumns(usedSlots); + countRoomUsedSlotsStatement->reset(); + + countRoomTotalSlotsStatement->setBinds(roomID); + if(countRoomTotalSlotsStatement->execute()) + countRoomTotalSlotsStatement->getColumns(totalSlots); + countRoomTotalSlotsStatement->reset(); + + + if (totalSlots > usedSlots) + return totalSlots - usedSlots; return 0; } @@ -443,10 +466,10 @@ std::vector LobbyDatabase::getActiveGameRooms() for (auto & room : result) { - countAccountsInRoomStatement->setBinds(room.roomID); - if(countAccountsInRoomStatement->execute()) - countAccountsInRoomStatement->getColumns(room.playersCount); - countAccountsInRoomStatement->reset(); + countRoomUsedSlotsStatement->setBinds(room.roomID); + if(countRoomUsedSlotsStatement->execute()) + countRoomUsedSlotsStatement->getColumns(room.playersCount); + countRoomUsedSlotsStatement->reset(); } return result; } diff --git a/lobby/LobbyDatabase.h b/lobby/LobbyDatabase.h index ef940e3b5..ae44537a4 100644 --- a/lobby/LobbyDatabase.h +++ b/lobby/LobbyDatabase.h @@ -41,7 +41,8 @@ class LobbyDatabase SQLiteStatementPtr getActiveAccountsStatement; SQLiteStatementPtr getAccountGameRoomStatement; SQLiteStatementPtr getAccountDisplayNameStatement; - SQLiteStatementPtr countAccountsInRoomStatement; + SQLiteStatementPtr countRoomUsedSlotsStatement; + SQLiteStatementPtr countRoomTotalSlotsStatement; SQLiteStatementPtr isAccountCookieValidStatement; SQLiteStatementPtr isGameRoomCookieValidStatement; diff --git a/lobby/LobbyServer.cpp b/lobby/LobbyServer.cpp index 0b745ed6b..6f05aa708 100644 --- a/lobby/LobbyServer.cpp +++ b/lobby/LobbyServer.cpp @@ -485,6 +485,9 @@ void LobbyServer::receiveJoinGameRoom(const NetworkConnectionPtr & connection, c auto roomStatus = database->getGameRoomStatus(gameRoomID); + if(roomStatus != LobbyRoomState::PRIVATE && roomStatus != LobbyRoomState::PUBLIC) + return; + if(roomStatus == LobbyRoomState::PRIVATE) { if(database->getAccountInviteStatus(accountID, gameRoomID) != LobbyInviteStatus::INVITED)