mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-27 11:35:22 +09:00
Fixed some bugs
This commit is contained in:
@@ -153,6 +153,7 @@ local Datastores = {}
|
||||
local Datastore = {}
|
||||
local Data = {}
|
||||
local copy = table.deep_copy
|
||||
local trace = debug.traceback
|
||||
|
||||
--- Save datastores in the global table
|
||||
global.datastores = Data
|
||||
@@ -250,7 +251,6 @@ function DatastoreManager.ingest(action, datastoreName, key, valueJson)
|
||||
elseif action == 'propagate' or action == 'request' then
|
||||
local success, value = pcall(game.json_to_table, valueJson)
|
||||
if not success or value == nil then value = tonumber(valueJson) or valueJson end
|
||||
datastore:raw_set(key) -- clear any existing data
|
||||
value = datastore:raise_event('on_load', key, value)
|
||||
datastore:set(key, value)
|
||||
|
||||
@@ -360,7 +360,7 @@ function Datastore:raw_set(key, value)
|
||||
end
|
||||
end
|
||||
|
||||
local function serialize_error(err) error('An error ocurred in a datastore serializer: '..err) end
|
||||
local function serialize_error(err) error('An error ocurred in a datastore serializer: '..trace(err)) end
|
||||
--[[-- Internal, Return the serialized key
|
||||
@tparam any rawKey The key that needs to be serialized, if it is already a string then it is returned
|
||||
@treturn string The key after it has been serialized
|
||||
@@ -389,11 +389,11 @@ self:write_action('save', 'TestKey', 'Foo')
|
||||
|
||||
]]
|
||||
function Datastore:write_action(action, key, value)
|
||||
local data = {action, self.name, '"'..key..'"'}
|
||||
local data = {action, self.name, key}
|
||||
if value ~= nil then
|
||||
data[4] = type(value) == 'table' and '"'..game.table_to_json(value)..'"' or '"'..tostring(value)..'"'
|
||||
data[4] = type(value) == 'table' and game.table_to_json(value) or tostring(value)
|
||||
end
|
||||
game.write_file('datastore.out', table.concat(data, ' ')..'\n', true, 0)
|
||||
game.write_file('ext/datastore.out', table.concat(data, ' ')..'\n', true, 0)
|
||||
end
|
||||
|
||||
----- Datastore Local
|
||||
@@ -520,7 +520,7 @@ function Datastore:increment(key, delta)
|
||||
return Datastore:set(key, value + (delta or 1))
|
||||
end
|
||||
|
||||
local function update_error(err) error('An error ocurred in datastore update: '..err, 2) end
|
||||
local function update_error(err) error('An error ocurred in datastore update: '..trace(err), 2) end
|
||||
--[[-- Use a function to update the value locally, will trigger on_update then on_save, save_to_disk and auto_save is required for on_save
|
||||
@tparam any key The key that you want to apply the update to, must be a string unless a serializer is set
|
||||
@tparam function callback The function that will be used to update the value at this key
|
||||
@@ -560,7 +560,7 @@ function Datastore:remove(key)
|
||||
if self.parent and self.parent.auto_save then return self.parent:save(key) end
|
||||
end
|
||||
|
||||
local function filter_error(err) print('An error ocurred in a datastore filter:', err) end
|
||||
local function filter_error(err) print('An error ocurred in a datastore filter:'..trace(err)) end
|
||||
--[[-- Internal, Used to filter elements from a table
|
||||
@tparam table tbl The table that will have the filter applied to it
|
||||
@tparam[opt] function callback The function that will be used as a filter, if none giving then the provided table is returned
|
||||
@@ -737,7 +737,7 @@ end
|
||||
----- Events
|
||||
-- @section events
|
||||
|
||||
local function event_error(err) print('An error ocurred in a datastore event handler:', err) end
|
||||
local function event_error(err) print('An error ocurred in a datastore event handler: '..trace(err)) end
|
||||
--[[-- Internal, Raise an event on this datastore
|
||||
@tparam string event_name The name of the event to raise for this datastore
|
||||
@tparam string key The key that this event is being raised for
|
||||
@@ -752,7 +752,7 @@ value = self:raise_event('on_save', key, value)
|
||||
function Datastore:raise_event(event_name, key, value, source)
|
||||
-- Raise the event for the children of this datastore
|
||||
if source ~= 'child' and next(self.children) then
|
||||
if type(value) ~= 'table' then value = self:raw_get(key, true) end
|
||||
if type(value) ~= 'table' then value = {} end
|
||||
for value_name, child in pairs(self.children) do
|
||||
value[value_name] = child:raise_event(event_name, key, value[value_name], 'parent')
|
||||
end
|
||||
@@ -769,7 +769,7 @@ function Datastore:raise_event(event_name, key, value, source)
|
||||
|
||||
-- Raise the event for the parent of this datastore
|
||||
if source ~= 'parent' and self.parent then
|
||||
self.parent:raise_event(event_name, key, self.parent:raw_get(key), 'child')
|
||||
self.parent:raise_event(event_name, key, self.parent:raw_get(key, true), 'child')
|
||||
end
|
||||
|
||||
-- If this is the save event and the table is empty then return nil
|
||||
|
||||
@@ -47,13 +47,6 @@ local Datastore = require 'expcore.datastore' --- @dep expcore.datastore
|
||||
local Commands = require 'expcore.commands' --- @dep expcore.commands
|
||||
require 'config.expcore.command_general_parse' --- @dep config.expcore.command_general_parse
|
||||
|
||||
--- Stores all the players whos data failed to load
|
||||
local FailedLoad = {}
|
||||
global.failed_player_data = FailedLoad
|
||||
Event.on_load(function()
|
||||
FailedLoad = global.failed_player_data
|
||||
end)
|
||||
|
||||
--- Common player data that acts as the root store for player data
|
||||
local PlayerData = Datastore.connect('PlayerData', true) -- saveToDisk
|
||||
PlayerData:set_serializer(Datastore.name_serializer) -- use player name
|
||||
@@ -87,11 +80,10 @@ Commands.new_command('save-data', 'Writes all your player data to a file on your
|
||||
game.write_file('expgaming_player_data.json', game.table_to_json(PlayerData:get(player, {})), false, player.index)
|
||||
end)
|
||||
|
||||
--- Async function called after 10 seconds with no player data loaded
|
||||
--- Async function called after 5 seconds with no player data loaded
|
||||
local check_data_loaded = Async.register(function(player)
|
||||
local player_data = PlayerData:get(player)
|
||||
if not player_data then
|
||||
FailedLoad[player.name] = true
|
||||
player.print{'expcore-data.data-failed'}
|
||||
Datastore.ingest('request', 'PlayerData', player.name, '{"failed_load":true}')
|
||||
end
|
||||
@@ -100,8 +92,8 @@ end)
|
||||
--- When player data loads tell the player if the load had failed previously
|
||||
PlayerData:on_load(function(player_name, player_data)
|
||||
if not player_data or player_data.failed_load then return end
|
||||
if FailedLoad[player_name] then
|
||||
FailedLoad[player_name] = nil
|
||||
local existing_data = PlayerData:get(player_name)
|
||||
if existing_data and existing_data.failed_load then
|
||||
game.players[player_name].print{'expcore-data.data-restore'}
|
||||
end
|
||||
end)
|
||||
@@ -128,7 +120,7 @@ end)
|
||||
Event.add(defines.events.on_player_joined_game, function(event)
|
||||
local player = game.players[event.player_index]
|
||||
PlayerData:request(player)
|
||||
Async.wait(600, check_data_loaded, player)
|
||||
Async.wait(300, check_data_loaded, player)
|
||||
end)
|
||||
|
||||
--- Unload player data when they leave
|
||||
@@ -136,7 +128,6 @@ Event.add(defines.events.on_player_left_game, function(event)
|
||||
local player = game.players[event.player_index]
|
||||
local player_data = PlayerData:get(player)
|
||||
if player_data.failed_load then
|
||||
FailedLoad[player.name] = nil
|
||||
PlayerData:raw_set(player)
|
||||
else PlayerData:unload(player) end
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user