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

View File

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

View File

@ -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

View File

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

View File

@ -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"] = "启用副将机制",

View File

@ -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");
}

View File

@ -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);