From 04f10090751c0668c1cb757cb2cd2aea76fa66cf Mon Sep 17 00:00:00 2001 From: Rintim Date: Fri, 9 Jun 2023 17:23:24 +0800 Subject: [PATCH] Provide hasMark methods (#180) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It provides two methods `Player:hasMark` and `Card:hasMark` for determining if the player/card has the mark, which is more expressive than `xxx:getMark` when only checking for the existence. By the way, xxx:hasMark may directly determine a `Mark` in xxx.mark, for which I added `table:hasKey` method in `util.lua`, but currently the code still checks if the result of `xxx:getMark` is not 0. --- 提供了`Player:hasMark`和`Card:hasMark`两个方法,用于在只需要判断是否有标记时可以直接使用`xxx:hasMark`,相比使用`xxx:getMark`而言更加表达代码的作用。 顺带一提,`xxx:hasMark`应该可以直接判断`Mark`是否在`xxx.mark`里,我在`util.lua`里添加了`table:hasKey`的方法用于判断,但目前的代码还是判断`xxx:getMark`的结果是否不为0。 --- lua/core/card.lua | 9 ++++++++- lua/core/player.lua | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lua/core/card.lua b/lua/core/card.lua index b7cd1198..1f2d409b 100644 --- a/lua/core/card.lua +++ b/lua/core/card.lua @@ -322,11 +322,18 @@ end --- 获取卡牌对应Mark的数量。 ---@param mark string @ 标记 ----@param count integer @ 为标记删除的数量 +---@return integer function Card:getMark(mark) return (self.mark[mark] or 0) end +--- 判定卡牌是否拥有对应的Mark。 +---@param mark string @ 标记 +---@return boolean +function Card:hasMark(mark) + return self:getMark(mark) ~= 0 +end + --- 获取卡牌有哪些Mark。 function Card:getMarkNames() local ret = {} diff --git a/lua/core/player.lua b/lua/core/player.lua index 21c8b9b0..73bb9b6c 100644 --- a/lua/core/player.lua +++ b/lua/core/player.lua @@ -192,11 +192,18 @@ end --- 获取角色对应Mark的数量。 ---@param mark string @ 标记 ----@param count integer @ 为标记删除的数量 +---@return integer function Player:getMark(mark) return (self.mark[mark] or 0) end +--- 判定角色是否拥有对应的Mark。 +---@param mark string @ 标记 +---@return boolean +function Player:hasMark(mark) + return self:getMark(mark) ~= 0 +end + --- 获取角色有哪些Mark。 function Player:getMarkNames() local ret = {}