Module:Bestiary: Difference between revisions
Jump to navigation
Jump to search
>OmegaK2 (Created page with "-- -- Module for bestiary templates -- local m_util = require('Module:Util') local getArgs = require('Module:Arguments').getArgs local p = {} -- ---------------------------...") |
(If rarity equals "Unique", don't output the rarity in front of the monster's name) |
||
(14 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
-- | ------------------------------------------------------------------------------- | ||
-- Module | -- | ||
-- | -- Module:Bestiary | ||
-- | |||
-- This module implements Template:Bestiary and Template:Beastcrafting recipes. | |||
------------------------------------------------------------------------------- | |||
require('Module:No globals') | |||
local m_util = require('Module:Util') | local m_util = require('Module:Util') | ||
local | local m_cargo = require('Module:Cargo') | ||
-- Should we use the sandbox version of our submodules? | |||
local use_sandbox = m_util.misc.maybe_sandbox('Bestiary') | |||
local | |||
--mw.loadData('Module: | -- The cfg table contains all localisable strings and configuration, to make it | ||
-- easier to port this module to another wiki. | |||
local cfg = use_sandbox and mw.loadData('Module:Bestiary/config/sandbox') or mw.loadData('Module:Bestiary/config') | |||
local i18n = cfg.i18n | |||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
-- Cargo | -- Cargo tables | ||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
Line 48: | Line 43: | ||
field = 'notes', | field = 'notes', | ||
type = 'Text', | type = 'Text', | ||
}, | |||
game_mode = { | |||
field = 'game_mode', | |||
type = 'Integer', | |||
}, | }, | ||
}, | }, | ||
Line 61: | Line 60: | ||
min_level = { | min_level = { | ||
field = 'min_level', | field = 'min_level', | ||
type = ' | type = 'Integer', | ||
}, | }, | ||
rarity = { | rarity = { | ||
Line 71: | Line 70: | ||
type = 'String', | type = 'String', | ||
}, | }, | ||
group = { | -- cargo is complaining. Rename to group when able | ||
field = ' | beast_group = { | ||
field = 'beast_group', | |||
type = 'String', | type = 'String', | ||
}, | }, | ||
Line 100: | Line 100: | ||
field = 'component_id', | field = 'component_id', | ||
type = 'String', | type = 'String', | ||
}, | |||
amount = { | |||
field = 'amount', | |||
type = 'Integer', | |||
}, | }, | ||
}, | }, | ||
Line 105: | Line 109: | ||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
-- | -- Main functions | ||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
local | local function _recipe_table(args) | ||
args.game_mode = args.game_mode or 'normal' | |||
if cfg.game_modes[args.game_mode] == nil then | |||
error(i18n.errors.invalid_game_mode) | |||
end | |||
local tables = {'bestiary_recipes', 'bestiary_recipe_components', 'bestiary_components', 'mods'} | |||
local fields = { | |||
'bestiary_recipes.id', | |||
'bestiary_recipes.header', | |||
'bestiary_recipes.subheader', | |||
'bestiary_recipes.notes', | |||
'bestiary_recipe_components.amount', | |||
'bestiary_components.min_level', | |||
'bestiary_components.rarity', | |||
'bestiary_components.family', | |||
'bestiary_components.beast_group', | |||
'bestiary_components.genus', | |||
'bestiary_components.monster', | |||
'mods.name', | |||
} | |||
local query = { | |||
join = table.concat({ | |||
'bestiary_recipes.id = bestiary_recipe_components.recipe_id', | |||
'bestiary_recipe_components.component_id = bestiary_components.id', | |||
'bestiary_components.mod_id = mods.id', | |||
}, ', '), | |||
where = string.format( | |||
'bestiary_recipes.game_mode = %s', | |||
cfg.game_modes[args.game_mode] | |||
), | |||
limit = 9999, | |||
} | |||
local results = m_cargo.query(tables, fields, query) | |||
local order = m_cargo.query( | |||
{'bestiary_recipes'}, | |||
{'bestiary_recipes.id'}, | |||
{ | |||
limit=9999, | |||
orderBy='bestiary_recipes.header, bestiary_recipes.subheader', | |||
} | |||
) | |||
results = m_cargo.map_results_to_id{results=results, field='bestiary_recipes.id'} | |||
local tbl = mw.html.create('table') | |||
local | tbl | ||
:addClass('wikitable sortable') | |||
:tag('tr') | |||
:tag('th') | |||
:wikitext(i18n.recipe_table.category) | |||
:done() | |||
:tag('th') | |||
:wikitext(i18n.recipe_table.description) | |||
:done() | |||
:tag('th') | |||
:wikitext(i18n.recipe_table.notes) | |||
:done() | |||
:tag('th') | |||
:wikitext(i18n.recipe_table.components) | |||
:done() | |||
for _, order_row in ipairs(order) do | |||
local row = results[order_row['bestiary_recipes.id']] | |||
if row then | |||
local components = {} | |||
for _, component in ipairs(row) do | |||
components[#components+1] = string.format('%sx ', component['bestiary_recipe_components.amount']) | |||
if component['bestiary_components.min_level'] and component['bestiary_components.min_level'] ~= '0' then | |||
components[#components] = components[#components] .. string.format('level %s+ ', component['bestiary_components.min_level']) | |||
end | |||
if component['bestiary_components.family'] ~= 'Any Creature' then | |||
components[#components] = components[#components] .. '[[' | |||
end | |||
if component['bestiary_components.rarity'] and component['bestiary_components.rarity'] ~= 'Unique' then | |||
components[#components] = components[#components] .. component['bestiary_components.rarity'] .. ' ' | |||
end | |||
for _, key in ipairs({'bestiary_components.family', | |||
'bestiary_components.beast_group', | |||
'bestiary_components.genus', | |||
'bestiary_components.monster'}) do | |||
if component[key] then | |||
components[#components] = components[#components] .. component[key] .. ' ' | |||
end | |||
end | |||
if component['bestiary_components.family'] ~= 'Any Creature' then | |||
components[#components] = components[#components] .. ']]' | |||
end | |||
if component['mods.name'] then | |||
components[#components] = components[#components] .. string.format('with modifier "%s"', component['mods.name']) | |||
end | |||
end | |||
tbl | |||
:tag('tr') | |||
:tag('td') | |||
:wikitext(row[1]['bestiary_recipes.header'] or '') | |||
:done() | |||
:tag('td') | |||
:wikitext(row[1]['bestiary_recipes.subheader'] or '') | |||
:done() | |||
:tag('td') | |||
:wikitext(row[1]['bestiary_recipes.notes'] or '') | |||
:done() | |||
:tag('td') | |||
:wikitext(table.concat(components, '<br>')) | |||
:done() | |||
end | end | ||
end | end | ||
return | return tostring(tbl) | ||
end | end | ||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
-- | -- Exported functions | ||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
local p = {} | |||
p.table_bestiary_recipes = m_cargo.declare_factory{data=tables.recipes} | |||
p.table_bestiary_components = m_cargo.declare_factory{data=tables.components} | |||
p.table_bestiary_recipe_components = m_cargo.declare_factory{data=tables.recipe_components} | |||
p.store_data = m_cargo.store_from_lua{tables=tables, module='Bestiary'} | |||
-- | |||
-- Template:Beastcrafting recipes | |||
-- | |||
p.recipe_table = m_util.misc.invoker_factory(_recipe_table, { | |||
wrappers = cfg.wrappers.recipe_table, | |||
}) | |||
return p | return p |
Latest revision as of 14:30, 26 May 2024
Module for handling bestiary related data.
Templates
- {{Bestiary}}
- {{Beastcrafting recipes}}
Subpages
- Module:Bestiary/components
- Module:Bestiary/components/doc
- Module:Bestiary/config
- Module:Bestiary/config/doc
- Module:Bestiary/config/sandbox
- Module:Bestiary/doc
- Module:Bestiary/recipe components
- Module:Bestiary/recipe components/doc
- Module:Bestiary/recipes
- Module:Bestiary/recipes/doc
- Module:Bestiary/sandbox
The above documentation is transcluded from Module:Bestiary/doc.
Editors can experiment in this module's sandbox and testcases pages.
Subpages of this module.
Editors can experiment in this module's sandbox and testcases pages.
Subpages of this module.
-------------------------------------------------------------------------------
--
-- Module:Bestiary
--
-- This module implements Template:Bestiary and Template:Beastcrafting recipes.
-------------------------------------------------------------------------------
require('Module:No globals')
local m_util = require('Module:Util')
local m_cargo = require('Module:Cargo')
-- Should we use the sandbox version of our submodules?
local use_sandbox = m_util.misc.maybe_sandbox('Bestiary')
-- The cfg table contains all localisable strings and configuration, to make it
-- easier to port this module to another wiki.
local cfg = use_sandbox and mw.loadData('Module:Bestiary/config/sandbox') or mw.loadData('Module:Bestiary/config')
local i18n = cfg.i18n
-- ----------------------------------------------------------------------------
-- Cargo tables
-- ----------------------------------------------------------------------------
local tables = {}
tables.recipes = {
table = 'bestiary_recipes',
fields = {
id = {
field = 'id',
type = 'String',
},
header = {
field = 'header',
type = 'Text',
},
subheader = {
field = 'subheader',
type = 'Text',
},
notes = {
field = 'notes',
type = 'Text',
},
game_mode = {
field = 'game_mode',
type = 'Integer',
},
},
}
tables.components = {
table = 'bestiary_components',
fields = {
id = {
field = 'id',
type = 'String',
},
min_level = {
field = 'min_level',
type = 'Integer',
},
rarity = {
field = 'rarity',
type = 'String',
},
family = {
field = 'family',
type = 'String',
},
-- cargo is complaining. Rename to group when able
beast_group = {
field = 'beast_group',
type = 'String',
},
genus = {
field = 'genus',
type = 'String',
},
mod_id = {
field = 'mod_id',
type = 'String',
},
monster = {
field = 'monster',
type = 'String',
},
},
}
tables.recipe_components = {
table = 'bestiary_recipe_components',
fields = {
recipe_id = {
field = 'recipe_id',
type = 'String',
},
component_id = {
field = 'component_id',
type = 'String',
},
amount = {
field = 'amount',
type = 'Integer',
},
},
}
-- ----------------------------------------------------------------------------
-- Main functions
-- ----------------------------------------------------------------------------
local function _recipe_table(args)
args.game_mode = args.game_mode or 'normal'
if cfg.game_modes[args.game_mode] == nil then
error(i18n.errors.invalid_game_mode)
end
local tables = {'bestiary_recipes', 'bestiary_recipe_components', 'bestiary_components', 'mods'}
local fields = {
'bestiary_recipes.id',
'bestiary_recipes.header',
'bestiary_recipes.subheader',
'bestiary_recipes.notes',
'bestiary_recipe_components.amount',
'bestiary_components.min_level',
'bestiary_components.rarity',
'bestiary_components.family',
'bestiary_components.beast_group',
'bestiary_components.genus',
'bestiary_components.monster',
'mods.name',
}
local query = {
join = table.concat({
'bestiary_recipes.id = bestiary_recipe_components.recipe_id',
'bestiary_recipe_components.component_id = bestiary_components.id',
'bestiary_components.mod_id = mods.id',
}, ', '),
where = string.format(
'bestiary_recipes.game_mode = %s',
cfg.game_modes[args.game_mode]
),
limit = 9999,
}
local results = m_cargo.query(tables, fields, query)
local order = m_cargo.query(
{'bestiary_recipes'},
{'bestiary_recipes.id'},
{
limit=9999,
orderBy='bestiary_recipes.header, bestiary_recipes.subheader',
}
)
results = m_cargo.map_results_to_id{results=results, field='bestiary_recipes.id'}
local tbl = mw.html.create('table')
tbl
:addClass('wikitable sortable')
:tag('tr')
:tag('th')
:wikitext(i18n.recipe_table.category)
:done()
:tag('th')
:wikitext(i18n.recipe_table.description)
:done()
:tag('th')
:wikitext(i18n.recipe_table.notes)
:done()
:tag('th')
:wikitext(i18n.recipe_table.components)
:done()
for _, order_row in ipairs(order) do
local row = results[order_row['bestiary_recipes.id']]
if row then
local components = {}
for _, component in ipairs(row) do
components[#components+1] = string.format('%sx ', component['bestiary_recipe_components.amount'])
if component['bestiary_components.min_level'] and component['bestiary_components.min_level'] ~= '0' then
components[#components] = components[#components] .. string.format('level %s+ ', component['bestiary_components.min_level'])
end
if component['bestiary_components.family'] ~= 'Any Creature' then
components[#components] = components[#components] .. '[['
end
if component['bestiary_components.rarity'] and component['bestiary_components.rarity'] ~= 'Unique' then
components[#components] = components[#components] .. component['bestiary_components.rarity'] .. ' '
end
for _, key in ipairs({'bestiary_components.family',
'bestiary_components.beast_group',
'bestiary_components.genus',
'bestiary_components.monster'}) do
if component[key] then
components[#components] = components[#components] .. component[key] .. ' '
end
end
if component['bestiary_components.family'] ~= 'Any Creature' then
components[#components] = components[#components] .. ']]'
end
if component['mods.name'] then
components[#components] = components[#components] .. string.format('with modifier "%s"', component['mods.name'])
end
end
tbl
:tag('tr')
:tag('td')
:wikitext(row[1]['bestiary_recipes.header'] or '')
:done()
:tag('td')
:wikitext(row[1]['bestiary_recipes.subheader'] or '')
:done()
:tag('td')
:wikitext(row[1]['bestiary_recipes.notes'] or '')
:done()
:tag('td')
:wikitext(table.concat(components, '<br>'))
:done()
end
end
return tostring(tbl)
end
-- ----------------------------------------------------------------------------
-- Exported functions
-- ----------------------------------------------------------------------------
local p = {}
p.table_bestiary_recipes = m_cargo.declare_factory{data=tables.recipes}
p.table_bestiary_components = m_cargo.declare_factory{data=tables.components}
p.table_bestiary_recipe_components = m_cargo.declare_factory{data=tables.recipe_components}
p.store_data = m_cargo.store_from_lua{tables=tables, module='Bestiary'}
--
-- Template:Beastcrafting recipes
--
p.recipe_table = m_util.misc.invoker_factory(_recipe_table, {
wrappers = cfg.wrappers.recipe_table,
})
return p