Module:Sandbox/GauTest/Township: Difference between revisions

WIP
No edit summary
(WIP)
Line 1: Line 1:
local Shared = require('Module:Shared')
local Shared = require('Module:Shared')
local Icons = require('Module:Icons')
local Icons = require('Module:Icons')
local Items = require('Module:Items')
local Monsters = require('Module:Monsters')
local GameData = require('Module:GameData')
local GameData = require('Module:GameData')
local Constants = require('Module:Constants')
local Constants = require('Module:Constants')
Line 304: Line 302:
function p._GetBuildingByName(name)
function p._GetBuildingByName(name)
-- Check for the special statue case
-- Check for the special statue case
if name == 'STATUE_NAME' then
if name == 'Statues' then
name = 'Statue of Worship'
name = 'Statue of Worship'
end
end
local STATUE_OF = 'Statue of '
local STATUE_OF = 'Statue of '
if string.sub(name, 1, string.len(STATUE_OF)) == STATUE_OF then
if string.sub(name, 1, string.len(STATUE_OF)) == STATUE_OF then
local building = Shared.clone(GameData.getEntityByName(Township.buildings, 'STATUE_NAME'))
local building = Shared.clone(GameData.getEntityByID(Township.buildings, 'melvorF:Statues'))
building.name = name
building.name = name
return building
return building
Line 760: Line 758:
end
end


-- Returns a row containing a task given a title and a task table
function p._GetTaskRow(title, task)
local ret = {}
-- If has description, we will need to rowspan the title by 2, and insert a description with colspan 2
local hasDescription = false
if task.description ~= nil then
hasDescription = true
end
local titlespan = hasDescription == true and 'rowspan="2"|' or ''
-- Title
table.insert(ret, '\r\n|-')
table.insert(ret, '\r\n!'..titlespan..title)
-- Description
if hasDescription then
table.insert(ret, '\r\n|colspan="2"|'..task.description)
table.insert(ret, '\r\n|-')
end
-- Requirements
table.insert(ret, '\r\n|')
local requirements = {}
for _, item in ipairs(task.goals.items) do
local itemname = GameData.getEntityByID('items', item.id).name
table.insert(requirements, Shared.formatnum(item.quantity)..' '..Icons.Icon({itemname, type='item'}))
end
for _, monster in ipairs(task.goals.monsters) do
local monstername = GameData.getEntityByID('monsters', monster.id).name
table.insert(requirements, Shared.formatnum(monster.quantity)..' '..Icons.Icon({monstername, type='monster'}))
end
for _, skill in ipairs(task.goals.skillXP) do
local skillname = GameData.getSkillData(skill.id).name
table.insert(requirements, Shared.formatnum(skill.quantity)..' '..Icons.Icon({skillname, type='skill'})..' XP')
end
for _, building in ipairs(task.goals.buildings) do
local buildingname = p._GetBuildingByID(building.id).name
table.insert(requirements, Shared.formatnum(building.quantity)..' '..Icons.Icon({buildingname, type='building'}))
end
-- We don't check tasks.requirements (so far it's only used to enumerate the Tutorial tasks so you only see 1 at a time)
table.insert(ret, table.concat(requirements, '<br>'))
-- Rewards
table.insert(ret, '\r\n|')
local rewards = {}
if task.rewards.gp ~= 0 then
table.insert(rewards, Icons.GP(task.rewards.gp))
end
if task.rewards.slayerCoins ~= 0 then
table.insert(rewards, Icons.SC(task.rewards.slayerCoins))
end
for _, item in ipairs(task.rewards.items) do
local itemname = GameData.getEntityByID('items', item.id).name
table.insert(rewards, Shared.formatnum(item.quantity)..' '..Icons.Icon({itemname, type='item'}))
end
for _, skill in ipairs(task.rewards.skillXP) do
local skillname = GameData.getSkillData(skill.id).name
table.insert(rewards, Shared.formatnum(skill.quantity)..' '..Icons.Icon({skillname, type='skill'})..' XP')
end
for _, townshipResource in ipairs(task.rewards.townshipResources) do
local resourcename = p._GetResourceByID(townshipResource.id).name
table.insert(rewards, Shared.formatnum(townshipResource.quantity)..' '..Icons.Icon({resourcename, type='resource'}))
end
table.insert(ret, table.concat(rewards, '<br>'))
return table.concat(ret)
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 774: Line 838:
-- Filter out other categories
-- Filter out other categories
if task.category == category then
if task.category == category then
-- If has description, we will need to rowspan the title by 2, and insert a description with colspan 2
local hasDescription = false
if task.description ~= nil then
hasDescription = true
end
local titlespan = hasDescription == true and 'rowspan="2"|' or ''
-- Title
table.insert(ret, '\r\n|-')
taskcount = taskcount + 1
taskcount = taskcount + 1
table.insert(ret, '\r\n!'..titlespan..categoryname..' '..taskcount)
local title = categoryname..' '..taskcount
-- Description
table.insert(ret, p._GetTaskRow(title, task))
if hasDescription then
end
table.insert(ret, '\r\n|colspan="2"|'..task.description)
end
table.insert(ret, '\r\n|-')
table.insert(ret, '\r\n|}')
return table.concat(ret)
end
 
-- Returns a table containing all the tasks that reference an item or monster
-- 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', name).monsterIDs
for _, monster in ipairs(monsters) do
IDs[v] = monster
end
end
-- Requirements
end
table.insert(ret, '\r\n|')
if referenceType == 'item' then
local requirements = {}
IDs[GameData.getEntityByName('items', referenceName).id] = true
for _, item in ipairs(task.goals.items) do
end
local itemname = Items.getItemByID(item.id).name
if referenceType == 'monster' then
table.insert(requirements, Shared.formatnum(item.quantity)..' '..Icons.Icon({itemname, type='item'}))
IDs[GameData.getEntityByName('monsters', referenceName).id] = true
end
end
for _, monster in ipairs(task.goals.monsters) do
return IDs
local monstername = Monsters.getMonsterByID(monster.id).name
end
table.insert(requirements, Shared.formatnum(monster.quantity)..' '..Icons.Icon({monstername, type='monster'}))
-- For a task, returns where to search for the desired IDs, given the type
end
local function GetGetSearchTables(referenceType)
for _, skill in ipairs(task.goals.skillXP) do
local function searchItems(task)
local skillname = GameData.getSkillData(skill.id).name
return {task.goals.items, task.rewards.items}
table.insert(requirements, Shared.formatnum(skill.quantity)..' '..Icons.Icon({skillname, type='skill'})..' XP')
end
end
local function searchMonsters(task)
for _, building in ipairs(task.goals.buildings) do
return {task.goals.monsters}
local buildingname = p._GetBuildingByID(building.id).name
end
table.insert(requirements, Shared.formatnum(building.quantity)..' '..Icons.Icon({buildingname, type='building'}))
-- item -> searchItems; monster or dungeon -> searchMonsters
end
return referenceType == 'item' and searchItems or searchMonsters
-- We don't check tasks.requirements (so far it's only used to enumerate the Tutorial tasks so you only see 1 at a time)
end
table.insert(ret, table.concat(requirements, '<br>'))
-- Rewards
local args = frame.args ~= nil and frame.args or frame
table.insert(ret, '\r\n|')
local referenceName = args[1]
local rewards = {}
local referenceType = args[2]
if task.rewards.gp ~= 0 then
local referenceIDs = GetReferenceIDs(referenceName, referenceType)
table.insert(rewards, Icons.GP(task.rewards.gp))
-- GetSearchTables = function searchItems/Monsters(task)
end
local GetSearchTables = GetGetSearchTables(referenceType)
if task.rewards.slayerCoins ~= 0 then
local function checkTask(task)
table.insert(rewards, Icons.SC(task.rewards.slayerCoins))
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 GameData.getEntities(searchTable, checkID) ~= nil then
return true
end
end
for _, item in ipairs(task.rewards.items) do
local itemname = Items.getItemByID(item.id).name
table.insert(rewards, Shared.formatnum(item.quantity)..' '..Icons.Icon({itemname, type='item'}))
end
for _, skill in ipairs(task.rewards.skillXP) do
local skillname = GameData.getSkillData(skill.id).name
table.insert(rewards, Shared.formatnum(skill.quantity)..' '..Icons.Icon({skillname, type='skill'})..' XP')
end
for _, townshipResource in ipairs(task.rewards.townshipResources) do
local resourcename = p._GetResourceByID(townshipResource.id).name
table.insert(rewards, Shared.formatnum(townshipResource.quantity)..' '..Icons.Icon({resourcename, type='resource'}))
end
table.insert(ret, table.concat(rewards, '<br>'))
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