×
Create a new article
Write your page title here:
We currently have 22 articles on The Grand Library of Hylia. Type your article name above or click on one of the titles below and start writing!



The Grand Library of Hylia

Module:RandomCargoRecord: Difference between revisions

No edit summary
No edit summary
 
(20 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}
local h = {}
local cargo = mw.ext.cargo
local cargo = mw.ext.cargo


function p.randomCargoRecord(frame)
function p.randomCargoRecord(frame)
     local getArgs = require('Module:Arguments').getArgs
     local getArgs = require('Module:Arguments').getArgs
     local tableName = h.parseArg(frameArgs[1])
    local args = getArgs(frame);
     local tableName = args[1]


     if tableName == nil or tableName == "" then
     if tableName == nil or tableName == "" then
         hasErrors = true
         hasErrors = true
         h.warn("<code>tableName</code> parameter is required")
         return string.format("<code>tableName</code> parameter is required")
     end
     end
    if hasErrors then return CATEGORY_INVALID_ARGS end


local countFields = 'COUNT(*)=total'
local countFields = 'COUNT(*)=total'
local countArgs = {}
local countArgs = {
format = 'list',
limit = 1
}
local countResults = cargo.query(tableName, countFields, countArgs)
local countResults = cargo.query(tableName, countFields, countArgs)
local total = countResults.total
local total = 0
for r = 1, #countResults do
        local result = countResults[r]
total = result.total
    end


local queryFields = 'game,link'
if total == nil or total == "" then
local countOffset = math.random(1, total)
        return string.format("<code>total</code> could not be calculated from table %s", tableName)
    end
 
local number = require('Module:Random').number
local queryFields = 'game=game,link=link'
local randomArgs = {
[1] = 1,
[2] = total
}
local countOffset = number(randomArgs)
 
if countOffset >= 1 then
countOffset = countOffset - 1
end


local args = {
local args = {
format = table,
format = 'table',
fields = queryFields,
limit = 1,
limit = 1,
offset = countOffset
offset = countOffset
}
}
local result = cargo.query(tableName, fields, args)
   
local results = cargo.query(tableName, queryFields, args)
     for r = 1, #results do
     for r = 1, #results do
         local result = results[r]
         local result = results[r]
         return string.format("<h1>%s</h1>", result.link)
         return string.format("[[%s]]", result.link)
     end
     end
end
end
return p

Latest revision as of 07:51, 27 December 2023

Documentation for this module may be created at Module:RandomCargoRecord/doc

local p = {}
local cargo = mw.ext.cargo

function p.randomCargoRecord(frame)
    local getArgs = require('Module:Arguments').getArgs
    local args = getArgs(frame);
    local tableName = args[1]

    if tableName == nil or tableName == "" then
        hasErrors = true
        return string.format("<code>tableName</code> parameter is required")
    end

	local countFields = 'COUNT(*)=total'
	local countArgs = {
		format = 'list',
		limit = 1
	}
	local countResults = cargo.query(tableName, countFields, countArgs)
	local total = 0
	for r = 1, #countResults do
        local result = countResults[r]
		total = result.total
    end

	if total == nil or total == "" then
        return string.format("<code>total</code> could not be calculated from table %s", tableName)
    end

	local number = require('Module:Random').number
	local queryFields = 'game=game,link=link'
	local randomArgs = {
		[1] = 1,
		[2] = total
	}
	local countOffset = number(randomArgs)

	if countOffset >= 1 then
		countOffset = countOffset - 1
	end

	local args = {
		format = 'table',
		limit = 1,
		offset = countOffset
	}
    
	local results = cargo.query(tableName, queryFields, args)
	
    for r = 1, #results do
        local result = results[r]
        return string.format("[[%s]]", result.link)
    end
end

return p