CraftSystem: add ImmersiveCraftCounter`
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
local posToString, getSize = core.pos_to_string, table.get_size
|
||||
|
||||
local CRAFT_LIMIT_PER_PLAYER = 2
|
||||
local CRAFT_LIMIT_PER_SERVER = 10
|
||||
|
||||
---Designed to keep track of active immersive
|
||||
---crafts so as not to overload the server.
|
||||
---
|
||||
---The counter is individual for each player,
|
||||
---but the total number of active crafts is also tracked.
|
||||
---@class CraftSystem.ImmersiveCraftCounter
|
||||
---@field craftsCountForPlayers table<string, table<string, boolean>>
|
||||
---@field craftsCountForServer table<string, boolean>
|
||||
local ImmersiveCraftCounter = {
|
||||
craftsCountForPlayers = {},
|
||||
craftsCountForServer = {}
|
||||
}
|
||||
|
||||
---Add craft to the counter.
|
||||
---
|
||||
---Returns `false` if the counter is already full.
|
||||
---@param pos MapPosition
|
||||
---@param Player Player
|
||||
---@return boolean
|
||||
function ImmersiveCraftCounter:addCraft(pos, Player)
|
||||
local playerName = Player:get_player_name()
|
||||
if self.craftsCountForPlayers[playerName] == nil then
|
||||
self.craftsCountForPlayers[playerName] = {}
|
||||
end
|
||||
|
||||
if getSize(self.craftsCountForPlayers[playerName]) >= CRAFT_LIMIT_PER_PLAYER then
|
||||
return false
|
||||
end
|
||||
|
||||
if getSize(self.craftsCountForServer) >= CRAFT_LIMIT_PER_SERVER then
|
||||
return false
|
||||
end
|
||||
|
||||
local posString = posToString(pos)
|
||||
self.craftsCountForPlayers[playerName][posString] = true
|
||||
self.craftsCountForServer[posString] = true
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
---Remove craft from the counter.
|
||||
---@param pos MapPosition
|
||||
---@param Player Player
|
||||
function ImmersiveCraftCounter:removeCraft(pos, Player)
|
||||
local playerCrafts = self.craftsCountForPlayers[Player:get_player_name()]
|
||||
if playerCrafts == nil then
|
||||
return
|
||||
end
|
||||
|
||||
local posString = posToString(pos)
|
||||
playerCrafts[posString] = nil
|
||||
self.craftsCountForServer[posString] = nil
|
||||
end
|
||||
|
||||
-- TODO: сделать сохранение данных во время перезагрузки сервера
|
||||
|
||||
return ImmersiveCraftCounter
|
||||
Reference in New Issue
Block a user