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) 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(os.date("%H"), 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(os.date("%H"), 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 = ""

-- Cargo query local tables = "nh_fish" local month_field = "n_m" .. os.date("%m"):match("0*(%d+)") 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)

-- 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

-- Get current ACNH bugs on server time function p.nh_bug(frame) local current_bug = {} local output = ""

-- Cargo query local tables = "nh_bug" local month_field = "n_m" .. os.date("%m"):match("0*(%d+)") local fields = month_field .. "_time=time,_pageName=pagename,name,image,sell_nook=sell,location=location" local args = { where = month_field .. "=1",       limit = 80 }   local results = cargo.query(tables, fields, args)

-- Get table of current bug current_bug = get_current(results)

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

-- Get current ACNH sea creatures on server time function p.nh_sea_creature(frame) local current_sea_creature = {} local output = ""

-- Cargo query local tables = "nh_sea_creature" local month_field = "n_m" .. os.date("%m"):match("0*(%d+)") 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 sea_creature current_sea_creature = get_current(results)

-- Output results for key,value in pairs(results) do       local key = value.name if has_value(current_sea_creature, value.name) then output = output .. frame:expandTemplate{ title = "CargoDisplayNHSeaCreatureShort", 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