From beaaa09fbe58dee317aa637d1318f4e3a8713f93 Mon Sep 17 00:00:00 2001 From: notify Date: Sat, 27 May 2023 21:58:32 +0800 Subject: [PATCH] Password room (#164) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 密码房 --- Fk/LobbyElement/RoomGeneralSettings.qml | 14 ++++ Fk/Logic.js | 1 + Fk/Pages/Lobby.qml | 102 +++++++++++++++++------- lang/zh_CN.ts | 4 + lua/client/i18n/zh_CN.lua | 2 + src/network/router.cpp | 16 +++- src/server/server.cpp | 6 +- 7 files changed, 113 insertions(+), 32 deletions(-) diff --git a/Fk/LobbyElement/RoomGeneralSettings.qml b/Fk/LobbyElement/RoomGeneralSettings.qml index 7d3044c2..f0b676a4 100644 --- a/Fk/LobbyElement/RoomGeneralSettings.qml +++ b/Fk/LobbyElement/RoomGeneralSettings.qml @@ -113,6 +113,19 @@ ColumnLayout { } } + RowLayout { + anchors.rightMargin: 8 + spacing: 16 + Text { + text: Backend.translate("Room Password") + } + TextField { + id: roomPassword + maximumLength: 16 + font.pixelSize: 18 + } + } + Switch { id: freeAssignCheck checked: Debugging ? true : false @@ -154,6 +167,7 @@ ColumnLayout { disabledPack: config.disabledPack, generalNum: config.preferredGeneralNum, luckTime: config.preferredLuckTime, + password: roomPassword.text, disabledGenerals, }]) ); diff --git a/Fk/Logic.js b/Fk/Logic.js index 4c469499..d94bf479 100644 --- a/Fk/Logic.js +++ b/Fk/Logic.js @@ -113,6 +113,7 @@ callbacks["UpdateRoomList"] = function(jsonData) { gameMode: room[2], playerNum: room[3], capacity: room[4], + hasPassword: room[5], }); }); } diff --git a/Fk/Pages/Lobby.qml b/Fk/Pages/Lobby.qml index 095ba1c8..10ecebb0 100644 --- a/Fk/Pages/Lobby.qml +++ b/Fk/Pages/Lobby.qml @@ -12,6 +12,8 @@ Item { id: root property alias roomModel: roomModel + property string password + Rectangle { width: parent.width / 2 - roomListLayout.width / 2 height: parent.height * 0.7 @@ -47,7 +49,7 @@ Item { id: roomDelegate Item { - height: 22 + height: 48 width: roomList.width RowLayout { @@ -55,49 +57,40 @@ Item { spacing: 16 Text { text: roomId + color: "grey" } Text { - horizontalAlignment: Text.AlignHCenter + horizontalAlignment: Text.AlignLeft Layout.fillWidth: true - text: roomName + text: roomName + (hasPassword ? "(🔒)" : "") + font.pixelSize: 20 } Text { - text: gameMode + text: Backend.translate(gameMode) } Text { color: (playerNum == capacity) ? "red" : "black" text: playerNum + "/" + capacity + font.pixelSize: 20 + font.bold: true } - Text { - text: Backend.translate("Enter") - font.pixelSize: 24 - TapHandler { - onTapped: { - config.observing = false; - mainWindow.busy = true; - ClientInstance.notifyServer( - "EnterRoom", - JSON.stringify([roomId]) - ); - } - } - } + Button { + text: (playerNum < capacity) ? Backend.translate("Enter") : + Backend.translate("Observe") - Text { - text: Backend.translate("Observe") - font.pixelSize: 24 - TapHandler { - onTapped: { - config.observing = true; - mainWindow.busy = true; - ClientInstance.notifyServer( - "ObserveRoom", - JSON.stringify([roomId]) - ); + onClicked: { + if (hasPassword) { + lobby_dialog.sourceComponent = enterPassword; + lobby_dialog.item.roomId = roomId; + lobby_dialog.item.playerNum = playerNum; + lobby_dialog.item.capacity = capacity; + lobby_drawer.open(); + } else { + enterRoom(roomId, playerNum, capacity, ""); } } } @@ -233,6 +226,57 @@ Item { } } + Component { + id: enterPassword + ColumnLayout { + property int roomId + property int playerNum + property int capacity + signal finished() + anchors.fill: parent + anchors.margins: 16 + + Text { + text: Backend.translate("Please input room's password") + } + + TextField { + id: passwordEdit + onTextChanged: root.password = text; + } + + Button { + text: "OK" + onClicked: { + enterRoom(roomId, playerNum, capacity, root.password); + parent.finished(); + } + } + + Component.onCompleted: { + passwordEdit.text = ""; + } + } + } + + function enterRoom(roomId, playerNum, capacity, pw) { + if (playerNum < capacity) { + config.observing = false; + mainWindow.busy = true; + ClientInstance.notifyServer( + "EnterRoom", + JSON.stringify([roomId, pw]) + ); + } else { + config.observing = true; + mainWindow.busy = true; + ClientInstance.notifyServer( + "ObserveRoom", + JSON.stringify([roomId, pw]) + ); + } + } + property int lobbyPlayerNum: 0 property int serverPlayerNum: 0 diff --git a/lang/zh_CN.ts b/lang/zh_CN.ts index bee2550c..eb2d51c7 100644 --- a/lang/zh_CN.ts +++ b/lang/zh_CN.ts @@ -163,6 +163,10 @@ server is using version %1, please update 服务端使用的是版本%1,该更新你的客户端了 + + room password error + 房间密码错误 + diff --git a/lua/client/i18n/zh_CN.lua b/lua/client/i18n/zh_CN.lua index 36148c1f..85e87701 100644 --- a/lua/client/i18n/zh_CN.lua +++ b/lua/client/i18n/zh_CN.lua @@ -32,6 +32,8 @@ Fk:loadTranslationTable{ ["Select general num"] = "选将数目", ["Operation timeout"] = "操作时长(秒)", ["Luck Card Times"] = "手气卡次数", + ["Room Password"] = "房间密码", + ["Please input room's password"] = "请输入房间的密码", ["Game Mode"] = "游戏模式", ["Enable free assign"] = "自由选将", ["Enable deputy general"] = "启用副将机制", diff --git a/src/network/router.cpp b/src/network/router.cpp index a5e2fdc8..d18e1e39 100644 --- a/src/network/router.cpp +++ b/src/network/router.cpp @@ -211,7 +211,13 @@ void Router::handlePacket(const QByteArray &rawPacket) { auto roomId = arr[0].toInt(); auto room = ServerInstance->findRoom(roomId); if (room) { - room->addPlayer(sender); + auto settings = QJsonDocument::fromJson(room->getSettings()); + auto password = settings["password"].toString(); + if (password.isEmpty() || arr[1].toString() == password) { + room->addPlayer(sender); + } else { + sender->doNotify("ErrorMsg", "room password error"); + } } else { sender->doNotify("ErrorMsg", "no such room"); } @@ -222,7 +228,13 @@ void Router::handlePacket(const QByteArray &rawPacket) { auto roomId = arr[0].toInt(); auto room = ServerInstance->findRoom(roomId); if (room) { - room->addObserver(sender); + auto settings = QJsonDocument::fromJson(room->getSettings()); + auto password = settings["password"].toString(); + if (password.isEmpty() || arr[1].toString() == password) { + room->addObserver(sender); + } else { + sender->doNotify("ErrorMsg", "room password error"); + } } else { sender->doNotify("ErrorMsg", "no such room"); } diff --git a/src/server/server.cpp b/src/server/server.cpp index 41e7a55e..1c2f68da 100644 --- a/src/server/server.cpp +++ b/src/server/server.cpp @@ -138,11 +138,15 @@ void Server::updateRoomList() { QJsonArray arr; foreach (Room *room, rooms) { QJsonArray obj; + auto settings = QJsonDocument::fromJson(room->getSettings()); + auto password = settings["password"].toString(); + obj << room->getId(); // roomId obj << room->getName(); // roomName - obj << "Role"; // gameMode + obj << settings["gameMode"]; // gameMode obj << room->getPlayers().count(); // playerNum obj << room->getCapacity(); // capacity + obj << !password.isEmpty(); arr << obj; } auto jsonData = JsonArray2Bytes(arr);