forked from tetraminus/Tetrapak
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebGenerator.lua
More file actions
194 lines (125 loc) · 4.13 KB
/
WebGenerator.lua
File metadata and controls
194 lines (125 loc) · 4.13 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
WebGenerator = {}
local function toWebRarity(rarity )
if rarity == 1 then
return "Common"
elseif rarity == 2 then
return "Uncommon"
elseif rarity == 3 then
return "Rare"
elseif rarity == 4 then
return "Legendary"
elseif rarity == CURSERARITY then
return "Curse"
end
-- capitalise the first letter
return string.upper(string.sub(rarity, 1, 1)) .. string.sub(rarity, 2)
end
local function getDefaultLocText(card)
if not card.config or not card.config.center then
local retval = card.loc_text
if type(retval) == "table" then
return retval
end
return card.loc_txt
end
print("card", table.tostring(card))
local center = card.config.center
if not center.config then
local retval = center.loc_text
if type(retval) == "table" then
return retval
end
return center.loc_txt
end
local card = {
config = center.config
}
card.config.center = center
card.ability = center.config
print("card", table.tostring(card))
--card.config.center = center
local vars = {}
if center.loc_def then
vars = center.loc_def(card)
end
local loc_txt = center.loc_text
if type(loc_txt) ~= "table" then
loc_txt = center.loc_txt
end
for k, v in pairs(vars) do
for k2, v2 in pairs(loc_txt.text) do
loc_txt.text[k2] = string.gsub(v2, "#" .. k .. "#", v)
end
end
return loc_txt
end
local function CreateWebEntry(center, type)
local entry = {}
entry.name = center.name
entry.loc_txt = center.loc_txt
entry.rarity = toWebRarity(center.rarity or type)
print("creating web entry for ", center.name)
-- output in format
-- {
-- name: "Joker",
-- text: [
-- "{C:mult}+4{} Mult"
-- ],
-- image_url: "img/j_joker.png",
-- rarity: "Common"
-- }
local FinalEntry = ""
FinalEntry = FinalEntry .. "{\n"
FinalEntry = FinalEntry .. "name: \"" .. getDefaultLocText(center).name .. "\",\n"
FinalEntry = FinalEntry .. "text: [\n"
for k, v in pairs(getDefaultLocText(center).text) do
FinalEntry = FinalEntry .. "\"" .. v .. "\",\n"
end
FinalEntry = FinalEntry .. "],\n"
local imgname = center.key
--remove everything before the generated id, as the image is
local toremove = tpmakeID("tetrapak_") -- remove the generated id and all before it
imgname = string.sub(imgname, string.find(imgname, toremove) + string.len(toremove))
local assetpath = "assets/1x/"..type.."s/" .. imgname .. ".png"
FinalEntry = FinalEntry .. "image_url: \"" .. assetpath .. "\",\n"
if entry.rarity then
FinalEntry = FinalEntry .. "rarity: \"" .. entry.rarity .. "\"\n"
else
-- use the type instead
FinalEntry = FinalEntry .. "rarity: \"" .. type .. "\"\n"
end
FinalEntry = FinalEntry .. "},\n"
return FinalEntry
end
function WebGenerator:generateWeb()
print("Generating web data")
local entries = {}
local output = "let data = {\n"
for k, defs in pairs(Tetrapak.Registry) do
entries[k] = {}
for k2, def in pairs(defs) do
local type = k:lower()
--remove the s from the end
type = string.sub(type, 1, string.len(type) - 1)
table.insert(entries[k], CreateWebEntry(def, type))
end
output = output .. " " .. k:lower() .. ": [\n"
for k, v in pairs(entries[k]) do
output = output .. v
end
output = output .. " ],\n"
end
output = output .. "}\n"
local mod = SMODS.current_mod
local modpath = mod.path
-- create the web folder
if not NFS.getInfo(modpath .. "web") then
NFS.createDirectory(modpath .. "web")
end
-- write to file if it exists, otherwise create it
local file = NFS.newFile(modpath .. "web/data.js")
file:open("w")
file:write(output)
file:close()
print("Web data generated")
end