Bugfixes for "Remove One Use" hotkey

This commit is contained in:
Chr1Z93 2024-07-26 10:48:54 +02:00
parent ba8f6c9c3e
commit 09267294c6
3 changed files with 97 additions and 26 deletions

View File

@ -102,9 +102,11 @@ function takeCardIntoThreatArea(playerColor, hoveredObject)
modifierY = -90
end
-- contruct feedback message
local cardName = hoveredObject.getName()
if cardName == "" then cardName = "card" end
broadcastToAll("Moved " .. cardName .. " to " .. getColoredName(playerColor) .. "'s threat area.", "White")
if cardName == "" then cardName = "a card" end
local playerName = getColoredName(playerColor)
broadcastToAll("Moved " .. cardName .. " to " .. playerName .. "'s threat area.", "White")
-- get new rotation (rounded)
local cardRot = hoveredObject.getRotation()
@ -256,27 +258,66 @@ function removeOneUse(playerColor, hoveredObject)
if hoveredObject.type == "Tile" then
targetObject = hoveredObject
elseif hoveredObject.type == "Card" then
-- grab the first use type from the metadata (or nil)
local notes = JSON.decode(hoveredObject.getGMNotes()) or {}
local usesData = notes.uses or {}
local useInfo = usesData[1] or {}
local searchForType = useInfo.type
if searchForType then searchForType = searchForType:lower() end
local searchResult = searchLib.onObject(hoveredObject, "isTileOrToken")
for _, obj in ipairs(searchLib.onObject(hoveredObject, "isTileOrToken")) do
if not obj.locked and obj.memo ~= "resourceCounter" then
-- check for matching object, otherwise use the first hit
if obj.memo and obj.memo == searchForType then
targetObject = obj
break
elseif not targetObject then
targetObject = obj
if #searchResult == 0 then
broadcastToColor("No tokens found!", playerColor, "Yellow")
return
end
-- index the found tokens by memo (only the first of each type)
local indexByMemo = {}
for _, obj in ipairs(searchResult) do
if not obj.locked then
if obj.memo and indexByMemo[obj.memo] == nil then
indexByMemo[obj.memo] = obj
elseif indexByMemo["NO_MEMO"] == nil then
indexByMemo["NO_MEMO"] = obj
end
end
end
-- use metadata (if present) to determine targetObject
local usesAreTypeOfResource = false
local notes = JSON.decode(hoveredObject.getGMNotes()) or {}
for _, useInfo in ipairs(notes.uses or {}) do
if useInfo.type then
local discardMemo = useInfo.type:lower()
if indexByMemo[discardMemo] then
targetObject = indexByMemo[discardMemo]
break
end
end
if useInfo.token == "resource" then
usesAreTypeOfResource = true
end
end
-- check for alternatives (check resources first if tokens are a type of resource)
if not targetObject then
if usesAreTypeOfResource and indexByMemo["resource"] then
targetObject = indexByMemo["resource"]
else
for memo, obj in pairs(indexByMemo) do
if memo ~= "resourceCounter" and memo ~= "NO_MEMO" then
targetObject = obj
break
end
end
end
end
-- if there's still not a target check for clickable counter and token without memo
if not targetObject then
if indexByMemo["resourceCounter"] then
indexByMemo["resourceCounter"].call("modifyValue", -1)
return
elseif indexByMemo["NO_MEMO"] then
targetObject = indexByMemo["NO_MEMO"]
end
end
end
-- error handling
if not targetObject then
broadcastToColor("No tokens found!", playerColor, "Yellow")
return
@ -297,6 +338,16 @@ function removeOneUse(playerColor, hoveredObject)
end
-- feedback message
local cardName
if hoveredObject.type == "Card" then
cardName = hoveredObject.getName()
else
local searchResult = searchLib.belowPosition(targetObject.getPosition(), "isCard")
if #searchResult > 0 then
cardName = searchResult[1].getName()
end
end
local tokenName = targetObject.getName()
if tokenName == "" then
if targetObject.memo ~= "" then
@ -311,11 +362,18 @@ function removeOneUse(playerColor, hoveredObject)
tokenName = titleCase(targetObject.memo)
end
else
tokenName = "Unknown"
tokenName = "unknown token"
end
end
broadcastToAll(getColoredName(playerColor) .. " removed a token: " .. tokenName, playerColor)
-- construct feedback message
local playerName = getColoredName(playerColor)
local article = getArticle(tokenName)
local cardInfo = ""
if cardName and cardName ~= "" then
cardInfo = " from " .. cardName
end
broadcastToAll(playerName .. " removed" .. article .. tokenName .. cardInfo .. ".", "White")
local discardForMatColor = getColorToDiscardFor(hoveredObject, playerColor)
playermatApi.discardListOfObjects(discardForMatColor, { targetObject })
@ -427,7 +485,7 @@ function takeClueFromLocation(playerColor, hoveredObject)
end
elseif hoveredObject.type == "Infinite" and hoveredObject.getName() == "Clue tokens" then
clue = hoveredObject.takeObject()
cardName = "token pool"
cardName = "the token pool"
else
broadcastToColor("Hover a clue or card with clues and try again.", messageColor, "Yellow")
return
@ -460,11 +518,13 @@ function takeClueFromLocation(playerColor, hoveredObject)
clue.setRotation(rot)
end
if cardName then
broadcastToAll(getColoredName(playerColor) .. " took one clue from " .. cardName .. ".", "White")
else
broadcastToAll(getColoredName(playerColor) .. " took one clue.", "White")
-- construct feedback message
local playerName = getColoredName(playerColor)
local cardInfo = ""
if cardName and cardName ~= "" then
cardInfo = " from " .. cardName
end
broadcastToAll(playerName .. " took one clue" .. cardInfo .. ".", "White")
victoryDisplayApi.update()
end
@ -496,6 +556,13 @@ function titleCase(str)
return first:upper() .. rest:lower()
end
-- gets the proper article (disregarding silent "h")
function getArticle(str)
local vowels = { a = true, e = true, i = true, o = true, u = true }
local firstLetter = string.lower(str:sub(1, 1))
return vowels[firstLetter] and " an " or " a "
end
-- returns the color of the first seated player
function getFirstSeatedPlayer()
for _, color in ipairs(getSeatedPlayers()) do

View File

@ -49,6 +49,10 @@ function updateVal(newVal)
end
function addOrSubtract(_, _, isRightClick)
val = math.min(math.max(val + (isRightClick and -1 or 1), MIN_VALUE), MAX_VALUE)
modifyValue(isRightClick and -1 or 1)
end
function modifyValue(mod)
val = math.min(math.max(val + tonumber(mod), MIN_VALUE), MAX_VALUE)
self.editButton({ index = 0, label = tostring(val) })
end

View File

@ -113,7 +113,7 @@ function addUseToCard(card, useType)
end
local match = false
for _, useInfo in ipairs(metadata.uses) do
for _, useInfo in ipairs(metadata.uses or {}) do
if useInfo.token == useType then
-- artificially create replenish data to re-use that existing functionality
useInfo.count = 999