General & Cards overview (#18)

* adjust room list of the lobby

* add client lua func

* overviews

Co-authored-by: Notify-ctrl <notify-ctrl@qq.com>
This commit is contained in:
notify 2022-04-15 18:37:20 +08:00 committed by GitHub
parent af4924c260
commit fd2d7b4d10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 235 additions and 37 deletions

View File

@ -6,7 +6,9 @@ function GetGeneralData(name)
local general = Fk.generals[name] local general = Fk.generals[name]
if general == nil then general = Fk.generals["diaochan"] end if general == nil then general = Fk.generals["diaochan"] end
return json.encode { return json.encode {
general.kingdom kingdom = general.kingdom,
hp = general.hp,
maxHp = general.maxHp
} }
end end
@ -24,3 +26,39 @@ function GetCardData(id)
color = card.color, color = card.color,
} }
end end
function GetAllGeneralPack()
local ret = {}
for _, name in ipairs(Fk.package_names) do
if Fk.packages[name].type == Package.GeneralPack then
table.insert(ret, name)
end
end
return json.encode(ret)
end
function GetGenerals(pack_name)
local ret = {}
for _, g in ipairs(Fk.packages[pack_name].generals) do
table.insert(ret, g.name)
end
return json.encode(ret)
end
function GetAllCardPack()
local ret = {}
for _, name in ipairs(Fk.package_names) do
if Fk.packages[name].type == Package.CardPack then
table.insert(ret, name)
end
end
return json.encode(ret)
end
function GetCards(pack_name)
local ret = {}
for _, c in ipairs(Fk.packages[pack_name].cards) do
table.insert(ret, c.id)
end
return json.encode(ret)
end

View File

@ -1,5 +1,6 @@
---@class Engine : Object ---@class Engine : Object
---@field packages table<string, Package> ---@field packages table<string, Package>
---@field package_names string[]
---@field skills table<string, Skill> ---@field skills table<string, Skill>
---@field related_skills table<string, Skill[]> ---@field related_skills table<string, Skill[]>
---@field global_trigger TriggerSkill[] ---@field global_trigger TriggerSkill[]
@ -19,6 +20,7 @@ function Engine:initialize()
Fk = self Fk = self
self.packages = {} -- name --> Package self.packages = {} -- name --> Package
self.package_names = {}
self.skills = {} -- name --> Skill self.skills = {} -- name --> Skill
self.related_skills = {} -- skillName --> relatedSkill[] self.related_skills = {} -- skillName --> relatedSkill[]
self.global_trigger = {} self.global_trigger = {}
@ -37,6 +39,7 @@ function Engine:loadPackage(pack)
error(string.format("Duplicate package %s detected", pack.name)) error(string.format("Duplicate package %s detected", pack.name))
end end
self.packages[pack.name] = pack self.packages[pack.name] = pack
table.insert(self.package_names, pack.name)
-- add cards, generals and skills to Engine -- add cards, generals and skills to Engine
if pack.type == Package.CardPack then if pack.type == Package.CardPack then
@ -48,9 +51,26 @@ function Engine:loadPackage(pack)
end end
function Engine:loadPackages() function Engine:loadPackages()
for _, dir in ipairs(FileIO.ls("packages")) do local directories = FileIO.ls("packages")
-- load standard & standard_cards first
self:loadPackage(require("packages.standard"))
self:loadPackage(require("packages.standard_cards"))
table.removeOne(directories, "standard")
table.removeOne(directories, "standard_cards")
for _, dir in ipairs(directories) do
if FileIO.isDir("packages/" .. dir) then if FileIO.isDir("packages/" .. dir) then
self:loadPackage(require(string.format("packages.%s", dir))) local pack = require(string.format("packages.%s", dir))
-- Note that instance of Package is a table too
-- so dont use type(pack) == "table" here
if pack[1] ~= nil then
for _, p in ipairs(pack) do
self:loadPackage(p)
end
else
self:loadPackage(pack)
end
end end
end end
end end

View File

@ -3,6 +3,7 @@ extension.metadata = require "packages.standard.metadata"
dofile "packages/standard/game_rule.lua" dofile "packages/standard/game_rule.lua"
Fk:loadTranslationTable{ Fk:loadTranslationTable{
["standard"] = "标准包",
["wei"] = "", ["wei"] = "",
["shu"] = "", ["shu"] = "",
["wu"] = "", ["wu"] = "",

View File

@ -1,6 +1,10 @@
local extension = Package:new("standard_cards", Package.CardPack) local extension = Package:new("standard_cards", Package.CardPack)
extension.metadata = require "packages.standard_cards.metadata" extension.metadata = require "packages.standard_cards.metadata"
Fk:loadTranslationTable{
["standard_cards"] = "标+EX"
}
local slash = fk.CreateBasicCard{ local slash = fk.CreateBasicCard{
name = "slash", name = "slash",
number = 7, number = 7,
@ -278,6 +282,7 @@ Fk:loadTranslationTable{
extension:addCards({ extension:addCards({
indulgence, indulgence,
indulgence:clone(Card.Club, 6), indulgence:clone(Card.Club, 6),
indulgence:clone(Card.Heart, 6),
}) })
local crossbow = fk.CreateWeapon{ local crossbow = fk.CreateWeapon{

View File

@ -0,0 +1,52 @@
import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.0
import "RoomElement"
Item {
id: root
property bool loaded: false
ListView {
width: Math.floor(root.width / 98) * 98
height: parent.height
anchors.centerIn: parent
ScrollBar.vertical: ScrollBar {}
model: ListModel {
id: packages
}
delegate: ColumnLayout {
Text { text: Backend.translate(name) }
GridLayout {
columns: root.width / 98
Repeater {
model: JSON.parse(Backend.getCards(name))
CardItem {
autoBack: false
Component.onCompleted: {
let data = JSON.parse(Backend.getCardData(modelData));
setData(data);
}
}
}
}
}
}
Button {
text: "Quit"
anchors.right: parent.right
onClicked: {
mainStack.pop();
}
}
function loadPackages() {
if (loaded) return;
let packs = JSON.parse(Backend.getAllCardPack());
packs.forEach((name) => packages.append({ name: name }));
loaded = true;
}
}

View File

@ -0,0 +1,53 @@
import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.0
import "RoomElement"
Item {
id: root
property bool loaded: false
ListView {
width: Math.floor(root.width / 98) * 98
height: parent.height
anchors.centerIn: parent
ScrollBar.vertical: ScrollBar {}
model: ListModel {
id: packages
}
delegate: ColumnLayout {
Text { text: Backend.translate(name) }
GridLayout {
columns: root.width / 98
Repeater {
model: JSON.parse(Backend.getGenerals(name))
GeneralCardItem {
autoBack: false
Component.onCompleted: {
let data = JSON.parse(Backend.getGeneralData(modelData));
name = modelData;
kingdom = data.kingdom;
}
}
}
}
}
}
Button {
text: "Quit"
anchors.right: parent.right
onClicked: {
mainStack.pop();
}
}
function loadPackages() {
if (loaded) return;
let packs = JSON.parse(Backend.getAllGeneralPack());
packs.forEach((name) => packages.append({ name: name }));
loaded = true;
}
}

View File

@ -10,27 +10,26 @@ Item {
Component { Component {
id: roomDelegate id: roomDelegate
Row { RowLayout {
spacing: 24 width: roomList.width * 0.9
spacing: 16
Text { Text {
width: 40 text: roomId
text: String(roomId)
} }
Text { Text {
width: 40 horizontalAlignment: Text.AlignHCenter
Layout.fillWidth: true
text: roomName text: roomName
} }
Text { Text {
width: 20
text: gameMode text: gameMode
} }
Text { Text {
width: 10
color: (playerNum == capacity) ? "red" : "black" color: (playerNum == capacity) ? "red" : "black"
text: String(playerNum) + "/" + String(capacity) text: playerNum + "/" + capacity
} }
Text { Text {
@ -60,30 +59,25 @@ Item {
RowLayout { RowLayout {
anchors.fill: parent anchors.fill: parent
Rectangle { Rectangle {
width: root.width * 0.7 Layout.preferredWidth: root.width * 0.7
height: root.height Layout.fillHeight: true
color: "#e2e2e1" color: "#e2e2e1"
radius: 4 radius: 4
Text { Text {
width: parent.width
horizontalAlignment: Text.AlignHCenter
text: "Room List" text: "Room List"
} }
ListView { ListView {
height: parent.height * 0.9 height: parent.height * 0.9
width: parent.width * 0.95 width: parent.width * 0.95
contentHeight: roomDelegate.height * count
ScrollBar.vertical: ScrollBar {}
anchors.centerIn: parent anchors.centerIn: parent
id: roomList id: roomList
delegate: roomDelegate delegate: roomDelegate
model: roomModel model: roomModel
} }
Rectangle {
id: scrollbar
anchors.right: roomList.right
y: roomList.visibleArea.yPosition * roomList.height
width: 10
radius: 4
height: roomList.visibleArea.heightRatio * roomList.height
color: "#a89da8"
}
} }
ColumnLayout { ColumnLayout {
@ -103,9 +97,17 @@ Item {
} }
Button { Button {
text: "Generals Overview" text: "Generals Overview"
onClicked: {
mainStack.push(generalsOverview);
mainStack.currentItem.loadPackages();
}
} }
Button { Button {
text: "Cards Overview" text: "Cards Overview"
onClicked: {
mainStack.push(cardsOverview);
mainStack.currentItem.loadPackages();
}
} }
Button { Button {
text: "Scenarios Overview" text: "Scenarios Overview"

View File

@ -308,6 +308,6 @@ Item {
if (!roomScene.isStarted) return; if (!roomScene.isStarted) return;
generalName.text = Backend.translate(general); generalName.text = Backend.translate(general);
let data = JSON.parse(Backend.getGeneralData(general)); let data = JSON.parse(Backend.getGeneralData(general));
kingdom = data[0]; kingdom = data.kingdom;
} }
} }

View File

@ -18,20 +18,11 @@ Window {
anchors.fill: parent anchors.fill: parent
} }
Component { Component { id: init; Init {} }
id: init Component { id: lobby; Lobby {} }
Init {} Component { id: generalsOverview; GeneralsOverview {} }
} Component { id: cardsOverview; CardsOverview {} }
Component { id: room; Room {} }
Component {
id: lobby
Lobby {}
}
Component {
id: room
Room {}
}
property bool busy: false property bool busy: false
BusyIndicator { BusyIndicator {

View File

@ -119,4 +119,36 @@ QString QmlBackend::getCardData(int id) {
CALLFUNC CALLFUNC
} }
QString QmlBackend::getAllGeneralPack() {
lua_State *L = ClientInstance->getLuaState();
lua_getglobal(L, "GetAllGeneralPack");
lua_pushinteger(L, 0);
CALLFUNC
}
QString QmlBackend::getGenerals(const QString &pack_name) {
lua_State *L = ClientInstance->getLuaState();
lua_getglobal(L, "GetGenerals");
lua_pushstring(L, pack_name.toUtf8().data());
CALLFUNC
}
QString QmlBackend::getAllCardPack() {
lua_State *L = ClientInstance->getLuaState();
lua_getglobal(L, "GetAllCardPack");
lua_pushinteger(L, 0);
CALLFUNC
}
QString QmlBackend::getCards(const QString &pack_name) {
lua_State *L = ClientInstance->getLuaState();
lua_getglobal(L, "GetCards");
lua_pushstring(L, pack_name.toUtf8().data());
CALLFUNC
}
#undef CALLFUNC #undef CALLFUNC

View File

@ -29,6 +29,10 @@ public:
Q_INVOKABLE QString translate(const QString &src); Q_INVOKABLE QString translate(const QString &src);
Q_INVOKABLE QString getGeneralData(const QString &general_name); Q_INVOKABLE QString getGeneralData(const QString &general_name);
Q_INVOKABLE QString getCardData(int id); Q_INVOKABLE QString getCardData(int id);
Q_INVOKABLE QString getAllGeneralPack();
Q_INVOKABLE QString getGenerals(const QString &pack_name);
Q_INVOKABLE QString getAllCardPack();
Q_INVOKABLE QString getCards(const QString &pack_name);
signals: signals:
void notifyUI(const QString &command, const QString &jsonData); void notifyUI(const QString &command, const QString &jsonData);