Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 47 additions & 36 deletions features/player_logistic_requests.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
-- This module saves players' logistic requests slots between maps
-- Dependencies

--TODO: Rewrite, feature deprecated for 2.0
--[[
local Color = require 'resources.color_presets'
local Command = require 'utils.command'
local Event = require 'utils.event'
local Game = require 'utils.game'
local Global = require 'utils.global'
local LogisticPoint = require 'utils.logistic_point'
local Ranks = require 'resources.ranks'
local Server = require 'features.server'
local Token = require 'utils.token'
local Color = require 'resources.color_presets'
local Ranks = require 'resources.ranks'

-- Constants
local data_set_name = 'player_logistic_requests'
local logistic_slots = 100
local logistic_slots_limit = 100

-- Localized globals
local primitives = {
Expand All @@ -39,42 +38,42 @@ Global.register(
--- Scans all player's logistic request slots into a table, then saves that table server-side
local function save_bars(_, player)
if not primitives.server_available then
Game.player_print({'common.server_unavailable'}, Color.fail)
Game.player_print({'common.server_unavailable'}, Color.fail, player)
return
end

-- Player's force doesn't have logistics
local force = player.force
if not (force and force.character_logistic_requests) then
Game.player_print({'player_logistic_requests.logistics_not_available'}, Color.fail)
Game.player_print({'player_logistic_requests.logistics_not_available'}, Color.fail, player)
return
end

local bars = {}
-- Validate player's LuaLogisticPoint
local requester_point = player.get_requester_point()
if not (requester_point and requester_point.valid) then
Game.player_print({'player_logistic_requests.invalid_logistic_point'}, Color.fail, player)
return
end

for i = 1, logistic_slots do
local item_prot = player.get_personal_logistic_slot(i)
if item_prot and item_prot.name then
bars[i] = item_prot
-- Trim saved slots to limit
local bars = LogisticPoint.get_filters(requester_point)
if #bars > logistic_slots_limit then
for i = #bars, (logistic_slots_limit + 1), -1 do
bars[i] = nil
end
end

-- Save data to db
Server.set_data(data_set_name, player.name, bars)
Game.player_print({'player_logistic_requests.save_bars'}, Color.success)
Game.player_print({'player_logistic_requests.save_bars'}, Color.success, player)
end

--- Returns a valid entity prototype string name or nil.
-- For invalid items, a message will be printed to the player.
local function validate_entry(item, proto_table, player)
if not (item and item.name) then
return
end

if proto_table[item.name] then
return item
local function get_player_group(player)
if not (player and player.valid and player.name) then
return ''
end

player.print({'player_logistic_requests.incompatible_item', item.name}, {color = Color.warning})
return 'RedMew::'..player.name
end

--- Sets the logistic request slots of a player.
Expand All @@ -93,35 +92,48 @@ local set_bars_callback =
return
end

local item_prototypes = prototypes.item
local item
for i = 1, logistic_slots do
item = validate_entry(bars[i], item_prototypes, player)
if item then
player.set_personal_logistic_slot(i, item) -- false if personal logistics are not researched yet.
-- Validate player's LuaLogisticPoint
local requester_point = player.get_requester_point()
if not (requester_point and requester_point.valid) then
Game.player_print({'player_logistic_requests.invalid_logistic_point'}, Color.fail, player)
return
end

-- Validate entries
for i = #bars, 1, -1 do
local name = (bars[i].value and bars[i].value.name)
if (name == nil) or (type(name) ~= 'string') then
bars[i] = nil
else
if not prototypes.item[name] then
Game.player_print({'player_logistic_requests.incompatible_item', name}, Color.warning, player)
bars[i] = nil
end
end
end

LogisticPoint.add_filters(requester_point, bars, get_player_group(player))
Game.player_print({'player_logistic_requests.load_bars_success'}, Color.success, player)
initialized_players[player.name] = true
end
)

--- Calls data from the server and sends it to the set_bars_callback
local function load_bars(_, player)
if not primitives.server_available then
Game.player_print({'common.server_unavailable'}, Color.fail)
Game.player_print({'common.server_unavailable'}, Color.fail, player)
return
end

-- Player's force doesn't have logistics
local force = player.force
if not (force and force.character_logistic_requests) then
Game.player_print({'player_logistic_requests.logistics_not_available'}, Color.fail)
Game.player_print({'player_logistic_requests.logistics_not_available'}, Color.fail, player)
return
end

Server.try_get_data(data_set_name, player.name, set_bars_callback)
Game.player_print({'player_logistic_requests.load_bars'})
Game.player_print({'player_logistic_requests.load_bars'}, Color.success, player)
end

-- Auto loads all logistic requests for players joining after logistics has been researched
Expand Down Expand Up @@ -189,12 +201,12 @@ end
--- Erases server-side data stored for this player's logistic requests slots
local function delete_bars(_, player)
if not primitives.server_available then
Game.player_print({'common.server_unavailable'}, Color.fail)
Game.player_print({'common.server_unavailable'}, Color.fail, player)
return
end

Server.set_data(data_set_name, player.name, nil)
Game.player_print({'player_logistic_requests.delete_bars'}, Color.success)
Game.player_print({'player_logistic_requests.delete_bars'}, Color.success, player)
end

-- Events
Expand Down Expand Up @@ -228,4 +240,3 @@ Command.add(
},
delete_bars
)
]]
2 changes: 2 additions & 0 deletions locale/en/redmew_features.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ incompatible_item=Incompatible item found in saved data and will not be loaded:
[player_logistic_requests]
save_bars=Your logistic requests have been saved to the server.
load_bars=Attempting to load logistic requests from server...
load_bars_success=...logistic requests load complete.
delete_bars=Saved data has been removed from the server.
logistics_not_available=Command failed: player`s logistics not available
incompatible_item=Incompatible item found in saved data and will not be loaded: __1__
invalid_logistic_point=Command failed: invalid player`s logistics

[player_stats]
rocks_smashed=Rocks smashed
Expand Down
5 changes: 3 additions & 2 deletions utils/logistic_point.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ end

---@param logistic_point LuaLogisticPoint
---@param filters table<LuaItemStack|SignalFilter>
Public.add_filters = function(logistic_point, filters)
local section = logistic_point.add_section()
---@param group? string
Public.add_filters = function(logistic_point, filters, group)
local section = logistic_point.add_section(group)
for index, filter in pairs(filters) do
section.set_slot(parse_item_stack(filter), index)
end
Expand Down