Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
52 changes: 52 additions & 0 deletions lua/wikis/commons/ControlsSettingsTable.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
-- @Liquipedia
-- page=Module:ControlsSettingsTable
--
-- Please see https://github.com/Liquipedia/Lua-Modules to contribute
--
local Lua = require('Module:Lua')

local Arguments = Lua.import('Module:Arguments')
local Class = Lua.import('Module:Class')

local ControlsSettingsTableWidget = Lua.import('Module:Widget/ControlsSettingsTable')

local ControlsSettingsTable = Class.new()

---@param lpdbConfig string[]
---@param columnConfig ColumnConfig[]
---@param frame table
---@return Widget?
function ControlsSettingsTable.create(lpdbConfig, columnConfig, frame)
local args = Arguments.getArgs(frame)
local widget = ControlsSettingsTableWidget(columnConfig, args)
ControlsSettingsTable.saveToLpdb(lpdbConfig, args)
return widget:tryMake()
end

---@param lpdbConfig string[]
---@param args {[string]: string?}
function ControlsSettingsTable.saveToLpdb(lpdbConfig, args)
local title = mw.title.getCurrentTitle().text
local extradata = ControlsSettingsTable.generateLpdbExtradata(lpdbConfig, args)
mw.ext.LiquipediaDB.lpdb_settings(title, {
name = 'movement',
reference = args.ref,
lastupdated = args.date,
gamesettings = mw.ext.LiquipediaDB.lpdb_create_json(extradata),
type = (args.controller or ''):lower(),
})
end

---@param lpdbConfig string[]
---@param args {[string]: string?}
---@return {[string]: string?}
function ControlsSettingsTable.generateLpdbExtradata(lpdbConfig, args)
local result = {}
for _, key in ipairs(lpdbConfig) do
result[key:lower()] = args[key:lower()]
end
return result
end

return ControlsSettingsTable
49 changes: 49 additions & 0 deletions lua/wikis/commons/ControlsSettingsTable/Custom.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
-- @Liquipedia
-- page=Module:ControlsSettingsTable/Custom
--
-- Please see https://github.com/Liquipedia/Lua-Modules to contribute
--

local Lua = require('Module:Lua')

local Arguments = Lua.import('Module:Arguments')
local Class = Lua.import('Module:Class')

local ControlsSettingsTable = Lua.import('Module:ControlsSettingsTable')

local CustomControlsSettingsTable = Class.new(ControlsSettingsTable)

---@type string[]
local LPDB_CONFIG = {}
--local LPDB_CONFIG = {'Accelerate', 'Brake', 'Steering'}

---@type ColumnConfig[]
local BASE_COLUMN_CONFIG = {}
--[[
local BASE_COLUMN_CONFIG = {
{key = 'Steering', title = 'Steering'},
{keys = {{key = 'Accelerate'}, ' / ', {key = 'Brake'}}, title = 'Accelerate/Brake'}
}
--]]

---@param args {[string]: string?}
---@return ColumnConfig[]
local function makeColumnConfig(args)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

doesn't depend on args
this is just a copy ...
seems like a useless function

local COLUMN_CONFIG = {}
for _, col in ipairs(BASE_COLUMN_CONFIG) do
table.insert(COLUMN_CONFIG, col)
end

return COLUMN_CONFIG
end

---@param frame table
---@return Widget?
function CustomControlsSettingsTable.create(frame)
local args = Arguments.getArgs(frame)
local COLUMN_CONFIG = makeColumnConfig(args)
return ControlsSettingsTable.create(LPDB_CONFIG, COLUMN_CONFIG, frame)
end

return CustomControlsSettingsTable
174 changes: 174 additions & 0 deletions lua/wikis/commons/Widget/ControlsSettingsTable.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
---
-- @Liquipedia
-- page=Module:Widget/ControlsSettingsTable
--
-- Please see https://github.com/Liquipedia/Lua-Modules to contribute
--

local Lua = require('Module:Lua')

local Array = Lua.import('Module:Array')
local Class = Lua.import('Module:Class')
local Page = Lua.import('Module:Page')
local String = Lua.import('Module:StringUtils')
local Template = Lua.import('Module:Template')

local Widget = Lua.import('Module:Widget')
local HtmlWidgets = Lua.import('Module:Widget/Html/All')
local WidgetUtil = Lua.import('Module:Widget/Util')

local SETTINGS_LINK = 'Control settings'

---@alias ColumnConfig
---| {key: string, title: string}
---| {keys: ({key: string} | string)[], title: string}
---@alias ColumnValue {title: string, value: fun(data: {[string]: string?}): string?}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

these aliases are bad (way too generic)


---@class ControlsSettingsTableWidget: Widget
---@field args {[string]: string?}
---@field columnConfig ColumnConfig[]
---@field frame table
local ControlsSettingsTableWidget = Class.new(Widget,
function(self, columnConfig, args, frame)
self.columnConfig = columnConfig
self.args = args
self.frame = frame or mw.getCurrentFrame()
end
)

---@return Widget?
function ControlsSettingsTableWidget:render()
local header = self:renderHeader()
local footer = self:renderFooter()
local visibleColumns = self:getVisibleColumns()

return HtmlWidgets.Div{
classes = {'table-responsive'},
children = {self:renderTable(header, footer, visibleColumns)}
}
end

---@return string
function ControlsSettingsTableWidget:renderHeader()
local args = self.args
local frame = self.frame
local header = Page.exists(SETTINGS_LINK) and '[['.. SETTINGS_LINK ..']] ' or SETTINGS_LINK..' '
if args.ref == 'insidesource' then
header = header .. frame:callParserFunction{
name = '#tag',
args = { 'ref', Template.safeExpand(frame, 'inside source') }
}
elseif args.ref then
header = header .. frame:callParserFunction{
name = '#tag',
args = { 'ref', args['ref'] }
}
end
return header .. " <small>([[List of player control settings|list of]])</small>'''"
end

---@return string
function ControlsSettingsTableWidget:renderFooter()
local args = self.args
if args.date then
local year, month, day = (args.date):match('(%d+)-(%d+)-(%d+)')
local dayAgo = math.floor((os.time() - os.time{year=year, month=month, day=day}) / 86400)
return '<i>Last updated on '.. args.date ..' (' .. dayAgo ..' days ago).</i>'
end
return '<span class="cinnabar-text"><i>No date of last update specified!</i></span>'
end

---@return ColumnValue[]
function ControlsSettingsTableWidget:getVisibleColumns()
return Array.flatMap(self.columnConfig, function(config)
local column = self:makeColumn(config)
return String.isNotEmpty(column.value(self.args)) and { column } or {}
end)
end

---@param config ColumnConfig
---@return ColumnValue
function ControlsSettingsTableWidget:makeColumn(config)
return {
title = config.title,
value = function(data)
if config.keys then
local values = {}
local hasValue = false
for _, item in ipairs(config.keys) do
if type(item) == 'table' then
local formatted = self:formatKeyValue(item.key, data)
table.insert(values, formatted or '-')
if formatted then hasValue = true end
else
table.insert(values, item)
end
end
return hasValue and table.concat(values) or nil
elseif config.key then
return self:formatKeyValue(config.key, data)
end
end
}
end

---@param key string
---@param data {[string]: string?}
---@return string?
function ControlsSettingsTableWidget:formatKeyValue(key, data)
local keyValue = data[key:lower()]
if not keyValue or String.isEmpty(keyValue) then
return nil
end
if data.controller and data.controller:lower() == 'kbm' then
return '<kbd>' .. keyValue .. '</kbd>'
end
return '[[File:' .. self:getImageName(data.controller, keyValue) .. '.svg|' .. key .. '|link=]]'
end

---@param device string?
---@param key string?
---@return string?
function ControlsSettingsTableWidget:getImageName(device, key)
return Template.safeExpand(self.frame, 'Button translation', {(device or ''):lower(), (key or ''):lower()})
end

---@param header string
---@param footer string
---@param visibleColumns ColumnValue[]
---@return Widget
function ControlsSettingsTableWidget:renderTable(header, footer, visibleColumns)
return HtmlWidgets.Table{
classes = {'wikitable', 'controls-responsive-table'},
css = {['table-layout'] = 'auto'},
children = WidgetUtil.collect(
HtmlWidgets.Tr{children = {
HtmlWidgets.Th{
attributes = {colspan = #self.columnConfig},
children = header
}
}},
HtmlWidgets.Tr{children = Array.map(visibleColumns, function(column)
return HtmlWidgets.Th{children = column.title}
end)},
HtmlWidgets.Tr{children = Array.map(visibleColumns, function(column)
return HtmlWidgets.Td{
attributes = {['data-label'] = column.title},
children = column.value(self.args)
}
end)},
HtmlWidgets.Tr{children = {
HtmlWidgets.Th{
attributes = {colspan = #self.columnConfig},
css = {
['font-size'] = '85%',
padding = '2px',
},
children = footer
}
}}
)
}
end

return ControlsSettingsTableWidget
39 changes: 37 additions & 2 deletions stylesheets/commons/ResponsiveTable.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
/*******************************************************************************
Template(s): Rocket League Responsive Table
Author(s): QuadratClown
Template(s): Responsive Table
Author(s): QuadratClown, SobakaPirat
*******************************************************************************/
.controls-responsive-table {
margin: 0;

td {
text-align: center;
}

@media screen and ( max-width: 947px ) {
width: 100%;

tr {
display: block;
}

tr:nth-child( 2 ) {
display: none;
}

th,
td {
display: block;
width: 100%;
font-size: 0.8em;
text-align: right;
border-left: 0;

&::before {
content: attr( data-label );
float: left;
font-weight: bold;
}
}
}
}

.rl-responsive-table {
border-collapse: collapse;
margin: 0;
Expand Down
Loading