2022-03-30 08:33:56 +00:00
|
|
|
---@class Player : Object
|
2022-04-03 13:43:35 +00:00
|
|
|
---@field id integer
|
2022-04-01 12:51:01 +00:00
|
|
|
---@field hp integer
|
|
|
|
---@field maxHp integer
|
|
|
|
---@field kingdom string
|
|
|
|
---@field role string
|
|
|
|
---@field general string
|
|
|
|
---@field handcard_num integer
|
|
|
|
---@field seat integer
|
|
|
|
---@field phase Phase
|
|
|
|
---@field faceup boolean
|
|
|
|
---@field chained boolean
|
|
|
|
---@field dying boolean
|
|
|
|
---@field dead boolean
|
|
|
|
---@field state string
|
|
|
|
---@field player_skills Skill[]
|
2022-04-02 13:39:44 +00:00
|
|
|
---@field flag string[]
|
|
|
|
---@field tag table<string, any>
|
|
|
|
---@field mark table<string, integer>
|
2022-04-08 10:39:58 +00:00
|
|
|
---@field player_cards table<integer, integer[]>
|
|
|
|
---@field special_cards table<string, integer[]>
|
2022-04-30 07:27:56 +00:00
|
|
|
---@field cardUsedHistory table<string, integer>
|
2022-03-27 06:49:41 +00:00
|
|
|
local Player = class("Player")
|
2022-03-25 04:28:07 +00:00
|
|
|
|
2022-04-01 12:51:01 +00:00
|
|
|
---@alias Phase integer
|
|
|
|
|
|
|
|
Player.RoundStart = 1
|
|
|
|
Player.Start = 2
|
|
|
|
Player.Judge = 3
|
|
|
|
Player.Draw = 4
|
|
|
|
Player.Play = 5
|
|
|
|
Player.Discard = 6
|
|
|
|
Player.Finish = 7
|
|
|
|
Player.NotActive = 8
|
|
|
|
Player.PhaseNone = 9
|
|
|
|
|
2022-04-08 10:39:58 +00:00
|
|
|
---@alias PlayerCardArea integer
|
|
|
|
|
|
|
|
Player.Hand = 1
|
|
|
|
Player.Equip = 2
|
|
|
|
Player.Judge = 3
|
|
|
|
Player.Special = 4
|
|
|
|
|
2022-03-25 04:28:07 +00:00
|
|
|
function Player:initialize()
|
2022-04-30 07:27:56 +00:00
|
|
|
self.id = 114514
|
|
|
|
self.hp = 0
|
|
|
|
self.maxHp = 0
|
|
|
|
self.kingdom = "qun"
|
|
|
|
self.role = ""
|
|
|
|
self.general = ""
|
|
|
|
self.seat = 0
|
|
|
|
self.phase = Player.PhaseNone
|
|
|
|
self.faceup = true
|
|
|
|
self.chained = false
|
|
|
|
self.dying = false
|
|
|
|
self.dead = false
|
|
|
|
self.state = ""
|
|
|
|
|
|
|
|
self.player_skills = {}
|
|
|
|
self.flag = {}
|
|
|
|
self.tag = {}
|
|
|
|
self.mark = {}
|
|
|
|
self.player_cards = {
|
|
|
|
[Player.Hand] = {},
|
|
|
|
[Player.Equip] = {},
|
|
|
|
[Player.Judge] = {},
|
|
|
|
}
|
|
|
|
self.special_cards = {}
|
|
|
|
|
|
|
|
self.cardUsedHistory = {}
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
|
|
|
|
2022-03-31 05:29:23 +00:00
|
|
|
---@param general General
|
|
|
|
---@param setHp boolean
|
|
|
|
---@param addSkills boolean
|
2022-03-25 04:28:07 +00:00
|
|
|
function Player:setGeneral(general, setHp, addSkills)
|
2022-04-30 07:27:56 +00:00
|
|
|
self.general = general
|
|
|
|
if setHp then
|
|
|
|
self.maxHp = general.maxHp
|
|
|
|
self.hp = general.hp
|
|
|
|
end
|
2022-03-25 04:28:07 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
if addSkills then
|
|
|
|
table.insertTable(self.player_skills, general.skills)
|
|
|
|
end
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
|
|
|
|
2022-04-02 13:39:44 +00:00
|
|
|
---@param flag string
|
|
|
|
function Player:hasFlag(flag)
|
2022-04-30 07:27:56 +00:00
|
|
|
return table.contains(self.flag, flag)
|
2022-04-02 13:39:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param flag string
|
|
|
|
function Player:setFlag(flag)
|
2022-04-30 07:27:56 +00:00
|
|
|
if flag == "." then
|
|
|
|
self:clearFlags()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if flag:sub(1, 1) == "-" then
|
|
|
|
flag = flag:sub(2, #flag)
|
|
|
|
table.removeOne(self.flag, flag)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if not self:hasFlag(flag) then
|
|
|
|
table.insert(self.flag, flag)
|
|
|
|
end
|
2022-04-02 13:39:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Player:clearFlags()
|
2022-04-30 07:27:56 +00:00
|
|
|
self.flag = {}
|
2022-04-02 13:39:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Player:addMark(mark, count)
|
2022-04-30 07:27:56 +00:00
|
|
|
count = count or 1
|
|
|
|
local num = self.mark[mark]
|
|
|
|
num = num or 0
|
|
|
|
self:setMark(mark, math.max(num + count, 0))
|
2022-04-02 13:39:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Player:removeMark(mark, count)
|
2022-04-30 07:27:56 +00:00
|
|
|
count = count or 1
|
|
|
|
local num = self.mark[mark]
|
|
|
|
num = num or 0
|
|
|
|
self:setMark(mark, math.max(num - count, 0))
|
2022-04-02 13:39:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Player:setMark(mark, count)
|
2022-04-30 07:27:56 +00:00
|
|
|
if self.mark[mark] ~= count then
|
|
|
|
self.mark[mark] = count
|
|
|
|
end
|
2022-04-02 13:39:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Player:getMark(mark)
|
2022-04-30 07:27:56 +00:00
|
|
|
return (self.mark[mark] or 0)
|
2022-04-02 13:39:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Player:getMarkNames()
|
2022-04-30 07:27:56 +00:00
|
|
|
local ret = {}
|
|
|
|
for k, _ in pairs(self.mark) do
|
|
|
|
table.insert(ret, k)
|
|
|
|
end
|
|
|
|
return ret
|
2022-04-02 13:39:44 +00:00
|
|
|
end
|
|
|
|
|
2022-04-08 10:39:58 +00:00
|
|
|
---@param playerArea PlayerCardArea
|
|
|
|
---@param cardIds integer[]
|
|
|
|
---@param specialName string
|
|
|
|
function Player:addCards(playerArea, cardIds, specialName)
|
2022-04-30 07:27:56 +00:00
|
|
|
assert(table.contains({ Player.Hand, Player.Equip, Player.Judge, Player.Special }, playerArea))
|
|
|
|
assert(playerArea ~= Player.Special or type(specialName) == "string")
|
|
|
|
|
|
|
|
if playerArea == Player.Special then
|
|
|
|
self.special_cards[specialName] = self.special_cards[specialName] or {}
|
|
|
|
table.insertTable(self.special_cards[specialName], cardIds)
|
|
|
|
else
|
|
|
|
table.insertTable(self.player_cards[playerArea], cardIds)
|
|
|
|
end
|
2022-04-08 10:39:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param playerArea PlayerCardArea
|
|
|
|
---@param cardIds integer[]
|
|
|
|
---@param specialName string
|
|
|
|
function Player:removeCards(playerArea, cardIds, specialName)
|
2022-04-30 07:27:56 +00:00
|
|
|
assert(table.contains({ Player.Hand, Player.Equip, Player.Judge, Player.Special }, playerArea))
|
|
|
|
assert(playerArea ~= Player.Special or type(specialName) == "string")
|
|
|
|
|
|
|
|
local fromAreaIds = playerArea == Player.Special and self.special_cards[specialName] or self.player_cards[playerArea]
|
|
|
|
if fromAreaIds then
|
|
|
|
for _, id in ipairs(cardIds) do
|
|
|
|
if #fromAreaIds == 0 then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
table.removeOne(fromAreaIds, id)
|
2022-04-08 10:39:58 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
2022-04-08 10:39:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param playerAreas PlayerCardArea
|
|
|
|
---@param specialName string
|
|
|
|
---@return integer[]
|
|
|
|
function Player:getCardIds(playerAreas, specialName)
|
2022-04-30 07:27:56 +00:00
|
|
|
local rightAreas = { Player.Hand, Player.Equip, Player.Judge }
|
|
|
|
playerAreas = playerAreas or rightAreas
|
|
|
|
assert(type(playerAreas) == "number" or type(playerAreas) == "table")
|
|
|
|
local areas = type(playerAreas) == "table" and playerAreas or { playerAreas }
|
|
|
|
|
|
|
|
local rightAreas = { Player.Hand, Player.Equip, Player.Judge, Player.Special }
|
|
|
|
local cardIds = {}
|
|
|
|
for _, area in ipairs(areas) do
|
|
|
|
assert(table.contains(rightAreas, area))
|
|
|
|
assert(area ~= Player.Special or type(specialName) == "string")
|
|
|
|
local currentCardIds = area == Player.Special and self.special_cards[specialName] or self.player_cards[area]
|
|
|
|
table.insertTable(cardIds, currentCardIds)
|
|
|
|
end
|
|
|
|
|
|
|
|
return cardIds
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param cardSubtype CardSubtype
|
|
|
|
---@return integer|null
|
|
|
|
function Player:getEquipment(cardSubtype)
|
|
|
|
for _, cardId in ipairs(self.player_cards[Player.Equip]) do
|
|
|
|
if Fk:getCardById(cardId).sub_type == cardSubtype then
|
|
|
|
return cardId
|
2022-04-08 10:39:58 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
2022-04-08 10:39:58 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
return nil
|
2022-04-08 10:39:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Player:getMaxCards()
|
2022-04-30 07:27:56 +00:00
|
|
|
local baseValue = math.max(self.hp, 0)
|
2022-04-08 10:39:58 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
return baseValue
|
2022-04-08 10:39:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param subtype CardSubtype
|
|
|
|
---@return integer|null
|
|
|
|
function Player:getEquipBySubtype(subtype)
|
2022-04-30 07:27:56 +00:00
|
|
|
local equipId = nil
|
|
|
|
for _, id in ipairs(self.player_cards[Player.Equip]) do
|
|
|
|
if Fk:getCardById(id).sub_type == subtype then
|
|
|
|
equipId = id
|
|
|
|
break
|
2022-04-08 10:39:58 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
2022-04-08 10:39:58 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
return equipId
|
2022-04-08 10:39:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Player:getAttackRange()
|
2022-04-30 07:27:56 +00:00
|
|
|
local weapon = Fk:getCardById(self:getEquipBySubtype(Card.SubtypeWeapon))
|
|
|
|
local baseAttackRange = math.max(weapon and weapon.attack_range or 1, 0)
|
|
|
|
|
|
|
|
return math.max(baseAttackRange, 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Player:addCardUseHistory(cardName, num)
|
|
|
|
assert(type(num) == "number" and num ~= 0)
|
|
|
|
|
|
|
|
self.cardUsedHistory[cardName] = self.cardUsedHistory[cardName] or 0
|
|
|
|
self.cardUsedHistory[cardName] = self.cardUsedHistory[cardName] + num
|
|
|
|
end
|
2022-04-08 10:39:58 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
function Player:resetCardUseHistory(cardName)
|
|
|
|
if self.cardUsedHistory[cardName] then
|
|
|
|
self.cardUsedHistory[cardName] = 0
|
|
|
|
end
|
2022-04-08 10:39:58 +00:00
|
|
|
end
|
|
|
|
|
2022-03-27 06:49:41 +00:00
|
|
|
return Player
|