From f755fe3e94705769fd99736baffc3660e419aef3 Mon Sep 17 00:00:00 2001 From: hjpalpha <75081997+hjpalpha@users.noreply.github.com> Date: Thu, 5 Oct 2023 10:06:41 +0200 Subject: [PATCH] Add percentage support in prize pools (#3343) * Update prize_pool_placement_base.lua * Update prize_pool_base.lua * trailing whitespace --- .../prize_pool/commons/prize_pool_base.lua | 32 ++++++++++++++++++- .../commons/prize_pool_placement_base.lua | 25 +++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/components/prize_pool/commons/prize_pool_base.lua b/components/prize_pool/commons/prize_pool_base.lua index f894d0d29f7..7aa58adea0f 100644 --- a/components/prize_pool/commons/prize_pool_base.lua +++ b/components/prize_pool/commons/prize_pool_base.lua @@ -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 = { @@ -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} @@ -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 diff --git a/components/prize_pool/commons/prize_pool_placement_base.lua b/components/prize_pool/commons/prize_pool_placement_base.lua index 5360cd35ad7..7fcbd9ba61c 100644 --- a/components/prize_pool/commons/prize_pool_placement_base.lua +++ b/components/prize_pool/commons/prize_pool_placement_base.lua @@ -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. @@ -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