Difference between revisions of "Module:Availability"

From Team Fortress Wiki
Jump to: navigation, search
(Added 5 strings, adjusted text on 4 strings, renamed one and removed two deprecated strings (which are not working anymore). I'll keep an eye on the error category to make sure nothing breaks.)
 
(96 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
local p = {}
 
local p = {}
  
-- Defined here so they can be referenced inside other functions, e.g. common_string
+
-- Imports the functions case_link(num) and crate_link(num), which return the pages for active/inactive cases and crates, based on the series number.
 +
-- This is stored in a separate file so that it can be updated *without* staff intervention.
 +
require('Module:Availability/active')
 +
 
 +
-- Defined here so that it can be referenced inside the template helper functions
 
local frame = nil
 
local frame = nil
local if_lang = nil
 
  
-- Expands the template {{tl|Common string}} for a given value.
+
-- Equivalent to {{tooltip|tool|tip}}
function common_string(arg)
+
function tooltip(tool, tip)
return frame:expandTemplate{title='Common string', args={arg}}
+
  return frame:expandTemplate{title='Tooltip', args={tool, tip}}
 
end
 
end
  
 +
-- Equivalent to {{item name|arg}}
 +
function item_name(arg)
 +
  return frame:expandTemplate{title='Item name', args={arg}}
 +
end
 +
 +
-- Equivalent to {{item link|arg}}
 +
function item_link(arg)
 +
  return frame:expandTemplate{title='Item link', args={arg}}
 +
end
 +
 +
-- Equivalent to {{common string|arg}} or {{common string|arg|opt_arg}}
 +
function common_string(arg, opt_arg)
 +
  return frame:expandTemplate{title='Common string', args={arg, opt_arg}}
 +
end
 +
 +
-- Returns '(text)', with correct parenthesis characters in Chinese/Japanese. See also [[Template:QuAD/p]].
 
function paren(text)
 
function paren(text)
if if_lang == '/zh_hant' or if_lang == '/zh-hans' then
+
  if if_lang == '/zh_hans' or if_lang == '/zh-hant' or if_lang == '/ja' then
return '(' .. text .. ')'
+
    return ' (' .. text .. ')'
else
+
  else
return '(' .. text .. ')'
+
    return ' (' .. text .. ')'
end
+
  end
 +
end
 +
 
 +
-- Inserts a language-appropriate list comma, used in lists of 3 or more items:
 +
-- For example: "I went to the store to buy apples, oranges and pears."
 +
function comma()
 +
  if if_lang == '/zh-hans' then
 +
    return '、'
 +
  elseif if_lang == '/zh-hant' then
 +
    return ','
 +
  else
 +
    return ', '
 +
  end
 
end
 
end
  
 
-- Replaces the old Dictionary/templatecore strings
 
-- Replaces the old Dictionary/templatecore strings
 
lookup = {
 
lookup = {
-- Singleton strings (without a number)
+
  -- Singleton strings (without a number)
['drop'] = function() return '[[Item drop system' .. if_lang .. '|' .. common_string('drop') .. ']]' end,
+
  ['audition-reel'] = function() return '[[Mann Co. Supply Crate/Seriesless crates' .. if_lang .. '#Mann Co. Audition Reel|' .. common_string('audition reel') .. ']]' end,
['craft'] = function() return '[[Crafting' .. if_lang .. '|' .. common_string('craft') .. ']]' end,
+
  ['australium'] = function() return '[[Mann Up' .. if_lang .. '|' .. common_string('generic reward') .. ']]' .. paren('[[Australium weapons' .. if_lang .. '|' .. common_string('australium') .. ']]') end,
['purchase'] = function() return '[[Mann Co. Store' .. if_lang .. '|' .. common_string('purchase') .. ']]' end,
+
  ['botkiller'] = function() return '[[Mann Up' .. if_lang .. '|' .. common_string('generic reward') .. ']]' .. paren('[[Botkiller weapons' .. if_lang .. '|' .. common_string('botkiller') .. ']]') end,
['promotional'] = function() return 'Promotional items' .. if_lang .. '|' .. common_string('promotional') .. ']]' end,
+
  ['box-companion'] = function() return item_link('What\'s in the Companion Square Box?') end,
-- Multipart strings (with a number)
+
  ['box-portal'] = function() return item_link('What\'s in the Portal 2 Soundtrack Box?') end,
['crate'] = function(num) return '[[Mann Co. Supply Crate/Retired series' .. if_lang .. '#' .. num .. '|' .. common_string('uncrate') .. ' #' .. num .. ']]' end,
+
  ['box-sandvich'] = function() return item_link('What\'s in the Sandvich Box?') end,
['crate-strange'] = function(num) return '[[Mann Co. Supply Crate/Retired series' .. if_lang .. '#' .. num .. '|' .. common_string('uncrate') .. '#' .. num .. ']] ' .. paren('[[Strange' .. if_lang .. '|' .. common_string('strange') .. ']]') end,
+
  ['box-tf'] = function() return item_link('What\'s in the Team Fortress 2 Soundtrack Box?') end,
 +
  ['bread-box'] = function() return item_link('Bread Box') end,
 +
  ['case-any'] = function(num) return '[[Mann Co. Supply Crate' .. if_lang .. '|' .. common_string('uncrate') .. ']]' .. paren(common_string('uncrate-anycase')) end,
 +
  ['chemistryset'] = function() return item_link('Chemistry Set') end,
 +
  ['collectors'] = function() return item_link('Chemistry Set') .. paren(item_link('Collector\'s')) end,
 +
  ['contract'] = function() return '[[Contracts' .. if_lang .. '|' .. common_string('contract') .. ']]' .. paren(item_link('Decorated')) end,
 +
  ['contract-unique'] = function() return '[[ConTracker' .. if_lang .. '|' .. common_string('contracker') .. ']]' end,
 +
  ['craft'] = function() return '[[Crafting' .. if_lang .. '|' .. common_string('craft') .. ']]' end,
 +
  ['craft-expired'] = function() return common_string('craft-expired') end,
 +
  ['creepy-crate'] = function() return '[[Mann Co. Supply Crate/Seriesless crates' .. if_lang .. '#Unlocked Creepy Crates|' .. item_name('unlocked creepy crate') .. ']]' end,
 +
  ['directors'] = function() return '[[Mann Co. Supply Crate/Seriesless crates' .. if_lang .. '#Mann Co. Director\'s Cut Reel|' .. common_string('director\'s cut') .. ']]' end,
 +
  ['distributed'] = function() return common_string('distributed') end,
 +
  ['distributed-duels'] = function() return common_string('distributed-duels') end,
 +
  ['distributed-expired'] = function() return common_string('distributed-expired') end,
 +
  ['distributed-gifts'] = function() return common_string('distributed-gifts') end,
 +
  ['distributed-stamps'] = function() return common_string('distributed-stamps') end,
 +
  ['drop'] = function() return '[[Item drop system' .. if_lang .. '|' .. common_string('drop') .. ']]' end,
 +
  ['drop-expired'] = function() return common_string('drop-expired') end,
 +
  ['festive-gift'] = function(num) return item_link('Smissmas 2015 Festive Gift') end,
 +
  ['gift-cauldron'] = function() return item_link('Halloween Gift Cauldron') end,
 +
  ['goodie-cauldron'] = function() return common_string('goodie-cauldron-limited') end,
 +
  ['halloween-package'] = function() return item_link('Halloween Package') end,
 +
  ['halloween-purchase'] = function() return '[[Mann Co. Store' .. if_lang .. '|' .. tooltip(common_string('purchase'), common_string('purchase-halloween')) .. ']]' end,
 +
  ['halloween-purchase-expired'] = function() return common_string('purchase-halloween-expired') end,
 +
  ['hauntedgift'] = function() return common_string('hauntedgift-expired') end,
 +
  ['jungleinferno-campaign'] = function() return common_string('jungleinferno-campaign') end,
 +
  ['jungleinferno-contracts'] = function() return item_link('Jungle Inferno Contracts Pass') end,
 +
  ['keyless-crate'] = function() return '[[Mann Co. Supply Crate/Seriesless crates' .. if_lang .. '#Unlocked Cosmetic Crates|' .. item_name('unlocked cosmetic crate') .. ']]' .. paren(item_link('Unique') .. ' ' .. common_string('and') .. ' ' .. item_link('Strange')) end,
 +
  ['keyless-crate-unusual'] = function() return '[[Mann Co. Supply Crate/Seriesless crates' .. if_lang .. '#Unlocked Cosmetic Crates|' .. item_name('unlocked cosmetic crate') .. ']]' .. paren(item_link('Unique') .. comma() .. item_link('Strange') .. ' ' .. common_string('and') .. ' ' .. item_link('Unusual')) end,
 +
  ['mannup'] = function() return '[[Mann Up' .. if_lang .. '|' .. common_string('generic reward') .. ']]' end,
 +
  ['mvm'] = function() return '[[Mann vs. Machine' .. if_lang .. '|' .. common_string('mvm reward') .. ']]' end,
 +
  ['mvm-both-types'] = function() return '[[Mann Up' .. if_lang .. '|' .. common_string('generic reward') .. ']]' .. paren('[[Botkiller weapons' .. if_lang .. '|' .. common_string('botkiller') .. ']] ' .. common_string('and') .. ' [[Australium weapons' .. if_lang .. '|' .. common_string('australium') .. ']]') end,
 +
  ['pallet-of-crates'] = function() return item_link('Pallet of Crates') end,
 +
  ['pile-of-ash'] = function() return common_string('pile of ash-expired') end,
 +
  ['pile-of-curses'] = function() return common_string('pile of curses-expired') end,
 +
  ['promotional'] = function() return '[[Promotional items' .. if_lang .. '|' .. common_string('promotional') .. ']]' end,
 +
  ['promotional-craft'] = function() return common_string('promotional-craft') end,
 +
  ['promotional-expired'] = function() return common_string('promotional-expired') end,
 +
  ['promotional-expired-codes'] = function() return common_string('promotional-expired-codes') end,
 +
  ['promotional-unlock'] = function() return common_string('promotional-unlock') end,
 +
  ['purchase'] = function() return '[[Mann Co. Store' .. if_lang .. '|' .. common_string('purchase') .. ']]' end,
 +
  ['purchase-limited'] = function() return common_string('purchase-limited') end,
 +
  ['retired'] = function() return common_string('retired') end,
 +
  ['stock'] = function() return '[[Stock weapons' .. if_lang .. '|' .. mw.getContentLanguage():ucfirst(item_name('stock')) .. ']]' end,
 +
  ['stockpile'] = function() return '[[Mann Co. Supply Crate/Seriesless crates' .. if_lang .. '#Mann Co. Stockpile Crate|' .. common_string('stockpile crate') .. ']]' end,
 +
  ['trade-up'] = function() return '[[Trade-Up' .. if_lang .. '|' .. common_string('trade-up') .. ']]' end,
 +
  ['transmute'] = function() return '[[Halloween Transmute' .. if_lang .. '|' .. common_string('halloween transmute') .. ']] ' .. paren('[[Trading' .. if_lang .. '|' .. common_string('Untradable') .. ']]') end,
 +
  ['unavailable'] = function() return common_string('unavailable') end,
 +
  ['uncrate-bonus'] = function(num) return '[[Mann Co. Supply Crate' .. if_lang .. '|' .. common_string('uncrate') .. ']]' .. paren(common_string('uncrate-bonus')) end,
 +
  ['unlock'] = function() return '[[Achievement items' .. if_lang .. '|' .. common_string('unlock') .. ']]' end,
 +
  ['unusual'] = function() return '[[Mann Co. Supply Crate' .. if_lang .. '|' .. common_string('uncrate') .. ']]' .. paren(item_link('Unusual')) end,
 +
  ['unusual-expired'] = function() return '[[Mann Co. Supply Crate' .. if_lang .. '|' .. common_string('uncrate') .. ']]' .. paren(item_link('Unusual')) .. ' ' .. common_string('unusual-expired') end,
 +
  ['warpaint'] = function() return item_link('War Paint') .. paren(item_link('Decorated')) end,
 +
 
 +
  -- Multipart strings (with a number)
 +
  ['case'] = function(num) return '[[' .. case_link(num) .. if_lang .. '#' .. num .. '|'  .. item_name('case ' .. num) .. ']]' end,
 +
  ['case-bonus'] = function(num) return '[[' .. case_link(num) .. if_lang .. '#' .. num .. '|' .. item_name('case ' .. num) .. ']]' .. paren(common_string('uncrate-bonus')) end,
 +
  ['crate'] = function(num) return '[[' .. crate_link(num) .. if_lang .. '#' .. num .. '|' .. common_string('uncrate') .. ' #' .. num .. ']]' end,
 +
  ['crate-strange'] = function(num) return '[[' .. crate_link(num) .. if_lang .. '#' .. num .. '|' .. common_string('uncrate') .. ' #' .. num .. ']]' .. paren(item_link('Strange')) end,
 +
  ['crate-festive'] = function(num) return '[[' .. crate_link(num) .. if_lang .. '#' .. num .. '|' .. common_string('uncrate') .. ' #' .. num .. ']]' .. paren('[[Festive weapons' .. if_lang .. '|' .. common_string('festive') .. ']]') .. ' ' .. common_string('uncrate-expired') end,
 +
  ['crate-haunted'] = function(num) return '[[' .. crate_link(num) .. if_lang .. '#' .. num .. '|' .. common_string('uncrate') .. ' #' .. num .. ']]' .. paren(item_link('Haunted')) .. ' ' .. common_string('uncrate-expired') end,
 +
  ['crate-bonus'] = function(num) return '[[' .. crate_link(num) .. if_lang .. '#' .. num .. '|' .. common_string('uncrate') .. ' #' .. num .. ']]' .. paren(common_string('uncrate-bonus')) end,
 +
  ['crate-expired'] = function(num) return '[[' .. crate_link(num) .. if_lang .. '#' .. num .. '|' .. common_string('uncrate') .. ' #' .. num .. ']] ' .. common_string('uncrate-expired') end,
 +
  ['unusual-'] = function(num) return '[[Mann Co. Supply Crate' .. if_lang .. '|' .. common_string('uncrate') .. ']]' .. paren('[[Unusual' .. if_lang .. '#Group ' .. num .. '|' .. item_name('unusual') .. ']]') end,
 +
  ['xmas'] = function(num) return '[[Smissmas ' .. num .. ' Festive Gift' .. if_lang .. '|' .. item_name('smissmas ' .. num .. ' festive gift') .. ']]' end,
 +
  ['winter'] = function(num) return '[[Mann Co. Supply Crate/Active series' .. if_lang .. '#Unlocked Winter ' .. num .. ' Cosmetic Case|' .. item_name('unlocked winter ' .. num .. ' cosmetic case') .. ']]' end,
 +
  ['winter-strange'] = function(num) return '[[Mann Co. Supply Crate/Active series' .. if_lang .. '#Unlocked Winter ' .. num .. ' Cosmetic Case|' .. item_name('unlocked winter ' .. num .. ' cosmetic case') .. ']]' .. paren('[[Unique' .. if_lang .. '|' .. item_name('unique') .. ']] ' .. common_string('and') .. ' [[Strange' .. if_lang .. '|' .. item_name('strange') .. ']]') end,
 +
  ['winter-unusual'] = function(num) return '[[Mann Co. Supply Crate/Active series' .. if_lang .. '#Unlocked Winter ' .. num .. ' Cosmetic Case|' .. item_name('unlocked winter ' .. num .. ' cosmetic case') .. ']]' .. paren('[[Unique' .. if_lang .. '|' .. item_name('unique') .. ']]' .. comma() .. '[[Strange' .. if_lang .. '|' .. item_name('strange') .. ']] ' .. common_string('and') .. ' [[Unusual' .. if_lang .. '|' .. item_name('unusual') .. ']]') end,
 +
}
 +
 
 +
-- Some older strings which have been updated (but should continue to work until they're replaced). Using these strings adds the page to a tracking category.
 +
local backwards_compat = {
 +
  ['case-bonus'] = 'uncrate-bonus', -- Only applies if there is no number provided, otherwise the dynamic case-bonus is used
 +
  ['crate-scorched'] = 'crate46',
 +
  ['crate74'] = 'crate74-haunted',
 +
  ['crate74-expired'] = 'crate74-haunted',
 +
  ['xmas2015'] = 'festive-gift',
 +
  ['winter2016'] = 'crate105',
 +
}
 +
 
 +
local backwards_compat_dynamic = {
 +
  ['winter'] = 'winter',
 +
  ['unusual-'] = 'unusual'
 
}
 
}
  
function p.avail(global_frame)
+
function p.avail(f)
-- Define the global variables above.
+
  frame = f
frame = global_frame
+
  local args = f:getParent().args
if_lang = frame:expandTemplate{title='If lang'}
+
  count = 0
 +
  for _ in pairs(args) do count = count + 1 end
 +
  if count == 0 then -- There is no other way to count table length in lua.
 +
    args = f.args
 +
  end
 +
  -- Precompute the page's language.
 +
  if_lang = frame:expandTemplate{title='If lang'}
 +
 
 +
  -- Avail is called with a long list of arguments. Each one needs to be translated and comma-separated.
 +
  avail_string = ''
 +
  for i, arg in ipairs(args) do
 +
    arg = mw.ustring.lower(arg)
 +
 
 +
    -- Backwards compatibility for static strings
 +
    if backwards_compat[arg] ~= nil then
 +
      avail_string = '[[Category:Outdated Availability Parameters]]' .. avail_string
 +
      arg = backwards_compat[arg]
 +
    end
 +
 
 +
    -- Matches strings like 'craft' 'crate1' 'crate26-strange'
 +
    -- Strings like 'crate26-strange' become 'crate-strange' and '26'.
 +
    name1, num, name2 = string.match(arg, '(%D*)(%d*)(%D*)')
 +
    name = name1 .. name2
 +
   
 +
    -- Backwards compatibility for dynamic strings
 +
    if backwards_compat_dynamic[name] ~= nil then
 +
      avail_string = '[[Category:Outdated Availability Parameters]]' .. avail_string
 +
      name = backwards_compat_dynamic[name]
 +
    end
  
-- Avail is called with a long list of arguments. Each one needs to be translated and comma-separated
+
    assert(lookup[name], 'Passed an invalid string to [[Module:Availability]]: "' .. name .. '".\nPlease do not translate these strings, they will be translated automatically')
avail_string = ''
+
    if (args[i+2] ~= nil)
for _, arg in ipairs(frame.args) do
+
    or (args[i+1] ~= nil and args['availability'] ~= nil)
-- Matches strings like 'craft' 'crate1' 'crate26-strange'
+
      then -- There are at least two arguments left in the list
-- Strings like 'crate26-strange' become 'crate-strange' and '26'.
+
      avail_string = avail_string .. lookup[name](num) .. comma()
name1, num, name2 = string.match(arg, '(%D*)(%d*)(%D*)')
+
    elseif (args[i+1] ~= nil and args['availability'] == nil)
name = name1 .. name2
+
    or (args[i+1] == nil and args['availability'] ~= nil)
avail_string = avail_string .. lookup[name](num)
+
      then -- There is exactly one more argument in the list
end
+
      avail_string = avail_string .. lookup[name](num) .. ' ' .. common_string('or') .. ' '
 +
    else -- args[i+1] == nil and args['availability] == nil
 +
      avail_string = avail_string .. lookup[name](num)
 +
    end
 +
  end -- End looping through arguments.
  
 +
  -- Custom availability strings are handled last, since they should always go at the end of the template output.
 +
  if args['availability'] ~= nil then
 +
    avail_string = avail_string .. args['availability'] .. '[[Category:Custom Availability Parameters]]'
 +
  end
  
return avail_string
+
  return avail_string
 
end
 
end
  
 
return p
 
return p

Latest revision as of 03:25, 1 January 2025

Module:Availability (aka Template:Availability) is used to display and autotranslate the 'Availability' section in {{Item infobox}}. Preferably, the shortcut {{avail}} should be used.

For future reference, if creating new additions, the key cannot include a number, or it will be parsed out. Thus, if you want to add a string like xmas2015, instead add it to the Multipart strings with xmas and use num to represent the 2015.


Entries

Parameter Output
audition-reel Audition Reel
box-companion What's in the Companion Square Box?
box-portal What's in the Portal 2 Soundtrack Box?
box-sandvich What's in the Sandvich Box?
box-tf What's in the Team Fortress 2 Soundtrack Box?
bread-box Bread Box
chemistryset Chemistry Set
collectors Chemistry Set (Collector's)
contract Contract (Decorated)
contract-unique ConTracker
craft Craft
craft-expired Craft (expired)
creepy-crate Unlocked Creepy Crate
directors Director's Cut Reel
distributed Distributed
distributed-duels Distributed (periodical)
distributed-expired Distributed (expired)
distributed-gifts Distributed (periodical)
distributed-stamps Distributed (periodical)
drop Drop
drop-expired Drop (expired)
festive-gift Smissmas 2015 Festive Gift
gift-cauldron Halloween Gift Cauldron
goodie-cauldron Antique Halloween Goodie Cauldron (limited)
halloween-package Halloween Package
halloween-purchase Purchase
halloween-purchase-expired Purchase (expired)
hauntedgift Haunted Halloween Gift (expired)
jungleinferno-campaign Jungle Inferno Campaign Pass (limited)
jungleinferno-contracts Jungle Inferno Contracts Pass
keyless-crate Unlocked Cosmetic Crate (Unique and Strange)
keyless-crate-unusual Unlocked Cosmetic Crate (Unique, Strange and Unusual)
pallet-of-crates Pallet of Crates
pile-of-ash Pile of Ash (expired)
pile-of-curses Pile of Curses (expired)
promotional Promotional
promotional-craft Promotional (craft)
promotional-expired Promotional (expired)
promotional-expired-codes Promotional (expired)
promotional-unlock Promotional (unlock)
purchase Purchase
purchase-limited Purchase (limited)
retired Retired
stock Stock
stockpile Stockpile Crate
trade-up Trade-Up
transmute Halloween Transmute (Untradable)
unavailable Unavailable
uncrate-bonus Uncrate (bonus)
unlock Unlock
unusual Uncrate (Unusual)
unusual-expired Uncrate (Unusual) (expired)
warpaint War Paint (Decorated)
MvM related strings
mannup Reward
australium Reward (Australium)
botkiller Reward (Botkiller)
mvm-both-types Reward (Botkiller and Australium)
Dynamic strings (mostly used for crate/case numbers). The numeric value is automatically parsed.
crate1 Uncrate #1
crate2-strange Uncrate #2 (Strange)
crate3-festive Uncrate #3 (Festive) (expired)
crate4-haunted Uncrate #4 (Haunted) (expired)
crate5-bonus Uncrate #5 (bonus)
crate6-expired Uncrate #6 (expired)
crate91 Uncrate #91
crate92 Uncrate #92
case104 Creepy Crawly Case
case105-bonus Unlocked Winter 2016 Cosmetic Case (bonus)

Usage

Custom

This will place the page into Custom Availability Parameters. Do not use this in articles.

Backwards compatibility

{{Availability}} changes over time, and thus has some parameters which have been deprecated. They will still function in this template, but their use will place the page into Outdated Availability Parameters. Please replace them with the appropriate new value.

Outdated Parameter New Parameter Output
case-bonus uncrate-bonus Uncrate (bonus)
crate-scorched crate46 Uncrate #46
crate74 crate74-haunted Uncrate #74 (Haunted) (expired)
crate74-expired crate74-haunted Uncrate #74 (Haunted) (expired)
xmas2015 festive-gift Smissmas 2015 Festive Gift
winter2016 case105 Uncrate #105
unusual-# unusual Uncrate (Unusual)

local p = {}

-- Imports the functions case_link(num) and crate_link(num), which return the pages for active/inactive cases and crates, based on the series number.
-- This is stored in a separate file so that it can be updated *without* staff intervention.
require('Module:Availability/active')

-- Defined here so that it can be referenced inside the template helper functions
local frame = nil

-- Equivalent to {{tooltip|tool|tip}}
function tooltip(tool, tip)
  return frame:expandTemplate{title='Tooltip', args={tool, tip}}
end

-- Equivalent to {{item name|arg}}
function item_name(arg)
  return frame:expandTemplate{title='Item name', args={arg}}
end

-- Equivalent to {{item link|arg}}
function item_link(arg)
  return frame:expandTemplate{title='Item link', args={arg}}
end

-- Equivalent to {{common string|arg}} or {{common string|arg|opt_arg}}
function common_string(arg, opt_arg)
  return frame:expandTemplate{title='Common string', args={arg, opt_arg}}
end

-- Returns '(text)', with correct parenthesis characters in Chinese/Japanese. See also [[Template:QuAD/p]].
function paren(text)
  if if_lang == '/zh_hans' or if_lang == '/zh-hant' or if_lang == '/ja' then
    return ' (' .. text .. ')'
  else
    return ' (' .. text .. ')'
  end
end

-- Inserts a language-appropriate list comma, used in lists of 3 or more items:
-- For example: "I went to the store to buy apples, oranges and pears."
function comma()
  if if_lang == '/zh-hans' then
    return '、'
  elseif if_lang == '/zh-hant' then
    return ','
  else
    return ', '
  end
end

-- Replaces the old Dictionary/templatecore strings
lookup = {
  -- Singleton strings (without a number)
  ['audition-reel'] = function() return '[[Mann Co. Supply Crate/Seriesless crates' .. if_lang .. '#Mann Co. Audition Reel|' .. common_string('audition reel') .. ']]' end,
  ['australium'] = function() return '[[Mann Up' .. if_lang .. '|' .. common_string('generic reward') .. ']]' .. paren('[[Australium weapons' .. if_lang .. '|' .. common_string('australium') .. ']]') end,
  ['botkiller'] = function() return '[[Mann Up' .. if_lang .. '|' .. common_string('generic reward') .. ']]' .. paren('[[Botkiller weapons' .. if_lang .. '|' .. common_string('botkiller') .. ']]') end,
  ['box-companion'] = function() return item_link('What\'s in the Companion Square Box?') end,
  ['box-portal'] = function() return item_link('What\'s in the Portal 2 Soundtrack Box?') end,
  ['box-sandvich'] = function() return item_link('What\'s in the Sandvich Box?') end,
  ['box-tf'] = function() return item_link('What\'s in the Team Fortress 2 Soundtrack Box?') end,
  ['bread-box'] = function() return item_link('Bread Box') end,
  ['case-any'] = function(num) return '[[Mann Co. Supply Crate' .. if_lang .. '|' .. common_string('uncrate') .. ']]' .. paren(common_string('uncrate-anycase')) end,
  ['chemistryset'] = function() return item_link('Chemistry Set') end,
  ['collectors'] = function() return item_link('Chemistry Set') .. paren(item_link('Collector\'s')) end,
  ['contract'] = function() return '[[Contracts' .. if_lang .. '|' .. common_string('contract') .. ']]' .. paren(item_link('Decorated')) end,
  ['contract-unique'] = function() return '[[ConTracker' .. if_lang .. '|' .. common_string('contracker') .. ']]' end,
  ['craft'] = function() return '[[Crafting' .. if_lang .. '|' .. common_string('craft') .. ']]' end,
  ['craft-expired'] = function() return common_string('craft-expired') end,
  ['creepy-crate'] = function() return '[[Mann Co. Supply Crate/Seriesless crates' .. if_lang .. '#Unlocked Creepy Crates|' .. item_name('unlocked creepy crate') .. ']]' end,
  ['directors'] = function() return '[[Mann Co. Supply Crate/Seriesless crates' .. if_lang .. '#Mann Co. Director\'s Cut Reel|' .. common_string('director\'s cut') .. ']]' end,
  ['distributed'] = function() return common_string('distributed') end,
  ['distributed-duels'] = function() return common_string('distributed-duels') end,
  ['distributed-expired'] = function() return common_string('distributed-expired') end,
  ['distributed-gifts'] = function() return common_string('distributed-gifts') end,
  ['distributed-stamps'] = function() return common_string('distributed-stamps') end,
  ['drop'] = function() return '[[Item drop system' .. if_lang .. '|' .. common_string('drop') .. ']]' end,
  ['drop-expired'] = function() return common_string('drop-expired') end,
  ['festive-gift'] = function(num) return item_link('Smissmas 2015 Festive Gift') end,
  ['gift-cauldron'] = function() return item_link('Halloween Gift Cauldron') end,
  ['goodie-cauldron'] = function() return common_string('goodie-cauldron-limited') end,
  ['halloween-package'] = function() return item_link('Halloween Package') end,
  ['halloween-purchase'] = function() return '[[Mann Co. Store' .. if_lang .. '|' .. tooltip(common_string('purchase'), common_string('purchase-halloween')) .. ']]' end,
  ['halloween-purchase-expired'] = function() return common_string('purchase-halloween-expired') end,
  ['hauntedgift'] = function() return common_string('hauntedgift-expired') end,
  ['jungleinferno-campaign'] = function() return common_string('jungleinferno-campaign') end,
  ['jungleinferno-contracts'] = function() return item_link('Jungle Inferno Contracts Pass') end,
  ['keyless-crate'] = function() return '[[Mann Co. Supply Crate/Seriesless crates' .. if_lang .. '#Unlocked Cosmetic Crates|' .. item_name('unlocked cosmetic crate') .. ']]' .. paren(item_link('Unique') .. ' ' .. common_string('and') .. ' ' .. item_link('Strange')) end,
  ['keyless-crate-unusual'] = function() return '[[Mann Co. Supply Crate/Seriesless crates' .. if_lang .. '#Unlocked Cosmetic Crates|' .. item_name('unlocked cosmetic crate') .. ']]' .. paren(item_link('Unique') .. comma() .. item_link('Strange') .. ' ' .. common_string('and') .. ' ' .. item_link('Unusual')) end,
  ['mannup'] = function() return '[[Mann Up' .. if_lang .. '|' .. common_string('generic reward') .. ']]' end,
  ['mvm'] = function() return '[[Mann vs. Machine' .. if_lang .. '|' .. common_string('mvm reward') .. ']]' end,
  ['mvm-both-types'] = function() return '[[Mann Up' .. if_lang .. '|' .. common_string('generic reward') .. ']]' .. paren('[[Botkiller weapons' .. if_lang .. '|' .. common_string('botkiller') .. ']] ' .. common_string('and') .. ' [[Australium weapons' .. if_lang .. '|' .. common_string('australium') .. ']]') end,
  ['pallet-of-crates'] = function() return item_link('Pallet of Crates') end,
  ['pile-of-ash'] = function() return common_string('pile of ash-expired') end,
  ['pile-of-curses'] = function() return common_string('pile of curses-expired') end,
  ['promotional'] = function() return '[[Promotional items' .. if_lang .. '|' .. common_string('promotional') .. ']]' end,
  ['promotional-craft'] = function() return common_string('promotional-craft') end,
  ['promotional-expired'] = function() return common_string('promotional-expired') end,
  ['promotional-expired-codes'] = function() return common_string('promotional-expired-codes') end,
  ['promotional-unlock'] = function() return common_string('promotional-unlock') end,
  ['purchase'] = function() return '[[Mann Co. Store' .. if_lang .. '|' .. common_string('purchase') .. ']]' end,
  ['purchase-limited'] = function() return common_string('purchase-limited') end,
  ['retired'] = function() return common_string('retired') end,
  ['stock'] = function() return '[[Stock weapons' .. if_lang .. '|' .. mw.getContentLanguage():ucfirst(item_name('stock')) .. ']]' end,
  ['stockpile'] = function() return '[[Mann Co. Supply Crate/Seriesless crates' .. if_lang .. '#Mann Co. Stockpile Crate|' .. common_string('stockpile crate') .. ']]' end,
  ['trade-up'] = function() return '[[Trade-Up' .. if_lang .. '|' .. common_string('trade-up') .. ']]' end,
  ['transmute'] = function() return '[[Halloween Transmute' .. if_lang .. '|' .. common_string('halloween transmute') .. ']] ' .. paren('[[Trading' .. if_lang .. '|' .. common_string('Untradable') .. ']]') end,
  ['unavailable'] = function() return common_string('unavailable') end,
  ['uncrate-bonus'] = function(num) return '[[Mann Co. Supply Crate' .. if_lang .. '|' .. common_string('uncrate') .. ']]' .. paren(common_string('uncrate-bonus')) end,
  ['unlock'] = function() return '[[Achievement items' .. if_lang .. '|' .. common_string('unlock') .. ']]' end,
  ['unusual'] = function() return '[[Mann Co. Supply Crate' .. if_lang .. '|' .. common_string('uncrate') .. ']]' .. paren(item_link('Unusual')) end,
  ['unusual-expired'] = function() return '[[Mann Co. Supply Crate' .. if_lang .. '|' .. common_string('uncrate') .. ']]' .. paren(item_link('Unusual')) .. ' ' .. common_string('unusual-expired') end,
  ['warpaint'] = function() return item_link('War Paint') .. paren(item_link('Decorated')) end,

  -- Multipart strings (with a number)
  ['case'] = function(num) return '[[' .. case_link(num) .. if_lang .. '#' .. num .. '|'  .. item_name('case ' .. num) .. ']]' end,
  ['case-bonus'] = function(num) return '[[' .. case_link(num) .. if_lang .. '#' .. num .. '|' .. item_name('case ' .. num) .. ']]' .. paren(common_string('uncrate-bonus')) end,
  ['crate'] = function(num) return '[[' .. crate_link(num) .. if_lang .. '#' .. num .. '|' .. common_string('uncrate') .. ' #' .. num .. ']]' end,
  ['crate-strange'] = function(num) return '[[' .. crate_link(num) .. if_lang .. '#' .. num .. '|' .. common_string('uncrate') .. ' #' .. num .. ']]' .. paren(item_link('Strange')) end,
  ['crate-festive'] = function(num) return '[[' .. crate_link(num) .. if_lang .. '#' .. num .. '|' .. common_string('uncrate') .. ' #' .. num .. ']]' .. paren('[[Festive weapons' .. if_lang .. '|' .. common_string('festive') .. ']]') .. ' ' .. common_string('uncrate-expired') end,
  ['crate-haunted'] = function(num) return '[[' .. crate_link(num) .. if_lang .. '#' .. num .. '|' .. common_string('uncrate') .. ' #' .. num .. ']]' .. paren(item_link('Haunted')) .. ' ' .. common_string('uncrate-expired') end,
  ['crate-bonus'] = function(num) return '[[' .. crate_link(num) .. if_lang .. '#' .. num .. '|' .. common_string('uncrate') .. ' #' .. num .. ']]' .. paren(common_string('uncrate-bonus')) end,
  ['crate-expired'] = function(num) return '[[' .. crate_link(num) .. if_lang .. '#' .. num .. '|' .. common_string('uncrate') .. ' #' .. num .. ']] ' .. common_string('uncrate-expired') end,
  ['unusual-'] = function(num) return '[[Mann Co. Supply Crate' .. if_lang .. '|' .. common_string('uncrate') .. ']]' .. paren('[[Unusual' .. if_lang .. '#Group ' .. num .. '|' .. item_name('unusual') .. ']]') end,
  ['xmas'] = function(num) return '[[Smissmas ' .. num .. ' Festive Gift' .. if_lang .. '|' .. item_name('smissmas ' .. num .. ' festive gift') .. ']]' end,
  ['winter'] = function(num) return '[[Mann Co. Supply Crate/Active series' .. if_lang .. '#Unlocked Winter ' .. num .. ' Cosmetic Case|' .. item_name('unlocked winter ' .. num .. ' cosmetic case') .. ']]' end,
  ['winter-strange'] = function(num) return '[[Mann Co. Supply Crate/Active series' .. if_lang .. '#Unlocked Winter ' .. num .. ' Cosmetic Case|' .. item_name('unlocked winter ' .. num .. ' cosmetic case') .. ']]' .. paren('[[Unique' .. if_lang .. '|' .. item_name('unique') .. ']] ' .. common_string('and') .. ' [[Strange' .. if_lang .. '|' .. item_name('strange') .. ']]') end,
  ['winter-unusual'] = function(num) return '[[Mann Co. Supply Crate/Active series' .. if_lang .. '#Unlocked Winter ' .. num .. ' Cosmetic Case|' .. item_name('unlocked winter ' .. num .. ' cosmetic case') .. ']]' .. paren('[[Unique' .. if_lang .. '|' .. item_name('unique') .. ']]' .. comma() .. '[[Strange' .. if_lang .. '|' .. item_name('strange') .. ']] ' .. common_string('and') .. ' [[Unusual' .. if_lang .. '|' .. item_name('unusual') .. ']]') end,
}

-- Some older strings which have been updated (but should continue to work until they're replaced). Using these strings adds the page to a tracking category.
local backwards_compat = {
  ['case-bonus'] = 'uncrate-bonus', -- Only applies if there is no number provided, otherwise the dynamic case-bonus is used
  ['crate-scorched'] = 'crate46',
  ['crate74'] = 'crate74-haunted',
  ['crate74-expired'] = 'crate74-haunted',
  ['xmas2015'] = 'festive-gift',
  ['winter2016'] = 'crate105',
}

local backwards_compat_dynamic = {
  ['winter'] = 'winter',
  ['unusual-'] = 'unusual'
}

function p.avail(f)
  frame = f
  local args = f:getParent().args
  count = 0
  for _ in pairs(args) do count = count + 1 end
  if count == 0 then -- There is no other way to count table length in lua.
    args = f.args
  end
  -- Precompute the page's language.
  if_lang = frame:expandTemplate{title='If lang'}

  -- Avail is called with a long list of arguments. Each one needs to be translated and comma-separated.
  avail_string = ''
  for i, arg in ipairs(args) do
    arg = mw.ustring.lower(arg)

    -- Backwards compatibility for static strings
    if backwards_compat[arg] ~= nil then
      avail_string = '[[Category:Outdated Availability Parameters]]' .. avail_string
      arg = backwards_compat[arg]
    end

    -- Matches strings like 'craft' 'crate1' 'crate26-strange'
    -- Strings like 'crate26-strange' become 'crate-strange' and '26'.
    name1, num, name2 = string.match(arg, '(%D*)(%d*)(%D*)')
    name = name1 .. name2
    
    -- Backwards compatibility for dynamic strings
    if backwards_compat_dynamic[name] ~= nil then
      avail_string = '[[Category:Outdated Availability Parameters]]' .. avail_string
      name = backwards_compat_dynamic[name]
    end

    assert(lookup[name], 'Passed an invalid string to [[Module:Availability]]: "' .. name .. '".\nPlease do not translate these strings, they will be translated automatically')
    if (args[i+2] ~= nil)
    or (args[i+1] ~= nil and args['availability'] ~= nil)
      then -- There are at least two arguments left in the list
      avail_string = avail_string .. lookup[name](num) .. comma()
    elseif (args[i+1] ~= nil and args['availability'] == nil)
    or (args[i+1] == nil and args['availability'] ~= nil)
      then -- There is exactly one more argument in the list
      avail_string = avail_string .. lookup[name](num) .. ' ' .. common_string('or') .. ' '
    else -- args[i+1] == nil and args['availability] == nil
      avail_string = avail_string .. lookup[name](num)
    end
  end -- End looping through arguments.

  -- Custom availability strings are handled last, since they should always go at the end of the template output.
  if args['availability'] ~= nil then
    avail_string = avail_string .. args['availability'] .. '[[Category:Custom Availability Parameters]]'
  end

  return avail_string
end

return p