Anonymous

Module:Township: Difference between revisions

From Melvor Idle
Create p.GetTaskReferenceTable (task goal/reward for items/monsters/dungeons)
(Refactor p.GetTaskTable to include a subroutine p._GetTaskRow)
(Create p.GetTaskReferenceTable (task goal/reward for items/monsters/dungeons))
Line 3: Line 3:
local GameData = require('Module:GameData')
local GameData = require('Module:GameData')
local Constants = require('Module:Constants')
local Constants = require('Module:Constants')


local p = {}
local p = {}
Line 823: Line 822:
end
end


-- Returns all the tasks of a given category
function p.GetTaskTable(frame)
function p.GetTaskTable(frame)
local category = frame.args ~= nil and frame.args[1] or frame
local category = frame.args ~= nil and frame.args[1] or frame
Line 841: Line 841:
table.insert(ret, p._GetTaskRow(title, task))
table.insert(ret, p._GetTaskRow(title, task))
end
end
end
table.insert(ret, '\r\n|}')
return table.concat(ret)
end
-- Returns a table containing all the tasks that reference an item or monster
-- e.g. p.GetTaskReferenceTable({'Chicken Coop', 'dungeon'})
-- name = item or monster name
-- type = 'item' or 'monster' or 'dungeon'
function p.GetTaskReferenceTable(frame)
-- Returns a set containing all the desired IDs
local function GetReferenceIDs(referenceName, referenceType)
local IDs = {}
if referenceType == 'dungeon' then
-- We get the tasks associated with all monsters in the dungeon
local monsters = GameData.getEntityByName('dungeons', referenceName).monsterIDs
for _, monster in ipairs(monsters) do
IDs[monster] = true
end
end
if referenceType == 'item' then
IDs[GameData.getEntityByName('items', referenceName).id] = true
end
if referenceType == 'monster' then
IDs[GameData.getEntityByName('monsters', referenceName).id] = true
end
return IDs
end
-- For a task, returns where to search for the desired IDs, given the type
local function GetGetSearchTables(referenceType)
local function searchItems(task)
return {task.goals.items, task.rewards.items}
end
local function searchMonsters(task)
return {task.goals.monsters}
end
-- item -> searchItems; monster or dungeon -> searchMonsters
return referenceType == 'item' and searchItems or searchMonsters
end
local args = frame.args ~= nil and frame.args or frame
local referenceName = args[1]
local referenceType = args[2]
local referenceIDs = GetReferenceIDs(referenceName, referenceType)
-- GetSearchTables = function searchItems/Monsters(task)
local GetSearchTables = GetGetSearchTables(referenceType)
local function checkTask(task)
local function checkID(entry)
return referenceIDs[entry.id] ~= nil
end
for _, searchTable in ipairs(GetSearchTables(task)) do
-- Check to see if the table contains any of the IDs in referenceIDs
if searchTable[1] ~= nil then -- Make sure table is not empty
if #GameData.getEntities(searchTable, checkID) ~= 0 then -- Make sure we have at least 1 match
return true
end
end
end
return false
end
-- Find all tasks that contain the desired ids
local tasks = GameData.getEntities(Township.tasks, checkTask)
-- Build the table
local ret = {}
table.insert(ret, '\r\n{| class="wikitable" style="text-align:left"')
table.insert(ret, '\r\n!Task')
table.insert(ret, '\r\n!Requirements')
table.insert(ret, '\r\n!Rewards')
for _, task in ipairs(tasks) do
local categoryname = GameData.getEntityByID(Township.taskCategories, task.category).name
local title = '[[Township/Tasks#'..categoryname..'|'..categoryname..']]'
table.insert(ret, p._GetTaskRow(title, task))
end
end
table.insert(ret, '\r\n|}')
table.insert(ret, '\r\n|}')
572

edits