Module:Adventurer

From Dragalia Lost Wiki
Revision as of 01:16, 6 October 2019 by >Eave (preprocess template wikitext, since otherwise it's treated as plaintext)
Jump to navigation Jump to search
50px Documentation

Usage

This module is intended to back the rendering of more complicated Adventurer data-related templates. Currently it only supports the Template:Character info card template.

Example

Generally, this module should be called via the regular wikitext templates instead of directly. But here is example syntax for calling the functions from wikitext:

{{#invoke:Adventurer|infoCard|The Prince}}

Lua error: Error: Table Adventurers not found..

{{#invoke:Adventurer|infoCard|Name=Edward}}

Lua error: Error: Table Adventurers not found..


-- <includeonly> (prevents literal interpretation of links or categories within the module code)
-- Lua Templating for Adventurer related info.
local p = {}
local cargo = mw.ext.cargo

-- Usage: {{#invoke:Adventurer|infoCard|<name>}} or {{#invoke:Adventurer|infoCard|Name=<name>}}
-- Ex: {{#invoke:Adventurer|infoCard|Euden}} or {{#invoke:Adventurer|infoCard|Name=Elisanne}}
function p.infoCard(frame)
  local name = frame.args[1] or frame.args['Name']

  -- Query adventurer data
  local adv = getAdventurers({
    where = '_pageName="'..name..'"',
    groupBy = '_pageName',
    limit = 1
  })[1]
  if adv == nil then
    return ''
  end
  -- CoAbility
  local coability = getCoabilities({
    where = string.format('Id="%s"', adv['ExAbilityData1']),
    groupBy = '_pageName',
    limit = 1
  })[1]
  if coability then
    coability = coability['Name']
  end
  -- Abilities
  local abilityKeys = {'Abilities11', 'Abilities12', 'Abilities13', 'Abilities21', 'Abilities22', 'Abilities31', 'Abilities32'}
  local abilities = {}
  for i, key in ipairs(abilityKeys) do
    abilities[key] = ''
    if adv[key] and adv[key] ~= '0' then
      local ability = getAbilities({
        where = string.format('Id="%s"', adv[key]),
        groupBy = '_pageName',
        limit = 1
      })[1]
      if ability then
        abilities[key] = ability['Name']
      end
    end
  end

  -- Precompute some data elements
  -- Portrait Image
  local portraitFileName = string.format('%s 0%s r0%s portrait.png', adv['Id'], adv['VariationId'], adv['Rarity'])
  local portraitImage = mw.title.makeTitle('File', portraitFileName)
  if (portraitImage.exists) then
    portraitImage = string.format('[[File:%s|300px|link=%s]]', portraitFileName, name)
  else
    portraitImage = string.format('[[File:Unknown.png|300px|link=%s]]', name)
  end
  -- Skill 2 Tooltip
  local skill2tooltip = ''
  if (adv['Skill2Name'] ~= '') then
    skill2tooltip = string.format('{{SkillTooltip|%s}}', adv['Skill2Name'])
  end
  -- Max HP/ATK
  local maxhp = adv['MaxHp'] + adv['PlusHp0'] + adv['PlusHp1'] + adv['PlusHp2'] + adv['PlusHp3'] + adv['PlusHp4'] + adv['McFullBonusHp5']
  local maxatk = adv['MaxAtk'] + adv['PlusAtk0'] + adv['PlusAtk1'] + adv['PlusAtk2'] + adv['PlusAtk3'] + adv['PlusAtk4'] + adv['McFullBonusAtk5']
  -- Abilities
  local abilityTemplate = ', {{Icon|Ability|%s|size=24px}} {{AbilityTooltip|%s|%s}}'
  local ability1 = string.format('{{Icon|Ability|%s|size=24px}} {{AbilityTooltip|%s|%s|%s}}',
      abilities['Abilities11'], abilities['Abilities11'], abilities['Abilities12'], abilities['Abilities13'])
  local ability2 = abilities['Abilities21'] == '' and '' or
      string.format(abilityTemplate, abilities['Abilities21'], abilities['Abilities21'], abilities['Abilities22'])
  local ability3 = abilities['Abilities31'] == '' and '' or
      string.format(abilityTemplate, abilities['Abilities31'], abilities['Abilities31'], abilities['Abilities32'])

  -- Format the content
  local card = mw.html.create('div')
    :addClass('panel')
    :css('display','inline-block')
    :tag('div')
      :addClass('panel-heading')
      :wikitext('[['..name..']]')
      :done()
    :tag('div')
      :addClass('panel-body')
      :cssText('display:flex;flex-wrap:wrap;justify-content:center;')
      :tag('div')
        :wikitext(portraitImage)
        :done()
      :tag('div')
        :cssText('display:flex;flex-direction:column')
        :tag('div')
          :wikitext(string.format('{{TermDescription|Element|{{InlineIcon|Icon Element|%s}} %s}}', adv['ElementalType'], adv['ElementalType'])..
                    string.format('{{TermDescription|Weapon|{{InlineIcon|Icon Weapon|%s}} %s}}', adv['WeaponType'], adv['WeaponType'])..
                    string.format('{{TermDescriptionTooltip|{{Tooltip|Total Max HP|5★ Max HP with All Mana Circle Nodes and Bonus}}|%s (%s)|Total (5★ Max + Mana Circle Node Stats + Mana Circle Bonus)}}', adv['MaxHp'], maxhp)..
                    string.format('{{TermDescriptionTooltip|{{Tooltip|Total Max Str|5★ Max Str with All Mana Circle Nodes and Bonus}}|%s (%s)|Total (5★ Max + Mana Circle Node Stats + Mana Circle Bonus)}}', adv['MaxAtk'], maxatk)..
                    string.format('{{TermDescription|Class|[[File:Icon Type Row %s.png|105px|link=]]}}', adv['CharaType'])..
                    string.format('{{TermDescription|Base Rarity|[[File:Icon Rarity Row %s.png|75px|link=]]}}', adv['Rarity'])..
                    string.format('{{TermDescription|Obtained From|%s}}', adv['Obtain'])
                   )
          :done()
        :tag('hr')
          :cssText('margin:1em')
          :done()
        :tag('div')
          :tag('div')
            :cssText('width:360px')
            :wikitext("'''Skills''': ")
            :tag('div')
              :cssText('display:inline-block')
              :wikitext(string.format('{{SkillTooltip|%s}}%s', adv['Skill1Name'], skill2tooltip))
              :done()
            :done()
            :tag('div')
              :cssText('width:360px')
              :wikitext("'''Co-Ability''': ")
              :tag('div')
                :cssText('display:inline-block')
                :wikitext(string.format('{{Icon|CoAbility|%s|size=24px}} {{CoAbilityTooltip|%s|%s|%s|%s|%s}}', coability,
                                        adv['ExAbilityData1'], adv['ExAbilityData2'], adv['ExAbilityData3'], adv['ExAbilityData4'], adv['ExAbilityData5']))
                :done()
              :done()
            :tag('div')
              :cssText('width:360px')
              :wikitext("'''Abilities''': ")
              :tag('div')
                :cssText('display:inline-block')
                :wikitext(ability1..ability2..ability3)
                :done()
              :tag('br')
                :done()
    :allDone()
  return frame:preprocess(tostring(card))
end

function getAdventurers(queryArgs)
  local tables = 'Adventurers'
  local queryFields = 'Id,VariationId,CharaType,Rarity,ElementalType,WeaponType,MinHp3,MinHp4,MinHp5,MaxHp,PlusHp0,PlusHp1,PlusHp2,PlusHp3,PlusHp4,McFullBonusHp5,MinAtk3,MinAtk4,MinAtk5,MaxAtk,PlusAtk0,PlusAtk1,PlusAtk2,PlusAtk3,PlusAtk4,McFullBonusAtk5,Obtain,ExAbilityData1,ExAbilityData2,ExAbilityData3,ExAbilityData4,ExAbilityData5,Skill1Name,Skill2Name,Abilities11,Abilities12,Abilities13,Abilities21,Abilities22,Abilities31,Abilities32'
  return cargo.query(tables, queryFields, queryArgs)
end

function getCoabilities(queryArgs)
  local tables = 'CoAbilities'
  local queryFields = 'Name'
  return cargo.query(tables, queryFields, queryArgs)
end

function getAbilities(queryArgs)
  local tables = 'Abilities'
  local queryFields = 'Name'
  return cargo.query(tables, queryFields, queryArgs)
end

return p
--</includeonly>