Module:Sandbox/User:Lathow/Pof timers nobreeding: Difference between revisions

From WIDEVERSE Wiki
Jump to navigation Jump to search
No edit summary
 
m (1 revision imported)
 
(No difference)

Latest revision as of 22:23, 4 November 2021

Documentation for this module may be created at Module:Sandbox/User:Lathow/Pof timers nobreeding/doc

-- <nowiki>
local lang = mw.getContentLanguage()
local yesno = require('Module:Yesno')
local calc = require('Module:Form calculator')

local p = {}
local data = {
['rabbit'] = { cycleLength = 7.5, img = 'Common brown rabbit', page = 'Rabbit (Player-owned farm)', order = 1 },
	['chicken'] = { cycleLength = 75, img = 'Common white hen', page = 'Chicken (Player-owned farm)', order = 2 },
	['chinchompa'] = { cycleLength = 225, img = 'Grey chinchompa', page = 'Chinchompa (Player-owned farm)', order = 3 },
	['sheep'] = { cycleLength = 200, img = 'White ewe', page = 'Sheep (Player-owned farm)', order = 4 },
	['spider'] = { cycleLength = 360, img = 'Spirit spider (player-owned farm)', page = 'Spider (Player-owned farm)', order = 5 },
	['zygomite'] = { cycleLength = 1000, img = 'Gloomshroom zygomite', page = 'Zygomite (Player-owned farm)', order = 6 },
	['cow'] = { cycleLength = 900, img = 'Kandarin cow', page = 'Cow (Player-owned farm)', order = 7 },
	['yak'] = { cycleLength = 1200, img = 'Spirit yak', page = 'Yak (Player-owned farm)', order = 8 },
	['dragon'] = { cycleLength = 3000, img = 'Black dragon (player-owned farm)', page = 'Dragon (Player-owned farm)', order = 9 }
}
local data_order = {}
for k,v in pairs(data) do
	data_order[v.order] = k
end

function append(t,...)
	for i,v in ipairs({...}) do
		table.insert(t,v)
	end
end

function sortAnimals(a,b)
	return data[a].order < data[b].order
end

function animalFiles(t)
	table.sort(t, sortAnimals)
	local t2 = {}
	for i,v in ipairs(t) do
		t2[i] = string.format('[[File:%s.png|link=%s|%s]]', data[v].img, data[v].page, lang:ucfirst(v))
	end
	return t2
end

--Cycles in the last 10 hours:
--07 December 2018 22:00:00 (UTC): Chinchompa, Dragon, Zygomite
--08 December 2018 00:30:00 (UTC): Chinchompa
--08 December 2018 03:00:00 (UTC): Chinchompa
--08 December 2018 05:30:00 (UTC): Chinchompa
--08 December 2018 06:20:00 (UTC): Zygomite
--Cycles in the next 10 hours:
--08 December 2018 08:00:00 (UTC): Chinchompa
--08 December 2018 10:30:00 (UTC): Chinchompa
--08 December 2018 13:00:00 (UTC): Chinchompa
--08 December 2018 14:40:00 (UTC): Dragon, Zygomite
--08 December 2018 15:30:00 (UTC): Chinchompa
function p.test()
	return p.pof({'chinchompa', 'zygomite', 'dragon'}, 100)
end

function p.calculator()
	local args = {
		template = 'User:Lathow/Template:Pof timers nobreeding/t',
		form = 'jsCalc-pof-timers-form',
		result = 'jsCalc-pof-timers-result',
		outputtype = 'verticaltable',
		param1 = 'time', label1 = 'Timespan to display (hours; negative for previous ticks)', type1 = 'int', range1='-inf-inf', default1 = '10',
		cat='no'
	}
	for i,v in ipairs(data_order) do
		j = i + 1
		args['param'..j] = v
		args['label'..j] = 'Show '..v
		args['type'..j] = 'check'
		args['range'..j] = 'yes,no'
	end
	return tostring(calc._main(args))
end

function p.main(frame)
	return p._main(frame:getParent().args)
end

function p._main(args)
	local hours = tonumber(args.time)
	if not hours then
		return "Please enter the hours required as a number"
	elseif hours == 0 then
		return "Please enter a non-zero number of hours (negative is fine)"
	end
	local animals = {}
	
	for k,v in pairs(data) do
		if yesno(args[k]) then
			table.insert(animals, k)
		end
	end
	
	if #animals == 0 then
		return "Please select at least one animal"
	end
	return "'''This calculator may not be accurate to in-game time any more - please report inaccuracies and actual tick times [[Talk:Player-owned farm#Breeding tick calculator issues|here]] so that it can be corrected!'''<br />"..tostring(p.pof(animals, hours))
end

function p.pof(animals, timespan_hours)
	local times = {}
	for _,a in ipairs(animals) do
		local adata = data[a]
		local g = p.pof_generate(adata.cycleLength, timespan_hours)
		
		for _,t in ipairs(g) do
			if times[t] == nil then
				times[t] = {}
			end
			table.insert(times[t], a)
		end
	end
	
	local r = {
		'Cycles in the '
	}
	
	if timespan_hours > 0 then
		append(r, 'next ', timespan_hours)
	else
		append(r, 'last ', timespan_hours*-1)
	end
	append(r, lang:plural(timespan_hours, ' hour:\n', ' hours:\n'))
	
	local times_arr = {}
	for k in pairs(times) do table.insert(times_arr, k) end
	table.sort(times_arr)
	for _,t in ipairs(times_arr) do
		local a = times[t]
		a = animalFiles(a)
		table.insert(r, string.format('* %s (UTC):  %s\n', lang:formatDate('d F Y H:i:s', '@' .. t), table.concat(a, ' ')))
	end
	return table.concat(r)
end

function p.pof_generate(cycleLength, timespan_hours)
	local timeNow = os.time()
	local cycleInS = cycleLength * 60
	local lastCycleTime = math.floor(timeNow / cycleInS) * cycleInS

	local times = {}
	local startT, endT
	if timespan_hours > 0 then
		startT = lastCycleTime + cycleInS
		endT = timeNow + timespan_hours * 3600
	else
		-- timespan_hours is negative 
		endT = timeNow + timespan_hours * 3600
		startT = lastCycleTime
		cycleInS = cycleInS * -1
	end
	local cycleTime
	for cycleTime = startT, endT, cycleInS do
		table.insert(times, cycleTime)
	end
	
	return times
end

return p
-- </nowiki>