forked from pokemoncentral/wiki-lua-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypelist.lua
228 lines (178 loc) · 7.19 KB
/
Typelist.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
--[[
This module prints the list of all Pokémon, alternative forms included, having
a given type. It divides them into mono-typed, first-typed and second-typed.
--]]
local g = {}
local mw = require('mw')
local css = require('Css')
local ms = require('MiniSprite')
local list = require('Wikilib-lists')
local oop = require('Wikilib-oop')
local resp = require('Resp')
local txt = require('Wikilib-strings') -- luacheck: no unused
local tab = require('Wikilib-tables') -- luacheck: no unused
local multigen = require('Wikilib-multigen')
local pokes = require('Poké-data')
--[[
Base class for specialized list entry (mono-, first- and second-typed): it is
not utilizable directly because it does not filter the type.
--]]
g.Entry = oop.makeClass(list.PokeSortableEntry)
-- Returns the heading line for tables, given type, heading level and ending.
g.Entry.makeHeader = function(type, level, ending)
type = type == 'coleot' and 'Coleottero' or string.fu(type)
local headerTags = string.rep('=', level)
return table.concat({headerTags, 'Pokémon di tipo', type, ending,
headerTags}, ' ')
end
--[[
Constructor: first argument is an entry from Poké/data, second one its key.
Subclasses are to return nil, as specified by makeList in Wikilib/lists.
--]]
g.Entry.new = function(pokeData, name)
local this = g.Entry.super.new(name, pokeData.ndex)
return setmetatable(table.merge(this, pokeData), g.Entry)
end
--[[
Wikicode for a list entry: Pokémon ndex, mini sprite, name and types.
Mono-typed Pokémon display the type only once.
--]]
g.Entry.__tostring = function(this)
return string.interp([=[| class="width-xs-20" | ${ndex}
| class="width-xs-20" | ${static}
| class="width-xs-60" style="padding: 0.5ex 0.5em;" | [[${name}]]${form}
${types}]=],
{
ndex = this.ndex and string.tf(this.ndex) or '???',
static = ms.staticLua{string.tf(this.ndex or 0) ..
(this.formAbbr == 'base' and '' or this.formAbbr or '')},
name = this.name,
form = this.formsData and this.formsData.links[this.formAbbr] or '',
types = resp.twoTypeCellsLua(this.type1, this.type2, {{'thick'}},
{{'type-cell'}})
})
end
-- Mono-typed Pokémon entry class
g.MonoTypeEntry = oop.makeClass(g.Entry)
-- Creates mono-typed headings
g.MonoTypeEntry.makeHeader = function(type)
return g.MonoTypeEntry.super.makeHeader(type, 3, 'puro')
end
--[[
Constructor: the first argument is an entry from Poké/data, the second one its
key and the third is the type the Pokémon must have. As specified by makeList
in Wikilib/lists, returns nil whenever the Pokémon is either dual-typed or not
of the passed type.
--]]
g.MonoTypeEntry.new = function(pokeData, name, type)
pokeData = multigen.getGen(pokeData)
if pokeData.type1 ~= pokeData.type2 or type ~= pokeData.type1 then
return nil
end
return setmetatable(g.MonoTypeEntry.super.new(pokeData,
name), g.MonoTypeEntry)
end
-- First-typed Pokémon entry class
g.FirstTypeEntry = oop.makeClass(g.Entry)
-- Creates first-typed headings
g.FirstTypeEntry.makeHeader = function(type)
return g.FirstTypeEntry.super.makeHeader(type, 4,
'come tipo primario')
end
--[[
Constructor: the first argument is an entry from Poké/data, the second one its
key and the third is the type the Pokémon must have as first. As specified by
makeList in Wikilib/lists, returns nil whenever the Pokémon is either
mono-typed or its first type is not the passed one.
--]]
g.FirstTypeEntry.new = function(pokeData, name, type)
pokeData = multigen.getGen(pokeData)
if pokeData.type1 == pokeData.type2 or type ~= pokeData.type1 then
return nil
end
return setmetatable(g.FirstTypeEntry.super.new(pokeData,
name), g.FirstTypeEntry)
end
-- Second-typed Pokémon entry class
g.SecondTypeEntry = oop.makeClass(g.Entry)
-- Creates second-typed headings
g.SecondTypeEntry.makeHeader = function(type)
return g.SecondTypeEntry.super.makeHeader(type, 4,
'come tipo secondario')
end
--[[
Constructor: the first argument is an entry from Poké/data, the second one its
key and the third is the type the Pokémon must have as second. As specified by
makeList in Wikilib/lists, returns nil whenever the Pokémon is either
mono-typed or its second type is not the passed one.
--]]
g.SecondTypeEntry.new = function(pokeData, name, type)
pokeData = multigen.getGen(pokeData)
if pokeData.type1 == pokeData.type2
or type ~= pokeData.type2 then
return nil
end
return setmetatable(g.SecondTypeEntry.super.new(pokeData,
name), g.SecondTypeEntry)
end
--[[
Wikicode for list header: it takes the type name, for colors, and the number of
types, to print the correct amount of type columns.
--]]
local makeHeader = function(type, typesCount)
return string.interp([=[{| class="roundy sortable pull-center text-center roundy-footer white-rows" style="border-spacing: 0; padding: 0.3ex; ${bg};"
|- class="hidden-xs"
! class="black-text" style="padding-top: 0.5ex; padding-bottom: 0.5ex; padding-left: 0.5ex;" | [[Elenco Pokémon secondo il Pokédex Nazionale|#]]
! class="unsortable" |
! class="black-text" | [[Pokémon]]
${types}]=],
{
bg = css.horizGradLua{type = type},
types = typesCount < 2 and '! class="black-text" | [[Tipo]]'
or [=[! class="black-text" | [[Tipo|Tipo 1]]
! class="black-text" | [[Tipo|Tipo 2]]]=]
})
end
--[[
Creates heading and HTML table for Pokémons of a given type. Its first argument
is such type, the second one an Entry class complying to makeList in
Wikilib/listm and the third is the heading line. The latter argument is
optional, defaulting to the return of the Entry class makeHeader method.
--]]
g.makeTypeTable = function(type, Entry, header)
return table.concat({header or Entry.makeHeader(type),
list.makeList({
source = pokes,
iterator = list.pokeNames,
entryArgs = type,
makeEntry = Entry.new,
header = makeHeader(type,
Entry == g.MonoTypeEntry and 1 or 2),
separator = '|- class="roundy flex-xs flex-row flex-wrap flex-main-center flex-items-center" style="margin: 0.5rem 0;"'
})}, '\n')
end
--[[
Wikicode interface function: takes a type as the title of its page ('<type>')
and prints a list fo all the Pokémon having such type, dividing them into
mono-typed, first-typed and second-typed. Heading for sublists are also
displayed.
Examples:
{{#invoke: Typelist | Typelist | Ghiaccio }}
(in type pages only)
{{#invoke: Typelist | Typelist | {{BASEPAGENAME}} }}
--]]
g.typelist = function(frame)
-- Extracting type from page title
local monoType = string.trim(mw.text.decode(frame.args[1]
or 'sconosciuto')):lower()
-- Dual-typed Pokémon have 'coleottero' as 'coleot'
local dualType = monoType == 'coleottero' and 'coleot' or monoType
return table.concat({
g.makeTypeTable(monoType, g.MonoTypeEntry),
g.Entry.makeHeader(monoType, 3, 'parziale'),
g.makeTypeTable(dualType, g.FirstTypeEntry),
g.makeTypeTable(dualType, g.SecondTypeEntry)
}, '\n\n')
end
g.Typelist, g.TypeList, g.typeList = g.typelist, g.typelist, g.typelist
return g