Module:Sandbox/User:Alsang: Difference between revisions

From Brighter Shores Wiki
Jump to navigation Jump to search
Content added Content deleted
No edit summary
No edit summary
 
(54 intermediate revisions by the same user not shown)
Line 1: Line 1:
require('strict')
require('Module:Mw.html extension')
require('Module:Mw.html extension')
local param = require( 'Module:Paramtest' )
local currency = require('Module:Currency')
local lang = mw.getContentLanguage()
local xpdata = mw.loadData('Module:Experience/data')

local p = {}
local p = {}


-- non dynamic module, no inputs
function sort_entries(entry1, entry2)
function p.main()
-- Sort the variants based on the sum of the profession levels
local entry1_value = (entry1['Profession Level A'] or 0) + (entry1['Profession Level B'] or 0)
-- returns only directly needed parameter needed for the row,
local entry2_value = (entry2['Profession Level A'] or 0) + (entry2['Profession Level B'] or 0)
-- other parameters are determined by subqueries of chained pages
return entry1_value < entry2_value
end

function get_table_info(args, get_images)
local variant_name = args.variant or mw.title.getCurrentTitle().fullText
local query = {
local query = {
'[[Variant of::'..variant_name..']]',
'[[Activity JSON::~*]]',
'? = name',
'?Profession A #', -- Adding a # to this query makes it return plaintext instead of a link
'?Profession B #',
'?Activity JSON = activityJSON',
'?Profession Level A',
'limit = 1500'
'?Profession Level B',
'?Profession Level A High',
'?Profession Level B High',
'?Variant name',
get_images and '?Image#64px;x64px = Image' or nil,
limit = args.limit or 500,
}
}
local smw_data = mw.smw.ask(query)
local results = mw.smw.ask(query)
if not smw_data then
results = p.screenResults(results)
return nil

end
return p.displayTable(results)
table.sort(smw_data, sort_entries)
return smw_data
--for debugging
--return '<pre>'..mw.text.jsonEncode(results, mw.text.JSON_PRETTY)..'</pre>'

end
end


-- goes through a list of results and culls the ones which are not needed for this table
function p.variants_header(args)
function p.screenResults(results)
local smw_data = get_table_info(args)
local contents
local resultsScreened = {}
if smw_data then
-- iterate through products
local formatted = {}
for _, entry in ipairs(smw_data) do
for _, item in ipairs(results) do
local variant_name = entry['Variant name']
-- if theres only one activity, wrap in table
if variant_name then
if type(item.activityJSON)=='string' then
local page = entry[1]
item.activityJSON = { item.activityJSON }
local pipe = page:find('|')
end
if pipe then
page = page:sub(3, pipe-1)
for j,json in ipairs(item.activityJSON) do
local activityJSON = mw.text.jsonDecode(json)
if activityJSON.duration and tonumber(activityJSON.duration)==20 then
local itemNew = {}
itemNew.name = item.name
itemNew.XP = activityJSON.xp
itemNew.lvl = activityJSON.level
itemNew.profession = activityJSON.profession
itemNew.duration = activityJSON.duration
itemNew.product = activityJSON.output[1].name
itemNew.XPperHour = itemNew.XP and tonumber(itemNew.XP) and itemNew.duration and tonumber(itemNew.duration) and tonumber(itemNew.XP) * 3600 / tonumber(itemNew.duration)
itemNew.fitXP = xpdata.knowledge[itemNew.lvl]*4*0.0005
if itemNew.lvl>=200 then
itemNew.fitXP = itemNew.fitXP * 4
end
itemNew.fitXP = math.floor(itemNew.fitXP * 100 ) / 100
local query = mw.smw.ask('[[Sold item::' .. itemNew.product .. ']][[Shop sell price::!~N/A]]|?Shop sell price=data|mainlabel=-')
if type(query)=='table' then
-- price is returned as a number
itemNew.productSellPrice = tonumber(query[1].data)*0.005
else
else
-- will return nil if the item is not in a shop, or if it is in a shop but can only be sold to the shop
page = page:sub(3, -3)
itemNew.productSellPrice = nil
end
end

table.insert(resultsScreened,itemNew)
if page:sub(1, 1 + #variant_name + #' (grade ') == ':' .. variant_name .. ' (grade ' and page:sub(-1) == ')' then
local grade_number = page:sub(2 + #variant_name + #' (grade ', -2)
mw.log('"' .. grade_number .. '"')
variant_name = 'Grade ' .. grade_number
end

table.insert(formatted, ('[[%s|%s]]'):format(page, variant_name))
else
table.insert(formatted, entry[1])
end
end
end
end
contents = table.concat(formatted, '&nbsp;•&nbsp;')
else
contents = '[[Category:Empty variant list]]'
end
end
return mw.html.create('div')
-- sort the results by recipe level
:addClass('variants-header')
table.sort(resultsScreened, function(item1, item2)
:wikitext(contents)
local lvl1 = item1.lvl
:done()
local lvl2 = item2.lvl
end
if (lvl1 == nil) ~= (lvl2 == nil) then --one of two are empty
return lvl2 == nil -- true if lvl2 is nil but not lvl1, false if lvl1 is nil but not lvl2
end
if lvl1 == nil then
return false -- Both empty, equivalent
end
return lvl1 < lvl2 -- normal comparison
end)

return resultsScreened


function p.main(frame)
local args = frame:getParent().args
args.variant = args[1] or args.variant
return p.variants_header(args)
end
end


-- make the table
function p.variants_table(args)
function p.displayTable(results)
local smw_data = get_table_info(args, true)
local out = mw.html.create('table')
if not smw_data then
:addClass('wikitable sortable')
return ':No variants found.'
end
local profession_b_flag = false
for _, entry in ipairs(smw_data) do
if entry['Profession B'] or entry['Profession Level B'] then
profession_b_flag = true
end
break
end
local table = mw.html.create('table')
:addClass('sortable wikitable')
:tag('tr')
:tag('tr')
:tag('th')
:tag('th')
:attr{ colspan = '2' }
:wikitext('Page')
:wikitext('Item')
:done()
:tag('th')
:wikitext('Product')
:done()
:tag('th')
:wikitext('Profession')
:done()
:done()
:tag('th')
:tag('th')
:wikitext('Level')
:wikitext('Level')
:attrIf(profession_b_flag, 'colspan', 2)
:done()
:done()
:tag('th')
:wikitext('XP')
:done()
:tag('th')
:wikitext('0.0005 of levelup XP')
:done()
:tag('th')
:wikitext('sell price<br>per action')
:done()
-- :tag('th')
-- :wikitext('Duration')
-- :done()
-- :tag('th')
-- :wikitext('XP/hr')
-- :done()
:done()
:done()
local function formatProfessionLevel(profession, level, high)
if not (profession or level) then
return nil
end
profession = profession or 'Unknown profession'
local level_text = tostring(level or '?')
if high then
level_text = ('%s➨%d'):format(level_text, high)
end
return ('%s [[File:%s small icon.png|21x21px|link=%s]]'):format(level_text, profession, profession)
end


for _, entry in ipairs(smw_data) do
for i, item in ipairs(results) do
local row = out:tag('tr')
local profession_level_a = entry['Profession Level A'] or '?'
local profession_level_b = entry['Profession Level B'] or '?'
--level
local profession_a = entry['Profession A'] or ''
:tag('td')
local profession_b = entry['Profession B'] or ''
:wikitext(item.name)
table
:tag('tr')
:tag('td')
:css{ ['border-right'] = 'none' }
:wikitext(entry.Image)
:done()
:tag('td')
:css{ ['border-left'] = 'none' }
:wikitext(entry[1])
:done()
:tag('td')
:css{ ['text-align'] = 'right' }
:wikitext(formatProfessionLevel(entry['Profession A'], entry['Profession Level A'], entry['Profession Level A High']))
:IF(profession_b_flag)
:tag('td')
:IF(entry['Profession B'])
:css{ ['text-align'] = 'right' }
:wikitext(formatProfessionLevel(entry['Profession B'], entry['Profession Level B'], entry['Profession Level B High']))
:done()
:END()
:done()
:done()
:tag('td')
end
:wikitext('[[' .. item.product .. ']]')
:done()
:tag('td')
:wikitext('[[' .. item.profession .. ']]')
:done()
:tag('td')
:wikitext(item.lvl)
:done()
:tag('td')
:wikitext(item.XP)
:done()
:tag('td')
:wikitext(item.fitXP)
:done()
:tag('td')
:wikitext(item.productSellPrice)
:done()
-- :tag('td')
-- :wikitext(item.duration)
-- :done()
-- :tag('td')
-- :wikitext(item.XPperHour)
-- :done()


:done()
return table
end
end


return out
function p.main_table(frame)
local args = frame:getParent().args
args.variant = args[1] or args.variant
return p.variants_table(args)
end
end



Latest revision as of 00:51, 18 December 2024

Module documentation
This documentation is transcluded from Module:Sandbox/User:Alsang/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:Sandbox/User:Alsang/doc. [edit]
Module:Sandbox/User:Alsang's function main is invoked by Template:Sandbox/User:Alsang.
Module:Sandbox/User:Alsang requires Module:Currency.
Module:Sandbox/User:Alsang requires Module:Mw.html extension.
Module:Sandbox/User:Alsang requires Module:Paramtest.
Module:Sandbox/User:Alsang requires strict.
Module:Sandbox/User:Alsang loads data from Module:Experience/data.

require('strict')
require('Module:Mw.html extension')
local param = require( 'Module:Paramtest' )
local currency = require('Module:Currency')
local lang = mw.getContentLanguage()
local xpdata = mw.loadData('Module:Experience/data')

local p = {}

-- non dynamic module, no inputs
function p.main()
	
	-- returns only directly needed parameter needed for the row,
	-- other parameters are determined by subqueries of chained pages
	local query = {
		'[[Activity JSON::~*]]',
		'? = name',
		'?Activity JSON = activityJSON',
		'limit = 1500'
	}
	local results = mw.smw.ask(query)
	
	results = p.screenResults(results)

	return p.displayTable(results)
	
	--for debugging
	--return '<pre>'..mw.text.jsonEncode(results, mw.text.JSON_PRETTY)..'</pre>'

end

-- goes through a list of results and culls the ones which are not needed for this table
function p.screenResults(results)
	local resultsScreened = {}
	
	-- iterate through products
	for _, item in ipairs(results) do
		
		-- if theres only one activity, wrap in table
		if type(item.activityJSON)=='string' then
			item.activityJSON = { item.activityJSON }
		end
		
		for j,json in ipairs(item.activityJSON) do
		
			local activityJSON = mw.text.jsonDecode(json)
			
			if activityJSON.duration and tonumber(activityJSON.duration)==20 then
				local itemNew = {}
				itemNew.name = item.name
				itemNew.XP = activityJSON.xp
				itemNew.lvl = activityJSON.level
				itemNew.profession = activityJSON.profession
				itemNew.duration = activityJSON.duration
				itemNew.product = activityJSON.output[1].name
				
				itemNew.XPperHour = itemNew.XP and tonumber(itemNew.XP) and itemNew.duration and tonumber(itemNew.duration) and tonumber(itemNew.XP) * 3600 / tonumber(itemNew.duration)
				
				itemNew.fitXP = xpdata.knowledge[itemNew.lvl]*4*0.0005
				if itemNew.lvl>=200 then
					itemNew.fitXP = itemNew.fitXP * 4
				end
				itemNew.fitXP = math.floor(itemNew.fitXP * 100 ) / 100
				
				local query = mw.smw.ask('[[Sold item::' .. itemNew.product .. ']][[Shop sell price::!~N/A]]|?Shop sell price=data|mainlabel=-')
					if type(query)=='table' then
					-- price is returned as a number
					itemNew.productSellPrice = tonumber(query[1].data)*0.005
				else
					-- will return nil if the item is not in a shop, or if it is in a shop but can only be sold to the shop
					itemNew.productSellPrice = nil
				end
				
				table.insert(resultsScreened,itemNew)
			end
		end
	end
	
	-- sort the results by recipe level
	table.sort(resultsScreened, function(item1, item2)
		local lvl1 = item1.lvl
		local lvl2 = item2.lvl
		if (lvl1 == nil) ~= (lvl2 == nil) then --one of two are empty
			return lvl2 == nil -- true if lvl2 is nil but not lvl1, false if lvl1 is nil but not lvl2
		end
		if lvl1 == nil then
			return false  -- Both empty, equivalent
		end
		return lvl1 < lvl2 -- normal comparison
	end)

	return resultsScreened

end

-- make the table
function p.displayTable(results)
	local out = mw.html.create('table')
		:addClass('wikitable sortable')
		:tag('tr')
			:tag('th')
				:wikitext('Page')
			:done()
			:tag('th')
				:wikitext('Product')
			:done()
			:tag('th')
				:wikitext('Profession')
			:done()
			:tag('th')
				:wikitext('Level')
			:done()
			:tag('th')
				:wikitext('XP')
			:done()
			:tag('th')
				:wikitext('0.0005 of levelup XP')
			:done()
			:tag('th')
				:wikitext('sell price<br>per action')
			:done()
--			:tag('th')
--				:wikitext('Duration')
--			:done()
--			:tag('th')
--				:wikitext('XP/hr')
--			:done()
		:done()

	for i, item in ipairs(results) do
		local row = out:tag('tr')
		
			--level
			:tag('td')
				:wikitext(item.name)
			:done()
			:tag('td')
				:wikitext('[[' .. item.product .. ']]')
			:done()
			:tag('td')
				:wikitext('[[' .. item.profession .. ']]')
			:done()
			:tag('td')
				:wikitext(item.lvl)
			:done()
			:tag('td')
				:wikitext(item.XP)
			:done()
			:tag('td')
				:wikitext(item.fitXP)
			:done()
			:tag('td')
				:wikitext(item.productSellPrice)
			:done()
--			:tag('td')
--				:wikitext(item.duration)
--			:done()
--			:tag('td')
--				:wikitext(item.XPperHour)
--			:done()

		:done()
	end

	return out
end

return p