From Dark and Darker Wiki

Overview

Functions for making Quest page. Data comes from Data:Merchant.json.

Functions

draw_page

Creates a table of merchants

Parameters

none


draw_page examples

Default


{{#invoke:Merchant|draw_page}}


Click All to see all quests, or a merchant icon to see quests specific to that merchant!

None Merchants Tab.png
All Merchants Tab.png
Alchemist.png
Armourer.png
Cockatrice Merchant.png
Dealmaker.png
Expressman.png
Fortune Teller.png
Goblin Merchant.png
Goldsmith.png
Huntress.png
Jack O' Lantern.png
Krampus.png
Leathersmith.png
Miner.png
Navigator.png
Nicholas.png
Nightmare Mummy.png
Skeleton Merchant.png
Squire.png
Surgeon.png
Tailor.png
Tavern Master.png
The Collector.png
Treasurer.png
Valentine.png
Weaponsmith.png
Woodsman.png

local p = {}
local MD = mw.loadJsonData("Data:Merchant.json") --Global var holding tables from Data:Merchant.json
local concat = table.concat


---Counts number of keys. Tables from Merchant.json don't work with next() or the # operator
--- @param t table
--- @return number
local function count(t)
	local c = 0
	for _ in pairs(t) do c = c + 1 end
	return c
end


---Return a table row of data cells containing merchant information
--- @param quest table
--- @return string
local function row(name,quest)
	local text = {}
	text[#text+1] = "<tr><td>"..name.."</td><td>"
	if quest.requiredquest then
		text[#text+1] = "Complete quest:<br>"..quest.requiredquest
	end
	text[#text+1] = "</td><td>"
	for i=1,count(quest.objectives) do
		if i ~= 1 then text[#text+1] = "<br>" end
		text[#text+1] = quest.objectives[i] and quest.objectives[i].summary
	end
	text[#text+1] = "</td><td>"..quest.rewards.."</td></tr>"
	return concat(text)
end


--- Return a table row of appropriate headers cells
--- @return string
local function table_header(title)
	return [=[<table class="wikitable mw-collapsible jquery-tablesorter" cellspacing="0" style="margin-top:5px; padding-bottom:5px; width:100%; color:#eee; background:transparent; text-align:center; vertical-align:middle;">
	<caption style="font-size:20px; white-space:nowrap; border:1px solid; padding:3px;">Chapter: ]=]..title..[=[ </caption>
	<tr style="background-color: rgb(220,220,220,0.2)">
	<th style="width:10%">Quest</th>
	<th style="width:10%">Prerequisites</th>
	<th style="width:21%" class="tooltip">*Task(s)<span class="tooltiptext" style="font-weight:normal;">Items need to be [[Looted_Handled_Supplied|looted]] unless specified otherwise.</span></th>
	<th style="width:21%">Rewards</th>
	</tr>]=]
end


---Create monster table wikitext
--- @param title string
--- @param chapter table
--- @return string
local function draw_table(title,chapter)
	local text = {}
	text[#text+1] = table_header(title)
	for _,quest in ipairs(chapter.order) do
		text[#text+1] = row(quest,chapter[quest])
	end
	text[#text+1] = "</table>"
	return concat(text)
end


---Create a wikitext table for every quest chapter
--- @param merchant any
--- @return string
local function draw_chapters(merchant)
	local text = {}
	text[#text+1] = [=[<div class="line" style="margin:30px 0px 5px; background-image:linear-gradient(to right,#0A0A0A,#646464,#0A0A0A)"></div>
<p style="font-weight:bold;font-size:34px;text-align:center">]=]..merchant.."</p>"
	for _,chapter in ipairs(MD[merchant].questchapters.order) do
		text[#text+1] = draw_table(chapter,MD[merchant].questchapters[chapter])
	end
	return concat(text)
end


---Create a tab toggle
--- @param title string
--- @param content string
--- @param active string
--- @param tabid string
--- @param tabname string
--- @return string
local function tab_toggle(title,content,active,tabid,tabname)
	local class = [=[ class="tab-toggle tab"]=]
	if active == "yes" then
		class = [=[ class="selected-tab tab-toggle tab"]=]
	end
	return [=[<div style="display:inline-block"]=]..class..[=[ data-tabid="]=]..tabid..[=[" data-tab="]=]..title:gsub(" ","")..[=[" title="]=]..title..[=[">]=]..content..[=[</div>]=]
end


---Collect MD top-pevel keys into a sorted table
---@return table
local function keys_in_alph_order()
	local set = {}
	for key,_ in pairs(MD) do
		set[#set+1] = key
	end
	table.sort(set)
	return set
end


---Create an array of merchant tabs and their associated quest sections
--- @param frame any
--- @return string
function p.draw_page(frame)
	local merchant_list = keys_in_alph_order()
	local text = {}
	text[#text+1] = [=[<p style="text-align: center; font-weight: bold;"> ''Click All to see all quests, or a merchant icon to see quests specific to that merchant!'' </p><div style="display:flex; flex-direction:row; flex-wrap:wrap; justify-content:center;">]=]
	text[#text+1] = tab_toggle("None","[[File:None_Merchants_Tab.png|link=|130px]]","yes","0","")
	text[#text+1] = tab_toggle("All","[[File:All_Merchants_Tab.png|link=|130px]]","","0","")
	text[#text+1] = [=[</div><div style="display:flex;flex-direction:row;flex-wrap:wrap;justify-content:center;">]=]
	for _,merchant in ipairs(merchant_list) do
		if merchant == "Skeleton" or merchant == "Cockatrice" then
			text[#text+1] = tab_toggle(merchant:gsub("'",""),"[[File:"..merchant.."_Merchant.png|link=|130px]]","","0","")
		else
			text[#text+1] = tab_toggle(merchant:gsub("'",""),"[[File:"..merchant..".png|link=|130px]]","","0","")
		end
	end
	text[#text+1] = [=[</div><div class="None-data" style=""> </div>]=]
	for _,merchant in ipairs(merchant_list) do
		text[#text+1] = [=[<div class="]=]..merchant:gsub(" ",""):gsub("'","")..[=[-data All-data" style="display:none">]=]
		text[#text+1] = draw_chapters(merchant)
		text[#text+1] = "</div>"
	end
	return concat(text)
end


return p
---- Test on wiki with:  mw.log(p.draw_page())


-- TODO
-- Look into highlighting on hover.  Hover prereq cell should highlight the prereq's quest row.
--		Put quest name in class and then use class in prereq cell to highlight the quest row on prereq hover.