mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-25 16:33:12 +09:00
Merge pull request #845 from Cooldude2606/feature/sync-recipe-notifications
Add recipe notification sync to inventory sync
This commit is contained in:
4 files changed
+135
-21
No files matched your search
@@ -18,6 +18,7 @@ export type IpcPlayerData = {
|
|||||||
hotbar?: string[],
|
hotbar?: string[],
|
||||||
personal_logistic_slots?: {name:string, min:number, max:number}[],
|
personal_logistic_slots?: {name:string, min:number, max:number}[],
|
||||||
crafting_queue?: any,
|
crafting_queue?: any,
|
||||||
|
recipe_notifications?: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
const jsonPlayerData = Type.Object({
|
const jsonPlayerData = Type.Object({
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ return function(no_early_return)
|
|||||||
inventory_sync.players = inventory_sync.players or {}
|
inventory_sync.players = inventory_sync.players or {}
|
||||||
inventory_sync.players_waiting_for_acquire = inventory_sync.players_waiting_for_acquire or {}
|
inventory_sync.players_waiting_for_acquire = inventory_sync.players_waiting_for_acquire or {}
|
||||||
inventory_sync.players_in_cutscene_to_sync = inventory_sync.players_in_cutscene_to_sync or {}
|
inventory_sync.players_in_cutscene_to_sync = inventory_sync.players_in_cutscene_to_sync or {}
|
||||||
|
inventory_sync.failed_deserialization = inventory_sync.failed_deserialization or {}
|
||||||
|
|
||||||
inventory_sync.active_downloads = inventory_sync.active_downloads or {}
|
inventory_sync.active_downloads = inventory_sync.active_downloads or {}
|
||||||
inventory_sync.finished_downloads = inventory_sync.finished_downloads or {}
|
inventory_sync.finished_downloads = inventory_sync.finished_downloads or {}
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ local function remove_player(player)
|
|||||||
local script_data = get_script_data()
|
local script_data = get_script_data()
|
||||||
script_data.players_waiting_for_acquire[player.name] = nil
|
script_data.players_waiting_for_acquire[player.name] = nil
|
||||||
script_data.players_in_cutscene_to_sync[player.name] = nil
|
script_data.players_in_cutscene_to_sync[player.name] = nil
|
||||||
|
script_data.failed_deserialization[player.name] = nil
|
||||||
script_data.active_downloads[player.name] = nil
|
script_data.active_downloads[player.name] = nil
|
||||||
script_data.finished_downloads[player.name] = nil
|
script_data.finished_downloads[player.name] = nil
|
||||||
script_data.active_uploads[player.name] = nil
|
script_data.active_uploads[player.name] = nil
|
||||||
@@ -73,7 +74,8 @@ function inventory_sync.serialize_player(player, player_record)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local serialized_player = serialize.serialize_player(player)
|
local failed_deserialization = get_script_data().failed_deserialization[player.name] or {}
|
||||||
|
local serialized_player = serialize.serialize_player(player, failed_deserialization)
|
||||||
serialized_player.generation = player_record.generation
|
serialized_player.generation = player_record.generation
|
||||||
|
|
||||||
return serialized_player
|
return serialized_player
|
||||||
@@ -106,7 +108,8 @@ function inventory_sync.deserialize_player(player, finished_record)
|
|||||||
|
|
||||||
-- Deserialize downloaded player data
|
-- Deserialize downloaded player data
|
||||||
local serialized_player = compat.json_to_table(finished_record.data)
|
local serialized_player = compat.json_to_table(finished_record.data)
|
||||||
serialize.deserialize_player(player, serialized_player)
|
assert(type(serialized_player) == "table", "wrong type for serialized_player")
|
||||||
|
script_data.failed_deserialization[player.name] = serialize.deserialize_player(player, serialized_player)
|
||||||
|
|
||||||
-- Restore player position and driving state
|
-- Restore player position and driving state
|
||||||
restore_position(player, finished_record)
|
restore_position(player, finished_record)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ local serialize = {}
|
|||||||
|
|
||||||
local v2_logistic_api = compat.version_ge("2.0.0")
|
local v2_logistic_api = compat.version_ge("2.0.0")
|
||||||
local v2_storage_api = compat.version_ge("2.0.0")
|
local v2_storage_api = compat.version_ge("2.0.0")
|
||||||
|
local recipe_notifications_api = compat.version_ge("2.0.67")
|
||||||
|
|
||||||
function serialize.serialize_inventories(source, inventories)
|
function serialize.serialize_inventories(source, inventories)
|
||||||
local serialized = {}
|
local serialized = {}
|
||||||
@@ -342,28 +343,112 @@ function serialize.deserialize_crafting_queue(player, serialized)
|
|||||||
player.crafting_queue_progress = serialized.crafting_queue_progress
|
player.crafting_queue_progress = serialized.crafting_queue_progress
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param player LuaPlayer
|
||||||
|
---@param failed string[]?
|
||||||
|
---@return string?
|
||||||
|
function serialize.serialize_crafting_notifications(player, failed)
|
||||||
|
-- Get all unlocked recipes
|
||||||
|
local recipes = {}
|
||||||
|
for _, recipe in pairs(player.force.recipes) do
|
||||||
|
if recipe.enabled and not recipe.hidden then
|
||||||
|
recipes[recipe.name] = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Remove those with notifications
|
||||||
|
for _, recipe in pairs(player.get_recipe_notifications()) do
|
||||||
|
recipes[recipe.name] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Convert to a list of names
|
||||||
|
local recipe_names = failed or {} -- Includes previous recipes if any
|
||||||
|
local index = #recipe_names + 1
|
||||||
|
for name in pairs(recipes) do
|
||||||
|
recipe_names[index] = name
|
||||||
|
index = index + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
return helpers.encode_string(helpers.table_to_json(recipe_names))
|
||||||
|
end
|
||||||
|
|
||||||
|
--- @param player LuaPlayer
|
||||||
|
--- @param serialized string
|
||||||
|
--- @return string[]? failed Recipe names that do not exist
|
||||||
|
function serialize.deserialize_crafting_notifications(player, serialized)
|
||||||
|
-- Decode the recipe name strings
|
||||||
|
local recipe_names = helpers.json_to_table(assert(helpers.decode_string(serialized)))
|
||||||
|
assert(type(recipe_names) == "table", "wrong type decoded from json_to_table")
|
||||||
|
local clear_notification = player.clear_recipe_notification
|
||||||
|
local add_notification = player.add_recipe_notification
|
||||||
|
local notifications = {}
|
||||||
|
local failed = {}
|
||||||
|
|
||||||
|
-- Assume all recipes need a notification
|
||||||
|
for _, recipe in pairs(player.force.recipes) do
|
||||||
|
if recipe.enabled and not recipe.hidden then
|
||||||
|
notifications[recipe.name] = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Remove recipes the player has cleared
|
||||||
|
for _, recipe_name in pairs(recipe_names) do
|
||||||
|
if notifications[recipe_name] then
|
||||||
|
notifications[recipe_name] = false
|
||||||
|
else
|
||||||
|
-- Recipe does not exist on this server
|
||||||
|
failed[#failed+1] = recipe_name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Clear ones which are ones which are not required
|
||||||
|
for _, recipe in pairs(player.get_recipe_notifications()) do
|
||||||
|
if notifications[recipe.name] == false then
|
||||||
|
clear_notification(recipe.name)
|
||||||
|
end
|
||||||
|
notifications[recipe.name] = nil -- Prevents double add below
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Add any which the player did not have
|
||||||
|
for recipe_name, should_add in pairs(notifications) do
|
||||||
|
if should_add then
|
||||||
|
add_notification(recipe_name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Return failed recipes if any
|
||||||
|
return next(failed) and failed or nil
|
||||||
|
end
|
||||||
|
|
||||||
local controller_to_name = {}
|
local controller_to_name = {}
|
||||||
for name, value in pairs(defines.controllers) do
|
for name, value in pairs(defines.controllers) do
|
||||||
controller_to_name[value] = name
|
controller_to_name[value] = name
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Players are serialized into a table with the following fields:
|
--- @class SerializedPlayerData
|
||||||
-- name
|
--- @field generation number
|
||||||
-- controller: may not be cutscene or editor
|
--- @field name string
|
||||||
-- color
|
--- @field controller string
|
||||||
-- chat_color
|
--- @field color Color
|
||||||
-- tag
|
--- @field chat_color Color
|
||||||
-- force
|
--- @field tag string
|
||||||
-- cheat_mod
|
--- @field force string
|
||||||
-- flashlight
|
--- @field cheat_mode boolean
|
||||||
-- ticks_to_respawn (optional)
|
--- @field flashlight boolean
|
||||||
-- character (optional)
|
--- @field ticks_to_respawn number
|
||||||
-- inventories: non-character inventories
|
--- @field character table<string, any>?
|
||||||
-- hotbar: table of string indexes to names (optional)
|
--- @field inventories table<string, table>?
|
||||||
-- personal_logistic_slots (optional)
|
--- @field hotbar table<string, string>?
|
||||||
-- crafting_queue (optional)
|
--- @field personal_logistic_slots table?
|
||||||
function serialize.serialize_player(player)
|
--- @field crafting_queue table?
|
||||||
|
--- @field recipe_notifications string?
|
||||||
|
|
||||||
|
--- @param player LuaPlayer
|
||||||
|
--- @param failed_deserialization FailedDeserializationPlayerData
|
||||||
|
--- @return SerializedPlayerData
|
||||||
|
function serialize.serialize_player(player, failed_deserialization)
|
||||||
|
--- @type SerializedPlayerData
|
||||||
local serialized = {
|
local serialized = {
|
||||||
|
generation = 0, -- Gets replaced later
|
||||||
controller = controller_to_name[player.controller_type],
|
controller = controller_to_name[player.controller_type],
|
||||||
name = player.name,
|
name = player.name,
|
||||||
color = player.color,
|
color = player.color,
|
||||||
@@ -385,7 +470,9 @@ function serialize.serialize_player(player)
|
|||||||
-- Go back to waiting for respawn
|
-- Go back to waiting for respawn
|
||||||
local character = player.character
|
local character = player.character
|
||||||
player.ticks_to_respawn = serialized.ticks_to_respawn
|
player.ticks_to_respawn = serialized.ticks_to_respawn
|
||||||
character.destroy()
|
if character and character.valid then
|
||||||
|
character.destroy()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Serialize character
|
-- Serialize character
|
||||||
@@ -406,7 +493,7 @@ function serialize.serialize_player(player)
|
|||||||
if not serialized.hotbar then
|
if not serialized.hotbar then
|
||||||
serialized.hotbar = {}
|
serialized.hotbar = {}
|
||||||
end
|
end
|
||||||
serialized.hotbar[tostring(i)] = slot.name
|
serialized.hotbar[tostring(i)] = slot.name --[[@as string]]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -415,10 +502,23 @@ function serialize.serialize_player(player)
|
|||||||
serialized.crafting_queue = serialize.serialize_crafting_queue(player)
|
serialized.crafting_queue = serialize.serialize_crafting_queue(player)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Serialize recipe notifications
|
||||||
|
if recipe_notifications_api then
|
||||||
|
serialized.recipe_notifications = serialize.serialize_crafting_notifications(player, failed_deserialization.recipe_notifications)
|
||||||
|
end
|
||||||
|
|
||||||
return serialized
|
return serialized
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- @class FailedDeserializationPlayerData
|
||||||
|
--- @field recipe_notifications string[]?
|
||||||
|
|
||||||
|
--- @param player LuaPlayer
|
||||||
|
--- @param serialized SerializedPlayerData
|
||||||
|
--- @return FailedDeserializationPlayerData?
|
||||||
function serialize.deserialize_player(player, serialized)
|
function serialize.deserialize_player(player, serialized)
|
||||||
|
local failed_deserialization = {}
|
||||||
|
|
||||||
if player.controller_type ~= defines.controllers[serialized.controller] or serialized.controller == "ghost" then
|
if player.controller_type ~= defines.controllers[serialized.controller] or serialized.controller == "ghost" then
|
||||||
-- If targeting the character or ghost controller then create a character
|
-- If targeting the character or ghost controller then create a character
|
||||||
if serialized.controller == "character" or serialized.controller == "ghost" then
|
if serialized.controller == "character" or serialized.controller == "ghost" then
|
||||||
@@ -443,7 +543,9 @@ function serialize.deserialize_player(player, serialized)
|
|||||||
player.set_controller({ type = defines.controllers.god })
|
player.set_controller({ type = defines.controllers.god })
|
||||||
player.set_controller({ type = defines.controllers.ghost })
|
player.set_controller({ type = defines.controllers.ghost })
|
||||||
end
|
end
|
||||||
character.destroy()
|
if character and character.valid then
|
||||||
|
character.destroy()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
@@ -496,6 +598,13 @@ function serialize.deserialize_player(player, serialized)
|
|||||||
serialize.deserialize_crafting_queue(player, serialized.crafting_queue)
|
serialize.deserialize_crafting_queue(player, serialized.crafting_queue)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Deserialize recipe notifications
|
||||||
|
if recipe_notifications_api and serialized.recipe_notifications then
|
||||||
|
failed_deserialization.recipe_notifications =
|
||||||
|
serialize.deserialize_crafting_notifications(player, serialized.recipe_notifications)
|
||||||
|
end
|
||||||
|
|
||||||
|
return next(failed_deserialization) and failed_deserialization or nil
|
||||||
end
|
end
|
||||||
|
|
||||||
return serialize
|
return serialize
|
||||||
Reference in new issue
Block a user