Module:Experience

--
 -- All functions relating to experience needed etc
 -- 
  
 local p 
 = {}
 local commas 
 = require('Module:Addcommas')
  
 local XPAtLevel 
 = mw.loadData('Module:Experience/data')
 local XPAtLevelUnr 
 = mw.loadData('Module:Experience/dataunr')
  
 --
 -- Returns lowest needed XP to have required level
 -- Template:XP
 -- 
 function p.xp_at_level(frame)
 local level 
 = tonumber(frame.args[1])
  
 -- calling tonumber on a string returns nil
 if not level then
 return "You must pass numbers as arguments"
 end
  
 return XPAtLevel[level]
 end
  
 -- Same as above, but with the 120 level cap removed
  
 function p.xp_at_level_unr(frame)
 local level 
 = tonumber(frame.args[1])
  
 -- calling tonumber on a string returns nil
 if not level then
 return "You must pass numbers as arguments"
 end
  
 return XPAtLevelUnr[level]
 end
  
 --
 -- The maximum amount of experience you can have at a given level
 -- Template:Max XP for level
 --
 function p.max_xp_at_level(frame)
 local level 
 = tonumber(frame.args[1])
  
 -- calling tonumber on a string returns nil
 if not level then
 return "You must pass numbers as arguments"
 end
  
 if (level <= 0 or level > 120) then
 return "Value must be in the range 1 - 120"
 -- Highest level is 120, highest XP is 200M
 end
  
 if (level 
 == 120) then
 return 200000000
 end
  
 -- Just in case
 level 
 = math.floor(level)
  
 -- Return XP required for the *next* level minus 1
 local xp_at_next 
 = XPAtLevel[level + 1]
 return xp_at_next - 1
 end
  
 --
 -- What level you have when at a given xp
 -- Template:Level
 --
 function p.level_at_xp(frame)
 local xp 
 = frame.args[1]
  
 -- just in case
 if type(xp) 
 == 'string' then
 xp 
 = commas._strip(xp)
 xp 
 = tonumber(xp)
 end
  
 if (xp < 0 or xp > 200000000) then
 return "Value must be in the range 0 - 200,000,000"
 end
  
 -- calling tonumber on a string returns nil
 if not xp then
 return "You must pass numbers as arguments"
 end
  
 local level
 for i
 =120,1,-1 do
 if (XPAtLevel[i]<= xp) then
 level 
 = i
 break
 end
 end
  
 return level
 end
  
 -- Same as above, but with the 120 level cap removed
  
 function p.level_at_xp_unr(frame)
 local xp 
 = frame.args[1]
  
 -- just in case
 if type(xp) 
 == 'string' then
 xp 
 = commas._strip(xp)
 xp 
 = tonumber(xp)
 end
  
 if (xp < 0 or xp > 200000000) then
 return "Value must be in the range 0 - 200,000,000"
 end
  
 -- calling tonumber on a string returns nil
 if not xp then
 return "You must pass numbers as arguments"
 end
  
 local level
 for i
 =126,1,-1 do
 if (XPAtLevelUnr[i]<= xp) then
 level 
 = i
 break
 end
 end
  
 return level
 end
  
 --
 -- Experience needed to obtain a level from a given amount of xp
 -- If current xp is greater than target, returns 0
 -- Template:XP to Level
 --
 function p.xp_to_level(frame)
 local xp 
 = frame.args[1]
 local target 
 = tonumber(frame.args[2])
  
 -- just in case
 if type(xp) 
 == 'string' then
 xp 
 = commas._strip(xp)
 xp 
 = tonumber(xp)
 end
  
 if (xp < 0 or xp > 200000000) then
 return "XP value must be in the range 0 - 200,000,000"
 end
  
 if (target < 2 or target > 120) then
 return "Level value must be in the range 2 - 120"
 end
  
 -- calling tonumber on a string returns nil
 if not xp or not target then
 return "You must pass numbers as arguments"
 end
  
 local target_xp 
 = XPAtLevel[target]
  
 if (xp >= target_xp) then
 return 0
 else
 return target_xp - xp
 end
 end
  
 --
 -- Experience between two levels; will always return absolute value
 -- Template:XP level to level
 --
 function p.level_to_level(frame)
 local level1 
 = tonumber(frame.args[1])
 local level2 
 = tonumber(frame.args[2])
  
 -- just in case
 if type(level1) 
 == 'string' then
 level1 
 = commas._strip(level1)
 level1 
 = tonumber(level1)
 end
  
 if type(level2) 
 == 'string' then
 level2 
 = commas._strip(level2)
 level2 
 = tonumber(level2)
 end
  
 if not level1 or not level2 then
 return "You must pass numbers as arguments"
 end
  
 local difference 
 = XPAtLevel[level1]- XPAtLevel[level2]
 difference 
 = math.abs(difference)
 return difference
 end
  
 -- Same as above, but with the 120 level cap removed
  
 function p.level_to_level_unr(frame)
 local level1 
 = tonumber(frame.args[1])
 local level2 
 = tonumber(frame.args[2])
  
 -- just in case
 if type(level1) 
 == 'string' then
 level1 
 = commas._strip(level1)
 level1 
 = tonumber(level1)
 end
  
 if type(level2) 
 == 'string' then
 level2 
 = commas._strip(level2)
 level2 
 = tonumber(level2)
 end
  
 if not level1 or not level2 then
 return "You must pass numbers as arguments"
 end
  
 local difference 
 = XPAtLevelUnr[level1]- XPAtLevelUnr[level2]
 difference 
 = math.abs(difference)
 return difference
 end
  
 return p