Skip to content

Commit

Permalink
fix: some fixes with chest and load old games
Browse files Browse the repository at this point in the history
  • Loading branch information
danbka33 committed Aug 14, 2023
1 parent c935626 commit 1741593
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 11 deletions.
2 changes: 1 addition & 1 deletion info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Fed1sServerMod",
"version": "1.1.25",
"version": "1.1.26",
"title": "Fed1sServerMod",
"author": "danbka33",
"contact": "",
Expand Down
137 changes: 128 additions & 9 deletions scripts/chests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function Chests.on_entity_created(event)
Chests.on_necromancy_chest_created(entity, player)
elseif entity.name == "yellow-chest" then
Chests.on_yellow_chest_created(entity, player)
elseif entity.name == "blue-chest" then
elseif entity.name == "blue-chest2" then
Chests.on_blue_chest_created(entity, player)
end

Expand All @@ -141,7 +141,7 @@ function Chests.on_entity_removed(event)

if entity.name == "yellow-chest" then
global.yellowChest[entity.unit_number] = nil
elseif entity.name == "blue-chest" then
elseif entity.name == "blue-chest2" then
global.blueChest[entity.unit_number] = nil
elseif entity.name == "necromancy-chest" then
global.necromancyChest[entity.unit_number] = nil
Expand Down Expand Up @@ -257,6 +257,28 @@ function Chests.on_gui_click(event)
end
end
end

--if chestType == "blue" then
--
-- local blueChestData = Chests.get_make_blue_chest(id)
--
-- if blueChestData then
-- if event.element.name == "fed1s_gui_blue_chest_start_button" then
-- blueChestData.started = true
-- elseif event.element.name == "fed1s_gui_blue_chest_stop_button" then
-- blueChestData.started = false
-- end
--
-- for _, player in pairs(game.players) do
-- local blueChestGui = player.gui.relative["fed1s_blue_chest_gui"]
--
-- if blueChestGui then
-- blueChestGui.destroy()
-- return
-- end
-- end
-- end
--end
end
end

Expand Down Expand Up @@ -395,7 +417,7 @@ function Chests.on_nth_tick_60(event)

if not Chests.check_player_inventories_for_request(player, requested, chestInventory) then
yellowChestData.players = {}
global.tick_blue_player_connected = false
global.tick_yellow_player_connected = false
yellowChestData.started = false
global.tick_yellow_player_index = nil
global.tick_yellow_state = 0
Expand Down Expand Up @@ -449,15 +471,21 @@ function Chests.on_nth_tick_60(event)

local blueIndex, blueChestData = next(global.blueChest, global.tick_blue_index)
if blueIndex then
--game.print("Picked yellow chest " .. blueIndex)
--game.print("Picked blue chest " .. blueIndex)
global.tick_blue_index = blueIndex
blueChestData.requested = {}
blueChestData.players = {}

if blueChestData.started then
local blueChestEntity = blueChestData.entity

if not blueChestEntity or not blueChestEntity.valid then
global.blueChest[global.tick_blue_index] = nil
else
for _, player in pairs(game.players) do
blueChestData.players[player.index] = player
end

global.tick_blue_state = 1
for i = 1, blueChestData.entity.request_slot_count do
local slot = blueChestData.entity.get_request_slot(i);
Expand All @@ -466,6 +494,21 @@ function Chests.on_nth_tick_60(event)
blueChestData.requested[slot.name] = slot.count
end
end

local chestInventory = blueChestEntity.get_inventory(defines.inventory.chest);
if chestInventory then
for itemName, count in pairs(chestInventory.get_contents()) do
if itemName and count then
if blueChestData.requested[itemName] and blueChestData.requested[itemName] > 0 then
if count >= blueChestData.requested[itemName] then
blueChestData.requested[itemName] = 0
elseif count < blueChestData.requested[itemName] then
blueChestData.requested[itemName] = blueChestData.requested[itemName] - count
end
end
end
end
end
end
end
else
Expand All @@ -483,26 +526,37 @@ function Chests.on_nth_tick_60(event)

local blueChestData = global.blueChest[global.tick_blue_index]
local blueChestEntity = blueChestData.entity
local requested = blueChestData.requested

if blueChestEntity and blueChestEntity.valid then

if blueChestData.started then
-- 5 check per tick
for i = 1, 5, 1 do
local playerIndex, player = next(game.players, global.tick_blue_player_index)
local playerIndex, _ = next(blueChestData.players, global.tick_blue_player_index)

if playerIndex then
local player = game.players[playerIndex]

global.tick_blue_player_index = playerIndex

if not player.connected then
--game.print("Scout player: " .. player.name)
local requested = blueChestData.requested

local chestInventory = entity.get_inventory(defines.inventory.chest);
local chestInventory = blueChestEntity.get_inventory(defines.inventory.chest);

if not chestInventory then
break
end

Chests.check_player_inventories_for_request(player, requested, chestInventory)
if not Chests.check_player_inventories_for_request(player, requested, chestInventory) then
blueChestData.players = {}
blueChestData.started = false
global.tick_blue_player_index = nil
global.tick_blue_state = 0
game.print("Сундук сбора ресурсов оффлайн игроков полон. Сбор ресурсов остановлен", { 0, 1, 1, 1 })
break
end
end
else
global.tick_blue_player_index = nil
Expand All @@ -518,10 +572,16 @@ function Chests.on_nth_tick_60(event)
end

if not needRequest then
global.tick_blue_player_connected = false
blueChestData.started = false
blueChestData.players = {}
global.tick_blue_state = 0
global.tick_blue_player_index = nil
end
else
blueChestData.started = false
blueChestData.players = {}
global.tick_blue_state = 0
global.tick_blue_player_index = nil
end
else
-- Get next blue chest
Expand Down Expand Up @@ -604,6 +664,65 @@ function Chests.on_gui_opened(event)

end

--if entity.name == "blue-chest2" then
-- if player.permission_group.name == "Admin" or player.permission_group.name == "Manager" then
-- local blueChestData = Chests.get_make_blue_chest(entity.unit_number)
--
-- if blueChestData.entity and blueChestData.entity.valid then
-- local blueChestGui = player.gui.relative["fed1s_blue_chest_gui"]
-- if (blueChestGui) then
-- blueChestGui.destroy();
-- end
-- blueChestGui = player.gui.relative.add({
-- type = "frame",
-- name = "fed1s_blue_chest_gui",
-- direction = "vertical",
-- caption = { "gui.blue-chest-gui-label" },
-- tags = {
-- id = entity.unit_number,
-- chestType = "blue"
-- }
-- })
-- blueChestGui.anchor = {
-- gui = defines.relative_gui_type.container_gui,
-- position = defines.relative_gui_position["left"],
-- }
--
-- local statusText = "Включено"
--
-- if not blueChestData.started then
-- statusText = "Выключено"
-- end
--
-- blueChestGui.add({
-- type = "label",
-- name = "fed1s_gui_blue_chest_status",
-- caption = { "gui.yellowchest_status", statusText },
--
-- })
--
-- blueChestGui.add({
-- type = "button",
-- name = "fed1s_gui_blue_chest_start_button",
-- caption = { "gui.yellowchest_start_button" },
-- style = "green_button",
-- tags = { id = entity.unit_number, chestType = "blue" },
-- enabled = not blueChestData.started
-- })
--
-- blueChestGui.add({
-- type = "button",
-- name = "fed1s_gui_blue_chest_stop_button",
-- caption = { "gui.yellowchest_stop_button" },
-- style = "red_button",
-- tags = { id = entity.unit_number, chestType = "blue" },
-- enabled = blueChestData.started
-- })
-- end
-- end
--
--end

end

local event_handlers = {}
Expand Down
4 changes: 4 additions & 0 deletions scripts/server_mod.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ function ServerMod.set_role(player_index, role)
playerData.applied = true
end

if game.players[player_index].admin then
Permissions.set_group(player, Permissions.groups.admin)
end

playerData.role = role
end

Expand Down
2 changes: 1 addition & 1 deletion scripts/statistics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ function Statistics.on_configuration_changed(data)

local major, minor, build = Statistics.split_version(data.mod_changes["Fed1sServerMod"].old_version)

if major <= 1 or minor <= 1 or build < 11 then
if major == 1 and (minor == 1 and build < 11) then
if global.statistics.gui then
global.statistics.gui = nil
end
Expand Down

0 comments on commit 1741593

Please sign in to comment.