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

From WIDEVERSE Wiki
Jump to navigation Jump to search
(Undid revision 32766995 by Lathow (Talk))
 
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/Root timers nobreeding/doc

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

local p = {}
local data = {
	['frog'] = { cycleLength = 300, img = 'Common green frog', page = 'Frog (Player-owned_farm)', order = 1 },
	['salamander'] = { cycleLength = 1507.5, img = 'Green salamander (player-owned farm)', page = 'Salamander (Player-owned farm)', order = 2 },
	['jadinko'] = { cycleLength = 1400, img = 'Draconic jadinko (player-owned farm)', page = 'Jadinko (Player-owned farm)', order = 3 },
	['varanusaur'] = { cycleLength = 1600, img = 'Feral dinosaur (player-owned farm)', page = 'Varanusaur', order = 4 },
	['brutish dinosaur'] = { cycleLength = 3300, img = 'Brutish dinosaur (player-owned farm)', page = 'Brutish dinosaur (Player-owned farm)', order = 5 },
	['arcane apoterrasaur'] = { cycleLength = 3300, img = 'Arcane apoterrasaur (player-owned farm)', page = 'Arcane apoterrasaur (Player-owned farm)', order = 6 },
	['spicati apoterrasaur'] = { cycleLength = 3600, img = 'Spicati apoterrasaur (player-owned farm)', page = 'Spicati apoterrasaur (Player-owned farm)', order = 7 },
	['oculi apoterrasaur'] = { cycleLength = 3900, img = 'Oculi apoterrasaur (player-owned farm)', page = 'Oculi apoterrasaur (Player-owned farm)', order = 8 },
	['scimitops'] = { cycleLength = 3300, img = 'Scimitops (player-owned farm)', page = 'Scimitops (Player-owned farm)', order = 9 },
	['asciatops'] = { cycleLength = 3600, img = 'Asciatops (player-owned farm)', page = 'Asciatops (Player-owned farm)', order = 10 },
	['malletops'] = { cycleLength = 3900, img = 'Malletops (player-owned farm)', page = 'Malletops (Player-owned farm)', order = 11 },
	['bagrada rex'] = { cycleLength = 3600, img = 'Bagrada rex (player-owned farm)', page = 'Bagrada rex (Player-owned farm)', order = 12 },
	['corbicula rex'] = { cycleLength = 3600, img = 'Corbicula rex (player-owned farm)', page = 'Corbicula rex (Player-owned farm)', order = 13 },
	['pavosaurus rex'] = { cycleLength = 4200, img = 'Pavosaurus rex (player-owned farm)', page = 'Pavosaurus rex (Player-owned farm)', order = 14 },
}
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.root({'salamander', 'varanusaur', 'pavosaurus rex'}, 100)
end

function p.calculator()
	local args = {
		template = 'User:Lathow/Template:Root timers nobreeding/t',
		form = 'jsCalc-root-timers-form',
		result = 'jsCalc-root-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.root(animals, hours))
end

function p.root(animals, timespan_hours)
	local times = {}
	for _,a in ipairs(animals) do
		local adata = data[a]
		local g = p.root_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.root_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>