Animal Crossing: Pocket Camp Wiki
Advertisement
Animal Crossing: Pocket Camp Wiki

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

-- This module blackboxes several Wiki operations (eg. Fetching skill info)
-- and is intended for use by other Lua modules. 
-- This module should not be called from #invoke directly.

local p = {}

local cargo = mw.ext.cargo

----------------------------------
----------------------------------
-- Misc. Wiki-related functions
----------------------------------
----------------------------------

--- Add a tooltip with content tooltipText to targetText
-- @param targetText The visible text
-- @param tooltipText The text that will appear when hovering the visible text
-- @return Text with tooltip
function p.createTooltipWithText(targetText, tooltipText)
  local div = mw.html.create('div')
    :addClass('tooltip')
    :css('border-bottom', '0')

  local contentDiv = mw.html.create('div')
    :wikitext(targetText)

  local tooltip = mw.html.create('span')
    :addClass('tooltiptext')
    :wikitext(tooltipText)

  div:node(tooltip)
  div:node(contentDiv)

  return div
end

--- Add a tooltip with content tooltipText to targetElement
-- @param targetElement The visible element (eg. images)
-- @param tooltipText The text that will appear when hovering the visible text
-- @return Element with tooltip
function p.createTooltipWithElement(targetElement, tooltipText)
  local div = mw.html.create('div')
    :addClass('tooltip')
    :css('border-bottom', '0')

  local tooltip = mw.html.create('span')
    :addClass('tooltiptext')
    :wikitext(tooltipText)

  div:node(tooltip)
  div:node(targetElement)

  return div
end

----------------------------------
----------------------------------
-- Misc. General functions
----------------------------------
----------------------------------

--- Checks if the specified value is nil or empty
-- @param val
-- @return True/false depending on whether the value provided is empty or not
function p.isNilOrEmpty(val)
    return (val == nil or val == '')
end

--- Returns '-' if the specified value is nil or empty
-- @param val
-- @return Return '-' or the value
function p.returnDefaultIfEmpty(val)
  if p.isNilOrEmpty(val) then
    return '-'
  end
  return val
end

--- Removes whitespaces from the string specified
-- Copied from http://lua-users.org/wiki/StringTrim
-- @param The trimmed string
function trim(s)
  return (s:gsub("^%s*(.-)%s*$", "%1"))
end

--- Splits a string based on the provided separator
-- Copied from http://lua-users.org/wiki/SplitJoin
-- strsplit(seperator, string)
-- @param d The character we want to separate on
-- @param p The string we want to split
-- @return Table of the separated values
function p.strsplit(d,p)
   if p == nil then return '' end
   local t, ll
   t={}
   ll=0
   if(#p == 1) then
      return {p}
   end
   while true do
      l = string.find(p, d, ll, true) -- find the next d in the string
      if l ~= nil then -- if "not not" found then..
         table.insert(t, string.sub(p,ll,l-1)) -- Save it in our array.
         ll = l + 1 -- save just after where we found it for searching next time.
      else
         table.insert(t, string.sub(p,ll)) -- Save what's left in our array.
         break -- Break at end, as it should be, according to the lua manual.
      end
   end
   return t
end

--- Pretty print a timestamp in Month day, Year format.
-- @param timestamp The time we want to format
-- @return The provided timestamp in Month Day Year format
function returnFormattedDate(timestamp)
  return os.date('%B %d, %Y', tonumber(timestamp))
end

--- Gets the day, month, and year of current date
-- @return The current date in Day Month Year format
function getCurrentDateDMY()
  return os.date('%d %B %Y')
end

--- Function that converts months from text to numbers
-- @param name The name of the month
-- @return The numerical value of the month provided
function p.monthNum(name)
  local monthNum = {
  ["January"] = 1,
  ["February"] = 2,
  ["March"] = 3,
  ["April"] = 4,
  ["May"] = 5,
  ["June"] = 6,
  ["July"] = 7,
  ["August"] = 8,
  ["September"] = 9,
  ["October"] = 10,
  ["November"] = 11,
  ["December"] = 12
  }
  if monthNum[name] ~= nil then
    return monthNum[name]
  end
  return name
end

--- Returns the length of a specified table
-- @param T Table we want to get the length for
-- @return Length of the specified table
function p.tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end

--- Because key,value pairs are stored arbitrarily in the tables, this
--- function sorts the keys and returns the key,value pairs in sorted order.
-- @param unsortedTable The unsorted table
-- @return Sorted version of the provided table
function p.sortedPairs(unsortedTable)
    -- Get all the keys from the table
    local keys = {}
    if (unsortedTable ~= nil) then
        for key in pairs(unsortedTable) do
            keys[#keys+1] = key
        end
    end

    -- Sort'em!
    table.sort(keys)

    local i = 0
    return function()
        i = i + 1
        if keys[i] then
            return keys[i], unsortedTable[keys[i]]
        end
    end
end

--- Returns the keys of a table, sorted by the values they're associated with,
--- given a tbl and a sortFunction e.g. "function(a, b) return a < b end"
-- @param tbl The table we want to get the keys from
-- @param sortFunction Function that determines how we want to sort the keys
-- @return Table of keys in sorted order
function p.getKeysSortedByValue(tbl, sortFunction)
  local keys = {}
  for key in pairs(tbl) do
    table.insert(keys, key)
  end

  table.sort(keys, function(a, b)
    return sortFunction(tbl[a], tbl[b])
  end)

  return keys
end

----------------------------------
----------------------------------
-- Misc. ACPC-related functions
----------------------------------
----------------------------------

--- Returns the Gulliver direction based on given theme
-- @param theme
-- @return The Gulliver direction
function p.getGulliverDirectionFromTheme(theme)
  local gulliverDirections = {
  ["Natural"] = "North-A",
  ["Hip"] = "North-B",
  ["Cute"] = "East-A",
  ["Harmonious"] = "East-B",
  ["Modern"] = "East-C",
  ["Cool"] = "South-A",
  ["Elegant"] = "South-B",
  ["Historical"] = "South-C",
  ["Sporty"] = "West-A",
  ["Rustic"] = "West-B",
  ["Civic"] = "West-C",
  }
  if gulliverDirections[theme] ~= nil then
    return gulliverDirections[theme]
  end
  return theme
end

return p
Advertisement