Medoke (обсуждение | вклад) мНет описания правки |
Medoke (обсуждение | вклад) мНет описания правки |
||
Строка 1: | Строка 1: | ||
local p = {} |
local p = {} |
||
+ | |||
local cargo = mw.ext.cargo |
local cargo = mw.ext.cargo |
||
local getArgs = require('Модуль:Arguments').getArgs |
local getArgs = require('Модуль:Arguments').getArgs |
||
+ | |||
+ | local i18n = { |
||
+ | error = { |
||
+ | noAbilityData = 'Для этой способности нет данных' |
||
⚫ | |||
+ | } |
||
local runes = { |
local runes = { |
||
Строка 13: | Строка 20: | ||
} |
} |
||
+ | |||
⚫ | |||
+ | local function getAbilityData( source, name ) |
||
⚫ | |||
+ | local cargoData = cargoQuery( 'abilities', 'image, type', { |
||
⚫ | |||
+ | where = string.format( 'source="%s" AND title="%s"', source, name ), |
||
+ | groupBy = 'source, title' |
||
+ | } )[1] |
||
⚫ | |||
end |
end |
||
− | function |
+ | local function getItemIcon( name ) |
+ | local cargoData = cargoQuery( 'items', 'image', { |
||
− | if not data then |
||
+ | where = string.format( '_pageName="%s"', name ), |
||
− | mw.log('Генерируются данные.') |
||
+ | groupBy = '_pageID' |
||
− | data = mw.ext.cargo.query( 'abilities', 'image, type', { where='source="' .. args.source .. '" AND title="' .. args.name .. '"', groupBy='source, title' } )[1] |
||
+ | } )[1] |
||
+ | return cargoData.image |
||
+ | end |
||
+ | |||
+ | local function getRuneIcon( name ) |
||
⚫ | |||
+ | end |
||
+ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | end |
||
+ | |||
+ | function p._main( args, abilityData ) |
||
+ | abilityData = abilityData or getAbilityData( args.source, args.name ) |
||
+ | assert( abilityData, i18n.error.noAbilityData ) |
||
+ | |||
+ | local abilityType = abilityData.type |
||
+ | if abilityType == 'item' then |
||
+ | abilityData.image = getItemIcon( args.source ) |
||
⚫ | |||
+ | abilityData.image = getRuneIcon( runes[mw.ustring.lower(args.name)] ) |
||
end |
end |
||
+ | |||
⚫ | |||
− | if |
+ | if abilityData.image ~= '' then |
+ | return abilityData.image |
||
− | return mw.ext.cargo.query( 'items', 'image,', { where='_pageName="' .. args.source .. '"', groupBy='_pageID' } )[1]['image'] |
||
⚫ | |||
⚫ | |||
− | elseif data.image ~= '' then |
||
⚫ | |||
else |
else |
||
return 'File:Unknown icon.png' |
return 'File:Unknown icon.png' |
||
end |
end |
||
end |
end |
||
− | |||
return p |
return p |
Версия от 21:03, 14 января 2020
This module provides a simple way of generating an ability icon.
Использование
First, load the module.
local aicon = require('Модуль:Ability icon')
The function itself has two required and one optional argument:
source
The abilities source.name
The abilities name.data
(Optional) Already retrieved Cargo data, to prevent unnecessary queries.
Тесты
4 tests failed.
Name | Expected | Actual | |
---|---|---|---|
testDefault | |||
testGivenData | |||
testItem | Lua error -- Модуль:Ability_icon:25: bad argument #3 to 'format' (string expected, got nil) | ||
testItemAbility | Lua error -- Модуль:Ability_icon:24: attempt to call global 'cargoQuery' (a nil value) | ||
testNormalAbility | Lua error -- Модуль:Ability_icon:24: attempt to call global 'cargoQuery' (a nil value) | ||
testRuneAbility | Lua error -- Модуль:Ability_icon:24: attempt to call global 'cargoQuery' (a nil value) |
Зависимости
local p = {}
local cargo = mw.ext.cargo
local getArgs = require('Модуль:Arguments').getArgs
local i18n = {
error = {
noAbilityData = 'Для этой способности нет данных'
}
}
local runes = {
['богатство'] = 'Bounty',
['волшебство'] = 'Arcane',
['двойной урон'] = 'Double Damage',
['ускорение'] = 'Haste',
['иллюзии'] = 'Illusion',
['невидимость'] = 'Invisibility',
['регенерация'] = 'Regeneration',
}
local function getAbilityData( source, name )
local cargoData = cargoQuery( 'abilities', 'image, type', {
where = string.format( 'source="%s" AND title="%s"', source, name ),
groupBy = 'source, title'
} )[1]
return cargoData
end
local function getItemIcon( name )
local cargoData = cargoQuery( 'items', 'image', {
where = string.format( '_pageName="%s"', name ),
groupBy = '_pageID'
} )[1]
return cargoData.image
end
local function getRuneIcon( name )
return string.format( 'File:Bottle (%s) icon.png', runes[mw.ustring.lower(args.name)])
end
function p.main( frame )
local args = getArgs( frame )
return p._main( args )
end
function p._main( args, abilityData )
abilityData = abilityData or getAbilityData( args.source, args.name )
assert( abilityData, i18n.error.noAbilityData )
local abilityType = abilityData.type
if abilityType == 'item' then
abilityData.image = getItemIcon( args.source )
elseif abilityType == 'rune' then
abilityData.image = getRuneIcon( runes[mw.ustring.lower(args.name)] )
end
if abilityData.image ~= '' then
return abilityData.image
else
return 'File:Unknown icon.png'
end
end
return p