Module:Pokémon matchup

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Module documentation[create]
local p={}
local TableTools = require('Module:TableTools')

function get_matchup(type)
  local out={} out.immunities={} out.resistances={} out.weaknesses={}

  if type=="normal" then out.weaknesses={"fighting"} out.immunities={"ghost"} end

  if type=="fire" then out.weaknesses={"water","ground","rock"} out.resistances={"fire","grass","ice","bug","steel","fairy"} end

  if type=="water" then out.weaknesses={"grass","electric"} out.resistances={"fire","water","ice","steel"} end

  if type=="grass" then out.weaknesses={"fire","ice","poison","flying","bug"} out.resistances={"water","grass","electric","ground"} end

  if type=="electric" then out.weaknesses={"ground"} out.resistances={"electric","flying","steel"} end

  if type=="ice" then out.weaknesses={"fire","fighting","rock","steel"} out.resistances={"ice"} end

  if type=="fighting" then out.weaknesses={"flying","psychic","fairy"} out.resistances={"bug","rock","dark"} end

  if type=="poison" then out.weaknesses={"ground","psychic"} out.resistances={"grass","fighting","poison","bug","fairy"} end

  if type=="ground" then out.weaknesses={"water","grass","ice"} out.resistances={"poison","rock"} out.immunities={"electric"} end

  if type=="flying" then out.weaknesses={"electric","ice","rock"} out.resistances={"grass","fighting","bug"} out.immunities={"ground"} end

  if type=="psychic" then out.weaknesses={"bug","ghost","dark"} out.resistances={"fighting","psychic"} end

  if type=="bug" then out.weaknesses={"fire","flying","rock"} out.resistances={"grass","fighting","ground"} end

  if type=="rock" then out.weaknesses={"water","grass","fighting","ground","steel"} out.resistances={"normal","fire","poison","flying"} end

  if type=="ghost" then out.weaknesses={"ghost","dark"} out.resistances={"poison","bug"} out.immunities={"normal","fighting"} end

  if type=="dragon" then out.weaknesses={"ice","dragon","fairy"} out.resistances={"fire","water","grass","electric"} end

  if type=="dark" then out.weaknesses={"fighting","bug","fairy"} out.resistances={"ghost","dark"} out.immunities={"psychic"} end

  if type=="steel" then out.weaknesses={"fire","fighting","ground"} out.resistances={"normal","grass","ice","flying","psychic","bug","rock","dragon","steel","fairy"} out.immunities={"poison"} end

  if type=="fairy" then out.weaknesses={"poison","steel"} out.resistances={"fighting","bug","dark"} out.immunities={"dragon"} end

  return out
end

function p.matchup(frame)
  local out1=""
  local out2=""
  local immunities=get_matchup(frame.args.type1).immunities
  local all_weaknesses=get_matchup(frame.args.type1).weaknesses
  local all_resistances=get_matchup(frame.args.type1).resistances
  -- Get matchups and combine them:
  for _,v in pairs(get_matchup(frame.args.type2).weaknesses) do
    all_weaknesses[TableTools.size(all_weaknesses)+1]=v
  end
  for _,v in pairs(get_matchup(frame.args.type2).resistances) do
    all_resistances[TableTools.size(all_resistances)+1]=v
  end
  for _,v in pairs(get_matchup(frame.args.type2).immunities) do
    immunities[TableTools.size(immunities)+1]=v
  end

  -- remove duplicates
 -- local x4weaknesses={}
 -- local x4resistances={}
 -- for _,v in pairs(all_weaknesses) do
 --   
 -- end
  all_weaknesses=TableTools.removeDuplicates(all_weaknesses)
  all_resistances=TableTools.removeDuplicates(all_resistances)
  immunities=TableTools.removeDuplicates(immunities)

  --categories
  local cats=""
  if #frame.args.type1>0 and #frame.args.type2>0 and frame.args.namespace=="" then


    if frame.args.type1>frame.args.type2 then cats="[[Category:Book:Pokémon/"..frame:expandTemplate{
      title="UpperCase first",
      args={frame.args.type1}
    }.."-"..frame:expandTemplate{
      title="UpperCase first",
      args={frame.args.type2}
    }.." pokémon]]"


    else cats="[[Category:Book:Pokémon/"..frame:expandTemplate{
      title="UpperCase first",
      args={frame.args.type2}
    }.."-"..frame:expandTemplate{
      title="UpperCase first",
      args={frame.args.type1}
    }.." Pokémon]]"
    end
  end

  -- Remove cancelled out matchups
  for k1,v1 in pairs(all_resistances) do
    for k2,v2 in pairs(all_weaknesses) do
      if v1==v2 then
        all_weaknesses[k2]="removed"
        all_resistances[k1]="removed"
      end
      for _,v3 in pairs(immunities) do
        if v1==v3 then
          all_resistances[k1]="removed"
        end if v2==v3 then
          all_weaknesses[k2]="removed"
        end
      end
    end
  end

  -- Get output
  for _,v in pairs(all_weaknesses) do if not(v=="removed") then
    if #out1>0 then
      out1=out1 .. ", " .. v
    else
      out1= v
    end
  end end
  for _,v in pairs(all_resistances) do if not(v=="removed") then
    if #out2>0 then
      out2=out2 .. ", " .. v
    else
      out2=v
    end
  end end
  local immunityText=""
  for _,v in pairs(immunities) do
    if #immunityText>0 then
      immunityText=immunityText .. ", " ..  v
    else
      immunityText="Move types that deal no damage to "..frame.args.pagename..": "..v
    end
  end
  
  local outText=""
  if #out1>0 then outText="Move types that deals more damage to "..frame.args.pagename..": " .. out1 ..".<br>" end
  if #out2>0 then outText=outText .. "Move types that deal less damage to "..frame.args.pagename..": " .. out2 .. ".<br>" end
  outText=outText .. immunityText .. "<br>"
  return outText..cats
end

return p