From acda9f4eb8659064442b216462ac2787065d6e07 Mon Sep 17 00:00:00 2001 From: Nyutanislavsky Date: Sun, 16 Jul 2023 19:17:03 +0800 Subject: [PATCH] Choose kingdom (#226) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 封装选择势力 2. 修复可以选到主公副将 --------- Signed-off-by: Mechanel --- lua/core/engine.lua | 15 ++-------- lua/server/gamelogic.lua | 59 ++++++++-------------------------------- lua/server/room.lua | 44 +++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 62 deletions(-) diff --git a/lua/core/engine.lua b/lua/core/engine.lua index dc653f6c..a1e09ce9 100644 --- a/lua/core/engine.lua +++ b/lua/core/engine.lua @@ -352,22 +352,11 @@ function Engine:getGeneralsRandomly(num, generalPool, except, filter) end end - if #availableGenerals == 0 then + if #availableGenerals < num then return {} end - local result = {} - for i = 1, num do - local randomGeneral = math.random(1, #availableGenerals) - table.insert(result, availableGenerals[randomGeneral]) - table.remove(availableGenerals, randomGeneral) - - if #availableGenerals == 0 then - break - end - end - - return result + return table.random(availableGenerals, num) end --- 获取已经启用的所有武将的列表。 diff --git a/lua/server/gamelogic.lua b/lua/server/gamelogic.lua index bf862532..b2230be8 100644 --- a/lua/server/gamelogic.lua +++ b/lua/server/gamelogic.lua @@ -86,7 +86,7 @@ function GameLogic:chooseGenerals() local generalNum = room.settings.generalNum local n = room.settings.enableDeputy and 2 or 1 local lord = room:getLord() - local lord_general = nil + local lord_generals = {} if lord ~= nil then room.current = lord @@ -94,11 +94,14 @@ function GameLogic:chooseGenerals() for i = 1, #generals do generals[i] = generals[i].name end - lord_general = room:askForGeneral(lord, generals, n) - local deputy - if type(lord_general) == "table" then - deputy = lord_general[2] - lord_general = lord_general[1] + lord_generals = room:askForGeneral(lord, generals, n) + local lord_general, deputy + if type(lord_generals) == "table" then + deputy = lord_generals[2] + lord_general = lord_generals[1] + else + lord_general = lord_generals + lord_generals = {lord_general} end room:setPlayerGeneral(lord, lord_general, true) @@ -125,7 +128,7 @@ function GameLogic:chooseGenerals() end local nonlord = room:getOtherPlayers(lord, true) - local generals = Fk:getGeneralsRandomly(#nonlord * generalNum, nil, {lord_general}) + local generals = Fk:getGeneralsRandomly(#nonlord * generalNum, nil, lord_generals) table.shuffle(generals) for _, p in ipairs(nonlord) do local arg = {} @@ -153,47 +156,7 @@ function GameLogic:chooseGenerals() p.default_reply = "" end - local specialKingdomPlayers = table.filter(nonlord, function(p) - return p.kingdom == "god" or Fk.generals[p.general].subkingdom - end) - - if #specialKingdomPlayers > 0 then - local choiceMap = {} - for _, p in ipairs(specialKingdomPlayers) do - local allKingdoms = {} - if p.kingdom == "god" then - allKingdoms = table.simpleClone(Fk.kingdoms) - - local exceptedKingdoms = { "god" } - for _, kingdom in ipairs(exceptedKingdoms) do - table.removeOne(allKingdoms, kingdom) - end - else - local curGeneral = Fk.generals[p.general] - allKingdoms = { curGeneral.kingdom, curGeneral.subkingdom } - end - - choiceMap[p.id] = allKingdoms - - local data = json.encode({ allKingdoms, "AskForKingdom", "#ChooseInitialKingdom" }) - p.request_data = data - end - - room:notifyMoveFocus(nonlord, "AskForKingdom") - room:doBroadcastRequest("AskForChoice", specialKingdomPlayers) - - for _, p in ipairs(specialKingdomPlayers) do - local kingdomChosen - if p.reply_ready then - kingdomChosen = p.client_reply - else - kingdomChosen = choiceMap[p.id][1] - end - - p.kingdom = kingdomChosen - room:notifyProperty(p, p, "kingdom") - end - end + room:askForChooseKingdom(nonlord) end function GameLogic:buildPlayerCircle() diff --git a/lua/server/room.lua b/lua/server/room.lua index b9cfa5eb..d6da4487 100644 --- a/lua/server/room.lua +++ b/lua/server/room.lua @@ -1245,7 +1245,7 @@ end ---@param player ServerPlayer @ 询问目标 ---@param generals string[] @ 可选武将 ---@param n integer @ 可选数量,默认为1 ----@return string @ 选择的武将 +---@return string|string[] @ 选择的武将 function Room:askForGeneral(player, generals, n) local command = "AskForGeneral" self:notifyMoveFocus(player, command) @@ -1269,6 +1269,48 @@ function Room:askForGeneral(player, generals, n) return defaultChoice end +--- 询问玩家若为神将、双势力需选择一个势力。 +---@param player ServerPlayer[]|nil @ 询问目标 +function Room:askForChooseKingdom(players) + players = players or self.alive_players + local specialKingdomPlayers = table.filter(players, function(p) + return p.kingdom == "god" or Fk.generals[p.general].subkingdom + end) + + if #specialKingdomPlayers > 0 then + local choiceMap = {} + for _, p in ipairs(specialKingdomPlayers) do + local allKingdoms = {} + if p.kingdom == "god" then + allKingdoms = table.filter({"wei", "shu", "wu", "qun", "jin"}, function(k) return table.contains(Fk.kingdoms, k) end) + else + local curGeneral = Fk.generals[p.general] + allKingdoms = { curGeneral.kingdom, curGeneral.subkingdom } + end + + choiceMap[p.id] = allKingdoms + + local data = json.encode({ allKingdoms, allKingdoms, "AskForKingdom", "#ChooseInitialKingdom" }) + p.request_data = data + end + + self:notifyMoveFocus(players, "AskForKingdom") + self:doBroadcastRequest("AskForChoice", specialKingdomPlayers) + + for _, p in ipairs(specialKingdomPlayers) do + local kingdomChosen + if p.reply_ready then + kingdomChosen = p.client_reply + else + kingdomChosen = choiceMap[p.id][1] + end + + p.kingdom = kingdomChosen + self:notifyProperty(p, p, "kingdom") + end + end +end + --- 询问chooser,选择target的一张牌。 ---@param chooser ServerPlayer @ 要被询问的人 ---@param target ServerPlayer @ 被选牌的人