Module:Sandbox/Abcboy

Documentation for this module may be created at Module:Sandbox/Abcboy/doc

local p = {}

-- Table of supported game names.
local gameArticle = {
  -- Generation I
  ["Red (Japanese)"] = "Pokémon Red and Green Versions",
  ["Green"] = "Pokémon Red and Green Versions",
  ["Blue (Japanese)"] = "Pokémon Blue Version (Japanese)",
  ["Red"] = "Pokémon Red and Blue Versions",
  ["Blue"] = "Pokémon Red and Blue Versions",
  ["Yellow"] = "Pokémon Yellow Version",
  
  -- Generation II
  ["Gold"] = "Pokémon Gold and Silver Versions",
  ["Silver"] = "Pokémon Gold and Silver Versions",
  ["Crystal"] = "Pokémon Crystal Version",
  
  -- Generation III
  ["Ruby"] = "Pokémon Ruby and Sapphire Versions",
  ["Sapphire"] = "Pokémon Ruby and Sapphire Versions",
  ["FireRed"] = "Pokémon FireRed and LeafGreen Versions",
  ["LeafGreen"] = "Pokémon FireRed and LeafGreen Versions",
  ["Emerald"] = "Pokémon Emerald Version",
  
  -- Generation IV
  ["Diamond"] = "Pokémon Diamond and Pearl Versions",
  ["Pearl"] = "Pokémon Diamond and Pearl Versions",
  ["Platinum"] = "Pokémon Platinum Version",
  ["HeartGold"] = "Pokémon HeartGold and SoulSilver Versions",
  ["SoulSilver"] = "Pokémon HeartGold and SoulSilver Versions",
  
  -- Generation V
  ["Black"] = "Pokémon Black and White Versions",
  ["White"] = "Pokémon Black and White Versions",
  ["Black 2"] = "Pokémon Black and White Versions 2",
  ["White 2"] = "Pokémon Black and White Versions 2",
  
  -- Generation VI
  ["X"] = "Pokémon X and Y",
  ["Y"] = "Pokémon X and Y",
  ["Omega Ruby"] = "Pokémon Omega Ruby and Alpha Sapphire",
  ["Alpha Sapphire"] = "Pokémon Omega Ruby and Alpha Sapphire",
  
  -- Generation VII
  ["Sun"] = "Pokémon Sun and Moon",
  ["Moon"] = "Pokémon Sun and Moon",
  ["Ultra Sun"] = "Pokémon Ultra Sun and Ultra Moon",
  ["Ultra Moon"] = "Pokémon Ultra Sun and Ultra Moon",
  ["Let's Go, Pikachu!"] = "Pokémon: Let's Go, Pikachu! and Let's Go, Eevee!",
  ["Let's Go, Eevee!"] = "Pokémon: Let's Go, Pikachu! and Let's Go, Eevee!",
  
  -- Generation VIII
  ["Sword"] = "Pokémon Sword and Shield",
  ["Shield"] = "Pokémon Sword and Shield",
  ["Brilliant Diamond"] = "Pokémon Brilliant Diamond and Shining Pearl",
  ["Shining Pearl"] = "Pokémon Brilliant Diamond and Shining Pearl",
  -- ["Legends: Arceus"] = "Pokémon Legends: Arceus",
  ["Arceus"] = "Pokémon Legends: Arceus",
  
  -- Generation IX
  ["Scarlet"] = "Pokémon Scarlet and Violet",
  ["Violet"] = "Pokémon Scarlet and Violet",
  -- ["Legends: Z-A"] = "Pokémon Legends: Z-A",
  ["Z-A"] = "Pokémon Legends: Z-A",
  
  -- Side series
  ["Stadium (Japanese)"] = "Pokémon Stadium (Japanese)",
  -- ["Stadium"] = "Pokémon Stadium",
  -- ["Stadium 2"] = "Pokémon Stadium 2",
  ["Box"] = "Pokémon Box Ruby & Sapphire",
  -- ["Colosseum"] = "Pokémon Colosseum",
  ["XD"] = "Pokémon XD: Gale of Darkness",
  -- ["Battle Revolution"] = "Pokémon Battle Revolution",
  ["Ranch"] = "My Pokémon Ranch",
  -- ["Bank"] = "Pokémon Bank",
  ["Transporter"] = "Poké Transporter",
  -- ["HOME"] = "HOME",
  
  -- Pokémon Mystery Dungeon
  ["Red Rescue Team"] = "Pokémon Mystery Dungeon: Red Rescue Team and Blue Rescue Team",
  ["Blue Rescue Team"] = "Pokémon Mystery Dungeon: Red Rescue Team and Blue Rescue Team",
  ["Explorers of Time"] = "Pokémon Mystery Dungeon: Explorers of Time and Explorers of Darkness",
  ["Explorers of Darkness"] = "Pokémon Mystery Dungeon: Explorers of Time and Explorers of Darkness",
  ["Explorers of Sky"] = "Pokémon Mystery Dungeon: Explorers of Sky",
  ["Keep Going! Blazing Adventure Squad"] = "Pokémon Mystery Dungeon (WiiWare)",
  ["Let's Go! Stormy Adventure Squad"] = "Pokémon Mystery Dungeon (WiiWare)",
  ["Go For It! Light Adventure Squad"] = "Pokémon Mystery Dungeon (WiiWare)",
  ["Gates to Infinity"] = "Pokémon Mystery Dungeon: Gates to Infinity",
  -- ["Super Mystery Dungeon"] = "Pokémon Super Mystery Dungeon",
  ["Rescue Team DX"] = "Pokémon Mystery Dungeon: Rescue Team DX",
  
  -- Ranger
  ["Ranger"] = "Pokémon Ranger (video game)",
  ["Shadows of Almia"] = "Pokémon Ranger: Shadows of Almia",
  ["Guardian Signs"] = "Pokémon Ranger: Guardian Signs",
}

-- Returns the title of the article for the given game.
-- If not in the table, returns "Pokémon <game>".
function getArticle(game)
  return gameArticle[game] or ("Pokémon " .. game)
end

-- Table of supported game prefixes.
-- Make sure to include the trailing space.
local gamePrefix = {
  -- Core series
  ["Let's Go, Pikachu!"] = "Pokémon: ",
  ["Let's Go, Eevee!"] = "Pokémon: ",
  ["Arceus"] = "Pokémon Legends: ",
  ["Z-A"] = "Pokémon Legends: ",
  
  -- Side series
  ["Ranch"] = "My Pokémon ",
  ["Transporter"] = "Poké ",
  
  -- Pokémon Mystery Dungeon
  ["Red Rescue Team"] = "Pokémon Mystery Dungeon: ",
  ["Blue Rescue Team"] = "Pokémon Mystery Dungeon: ",
  ["Explorers of Time"] = "Pokémon Mystery Dungeon: ",
  ["Explorers of Darkness"] = "Pokémon Mystery Dungeon: ",
  ["Explorers of Sky"] = "Pokémon Mystery Dungeon: ",
  ["Keep Going! Blazing Adventure Squad"] = "Pokémon Mystery Dungeon: ",
  ["Let's Go! Stormy Adventure Squad"] = "Pokémon Mystery Dungeon: ",
  ["Go For It! Light Adventure Squad"] = "Pokémon Mystery Dungeon: ",
  ["Gates to Infinity"] = "Pokémon Mystery Dungeon: ",
  ["Rescue Team DX"] = "Pokémon Mystery Dungeon: ",
  
  -- Ranger
  ["Shadows of Almia"] = "Pokémon Ranger: ",
  ["Guardian Signs"] = "Pokémon Ranger: ",
}

-- Returns the prefix in the title of the article for the given game.
-- If not in the table, returns "Pokémon ".
function getPrefix(game)
  return gamePrefix[game] or "Pokémon "
end

-- Returns a link to the given article, with optional display text.
-- Any parentheticals are removed.
function makeLink(article, disp)
  disp = (disp or article):gsub(" %(.-%)", "")
  if article == disp then
    return "[[" .. article .. "]]"
  end
  return "[[" .. article .. "|" .. disp .. "]]"
end

-- Returns a link to a single game's article, with optional display text.
function makeLinkSingle(game, prefix, disp)
  if disp == nil then
    if prefix == nil then
      disp = game
    else
      disp = prefix .. game
    end
  end
  return makeLink(getArticle(game), disp)
end

-- Returns a link to a pair of games' article, with optional display text.
function makeLinkPair(article, game1, game2, prefix, disp)
  if disp == nil then
    if prefix == nil then
      disp = game1 .. " and " .. game2
    else
      disp = prefix .. game1 .. " and " .. game2
    end
  end
  return makeLink(article, disp)
end

-- Returns links to two games, in the format "A and B".
function makeLinksTwo(games, prefix, disp)
  -- If paired versions, return a single link
  local article1 = getArticle(games[1])
  local article2 = getArticle(games[2])
  if article1 == article2 then
    return makeLinkPair(article1, games[1], games[2], prefix, disp)
  end
  
  -- Unrelated games, return separate links
  return makeLinkSingle(games[1], prefix, disp) .. " and " .. makeLinkSingle(games[2])
end

-- Returns links to three or more games, in the format "A, B, ..., Y, and Z".
function makeLinksMany(games, prefix, disp)
  local t = { }
  for i, game in pairs(games) do
    if i == 1 then
      t[i] = makeLinkSingle(game, prefix, disp)
    else
      t[i] = makeLinkSingle(game)
    end
  end
  return table.concat(t, ", ", 1, #t - 1) .. ", and " .. t[#t]
end

function p._game(count, games, prefixFlag, disp)
  -- Handle no games.
  if count == 0 then
    return ""
  end
  
  -- If "Red and Green" or "Red, Green, and Blue" is provided,
  -- ensure that Red and Blue link to the Japanese version.
  if games[1] == "Red" and games[2] == "Green" then
    games[1] = "Red (Japanese)"
    if games[3] == "Blue" then
      games[3] = "Blue (Japanese)"
    end
  end
  
  -- Look up prefix for first game.
  local prefix = nil
  if prefixFlag then
    prefix = getPrefix(games[1])
  end
  
  -- Treat empty disp as nil.
  if disp == "" then
    disp = nil
  end
  
  -- Call helper based on the number of games provided.
  if count == 1 then
    return makeLinkSingle(games[1], prefix, disp)
  elseif count == 2 then
    return makeLinksTwo(games, prefix, disp)
  else
    return makeLinksMany(games, prefix, disp)
  end
end

function p.game(frame)
  -- Grab all game names from unnamed parameters in order.
  local templateArgs = frame:getParent().args
  local games = {}
  local i = 1
  while templateArgs[i] do
    games[i] = templateArgs[i]
    i = i + 1
  end
  local count = i - 1
  
  -- Optional display text for the first link.
  local disp = templateArgs.disp
  return p._game(count, games, true, disp)
end

function p.v(frame)
  -- Grab all game names from unnamed parameters in order.
  local templateArgs = frame:getParent().args
  local games = {}
  local i = 1
  while templateArgs[i] do
    games[i] = templateArgs[i]
    i = i + 1
  end
  local count = i - 1
  
  -- Optional display text for the first link.
  local disp = templateArgs.disp
  return p._game(count, games, false, disp)
end

return p