-
Notifications
You must be signed in to change notification settings - Fork 103
feat: ControlsSettingsTable module #7264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
SobakaPirat
wants to merge
10
commits into
Liquipedia:main
Choose a base branch
from
SobakaPirat:ControlsSettingsTable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5e4e3ac
feat: add ControlsSettingsTable module
SobakaPirat bb43c74
overhaul
SobakaPirat cfc0485
Table2 version
SobakaPirat 72a0785
squash widget
SobakaPirat 4b5ed35
missing eof
SobakaPirat e4bc440
move ButtonTranslation to Icon
SobakaPirat 5bfb877
split widget
SobakaPirat eaee1d4
linter
SobakaPirat c6ff315
private functions
SobakaPirat 977d119
define Info.controlsSettingsTable
SobakaPirat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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?} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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