diff --git a/lua/spec/prize_pool_spec.lua b/lua/spec/prize_pool_spec.lua index a6b871fa86b..cc404da9f9b 100644 --- a/lua/spec/prize_pool_spec.lua +++ b/lua/spec/prize_pool_spec.lua @@ -1,4 +1,5 @@ --- Triple Comment to Enable our LLS Plugin +local TeamTemplateMock = require('wikis.commons.Mock.TeamTemplate') describe('prize pool', function() local PrizePool = require('Module:PrizePool') local InfoboxLeague = require('Module:Infobox/League/Custom') @@ -13,6 +14,7 @@ describe('prize pool', function() stub(mw.ext.LiquipediaDB, "lpdb_tournament") LpdbPlacementStub = stub(mw.ext.LiquipediaDB, "lpdb_placement") InfoboxLeague.run(tournamentData) + TeamTemplateMock.setUp() end) after_each(function () @@ -20,9 +22,10 @@ describe('prize pool', function() ---@diagnostic disable-next-line: undefined-field mw.ext.LiquipediaDB.lpdb:revert() mw.ext.LiquipediaDB.lpdb_tournament:revert() + TeamTemplateMock.tearDown() end) - local prizePoolArgs = { + local prizePool1Args = { type = {type = 'solo'}, currencyroundprecision = 3, lpdb_prefix = 'abc', @@ -39,8 +42,19 @@ describe('prize pool', function() [2] = {qualified1 = true, [1] = {'Salt'}}, } + local prizePool2Args = { + type = {type = 'team'}, + currencyroundprecision = 2, + lpdb_prefix = 'abc', + import = false, + playershare = true, + [1] = {usdprize = '400000', playershare = '200000', [1] = {'mouz'}}, + [2] = {usdprize = '1,000', [1] = {'t1'}}, + } + it('parameters are correctly parsed', function() - local ppt = PrizePool(prizePoolArgs):create() + local ppt = PrizePool(prizePool1Args):create() + local ppt2 = PrizePool(prizePool2Args):create() assert.are_same( { @@ -73,6 +87,14 @@ describe('prize pool', function() }, ppt.prizes ) + assert.are_same( + { + {id = 'BASE_CURRENCY1', type = 'BASE_CURRENCY', index = 1, data = {roundPrecision = 2}}, + {id = 'PLAYER_SHARE1', type = 'PLAYER_SHARE', index = 1, data = {title = 'Player Share'}}, + {id = 'CLUB_SHARE1', type = 'CLUB_SHARE', index = 1, data = {title = 'Club Share', roundPrecision = 2}}, + }, + ppt2.prizes + ) assert.are_same( { @@ -96,11 +118,12 @@ describe('prize pool', function() describe('prize pool is correct', function() it('display', function() - GoldenTest('prize_pool', tostring(PrizePool(prizePoolArgs):create():build())) + GoldenTest('prize_pool', tostring(PrizePool(prizePool1Args):create():build())) + GoldenTest('prize_pool_player_share', tostring(PrizePool(prizePool2Args):create():build())) end) it('lpdb storage', function() - PrizePool(prizePoolArgs):create():build() + PrizePool(prizePool1Args):create():build() assert.stub(LpdbPlacementStub).was.called_with('ranking_abc1_Rathoz', { date = '2022-10-15', extradata = '{"prizepoints":"","prizepoints2":""}', @@ -157,42 +180,106 @@ describe('prize pool', function() type = 'Offline', qualified = 1, }) + + PrizePool(prizePool2Args):create():build() + assert.stub(LpdbPlacementStub).was.called_with('ranking_abc_mouz', { + date = '2022-10-15', + extradata = '{"playershare":200000,"prizepoints":"","prizepoints2":""}', + game = 'commons', + icon = 'test.png', + icondark = 'test dark.png', + image = 'MOUZ allmode.png', + imagedark = 'MOUZ allmode.png', + individualprizemoney = 0, + lastvsdata = '[]', + liquipediatier = '1', + liquipediatiertype = 'Qualifier', + opponentname = 'MOUZ', + opponentplayers = '[]', + opponenttype = 'team', + opponenttemplate = 'mouz 2021', + parent = 'FakePage', + participant = 'MOUZ', -- Legacy + participantlink = 'MOUZ', -- Legacy + participanttemplate = 'mouz 2021', + placement = 1, + players = '[]', -- Legacy + prizemoney = 400000, + prizepoolindex = 2, + series = 'Test Series', + shortname = 'Test Tourney', + startdate = '2022-10-13', + tournament = 'Test Tournament', + type = 'Offline', + qualified = 0, + }) + assert.stub(LpdbPlacementStub).was.called_with('ranking_abc_t1', { + date = '2022-10-15', + extradata = '{"prizepoints":"","prizepoints2":""}', + game = 'commons', + icon = 'test.png', + icondark = 'test dark.png', + image = 'T1 2019 allmode.png', + imagedark = 'T1 2019 allmode.png', + individualprizemoney = 0, + lastvsdata = '[]', + liquipediatier = '1', + liquipediatiertype = 'Qualifier', + opponentname = 'T1', + opponentplayers = '[]', + opponenttype = 'team', + opponenttemplate = 't1 2019', + parent = 'FakePage', + participant = 'T1', -- Legacy + participantlink = 'T1', -- Legacy + participanttemplate = 't1 2019', + placement = 2, + players = '[]', -- Legacy + prizemoney = 1000, + prizepoolindex = 2, + series = 'Test Series', + shortname = 'Test Tourney', + startdate = '2022-10-13', + tournament = 'Test Tournament', + type = 'Offline', + qualified = 0, + }) end) end) describe('enabling/disabling lpdb storage', function() it('normal behavior', function() - PrizePool(prizePoolArgs):create():build() + PrizePool(prizePool1Args):create():build() assert.stub(LpdbPlacementStub).called(2) end) it('disabled', function() - PrizePool(Table.merge(prizePoolArgs, {storelpdb = false})):create():build() + PrizePool(Table.merge(prizePool1Args, {storelpdb = false})):create():build() assert.stub(LpdbPlacementStub).called(0) end) it('wiki-var enabled', function() Variables.varDefine('disable_LPDB_storage', 'false') - PrizePool(prizePoolArgs):create():build() + PrizePool(prizePool1Args):create():build() assert.stub(LpdbPlacementStub).called(2) end) it('wiki-var enabled with override', function() Variables.varDefine('disable_LPDB_storage', 'false') - PrizePool(Table.merge(prizePoolArgs, {storelpdb = false})):create():build() + PrizePool(Table.merge(prizePool1Args, {storelpdb = false})):create():build() assert.stub(LpdbPlacementStub).called(0) end) it('wiki-var disable with override', function() Variables.varDefine('disable_LPDB_storage', 'true') - PrizePool(Table.merge(prizePoolArgs, {storelpdb = true})):create():build() + PrizePool(Table.merge(prizePool1Args, {storelpdb = true})):create():build() assert.stub(LpdbPlacementStub).called(2) end) it('wiki-var disable without override', function() Variables.varDefine('disable_LPDB_storage', 'true') - PrizePool(prizePoolArgs):create():build() + PrizePool(prizePool1Args):create():build() assert.stub(LpdbPlacementStub).called(0) end) end) diff --git a/lua/spec/snapshots/prize_pool_player_share.png b/lua/spec/snapshots/prize_pool_player_share.png new file mode 100644 index 00000000000..d981e2062c6 Binary files /dev/null and b/lua/spec/snapshots/prize_pool_player_share.png differ diff --git a/lua/wikis/commons/PrizePool/Base.lua b/lua/wikis/commons/PrizePool/Base.lua index 680786f4314..dfbd65563c6 100644 --- a/lua/wikis/commons/PrizePool/Base.lua +++ b/lua/wikis/commons/PrizePool/Base.lua @@ -63,6 +63,8 @@ local PRIZE_TYPE_QUALIFIES = 'QUALIFIES' local PRIZE_TYPE_POINTS = 'POINTS' local PRIZE_TYPE_PERCENTAGE = 'PERCENT' local PRIZE_TYPE_FREETEXT = 'FREETEXT' +local PRIZE_TYPE_PLAYER_SHARE = 'PLAYER_SHARE' +local PRIZE_TYPE_CLUB_SHARE = 'CLUB_SHARE' BasePrizePool.config = { showBaseCurrency = { @@ -357,9 +359,55 @@ BasePrizePool.prizeTypes = { end end, }, - [PRIZE_TYPE_FREETEXT] = { + [PRIZE_TYPE_PLAYER_SHARE] = { sortOrder = 60, + header = 'playershare', + headerParse = function (prizePool, input, context, index) + return {title = 'Player Share'} + end, + headerDisplay = function (data) + return TableCell{children = {data.title}} + end, + + row = 'playershare', + rowParse = function (placement, input, context, index) + return BasePrizePool._parseInteger(input) + end, + rowDisplay = function (headerData, data) + if data > 0 then + return TableCell{children = { + Currency.display(BASE_CURRENCY, data, + {formatValue = true, formatPrecision = headerData.roundPrecision, displayCurrencyCode = false}) + }} + end + end, + }, + [PRIZE_TYPE_CLUB_SHARE] = { + sortOrder = 70, + header = 'clubshare', + headerParse = function(prizePool, input, context, index) + return {title = String.isNotEmpty(input) and input or 'Club Share'} + end, + headerDisplay = function(data) + return TableCell{children = {data.title}} + end, + row = 'clubshare', + rowParse = function(placement, input, context, index) + return input + end, + rowDisplay = function(headerData, data) + if data and tonumber(data) > 0 then + return TableCell{children = { + Currency.display(BASE_CURRENCY, data, + {formatValue = true, formatPrecision = headerData.roundPrecision, displayCurrencyCode = false}) + }} + end + end, + }, + [PRIZE_TYPE_FREETEXT] = { + sortOrder = 80, + header = 'freetext', headerParse = function (prizePool, input, context, index) return {title = input} @@ -423,6 +471,22 @@ function BasePrizePool:create() self:setConfig('showBaseCurrency', true) self:addPrize(PRIZE_TYPE_BASE_CURRENCY, 1, {roundPrecision = self.options.currencyRoundPrecision}) + local hasPlayerShare = Array.any(self.prizes, function(prize) + return prize.type == PRIZE_TYPE_PLAYER_SHARE + end) + if hasPlayerShare then + local alreadyHasClubShare = Array.any(self.prizes, function(prize) + return prize.type == PRIZE_TYPE_CLUB_SHARE + end) + if not alreadyHasClubShare then + local clubShareTitle = self.args.clubshare + self:addPrize(PRIZE_TYPE_CLUB_SHARE, 1, { + title = String.isNotEmpty(clubShareTitle) and clubShareTitle or 'Club Share', + roundPrecision = self.options.currencyRoundPrecision + }) + end + end + if self.options.autoExchange then local canConvertCurrency = function(prize) return prize.type == PRIZE_TYPE_LOCAL_CURRENCY @@ -642,6 +706,26 @@ function BasePrizePool:_buildRows() self:applyToggleExpand(previousPlacement, placement, rows) + -- Calculate club share for the placement + Array.forEach(placement.opponents, function(opponent) + local basePrize + local playerShare + Array.forEach(self.prizes, function(prize) + if prize.type == PRIZE_TYPE_BASE_CURRENCY then + basePrize = opponent.prizeRewards[prize.id] or placement.prizeRewards[prize.id] + return + end + if prize.type == PRIZE_TYPE_PLAYER_SHARE then + playerShare = opponent.prizeRewards[prize.id] or placement.prizeRewards[prize.id] + end + end) + if basePrize and playerShare then + local clubShare = tonumber(basePrize) - tonumber(playerShare) + if not opponent.prizeRewards then opponent.prizeRewards = {} end + opponent.prizeRewards[PRIZE_TYPE_CLUB_SHARE .. '1'] = clubShare + end + end) + local cells = {} table.insert(cells, self:placeOrAwardCell(placement)) diff --git a/lua/wikis/commons/PrizePool/Placement.lua b/lua/wikis/commons/PrizePool/Placement.lua index 3e11a0935b8..662036ffc9f 100644 --- a/lua/wikis/commons/PrizePool/Placement.lua +++ b/lua/wikis/commons/PrizePool/Placement.lua @@ -26,6 +26,7 @@ local DASH = '-' local PRIZE_TYPE_BASE_CURRENCY = 'BASE_CURRENCY' local PRIZE_TYPE_POINTS = 'POINTS' local PRIZE_TYPE_QUALIFIES = 'QUALIFIES' +local PRIZE_TYPE_PLAYER_SHARE = 'PLAYER_SHARE' -- Allowed none-numeric score values. local SPECIAL_SCORES = {'W', 'FF' , 'L', 'DQ', 'D'} @@ -235,6 +236,7 @@ function Placement:_getLpdbData(...) local pointsReward = self:getPrizeRewardForOpponent(opponent, PRIZE_TYPE_POINTS .. 1) local pointsReward2 = self:getPrizeRewardForOpponent(opponent, PRIZE_TYPE_POINTS .. 2) local isQualified = self:getPrizeRewardForOpponent(opponent, PRIZE_TYPE_QUALIFIES .. '1') + local playerShare = tonumber(self:getPrizeRewardForOpponent(opponent, PRIZE_TYPE_PLAYER_SHARE .. 1)) local lpdbData = { image = image, @@ -262,6 +264,7 @@ function Placement:_getLpdbData(...) participantteam = (opponentType == Opponent.solo and players.p1team) and Opponent.toName{template = players.p1team, type = 'team', extradata = {}} or nil, + playershare = playerShare, }, qualified = isQualified and 1 or 0 -- TODO: We need to create additional LPDB Fields diff --git a/lua/wikis/commons/TeamParticipants/Repository.lua b/lua/wikis/commons/TeamParticipants/Repository.lua index 26a9629f4c6..1469e589300 100644 --- a/lua/wikis/commons/TeamParticipants/Repository.lua +++ b/lua/wikis/commons/TeamParticipants/Repository.lua @@ -93,12 +93,14 @@ function TeamParticipantsRepository.save(participant) lpdbData.players = lpdbData.opponentplayers -- Calculate individual prize money (prize money per player on team) - if lpdbData.prizemoney then + -- Opt to use playerShare over prizepool if available + local prizevalue = lpdbData.extradata and lpdbData.extradata.playershare or lpdbData.prizemoney + if prizevalue then local filteredPlayers = Array.filter(activeOpponent.players, function(player) return player.extradata.type ~= 'staff' end) local numberOfPlayersOnTeam = math.max(#(filteredPlayers), 1) - lpdbData.individualprizemoney = lpdbData.prizemoney / numberOfPlayersOnTeam + lpdbData.individualprizemoney = prizevalue / numberOfPlayersOnTeam end mw.ext.LiquipediaDB.lpdb_placement(lpdbData.objectName, Json.stringifySubTables(lpdbData))