Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 134 additions & 20 deletions lua/wikis/commons/Widget/StageWinnings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ local BASE_CURRENCY = 'USD'
---@field cutafter integer?
---@field title string?
---@field precision integer?
---@field legendTitle string?
---@field showLegend boolean?
---@field autoexchange boolean
---@field showMatchWL boolean
---@field showGameWL boolean
Expand All @@ -55,7 +57,7 @@ StageWinnings.defaultProps = {
delimiter = ',',
autoexchange = true,
prizeMode = 'matchWins',
title = 'Group Stage Winnings'
title = 'Group Stage Winnings',
}

---@return Widget?
Expand Down Expand Up @@ -104,26 +106,30 @@ function StageWinnings:render()
}
end

return TableWidgets.Table{
caption = props.title,
tableClasses = {'prizepooltable', 'collapsed'},
tableAttributes = {
['data-cutafter'] = (tonumber(props.cutafter) or 5),
['data-opentext'] = 'Show remaining participants',
['data-closetext'] = 'Hide remaining participants',
},
columns = WidgetUtil.collect(
{align = 'left'},
(Logic.readBool(props.showMatchWL) or props.prizeMode == 'matchWins') and {align = 'center'} or nil,
(Logic.readBool(props.showGameWL) or props.prizeMode == 'gameWins') and {align = 'center'} or nil,
Logic.readBool(props.showScore) and {align = 'left'} or nil,
Logic.isNotEmpty(props.localcurrency) and {align = 'right'} or nil,
(Logic.readBool(props.autoexchange) or Logic.isEmpty(props.localcurrency)) and {align = 'right'} or nil
),
children = {
self:_headerRow(),
TableWidgets.TableBody{children = Array.map(opponentList, FnUtil.curry(self._row, self))}
return HtmlWidgets.Div{children = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why wrap it in a div?
imo Fragment is better

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returning directly as an array would be even better

TableWidgets.Table{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo push the table building to a sep function if you add the showLegend option

caption = props.title,
tableClasses = {'prizepooltable', 'collapsed'},
tableAttributes = {
['data-cutafter'] = (tonumber(props.cutafter) or 5),
['data-opentext'] = 'Show remaining participants',
['data-closetext'] = 'Hide remaining participants',
},
columns = WidgetUtil.collect(
{align = 'left'},
(Logic.readBool(props.showMatchWL) or props.prizeMode == 'matchWins') and {align = 'center'} or nil,
(Logic.readBool(props.showGameWL) or props.prizeMode == 'gameWins') and {align = 'center'} or nil,
Logic.readBool(props.showScore) and {align = 'left'} or nil,
Logic.isNotEmpty(props.localcurrency) and {align = 'right'} or nil,
(Logic.readBool(props.autoexchange) or Logic.isEmpty(props.localcurrency)) and {align = 'right'} or nil
),
children = {
self:_headerRow(),
TableWidgets.TableBody{children = Array.map(opponentList, FnUtil.curry(self._row, self))}
},
},
Logic.readBool(props.showLegend) and Logic.isNotEmpty(valueByScore) and self:_getLegendTable(valueByScore) or nil
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indnet is bad

}
end

Expand Down Expand Up @@ -225,4 +231,112 @@ function StageWinnings._detailedScores(scoresTable)
)
end

---@param valueByScore table<string, integer>?
---@return Widget
function StageWinnings:_getLegendTable(valueByScore)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe outsource to extra widget?!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meaning move the entire module to a separate file right.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically move this function and the functions it calls to a new (widget) module

local props = self.props

local currencyDisplayConfig = {
displaySymbol = true,
formatValue = true,
displayCurrencyCode = false,
formatPrecision = tonumber(props.precision) or 0,
}
local showLocalCurrency = Logic.isNotEmpty(props.localcurrency)
local showBaseCurrency = Logic.readBool(props.autoexchange) or Logic.isEmpty(props.localcurrency)
local currencyRate = self.currencyRate or 1

---@param currency string
---@param amount string?
local function currencyCell(currency, amount)
return TableWidgets.Cell{children = Currency.display(currency, tonumber(amount) or 0, currencyDisplayConfig)}
end

---@param amount string?
local function localCurrencyCell(amount)
return showLocalCurrency and currencyCell(props.localcurrency, amount) or nil
end

---@param amount string?
local function baseCurrencyCell(amount)
return showBaseCurrency and currencyCell(BASE_CURRENCY, amount * currencyRate) or nil
end

local sortedScoreEntries = Array.extractValues(Table.map(valueByScore or {}, function(scoreline, prize)
local wins, losses = scoreline:match('^(%d+)%-(%d+)$')
return scoreline, {
scoreline = scoreline,
prize = prize,
wins = tonumber(wins),
losses = tonumber(losses),
}
end))
table.sort(sortedScoreEntries, function(a, b)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to reduce indents by changing processing order

if b.wins and a.wins ~= nil then
	return true
elseif a.wins and b.wins ~= nil then
	return false
elseif a.wins and b.wins then
	return a.scoreline < b.scoreline
end

local diffA = a.wins - a.losses
local diffB = b.wins - b.losses
if diffA ~= diffB then
	return diffA > diffB
elseif a.wins ~= b.wins then
	return a.wins > b.wins
elseif a.losses ~= b.losses then
	return a.losses < b.losses
end
return a.scoreline < b.scoreline

if a.wins ~= nil and b.wins ~= nil then
local diffA = a.wins - a.losses
local diffB = b.wins - b.losses
if diffA ~= diffB then
return diffA > diffB
end
if a.wins ~= b.wins then
return a.wins > b.wins
end
if a.losses ~= b.losses then
return a.losses < b.losses
end
return a.scoreline < b.scoreline
end
if a.wins ~= nil then
return true
end
if b.wins ~= nil then
return false
end
return a.scoreline < b.scoreline
end)

return TableWidgets.Table{
caption = props.legendTitle or 'Prize Distribution',
columns = WidgetUtil.collect(
{align = 'left'},
showLocalCurrency and {align = 'right'} or nil,
showBaseCurrency and {align = 'right'} or nil
),
children = {
TableWidgets.TableHeader{children = {
TableWidgets.Row{children = WidgetUtil.collect(
TableWidgets.CellHeader{children = 'Score'},
showLocalCurrency
and TableWidgets.CellHeader{children = Currency.display(props.localcurrency)} or nil,
showBaseCurrency
and TableWidgets.CellHeader{children = Currency.display(BASE_CURRENCY)} or nil
)}
}},
TableWidgets.TableBody{children = WidgetUtil.collect(
(tonumber(props.valueStart) or 0) > 0 and TableWidgets.Row{
children = WidgetUtil.collect(
TableWidgets.Cell{children = 'Starting Prize'},
localCurrencyCell(props.valueStart),
baseCurrencyCell(props.valueStart)
)
} or nil,
(props.prizeMode == 'matchWins' or props.prizeMode == 'gameWins') and TableWidgets.Row{
children = WidgetUtil.collect(
TableWidgets.Cell{children = props.prizeMode == 'matchWins' and 'Match Win' or 'Game Win'},
localCurrencyCell(props.valuePerWin),
baseCurrencyCell(props.valuePerWin)
)
} or nil,
props.prizeMode == 'scores' and WidgetUtil.collect(Array.map(sortedScoreEntries, function(scoreEntry)
return TableWidgets.Row{children = WidgetUtil.collect(
TableWidgets.Cell{children = scoreEntry.scoreline},
localCurrencyCell(scoreEntry.prize),
baseCurrencyCell(scoreEntry.prize)
)}
end)) or nil
)}
}
}
end

return StageWinnings
Loading