-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMiniSprite.lua
More file actions
171 lines (144 loc) · 4.72 KB
/
MiniSprite.lua
File metadata and controls
171 lines (144 loc) · 4.72 KB
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
--[[
Module to insert MiniSprites.
Examples:
{{#invoke: MiniSprite | static | 487O }}
{{#invoke: MiniSprite | ani | 63 | gen = 2 }}
{{#invoke: MiniSprite | static | 398 | gen = 7 }}
{{#invoke: MiniSprite | static | 398 | female = yes | shiny = yes }}
{{#invoke: MiniSprite | static | 398 | game = LPA }}
--]]
local o = {}
-- stylua: ignore start
local mw = require('mw')
local txt = require('Wikilib-strings')
local genlib = require('Wikilib-gens')
local wlib = require('Wikilib')
local gendata = require("Gens-data")
local pokes = require("Poké-data")
-- stylua: ignore end
-- Alias for generations with the same MS
local genAliases = {}
genAliases[3] = 5
genAliases[4] = 5
genAliases[6] = 7
-- Interp strings for static ms of specific games
local staticGamesString = {
spsc = "[[File:${num}MSSpSc.png|${name}|40px|link=${name}]]",
dlps = "[[File:${num}MSDLPS.png|${name}|40px|link=${name}]]",
lpa = "[[File:Iconlpa${gender}${shiny}${num}.png|${name}|40px|link=${name}]]",
sv = "[[File:${num}MSSV.png|${name}|40px|link=${name}]]",
}
-- Return true if the given generation has only static MS
local function staticOnly(gen)
return gen > 5
end
-- Return true if the given generation has only animated MS
local function aniOnly(gen)
return gen < 3
end
--[[
Preprocess the arguments to make them more suitable for the module internals.
--]]
local function preprocess(n, gen, game, link)
n = n or "0000"
game = game and string.lower(game)
local gameGen = game and genlib.getGen.game(game)
-- If game is specified, overrides the gen
gen = gameGen or tonumber(gen) or gendata.latest
gen = genAliases[gen] or gen
if not link then
local numberN = txt.parseInt(n)
if pokes[numberN] then
link = pokes[numberN].name
elseif n:find(".*[Uu]ovo.*") then
link = "Uova Pokémon"
else
link = "MissingNo"
end
end
return n, gen, game, link
end
--[[
Proxy for Wikicode interfaces: takes arguments from the MW format and returns
arguments for Lua calls.
Supports the following arguments. The ndex can be also the first argument.
- 1 | ndex: ndex number
- link (optional): link for the image (by default the Pokémon page)
- gen (optional): generation of the MS
- game (optional): game of the MS (if provided, overrides gen)
- female (optional): for gen 8 and after, use the female MS if given
- shiny (optional): for gen 8 and after, use the shiny MS if given
DEPRECATION WARNING: link and gen were supported as second and third positional
arguments, respectively. While they still are, the behavious is considered
deprecated and could be removed at any time.
--]]
local function splitMwArgs(args)
args = wlib.trimAll(mw.clone(args), true)
return {
ndex = args.ndex or args[1],
gen = args.gen or args[3],
game = args.game,
link = args.link or args[2],
female = args.female,
shiny = args.shiny,
}
end
-- Animated MS, Lua call
o.aniLua = function(args)
local n, gen, game, link =
preprocess(args.ndex or args[1], args.gen, args.game, args.link)
if staticOnly(gen) then
args.ndex = n
args.game = game
args.gen = gen
args.link = link
return o.staticLua(args)
end
-- Here we can assume gen < 8, so filenames are much more consistent
return txt.interp(
"[[File:Ani${num}MS${gen}.gif|${name}|link=${name}]]",
{ num = n, gen = gen, name = link }
)
end
o.ani_lua = o.aniLua
-- Animated MS, Wikicode interface
o.ani = function(frame)
return o.aniLua(splitMwArgs(frame.args))
end
o.Ani, o.AniP, o.aniP = o.ani, o.ani, o.ani
-- Static MS, Lua call
o.staticLua = function(args)
local n, gen, game, link =
preprocess(args.ndex or args[1], args.gen, args.game, args.link)
if aniOnly(gen) then
args.ndex = n
args.game = game
args.gen = gen
args.link = link
return o.aniLua(args)
end
local interpData = {
num = n,
gen = tostring(gen),
name = link,
gender = args.female == "yes" and "f" or "m",
shiny = args.shiny == "yes" and "sh" or "",
}
local interpString = staticGamesString[game]
if interpString == nil then
if gen < 8 then
interpString = "[[File:${num}MS${gen}.png|${name}|link=${name}]]"
else
interpString =
"[[File:Mini${gender}${shiny}${num}.png|${name}|40px|link=${name}]]"
end
end
return txt.interp(interpString, interpData)
end
o.static_lua = o.staticLua
-- Static MS, Wikicode interface
o.static = function(frame)
return o.staticLua(splitMwArgs(frame.args))
end
o.Static, o.staticP, o.StaticP = o.static, o.static, o.static
return o