Editing Module:Rates/Sandbox

From Ephinea PSO Wiki
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 23: Line 23:


--
--
-- Multiply a DAR and RDR together. Any number works but it's basically just for those
-- Rate calculation accessed through #invoke
--
--
-- @param dar {number}
-- @param frame {table}
-- @param rdr {number}
-- @return {number}
-- @return {number}
--
--
local function calcRate(dar, rdr)
function p.calcRate(frame)
  -- Separate the two numbers passed in as strings
   local mult = {}
   local mult = {}
   mult[1] = dar
   mult[1] = frame.args[1]
   mult[2] = rdr
   mult[2] = frame.args[2]
   -- Initialize numbers for string conversion
   -- Initialize numbers for string conversion
   local frac = {}
   local frac = {}
Line 48: Line 48:
     end
     end
   end
   end
   return (frac[1][1]/frac[1][2])*(frac[2][1]/frac[2][2])
   -- These probably don't need to be redefined, but I'm still super new to Lua so I'll come back to it later xd
  local num1 = frac[1][1]
  local den1 = frac[1][2]
  local num2 = frac[2][1]
  local den2 = frac[2][2]
  return (num1/den1)*(num2/den2)
end
end


Line 61: Line 66:
local function fmtRate(rate, fmt, digits)
local function fmtRate(rate, fmt, digits)
   local out = ""
   local out = ""
  -- Don't want nil comparisons or whatever
  if digits == nil then
    digits = 0
  end
   local frac = {}
   local frac = {}
   -- If either number is a fraction, split it up into numerator and denominator
   -- If either number is a fraction, split it up into numerator and denominator
Line 77: Line 86:
   if fmt == "f" then
   if fmt == "f" then
     if wholeFractions[rate] == nil then
     if wholeFractions[rate] == nil then
      if not digits then
        digits = 0
      end
       -- Set maximum number of decimal places
       -- Set maximum number of decimal places
       if 1/rate < 10 then
       if rate < 10 then
         -- Displays with two places between 1.01 ~ 9.99
         -- Displays with two places between 1.01 ~ 9.99
         digits = math.max(digits, 2)
         digits = math.max(digits, 2)
       elseif 1/rate < 100 then
       elseif rate < 100 then
         -- Displays with one place between 10.1 ~ 99.9
         -- Displays with one place between 10.1 ~ 99.9
         digits = math.max(digits, 1)
         digits = math.max(digits, 1)
      else
        -- Otherwise just displays the whole number at 100+
        digits = math.max(digits, 0)
       end
       end
       -- Even if we're displaying one or more decimal places, cut zeroes off
       -- Even if we're displaying one or more decimal places, cut zeroes off
Line 113: Line 122:
   local rate = {}
   local rate = {}
   rate[1] = frame.args[2]
   rate[1] = frame.args[2]
   if frame.args[3] == nil then
   rate[2] = frame.args[3]
    rate[2] = 1
  else
    rate[2] = frame.args[3]
  end
  if rate[1] == "0" or rate[2] == "0" then
    return nil
  end
   local out = ""
   local out = ""
   if fmt == "f" then
   if fmt == "f" then
     -- Fraction
     -- Fraction
     out = out .. fmtRate(rate[1], "f")
     out = out .. fmtRate(rate[1], "f", 2)
   elseif fmt == "p" then
   elseif fmt == "p" then
     -- Percentage
     -- Percentage
Line 133: Line 135:
     span
     span
       :addClass("more_info")
       :addClass("more_info")
       :attr("title", string.format("Drop Rate: %s (%s), Rare Rate: %s (%s)", fmtRate(rate[1], "f"), fmtRate(rate[1], "p"), fmtRate(rate[2], "f", 2), fmtRate(rate[2], "p")))
       :wikitext(string.format("%s %s", rate[1], rate[2]))
      --:attr("title", string.format("Drop Rate: %s (%s)&#010;Rare Rate: %s (%s)", fmtRate(rate[1], "f", 2), fmtRate(rate[1], "p"), fmtRate(rate[2], "f", 2), fmtRate(rate[2], "p")))
       :css("border-bottom", "1px dotted")
       :css("border-bottom", "1px dotted")
       :wikitext(string.format("%s", fmtRate(calcRate(rate[1], rate[2]), "f")))
       --:wikitext(string.format("%s", fmtRate(p.calcRate(rate[1], rate[2]))))
     --out = out .. fmtRate(rate, "f", 2) .. " (" .. fmtRate(rate, "p") .. ")"  
     --out = out .. fmtRate(rate, "f", 2) .. " (" .. fmtRate(rate, "p") .. ")"  
     return span:allDone()
     return span:allDone()
Please note that all contributions to Ephinea PSO Wiki are considered to be released under the CC BY-NC-SA 4.0 (see Ephinea PSO Wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)