Password room (#164)

密码房
This commit is contained in:
notify 2023-05-27 21:58:32 +08:00 committed by GitHub
parent 74a3540ca5
commit beaaa09fbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 113 additions and 32 deletions

View File

@ -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 { Switch {
id: freeAssignCheck id: freeAssignCheck
checked: Debugging ? true : false checked: Debugging ? true : false
@ -154,6 +167,7 @@ ColumnLayout {
disabledPack: config.disabledPack, disabledPack: config.disabledPack,
generalNum: config.preferredGeneralNum, generalNum: config.preferredGeneralNum,
luckTime: config.preferredLuckTime, luckTime: config.preferredLuckTime,
password: roomPassword.text,
disabledGenerals, disabledGenerals,
}]) }])
); );

View File

@ -113,6 +113,7 @@ callbacks["UpdateRoomList"] = function(jsonData) {
gameMode: room[2], gameMode: room[2],
playerNum: room[3], playerNum: room[3],
capacity: room[4], capacity: room[4],
hasPassword: room[5],
}); });
}); });
} }

View File

@ -12,6 +12,8 @@ Item {
id: root id: root
property alias roomModel: roomModel property alias roomModel: roomModel
property string password
Rectangle { Rectangle {
width: parent.width / 2 - roomListLayout.width / 2 width: parent.width / 2 - roomListLayout.width / 2
height: parent.height * 0.7 height: parent.height * 0.7
@ -47,7 +49,7 @@ Item {
id: roomDelegate id: roomDelegate
Item { Item {
height: 22 height: 48
width: roomList.width width: roomList.width
RowLayout { RowLayout {
@ -55,49 +57,40 @@ Item {
spacing: 16 spacing: 16
Text { Text {
text: roomId text: roomId
color: "grey"
} }
Text { Text {
horizontalAlignment: Text.AlignHCenter horizontalAlignment: Text.AlignLeft
Layout.fillWidth: true Layout.fillWidth: true
text: roomName text: roomName + (hasPassword ? "(🔒)" : "")
font.pixelSize: 20
} }
Text { Text {
text: gameMode text: Backend.translate(gameMode)
} }
Text { Text {
color: (playerNum == capacity) ? "red" : "black" color: (playerNum == capacity) ? "red" : "black"
text: playerNum + "/" + capacity text: playerNum + "/" + capacity
font.pixelSize: 20
font.bold: true
} }
Text { Button {
text: Backend.translate("Enter") text: (playerNum < capacity) ? Backend.translate("Enter") :
font.pixelSize: 24 Backend.translate("Observe")
TapHandler {
onTapped: {
config.observing = false;
mainWindow.busy = true;
ClientInstance.notifyServer(
"EnterRoom",
JSON.stringify([roomId])
);
}
}
}
Text { onClicked: {
text: Backend.translate("Observe") if (hasPassword) {
font.pixelSize: 24 lobby_dialog.sourceComponent = enterPassword;
TapHandler { lobby_dialog.item.roomId = roomId;
onTapped: { lobby_dialog.item.playerNum = playerNum;
config.observing = true; lobby_dialog.item.capacity = capacity;
mainWindow.busy = true; lobby_drawer.open();
ClientInstance.notifyServer( } else {
"ObserveRoom", enterRoom(roomId, playerNum, capacity, "");
JSON.stringify([roomId])
);
} }
} }
} }
@ -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 lobbyPlayerNum: 0
property int serverPlayerNum: 0 property int serverPlayerNum: 0

View File

@ -163,6 +163,10 @@
<source>server is using version %1, please update</source> <source>server is using version %1, please update</source>
<translation>使%1</translation> <translation>使%1</translation>
</message> </message>
<message>
<source>room password error</source>
<translation></translation>
</message>
</context> </context>
<context> <context>

View File

@ -32,6 +32,8 @@ Fk:loadTranslationTable{
["Select general num"] = "选将数目", ["Select general num"] = "选将数目",
["Operation timeout"] = "操作时长(秒)", ["Operation timeout"] = "操作时长(秒)",
["Luck Card Times"] = "手气卡次数", ["Luck Card Times"] = "手气卡次数",
["Room Password"] = "房间密码",
["Please input room's password"] = "请输入房间的密码",
["Game Mode"] = "游戏模式", ["Game Mode"] = "游戏模式",
["Enable free assign"] = "自由选将", ["Enable free assign"] = "自由选将",
["Enable deputy general"] = "启用副将机制", ["Enable deputy general"] = "启用副将机制",

View File

@ -211,7 +211,13 @@ void Router::handlePacket(const QByteArray &rawPacket) {
auto roomId = arr[0].toInt(); auto roomId = arr[0].toInt();
auto room = ServerInstance->findRoom(roomId); auto room = ServerInstance->findRoom(roomId);
if (room) { 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 { } else {
sender->doNotify("ErrorMsg", "no such room"); sender->doNotify("ErrorMsg", "no such room");
} }
@ -222,7 +228,13 @@ void Router::handlePacket(const QByteArray &rawPacket) {
auto roomId = arr[0].toInt(); auto roomId = arr[0].toInt();
auto room = ServerInstance->findRoom(roomId); auto room = ServerInstance->findRoom(roomId);
if (room) { 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 { } else {
sender->doNotify("ErrorMsg", "no such room"); sender->doNotify("ErrorMsg", "no such room");
} }

View File

@ -138,11 +138,15 @@ void Server::updateRoomList() {
QJsonArray arr; QJsonArray arr;
foreach (Room *room, rooms) { foreach (Room *room, rooms) {
QJsonArray obj; QJsonArray obj;
auto settings = QJsonDocument::fromJson(room->getSettings());
auto password = settings["password"].toString();
obj << room->getId(); // roomId obj << room->getId(); // roomId
obj << room->getName(); // roomName obj << room->getName(); // roomName
obj << "Role"; // gameMode obj << settings["gameMode"]; // gameMode
obj << room->getPlayers().count(); // playerNum obj << room->getPlayers().count(); // playerNum
obj << room->getCapacity(); // capacity obj << room->getCapacity(); // capacity
obj << !password.isEmpty();
arr << obj; arr << obj;
} }
auto jsonData = JsonArray2Bytes(arr); auto jsonData = JsonArray2Bytes(arr);