Choose kingdom (#226)

1. 封装选择势力
2. 修复可以选到主公副将

---------

Signed-off-by: Mechanel <nyutanislavsky@qq.com>
This commit is contained in:
Nyutanislavsky 2023-07-16 19:17:03 +08:00 committed by GitHub
parent 620780ac08
commit acda9f4eb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 62 deletions

View File

@ -352,22 +352,11 @@ function Engine:getGeneralsRandomly(num, generalPool, except, filter)
end end
end end
if #availableGenerals == 0 then if #availableGenerals < num then
return {} return {}
end end
local result = {} return table.random(availableGenerals, num)
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
end end
--- 获取已经启用的所有武将的列表。 --- 获取已经启用的所有武将的列表。

View File

@ -86,7 +86,7 @@ function GameLogic:chooseGenerals()
local generalNum = room.settings.generalNum local generalNum = room.settings.generalNum
local n = room.settings.enableDeputy and 2 or 1 local n = room.settings.enableDeputy and 2 or 1
local lord = room:getLord() local lord = room:getLord()
local lord_general = nil local lord_generals = {}
if lord ~= nil then if lord ~= nil then
room.current = lord room.current = lord
@ -94,11 +94,14 @@ function GameLogic:chooseGenerals()
for i = 1, #generals do for i = 1, #generals do
generals[i] = generals[i].name generals[i] = generals[i].name
end end
lord_general = room:askForGeneral(lord, generals, n) lord_generals = room:askForGeneral(lord, generals, n)
local deputy local lord_general, deputy
if type(lord_general) == "table" then if type(lord_generals) == "table" then
deputy = lord_general[2] deputy = lord_generals[2]
lord_general = lord_general[1] lord_general = lord_generals[1]
else
lord_general = lord_generals
lord_generals = {lord_general}
end end
room:setPlayerGeneral(lord, lord_general, true) room:setPlayerGeneral(lord, lord_general, true)
@ -125,7 +128,7 @@ function GameLogic:chooseGenerals()
end end
local nonlord = room:getOtherPlayers(lord, true) 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) table.shuffle(generals)
for _, p in ipairs(nonlord) do for _, p in ipairs(nonlord) do
local arg = {} local arg = {}
@ -153,47 +156,7 @@ function GameLogic:chooseGenerals()
p.default_reply = "" p.default_reply = ""
end end
local specialKingdomPlayers = table.filter(nonlord, function(p) room:askForChooseKingdom(nonlord)
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
end end
function GameLogic:buildPlayerCircle() function GameLogic:buildPlayerCircle()

View File

@ -1245,7 +1245,7 @@ end
---@param player ServerPlayer @ 询问目标 ---@param player ServerPlayer @ 询问目标
---@param generals string[] @ 可选武将 ---@param generals string[] @ 可选武将
---@param n integer @ 可选数量默认为1 ---@param n integer @ 可选数量默认为1
---@return string @ 选择的武将 ---@return string|string[] @ 选择的武将
function Room:askForGeneral(player, generals, n) function Room:askForGeneral(player, generals, n)
local command = "AskForGeneral" local command = "AskForGeneral"
self:notifyMoveFocus(player, command) self:notifyMoveFocus(player, command)
@ -1269,6 +1269,48 @@ function Room:askForGeneral(player, generals, n)
return defaultChoice return defaultChoice
end 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的一张牌。 --- 询问chooser选择target的一张牌。
---@param chooser ServerPlayer @ 要被询问的人 ---@param chooser ServerPlayer @ 要被询问的人
---@param target ServerPlayer @ 被选牌的人 ---@param target ServerPlayer @ 被选牌的人