CraftSystem: add player progression support
This commit is contained in:
@@ -3,18 +3,22 @@
|
||||
local CHECK = {YES = '[x]', NO = '[ ]', EMPTY = '-'}
|
||||
|
||||
---@class CraftSystem.BaseCraft : Class
|
||||
---@field name string
|
||||
---@field hidden boolean?
|
||||
---@field recipe ItemStackString[]
|
||||
---@field output ItemStackString
|
||||
---@field group CraftSystem.CraftGroup
|
||||
---@field name string
|
||||
---@field hidden boolean?
|
||||
---@field recipe ItemStackString[]
|
||||
---@field output ItemStackString
|
||||
---@field age ProgressionSystem.Age.Name,
|
||||
---@field milestone string?
|
||||
---@field group CraftSystem.CraftGroup
|
||||
local BaseCraft = Class:extend({})
|
||||
|
||||
---Definition table for the `CraftSystem.BaseCraft`.
|
||||
---@class CraftSystem.BaseCraftDef
|
||||
---@field recipe ItemStackString[]
|
||||
---@field output ItemStackString
|
||||
---@field group CraftSystem.CraftGroup?
|
||||
---@field recipe ItemStackString[]
|
||||
---@field output ItemStackString
|
||||
---@field age ProgressionSystem.Age.Name?
|
||||
---@field milestone string?
|
||||
---@field group CraftSystem.CraftGroup?
|
||||
|
||||
---@param name string
|
||||
---@param def CraftSystem.BaseCraftDef
|
||||
@@ -22,10 +26,12 @@ local BaseCraft = Class:extend({})
|
||||
function BaseCraft:new(name, def)
|
||||
---@type CraftSystem.BaseCraft
|
||||
local instance = self:getInstance({})
|
||||
instance.name = name
|
||||
instance.recipe = def.recipe
|
||||
instance.output = def.output
|
||||
instance.group = def.group
|
||||
instance.name = name
|
||||
instance.recipe = def.recipe
|
||||
instance.output = def.output
|
||||
instance.age = def.age
|
||||
instance.milestone = def.milestone
|
||||
instance.group = def.group
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
local tableInsert = table.insert
|
||||
local stringMatch = string.match
|
||||
local assert, pairs, ipairs = assert, pairs, ipairs
|
||||
|
||||
local tableInsert, tableSort, tableJoin = table.insert, table.sort, table.join
|
||||
local stringMatch, stringLower = string.match, string_utf8.lower
|
||||
|
||||
local require = Mod.getInfo('smc__core__craft_system').require
|
||||
|
||||
---@type CraftSystem.BaseCraftHandler.PlayerCraftsCache
|
||||
local PlayerCraftsCache = require('CraftSystem.BaseCraftHandler.PlayerCraftsCache')
|
||||
---@type CraftSystem.BaseCraftHandler.ProgressionCraftsCache
|
||||
local ProgressionCraftsCache = require('CraftSystem.BaseCraftHandler.ProgressionCraftsCache')
|
||||
|
||||
local PLAYERS_KNOW_ALL_RECIPES = true
|
||||
|
||||
local PLAYERS_KNOW_ALL_RECIPES = false
|
||||
|
||||
---Abstract class for handling crafting.
|
||||
---@class CraftSystem.BaseCraftHandler : Class
|
||||
@@ -22,6 +25,9 @@ local PLAYERS_KNOW_ALL_RECIPES = true
|
||||
---@field groupedCrafts (CraftSystem.BaseCraft[]|CraftSystem.BaseCraft)[]
|
||||
---
|
||||
---@field playersCraftsCache table<string, CraftSystem.BaseCraftHandler.PlayerCraftsCache>
|
||||
---
|
||||
---@field agesCrafts table<ProgressionSystem.Age.Name, CraftSystem.BaseCraftHandler.ProgressionCraftsCache>
|
||||
---@field milestonesCrafts table<ProgressionSystem.Age.Name, table<string, CraftSystem.BaseCraftHandler.ProgressionCraftsCache>>
|
||||
local BaseCraftHandler = Class:extend({
|
||||
craftsByName = {},
|
||||
craftsByGroup = {},
|
||||
@@ -29,7 +35,10 @@ local BaseCraftHandler = Class:extend({
|
||||
craftsList = {},
|
||||
groupedCrafts = {},
|
||||
|
||||
playersCraftsCache = {}
|
||||
playersCraftsCache = {},
|
||||
|
||||
agesCrafts = {},
|
||||
milestonesCrafts = {},
|
||||
})
|
||||
|
||||
---@class CraftSystem.BaseCraftHandlerDef
|
||||
@@ -46,22 +55,83 @@ function BaseCraftHandler:new(baseType, type, def)
|
||||
instance.type = type
|
||||
instance.defaultCraftDef = def.defaultCraftDef or {}
|
||||
|
||||
-- Initializing the temporary crafting cache for players.
|
||||
core.register_on_joinplayer(function (player, last_login)
|
||||
local playerName = player:get_player_name()
|
||||
local cache = PlayerCraftsCache:new(playerName)
|
||||
cache:update(instance.agesCrafts, instance.agesCrafts)
|
||||
|
||||
self.playersCraftsCache[playerName] = cache
|
||||
instance.playersCraftsCache[playerName] = cache
|
||||
end)
|
||||
|
||||
-- Clearing the temporary crafting cache for players.
|
||||
core.register_on_leaveplayer(function (player, timed_out)
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
self.playersCraftsCache[playerName] = nil
|
||||
instance.playersCraftsCache[playerName] = nil
|
||||
end)
|
||||
|
||||
-- Initializing the list of crafts by groups.
|
||||
core.register_on_mods_loaded(function()
|
||||
for groupName, crafts in pairs(instance.craftsByGroup) do
|
||||
tableSort(crafts, function(a, b)
|
||||
if a.output < b.output then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end)
|
||||
|
||||
if groupName ~= 'craft_group:UNDEFINED' then
|
||||
tableInsert(instance.groupedCrafts, crafts)
|
||||
else
|
||||
instance.groupedCrafts = tableJoin(instance.groupedCrafts, crafts)
|
||||
end
|
||||
end
|
||||
|
||||
tableSort(instance.groupedCrafts, function(a, b)
|
||||
local outputA = (a[1] and a[1].output) or a.output
|
||||
local outputB = (b[1] and b[1].output) or b.output
|
||||
|
||||
if outputA < outputB then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end)
|
||||
end)
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---Adds craft to its corresponding progression caches.
|
||||
---@param craft CraftSystem.BaseCraft
|
||||
function BaseCraftHandler:addCraftToProgressionTable(craft)
|
||||
local age = craft.age or 'zero'
|
||||
local milestone = craft.milestone
|
||||
|
||||
if milestone ~= nil then
|
||||
if self.milestonesCrafts[age] == nil then
|
||||
self.milestonesCrafts[age] = {}
|
||||
end
|
||||
|
||||
if self.milestonesCrafts[age][milestone] == nil then
|
||||
self.milestonesCrafts[age][milestone] = ProgressionCraftsCache:new()
|
||||
end
|
||||
|
||||
self.milestonesCrafts[age][milestone]:addCraft(craft)
|
||||
else
|
||||
if self.agesCrafts[age] == nil then
|
||||
self.agesCrafts[age] = ProgressionCraftsCache:new()
|
||||
end
|
||||
|
||||
self.agesCrafts[age]:addCraft(craft)
|
||||
end
|
||||
end
|
||||
|
||||
---Returns a temporary cache of the player's crafts.
|
||||
---
|
||||
---If the player is not online, an error will be raised.
|
||||
---@param playerName string
|
||||
---@return CraftSystem.BaseCraftHandler.PlayerCraftsCache
|
||||
function BaseCraftHandler:getPlayerCraftsCache(playerName)
|
||||
@@ -165,14 +235,14 @@ end
|
||||
---@param pattern string
|
||||
---@return CraftSystem.BaseCraft[]
|
||||
local function getMathedCrafts(crafts, pattern)
|
||||
local pattern = string_utf8.lower(pattern)
|
||||
local pattern = stringLower(pattern)
|
||||
local matchedCrafts = {}
|
||||
|
||||
for _, craft in ipairs(crafts) do
|
||||
local outputItem = ItemStack(craft.output)
|
||||
local itemSettings = Smc.getItemDefinition(outputItem).settings
|
||||
local ruName = string_utf8.lower(core.get_translated_string('ru', itemSettings._title))
|
||||
local enName = string_utf8.lower(core.get_translated_string('en', itemSettings._title))
|
||||
local ruName = stringLower(core.get_translated_string('ru', itemSettings._title))
|
||||
local enName = stringLower(core.get_translated_string('en', itemSettings._title))
|
||||
|
||||
if stringMatch(outputItem:get_name(), pattern) or
|
||||
stringMatch(ruName, pattern) or
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
local assert, pairs = assert, pairs
|
||||
|
||||
local tableMerge, tableJoin, tableInsert, tableSort = table.merge, table.join, table.insert, table.sort
|
||||
|
||||
local ProgressionSystem = Smc.requireProgressionSystem()
|
||||
|
||||
---A player's temporary cache containing known crafts
|
||||
---in the form of lists of various sorting types.
|
||||
---
|
||||
---Designed for quick access to the player's learned crafts.
|
||||
---
|
||||
---Initialized when a player joins the server and cleared when the player leaves the server.
|
||||
---
|
||||
---For more details see [BaseCraftHandler:new()](lua://CraftSystem.BaseCraftHandler)
|
||||
---@class CraftSystem.BaseCraftHandler.PlayerCraftsCache : Class
|
||||
---@field playerName string
|
||||
---
|
||||
@@ -22,14 +34,108 @@ function PlayerCraftsCache:new(playerName)
|
||||
local instance = self:getInstance({})
|
||||
instance.playerName = playerName
|
||||
|
||||
instance:update()
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
-- TODO
|
||||
function PlayerCraftsCache:update()
|
||||
-- ProgressionSystem:
|
||||
---Adds all crafts from the passed cache to the player's cache.
|
||||
---
|
||||
---**Note**: Doesn't init (update) the list of grouped recipes (`self.groupedCrafts`).
|
||||
---
|
||||
---To initialize (update), use `self:initGroupedCrafts()`.
|
||||
---@param cache CraftSystem.BaseCraftHandler.ProgressionCraftsCache
|
||||
function PlayerCraftsCache:addCraftsByProgressionCache(cache)
|
||||
self.craftsByName = tableMerge(self.craftsByName, cache.craftsByName, false)
|
||||
for group, crafts in pairs(cache.craftsByGroup) do
|
||||
local list = self.craftsByGroup[group]
|
||||
if list == nil then
|
||||
self.craftsByGroup[group] = crafts
|
||||
else
|
||||
self.craftsByGroup[group] = tableJoin(list, crafts)
|
||||
end
|
||||
end
|
||||
|
||||
for output, crafts in pairs(cache.craftsByOutput) do
|
||||
local list = self.craftsByOutput[output]
|
||||
if list == nil then
|
||||
self.craftsByOutput[output] = crafts
|
||||
else
|
||||
self.craftsByOutput[output] = tableJoin(list, crafts)
|
||||
end
|
||||
end
|
||||
|
||||
self.craftsList = tableJoin(self.craftsList, cache.craftsList)
|
||||
end
|
||||
|
||||
---Initializes a list of grouped crafts based
|
||||
---on the crafts contained in the cache (uses `self.craftsByGroup`).
|
||||
function PlayerCraftsCache:initGroupedCrafts()
|
||||
for groupName, crafts in pairs(self.craftsByGroup) do
|
||||
tableSort(crafts, function(a, b)
|
||||
if a.output < b.output then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end)
|
||||
|
||||
if groupName ~= 'craft_group:UNDEFINED' then
|
||||
tableInsert(self.groupedCrafts, crafts)
|
||||
else
|
||||
self.groupedCrafts = tableJoin(self.groupedCrafts, crafts)
|
||||
end
|
||||
end
|
||||
|
||||
tableSort(self.groupedCrafts, function(a, b)
|
||||
local outputA = (a[#a] and a[#a].output) or a.output
|
||||
local outputB = (b[#b] and b[#b].output) or b.output
|
||||
|
||||
if outputA < outputB then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---Overwrites the player's craft cache based on the passed global progression craft caches.
|
||||
---@param agesCrafts table<ProgressionSystem.Age.Name, CraftSystem.BaseCraftHandler.ProgressionCraftsCache>
|
||||
---@param milestonesCrafts table<ProgressionSystem.Age.Name, table<string, CraftSystem.BaseCraftHandler.ProgressionCraftsCache>>
|
||||
function PlayerCraftsCache:update(agesCrafts, milestonesCrafts)
|
||||
self.craftsByName = {}
|
||||
self.craftsByGroup = {}
|
||||
self.craftsByOutput = {}
|
||||
self.craftsList = {}
|
||||
self.groupedCrafts = {}
|
||||
|
||||
local player = core.get_player_by_name(self.playerName)
|
||||
assert(player ~= nil)
|
||||
|
||||
local playerMeta = player:get_meta()
|
||||
local ageID = ProgressionSystem:getPlayerAge(playerMeta)
|
||||
local milestones = ProgressionSystem:getPlayerMilestones(playerMeta)
|
||||
|
||||
for i = 0, ageID do
|
||||
local ageName = ProgressionSystem:getAgeByID(i).name
|
||||
|
||||
local ageCache = agesCrafts[ageName]
|
||||
if ageCache ~= nil then
|
||||
self:addCraftsByProgressionCache(ageCache)
|
||||
end
|
||||
|
||||
local milestonesList = milestones[ageID] or {}
|
||||
local milestoneCache
|
||||
for milestoneName, _ in pairs(milestonesList) do
|
||||
if milestonesCrafts[ageName] ~= nil then
|
||||
milestoneCache = milestonesCrafts[ageName][milestoneName]
|
||||
|
||||
if milestoneCache ~= nil then
|
||||
self:addCraftsByProgressionCache(milestoneCache)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
self:initGroupedCrafts()
|
||||
end
|
||||
|
||||
return PlayerCraftsCache
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
local tableInsert = table.insert
|
||||
|
||||
---Crafting cache for individual stages of global progression.
|
||||
---
|
||||
---Designed to store crafting associated with edges and milestones while the server is running,
|
||||
---allowing for quick access to the crafting list.
|
||||
---@class CraftSystem.BaseCraftHandler.ProgressionCraftsCache : Class
|
||||
---@field craftsByName table<string, CraftSystem.BaseCraft>
|
||||
---@field craftsByGroup table<CraftSystem.CraftGroup, CraftSystem.BaseCraft[]>
|
||||
---@field craftsByOutput table<ItemStackString, CraftSystem.BaseCraft[]>
|
||||
---@field craftsList CraftSystem.BaseCraft[]
|
||||
local ProgressionCraftsCache = Class:extend({
|
||||
craftsByName = {},
|
||||
craftsByGroup = {},
|
||||
craftsByOutput = {},
|
||||
craftsList = {},
|
||||
})
|
||||
|
||||
---@return CraftSystem.BaseCraftHandler.ProgressionCraftsCache
|
||||
function ProgressionCraftsCache:new()
|
||||
local instance = self:getInstance({})
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---Adds a craft to the cache.
|
||||
---
|
||||
---**Note**: If `craft.hidden = true`, the craft will not be added to the list of crafts by group.
|
||||
---@param craft CraftSystem.BaseCraft
|
||||
function ProgressionCraftsCache:addCraft(craft)
|
||||
local group = craft.group
|
||||
if self.craftsByGroup[group] == nil and craft.hidden ~= true then
|
||||
self.craftsByGroup[group] = {}
|
||||
end
|
||||
|
||||
local output = ItemStack(craft.output):get_name()
|
||||
if self.craftsByOutput[output] == nil then
|
||||
self.craftsByOutput[output] = {}
|
||||
end
|
||||
|
||||
local craftName = craft.name
|
||||
self.craftsByName[craftName] = craft
|
||||
if craft.hidden ~= true then
|
||||
tableInsert(self.craftsByGroup[group], craft)
|
||||
end
|
||||
tableInsert(self.craftsByOutput[output], craft)
|
||||
tableInsert(self.craftsList, craft)
|
||||
end
|
||||
|
||||
return ProgressionCraftsCache
|
||||
@@ -1,4 +1,6 @@
|
||||
local tableInsert, tableSort, tableJoin = table.insert, table.sort, table.join
|
||||
local assert = assert
|
||||
|
||||
local tableInsert, tableMerge = table.insert, table.merge
|
||||
|
||||
local modInfo = Mod.getInfo('smc__core__craft_system')
|
||||
local require = modInfo.require
|
||||
@@ -27,7 +29,7 @@ function ImmersiveCraftHandler:registerCraft(craftName, def)
|
||||
assert(self.craftsByName[craftName] == nil, ('A craft named `%s` already exists.'):format(
|
||||
craftName))
|
||||
|
||||
local def = table.merge(def, self.defaultCraftDef)
|
||||
local def = tableMerge(def, self.defaultCraftDef)
|
||||
|
||||
local group = 'craft_group:' .. (def.group or 'UNDEFINED')
|
||||
def.group = group
|
||||
@@ -41,37 +43,8 @@ function ImmersiveCraftHandler:registerCraft(craftName, def)
|
||||
self.craftsByName[craftName] = craft
|
||||
tableInsert(self.craftsByGroup[group], craft)
|
||||
tableInsert(self.craftsList, craft)
|
||||
|
||||
self:addCraftToProgressionTable(craft)
|
||||
end
|
||||
|
||||
-- Sort the list of crafts in alphabetical order by product name and combine crafts into groups.
|
||||
-- Provides a convenient list of crafting items in the player's inventory.
|
||||
core.register_on_mods_loaded(function()
|
||||
for groupName, crafts in pairs(ImmersiveCraftHandler.craftsByGroup) do
|
||||
tableSort(crafts, function(a, b)
|
||||
if a.output < b.output then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end)
|
||||
|
||||
if groupName ~= 'craft_group:UNDEFINED' then
|
||||
tableInsert(ImmersiveCraftHandler.groupedCrafts, crafts)
|
||||
else
|
||||
ImmersiveCraftHandler.groupedCrafts = tableJoin(ImmersiveCraftHandler.groupedCrafts, crafts)
|
||||
end
|
||||
end
|
||||
|
||||
tableSort(ImmersiveCraftHandler.groupedCrafts, function(a, b)
|
||||
local outputA = (a[1] and a[1].output) or a.output
|
||||
local outputB = (b[1] and b[1].output) or b.output
|
||||
|
||||
if outputA < outputB then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end)
|
||||
end)
|
||||
|
||||
return ImmersiveCraftHandler
|
||||
|
||||
@@ -57,37 +57,8 @@ function PrimitiveCraftHandler:registerCraft(craftName, def)
|
||||
end
|
||||
tableInsert(self.craftsByOutput[output], craft)
|
||||
tableInsert(self.craftsList, craft)
|
||||
|
||||
self:addCraftToProgressionTable(craft)
|
||||
end
|
||||
|
||||
-- Sort the list of crafts in alphabetical order by product name and combine crafts into groups.
|
||||
-- Provides a convenient list of crafting items in the player's inventory.
|
||||
core.register_on_mods_loaded(function()
|
||||
for groupName, crafts in pairs(PrimitiveCraftHandler.craftsByGroup) do
|
||||
tableSort(crafts, function(a, b)
|
||||
if a.output < b.output then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end)
|
||||
|
||||
if groupName ~= 'craft_group:UNDEFINED' then
|
||||
tableInsert(PrimitiveCraftHandler.groupedCrafts, crafts)
|
||||
else
|
||||
PrimitiveCraftHandler.groupedCrafts = tableJoin(PrimitiveCraftHandler.groupedCrafts, crafts)
|
||||
end
|
||||
end
|
||||
|
||||
tableSort(PrimitiveCraftHandler.groupedCrafts, function(a, b)
|
||||
local outputA = (a[1] and a[1].output) or a.output
|
||||
local outputB = (b[1] and b[1].output) or b.output
|
||||
|
||||
if outputA < outputB then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end)
|
||||
end)
|
||||
|
||||
return PrimitiveCraftHandler
|
||||
|
||||
Reference in New Issue
Block a user