|
7 | 7 |
|
8 | 8 | local Lua = require('Module:Lua') |
9 | 9 |
|
10 | | -local Arguments = Lua.import('Module:Arguments') |
11 | | -local Array = Lua.import('Module:Array') |
12 | | -local Json = Lua.import('Module:Json') |
13 | | -local Links = Lua.import('Module:Links') |
14 | | -local Logic = Lua.import('Module:Logic') |
15 | | -local Lpdb = Lua.import('Module:Lpdb') |
16 | | -local Namespace = Lua.import('Module:Namespace') |
17 | | -local Opponent = Lua.import('Module:Opponent') |
18 | | -local PlayerExt = Lua.import('Module:Player/Ext/Custom') |
19 | | -local Table = Lua.import('Module:Table') |
| 10 | +local MapTableController = Lua.import('Module:MapTable/Controller') |
20 | 11 |
|
21 | | -local Link = Lua.import('Module:Widget/Basic/Link') |
22 | | -local Html = Lua.import('Module:Widget/Html') |
23 | | -local InlinePlayerWidget = Lua.import('Module:Widget/PlayerDisplay/Inline') |
24 | | -local TableWidgets = Lua.import('Module:Widget/Table2/All') |
25 | | - |
26 | | --- Default variant used when building the links of a map |
27 | | -local LINK_VARIANT = 'map' |
28 | | - |
29 | | ----@class TrackmaniaMapTableRowArgs: table<string, string?> |
30 | | ----@field map string? |
31 | | ----@field mapper string? |
32 | | ----@field mapperFlag string? |
33 | | - |
34 | | ----@class TrackmaniaMapTableMapper: standardPlayer |
35 | | ----@field index integer |
36 | | - |
37 | | -local MapTable = {} |
38 | | - |
39 | | ----Reads the mapper entries of a single row and resolves their links and flags |
40 | | ----@param row TrackmaniaMapTableRowArgs |
41 | | ----@return TrackmaniaMapTableMapper[] |
42 | | -local function readMappers(row) |
43 | | - ---@type TrackmaniaMapTableMapper[] |
44 | | - local mappers = {} |
45 | | - |
46 | | - for key, mapper, index in Table.iter.pairsByPrefix(row, {'mapper', 'author', 'a'}, {requireIndex = false}) do |
47 | | - ---@type TrackmaniaMapTableMapper |
48 | | - local player = Table.merge( |
49 | | - Opponent.readSinglePlayerArgs{ |
50 | | - name = mapper, |
51 | | - link = row[key .. 'link'], |
52 | | - flag = row[key .. 'flag'], |
53 | | - }, |
54 | | - {index = index} |
55 | | - ) |
56 | | - |
57 | | - PlayerExt.populatePageName(player) |
58 | | - player.flag = player.flag or PlayerExt.fetchPlayerFlag(player.pageName) |
59 | | - |
60 | | - table.insert(mappers, player) |
61 | | - end |
62 | | - |
63 | | - return mappers |
64 | | -end |
65 | | - |
66 | | ----Builds the full links of a single map |
67 | | ----@private |
68 | | ----@param mapInput table |
69 | | ----@return {[string]: string} |
70 | | -local function makeFullLinks(mapInput) |
71 | | - return Table.filterByKey( |
72 | | - Links.makeFullLinksForTableItems(Links.transform(mapInput), LINK_VARIANT), |
73 | | - function(_, link) return link ~= '' end |
74 | | - ) |
75 | | -end |
76 | | - |
77 | | ----Builds the links cell content for a single map |
78 | | ----@private |
79 | | ----@param links {[string]: string} |
80 | | ----@return Renderable[] |
81 | | -local function makeLinksDisplay(links) |
82 | | - return Array.interleave( |
83 | | - Array.extractValues(Table.map(links, function(key, link) |
84 | | - return key, Link{ |
85 | | - link = link, |
86 | | - children = Links.makeIcon(Links.removeAppendedNumber(key), 25), |
87 | | - linktype = 'external', |
88 | | - } |
89 | | - end), Table.iter.spairs), |
90 | | - ' ' |
91 | | - ) |
92 | | -end |
93 | | - |
94 | | ----Builds the mapper cell content for a single map |
95 | | ----@private |
96 | | ----@param mappers TrackmaniaMapTableMapper[] |
97 | | ----@return Renderable[] |
98 | | -local function makeMapperDisplay(mappers) |
99 | | - return Array.interleave( |
100 | | - Array.map(mappers, function(mapper) return InlinePlayerWidget{player = mapper} end), |
101 | | - Html.Br{} |
102 | | - ) |
103 | | -end |
104 | | - |
105 | | ----Creates an LPDB map record per map, as `map` datapoints |
106 | | ----@param rows TrackmaniaMapTableRowArgs[] |
107 | | ----@param mappers TrackmaniaMapTableMapper[][] |
108 | | ----@param links {[string]: string}[] |
109 | | -local function createLpdbEntry(rows, mappers, links) |
110 | | - if not Namespace.isMain() or Lpdb.isStorageDisabled() then |
111 | | - return |
112 | | - end |
113 | | - |
114 | | - Array.forEach(rows, function(row, rowIndex) |
115 | | - local mapName = row.map |
116 | | - if Logic.isEmpty(mapName) then |
117 | | - return |
118 | | - end |
119 | | - |
120 | | - local extradata = {} |
121 | | - for _, mapper in ipairs(mappers[rowIndex]) do |
122 | | - extradata['author' .. mapper.index] = |
123 | | - mapper.pageName and mapper.pageName:gsub(' ', '_') or '' |
124 | | - extradata['author' .. mapper.index .. 'dn'] = mapper.displayName |
125 | | - extradata['author' .. mapper.index .. 'flag'] = mapper.flag or '' |
126 | | - end |
127 | | - |
128 | | - local rowLinks = links[rowIndex] |
129 | | - if Table.isNotEmpty(rowLinks) then |
130 | | - extradata.links = rowLinks |
131 | | - end |
132 | | - |
133 | | - mw.ext.LiquipediaDB.lpdb_datapoint('map_' .. mapName, Json.stringifySubTables({ |
134 | | - name = mapName, |
135 | | - type = 'map', |
136 | | - extradata = extradata, |
137 | | - })) |
138 | | - end) |
139 | | -end |
140 | | - |
141 | | ----@param rows TrackmaniaMapTableRowArgs[] |
142 | | ----@param mappers TrackmaniaMapTableMapper[][] |
143 | | ----@param links {[string]: string}[] |
144 | | ----@return Renderable |
145 | | -local function makeBody(rows, mappers, links) |
146 | | - return TableWidgets.TableBody{ |
147 | | - children = Array.map(rows, function(row, index) |
148 | | - return TableWidgets.Row{ |
149 | | - children = { |
150 | | - TableWidgets.Cell{children = makeMapperDisplay(mappers[index])}, |
151 | | - TableWidgets.Cell{children = row.map}, |
152 | | - TableWidgets.Cell{ |
153 | | - classes = {'plainlinks'}, |
154 | | - children = makeLinksDisplay(links[index]), |
155 | | - }, |
156 | | - }, |
157 | | - } |
158 | | - end), |
159 | | - } |
160 | | -end |
161 | | - |
162 | | ----@return Renderable |
163 | | -local function makeHeader() |
164 | | - return TableWidgets.TableHeader{ |
165 | | - children = { |
166 | | - TableWidgets.Row{ |
167 | | - children = { |
168 | | - TableWidgets.CellHeader{children = 'Author'}, |
169 | | - TableWidgets.CellHeader{children = 'Map'}, |
170 | | - TableWidgets.CellHeader{children = 'Links'}, |
171 | | - }, |
172 | | - }, |
173 | | - }, |
174 | | - } |
175 | | -end |
176 | | - |
177 | | --- Module entry point |
178 | | ----@param frame Frame |
179 | | ----@return Renderable |
180 | | -function MapTable.run(frame) |
181 | | - local args = Arguments.getArgs(frame) |
182 | | - |
183 | | - ---@type TrackmaniaMapTableRowArgs[] |
184 | | - local rows = Array.map( |
185 | | - Array.mapIndexes(function(index) return args[index] end), |
186 | | - Json.parseIfString |
187 | | - ) |
188 | | - |
189 | | - local mappers = Array.map(rows, readMappers) |
190 | | - local links = Array.map(rows, makeFullLinks) |
191 | | - |
192 | | - local renderedTable = TableWidgets.Table{ |
193 | | - children = { |
194 | | - makeHeader(), |
195 | | - makeBody(rows, mappers, links), |
196 | | - }, |
197 | | - } |
198 | | - |
199 | | - createLpdbEntry(rows, mappers, links) |
200 | | - |
201 | | - return renderedTable |
202 | | -end |
203 | | - |
204 | | -return MapTable |
| 12 | +--- see Module:MapTable/Controller for the entry point |
| 13 | +return MapTableController |
0 commit comments