Skip to content

Commit

Permalink
Add percentage support in prize pools (#3343)
Browse files Browse the repository at this point in the history
* Update prize_pool_placement_base.lua

* Update prize_pool_base.lua

* trailing whitespace
  • Loading branch information
hjpalpha authored Oct 5, 2023
1 parent cc82fb6 commit f755fe3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
32 changes: 31 additions & 1 deletion components/prize_pool/commons/prize_pool_base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ local PRIZE_TYPE_BASE_CURRENCY = 'BASE_CURRENCY'
local PRIZE_TYPE_LOCAL_CURRENCY = 'LOCAL_CURRENCY'
local PRIZE_TYPE_QUALIFIES = 'QUALIFIES'
local PRIZE_TYPE_POINTS = 'POINTS'
local PRIZE_TYPE_PERCENTAGE = 'PERCENT'
local PRIZE_TYPE_FREETEXT = 'FREETEXT'

BasePrizePool.config = {
Expand Down Expand Up @@ -314,9 +315,31 @@ BasePrizePool.prizeTypes = {
end
end,
},
[PRIZE_TYPE_FREETEXT] = {
[PRIZE_TYPE_PERCENTAGE] = {
sortOrder = 50,

header = 'percentage',
headerParse = function (prizePool, input, context, index)
assert(index == 1, 'Percentage only supports index 1')
return {title = 'Percentage'}
end,
headerDisplay = function (data)
return TableCell{content = {{data.title}}}
end,

row = 'percentage',
rowParse = function (placement, input, context, index)
return BasePrizePool._parseInteger(input)
end,
rowDisplay = function (headerData, data)
if String.isNotEmpty(data) then
return TableCell{content = {{data .. '%'}}}
end
end,
},
[PRIZE_TYPE_FREETEXT] = {
sortOrder = 60,

header = 'freetext',
headerParse = function (prizePool, input, context, index)
return {title = input}
Expand Down Expand Up @@ -381,8 +404,15 @@ function BasePrizePool:create(args)
local canConvertCurrency = function(prize)
return prize.type == PRIZE_TYPE_LOCAL_CURRENCY
end
local hasLocalCurrency1 = Array.any(self.prizes, function(prize)
return canConvertCurrency(prize) and prize.index == 1
end)
local hasPercentage1 = Array.any(self.prizes, function(prize)
return prize.type == PRIZE_TYPE_PERCENTAGE and prize.index == 1
end)

for _, placement in ipairs(self.placements) do
if hasPercentage1 then placement:_calculateFromPercentage(BasePrizePool.prizeTypes, hasLocalCurrency1) end
placement:_setBaseFromRewards(Array.filter(self.prizes, canConvertCurrency), BasePrizePool.prizeTypes)
end
end
Expand Down
25 changes: 25 additions & 0 deletions components/prize_pool/commons/prize_pool_placement_base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ local Class = require('Module:Class')
local Json = require('Module:Json')
local String = require('Module:StringUtils')
local Table = require('Module:Table')
local Variables = require('Module:Variables')

local Opponent = require('Module:OpponentLibraries').Opponent

local BASE_CURRENCY = 'USD'
local LOCAL_CURRENCY_VARIABLE_POST_FIX = 'local'
local PRIZE_TYPE_BASE_CURRENCY = 'BASE_CURRENCY'
local PRIZE_TYPE_LOCAL_CURRENCY = 'LOCAL_CURRENCY'
local PRIZE_TYPE_PERCENTAGE = 'PERCENT'

--- @class BasePlacement
--- A BasePlacement is a set of opponents who all share the same final place/award in the tournament.
Expand Down Expand Up @@ -180,6 +185,26 @@ function BasePlacement:_setBaseFromRewards(prizesToUse, prizeTypes)
end)
end

function BasePlacement:_calculateFromPercentage(prizeTypes, hasLocalCurrency)
local baseMoney = tonumber(Variables.varDefault(hasLocalCurrency and
('tournament_prizepool' .. LOCAL_CURRENCY_VARIABLE_POST_FIX) or
('tournament_prizepool' .. BASE_CURRENCY:lower())
)) or 0

Array.forEach(self.opponents, function(opponent)
if opponent.prizeRewards[PRIZE_TYPE_BASE_CURRENCY .. 1] or self.prizeRewards[PRIZE_TYPE_BASE_CURRENCY .. 1]
or opponent.prizeRewards[PRIZE_TYPE_LOCAL_CURRENCY .. 1] or self.prizeRewards[PRIZE_TYPE_LOCAL_CURRENCY .. 1]
or (opponent.prizeRewards[PRIZE_TYPE_PERCENTAGE .. 1] or self.prizeRewards[PRIZE_TYPE_PERCENTAGE .. 1] or 0) == 0
then
return
end

local percentage = opponent.prizeRewards[PRIZE_TYPE_PERCENTAGE .. 1] or self.prizeRewards[PRIZE_TYPE_PERCENTAGE .. 1]
opponent.prizeRewards[(hasLocalCurrency and PRIZE_TYPE_LOCAL_CURRENCY or PRIZE_TYPE_BASE_CURRENCY) .. 1]
= baseMoney * percentage / 100
end)
end

--- Returns true if the input matches the format of a date
function BasePlacement._isValidDateFormat(date)
if type(date) ~= 'string' or String.isEmpty(date) then
Expand Down

0 comments on commit f755fe3

Please sign in to comment.