5 Commits

12 changed files with 332 additions and 103 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,19 @@
---@meta
---@alias PlayerMetaRef.IntKey
---| 'progression_system.player_age'
---@alias PlayerMetaRef.FloatKey string
---@alias PlayerMetaRef.StringKey string
---| 'progression_system.player_milestones'
-- Overriding PlayerMetaRef for annotations and metadata field hints.
---@class PlayerMetaRef : MetaDataRef
---@field get_int fun(self, key:PlayerMetaRef.IntKey):integer
---@field set_int fun(self, key:PlayerMetaRef.IntKey, value:integer)
---@field get_float fun(self, key:PlayerMetaRef.FloatKey, string):number
---@field set_float fun(self, key:PlayerMetaRef.FloatKey, string, value:number)
---@field get_string fun(self, key:PlayerMetaRef.StringKey):string
---@field set_string fun(self, key:PlayerMetaRef.StringKey, value:string)

View File

@@ -23,7 +23,7 @@ zeroAge.id = 0
local ProgressionSystem = {
agesByName = { ['zero'] = zeroAge, },
agesByID = { [0] = zeroAge },
serverAge = 0,
serverAge = 10,
}
---@param ageName ProgressionSystem.Age.Name
@@ -49,9 +49,9 @@ end
---@param id integer
---@return ProgressionSystem.Age
function ProgressionSystem:getAgeByID(id)
assert(self.agesByName[id] ~= nil)
assert(self.agesByID[id] ~= nil)
return self.agesByName[id]
return self.agesByID[id]
end
---@param ageName ProgressionSystem.Age.Name
@@ -86,4 +86,37 @@ function ProgressionSystem:getServerAge()
return self.serverAge
end
---@param playerMeta PlayerMetaRef
---@return integer
function ProgressionSystem:getPlayerAge(playerMeta)
return playerMeta:get_int('progression_system.player_age')
end
---@param playerMeta PlayerMetaRef
---@param ageID integer
function ProgressionSystem:setPlayerAge(playerMeta, ageID)
assert(ageID <= ProgressionSystem.serverAge, 'The players progress age cannot exceed the servers progress age.')
playerMeta:set_int('progression_system.player_age', ageID)
end
---The table of progression milestones, which is stored in the player's metadata.
---
---Milestones are divided into their Ages and are listed by name.
---@alias ProgressionSystem.PlayerMilestones table<integer, table<string, ProgressionSystem.Milestone>>
---@param playerMeta PlayerMetaRef
---@return ProgressionSystem.PlayerMilestones
function ProgressionSystem:getPlayerMilestones(playerMeta)
local milestones = core.deserialize(playerMeta:get_string('progression_system.player_milestones')) or {}
return milestones
end
---@param playerMeta PlayerMetaRef
---@param milestones ProgressionSystem.PlayerMilestones
function ProgressionSystem:setPlayerMilestones(playerMeta, milestones)
playerMeta:set_string('progression_system.player_milestones', core.serialize(milestones))
end
return ProgressionSystem

View File

@@ -4,7 +4,6 @@ local tableInsert = table.insert
---@alias ProgressionSystem.Age.Name
---| 'zero'
---| ''
---@class ProgressionSystem.Age : Class
---@field name ProgressionSystem.Age.Name

View File

@@ -5,12 +5,16 @@
-- so global tables need to be preloaded manually.
-- --- Global APIs load --
-- Class
dofile(core.get_modpath('smc__system__class')..'/init.lua')
-- Ensure
dofile(core.get_modpath('smc__system__ensure')..'/init.lua')
-- Table
dofile(core.get_modpath('smc__system__table')..'/init.lua')
-- Class
dofile(core.get_modpath('smc__system__class')..'/init.lua')
-- Logger
dofile(core.get_modpath('smc__system__logger')..'/init.lua')
@@ -34,9 +38,6 @@ dofile(core.get_modpath('smc__system__mod')..'/init.lua')
-- String
-- dofile(core.get_modpath('smc__system__string')..'/init.lua')
-- Table
dofile(core.get_modpath('smc__system__table')..'/init.lua')
-- TODO: пока что закоментировано, поскольку вызывает ошибку из-за global_step
-- Smc lib loader
-- dofile(core.get_modpath('smc__system__helper_lib')..'/init.lua')

View File

@@ -1,4 +1,4 @@
local setMetaTable, tableCopy = setmetatable, table.copy_with_metatables
local tableMerge, setmetatable, tableCopy = table.merge, setmetatable, table.copy_with_metatables
---@class Class
Class = {}
@@ -7,7 +7,7 @@ Class = {}
---@param parent table
---@param child table?
function Class.extend(parent, child)
return setMetaTable(child or {}, { __index = tableCopy(parent) })
return tableMerge(child or {}, tableCopy(parent))
end
---Intended for class constructors.
@@ -25,7 +25,7 @@ function Class.getInstance(self, params, metatable)
metatable.__index = tableCopy(self)
local instance = setMetaTable(params, metatable)
local instance = setmetatable(params, metatable)
return instance
end

View File

@@ -1 +1,2 @@
name = smc__system__class
depends = smc__system__table