Module

DYK

From Dogcraft Wiki

This module is responsible for the functionality of the Did you know? facts that appear on the main page. It does this by randomly pulling a number of facts from the content of the Dogcraft Wiki:Did you know/Facts page.

Functions:

random

The "random" function can generate up to 5 random numbers per day. Its arguments are "length", which sets the highest random number it can select (default is 30), and "seed", which can select between the 5 daily numbers. Seed accepts numbers from 1 to 5.
Examples:

  • {{#invoke:DYK|random|length=33|seed=1}} generates: 32 (this number changes daily)
  • {{#invoke:DYK|random|length=33|seed=2}} generates: 6 (this number changes daily)
  • {{#invoke:DYK|random|length=33|seed=3}} generates: 14 (this number changes daily)
  • {{#invoke:DYK|random|length=33|seed=4}} generates: 21 (this number changes daily)
  • {{#invoke:DYK|random|length=33|seed=5}} generates: 28 (this number changes daily)

randomW

The "randomW" function can generate up to 5 random numbers per week. Its arguments are "length", which sets the highest random number it can select (default is 30), and "seed", which can select between the 5 daily numbers. Seed accepts numbers from 1 to 5.
Examples:

  • {{#invoke:DYK|randomW|length=33|seed=1}} generates: 18 (this number changes weekly)
  • {{#invoke:DYK|randomW|length=33|seed=2}} generates: 25 (this number changes weekly)
  • {{#invoke:DYK|randomW|length=33|seed=3}} generates: 32 (this number changes weekly)
  • {{#invoke:DYK|randomW|length=33|seed=4}} generates: 7 (this number changes weekly)
  • {{#invoke:DYK|randomW|length=33|seed=5}} generates: 14 (this number changes weekly)

facts

The "facts" function, which returns up to 5 facts per day, selected from Dogcraft_Wiki:Did_you_know. Its arguments are "text", which expects "{{Dogcraft_Wiki:Did_you_know/Facts}}" or a similarly layed out page, as the value, and "start" and "end", which determine which of the 5 facts to return.
Example: {{#invoke:DYK|facts|text={{Dogcraft_Wiki:Did_you_know/Facts}}|start=1|end=2}} generates:

(this changes daily)
local p = {} --p stands for package

--random number that changes daily
function p.random( frame )
    math.randomseed((os.date("%d")*os.date("%m")*os.date("%y"))+tonumber(frame.args['seed']))
	length = tonumber(frame.args['length']) or 30
	randnum = math.random(1,length)
    return randnum
end

--random number that changes weekly
function p.randomW( frame )
    math.randomseed((os.date("%m")*os.date("%y"))+tonumber(frame.args['seed']))
	length = tonumber(frame.args['length']) or 30
	randnum = math.random(1,length)
    return randnum
end

function randomdaily( maxrandom, constant )
    math.randomseed((os.date("%d")*os.date("%m")*os.date("%y"))+constant)
	length = maxrandom or 30
	randnum = math.random(1,length)
    return randnum
end

function p.facts( frame )
	text = frame.args['text'] or ""
    facts = {}
	i = 1
	result = ""
	resulttable = {}
	
	for fact in string.gmatch(text, '%* %.%.%. .-?') do
		facts[i] = fact .. "\n"
		i = i + 1
	end
	
	start = tonumber(frame.args['start'])
	final = tonumber(frame.args['end'])

	if (start > 5) then
		start = 5
	elseif (start < 1) then
		start = 1
	end
	
	if (final > 5) then
		final = 5
	elseif (final < 1) then
		final = 1
	end

	rsindex = 1
	for j=start,final,1 do
		key = randomdaily(facts.getn, j)
		resulttable[rsindex] = facts[key]
		rsindex = rsindex + 1
	end
	result = table.concat(resulttable, "\n")
	
	return frame:preprocess( result )
end

return p