Module:CurrentCritters

local p = {}

local cargo = mw.ext.cargo

-- Take hour and convert to timestamp local function parse_time(hour) return os.time {hour = hour, day = 1, month = 1, year = 1970} end

-- Convert standard hour ("4 PM") to military hour ("16") local function std_to_mil_time(time) local chunks = {} for substring in time:gmatch("%S+") do       table.insert(chunks, substring) end local time = chunks[1] if chunks[1] == "12" then if chunks[2] == "AM" then time = "00" else time = "12" end elseif chunks[2] == "PM" then time = time + 12 end return time end

-- Calculate if "between" time is between two times local function between_times(between, start, stop) between = parse_time(between) start = parse_time(start) stop = parse_time(stop) if stop < start then stop = stop + 24 * 60 * 60 end return (start <= between) and (between <= stop) end

-- Trim spaces surrounding string local function trim(s) return (string.gsub(s, "^%s*(.-)%s*$", "%1")) end

-- Check if list has value local function has_value(tab, val) for index, value in ipairs(tab) do       if value == val then return true end end

return false end

-- Get current critters given Cargo query results local function get_current(results, hour) local current_critters = {} for r = 1, #results do       local result = results[r] if result.time == "All day" then table.insert(current_critters, result.name) else local times = {} for range in string.gmatch(result.time:gsub("–", "-"), "([^&]+)") do               for time in string.gmatch(range, "([^-]+)") do                    table.insert(times, time) end end if between_times(hour, std_to_mil_time(times[1]), std_to_mil_time(times[2])) then table.insert(current_critters, result.name) end if times[3] then if between_times(hour, std_to_mil_time(times[3]), std_to_mil_time(times[4])) then table.insert(current_critters, result.name) end end end end

return current_critters end

-- Get current ACNH fish on server time function p.nh_fish(frame) local current_fish = {} local output = ""

local hour = "" if frame.args["hour"] then hour = frame.args["hour"]:match("0*(%d+)") else hour = os.date("%H"):match("0*(%d+)") end

local month = "" if frame.args["month"] then month = frame.args["month"]:match("0*(%d+)") else month = os.date("%m"):match("0*(%d+)") end local hemisphere = "n" if frame.args["hemisphere"] then hemisphere = frame.args["hemisphere"] end

-- Cargo query local tables = "nh_fish" local month_field = hemisphere .. "_m" .. month local fields = month_field .. "_time=time,_pageName=pagename,name,image,sell_nook=sell,shadow_size=shadow,location=location" local args = { where = month_field .. "=1",       limit = 80 }   local results = cargo.query(tables, fields, args)

-- Get table of current fish current_fish = get_current(results, hour, month)

-- Output results for key,value in pairs(results) do       local key = value.name if has_value(current_fish, value.name) then output = output .. frame:expandTemplate{ title = "CargoDisplayNHFishShort", args = { pagename=value.pagename, name=value.name, image=value.image, sell=value.sell, shadow=value.shadow, location=value.location, time=value.time } } end end return output end

return p