mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-30 12:31:41 +09:00
Updated Player Data Core
This commit is contained in:
@@ -220,8 +220,9 @@ local default = Roles.new_role('Guest','')
|
|||||||
'command/report',
|
'command/report',
|
||||||
'command/ratio',
|
'command/ratio',
|
||||||
'command/server-ups',
|
'command/server-ups',
|
||||||
'command/data-preference',
|
'command/save-data',
|
||||||
'command/set-data-preference',
|
'command/preference',
|
||||||
|
'command/set-preference',
|
||||||
'gui/player-list',
|
'gui/player-list',
|
||||||
'gui/rocket-info',
|
'gui/rocket-info',
|
||||||
'gui/science-info',
|
'gui/science-info',
|
||||||
|
|||||||
@@ -42,10 +42,18 @@ end)
|
|||||||
]]
|
]]
|
||||||
|
|
||||||
local Event = require 'utils.event' --- @dep utils.event
|
local Event = require 'utils.event' --- @dep utils.event
|
||||||
|
local Async = require 'expcore.async' --- @dep expcore.async
|
||||||
local Datastore = require 'expcore.datastore' --- @dep expcore.datastore
|
local Datastore = require 'expcore.datastore' --- @dep expcore.datastore
|
||||||
local Commands = require 'expcore.commands' --- @dep expcore.commands
|
local Commands = require 'expcore.commands' --- @dep expcore.commands
|
||||||
require 'config.expcore.command_general_parse' --- @dep config.expcore.command_general_parse
|
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
|
--- Common player data that acts as the root store for player data
|
||||||
local PlayerData = Datastore.connect('PlayerData', true) -- saveToDisk
|
local PlayerData = Datastore.connect('PlayerData', true) -- saveToDisk
|
||||||
PlayerData:set_serializer(Datastore.name_serializer) -- use player name
|
PlayerData:set_serializer(Datastore.name_serializer) -- use player name
|
||||||
@@ -58,7 +66,7 @@ DataSavingPreference:set_default('All')
|
|||||||
|
|
||||||
--- Sets your data saving preference
|
--- Sets your data saving preference
|
||||||
-- @command set-data-preference
|
-- @command set-data-preference
|
||||||
Commands.new_command('set-data-preference', 'Allows you to set your data saving preference')
|
Commands.new_command('set-preference', 'Allows you to set your data saving preference')
|
||||||
:add_param('option', false, 'string-options', PreferenceEnum)
|
:add_param('option', false, 'string-options', PreferenceEnum)
|
||||||
:register(function(player, option)
|
:register(function(player, option)
|
||||||
DataSavingPreference:set(player, option)
|
DataSavingPreference:set(player, option)
|
||||||
@@ -67,11 +75,37 @@ end)
|
|||||||
|
|
||||||
--- Gets your data saving preference
|
--- Gets your data saving preference
|
||||||
-- @command data-preference
|
-- @command data-preference
|
||||||
Commands.new_command('data-preference', 'Shows you what your current data saving preference is')
|
Commands.new_command('preference', 'Shows you what your current data saving preference is')
|
||||||
:register(function(player)
|
:register(function(player)
|
||||||
return {'expcore-data.get-preference', DataSavingPreference:get(player)}
|
return {'expcore-data.get-preference', DataSavingPreference:get(player)}
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
--- Gets your data and writes it to a file
|
||||||
|
Commands.new_command('save-data', 'Writes all your player data to a file on your computer')
|
||||||
|
:register(function(player)
|
||||||
|
player.print{'expcore-data.get-data'}
|
||||||
|
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
|
||||||
|
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
|
||||||
|
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
|
||||||
|
game.players[player_name].print{'expcore-data.data-restore'}
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
--- Remove data that the player doesnt want to have stored
|
--- Remove data that the player doesnt want to have stored
|
||||||
PlayerData:on_save(function(player_name, player_data)
|
PlayerData:on_save(function(player_name, player_data)
|
||||||
local dataPreference = DataSavingPreference:get(player_name)
|
local dataPreference = DataSavingPreference:get(player_name)
|
||||||
@@ -92,12 +126,19 @@ end)
|
|||||||
|
|
||||||
--- Load player data when they join
|
--- Load player data when they join
|
||||||
Event.add(defines.events.on_player_joined_game, function(event)
|
Event.add(defines.events.on_player_joined_game, function(event)
|
||||||
PlayerData:request(game.players[event.player_index])
|
local player = game.players[event.player_index]
|
||||||
|
PlayerData:request(player)
|
||||||
|
Async.wait(600, check_data_loaded, player)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
--- Unload player data when they leave
|
--- Unload player data when they leave
|
||||||
Event.add(defines.events.on_player_left_game, function(event)
|
Event.add(defines.events.on_player_left_game, function(event)
|
||||||
PlayerData:unload(game.players[event.player_index])
|
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)
|
end)
|
||||||
|
|
||||||
----- Module Return -----
|
----- Module Return -----
|
||||||
|
|||||||
@@ -39,4 +39,7 @@ left-button-tooltip=Hide all open windows.
|
|||||||
|
|
||||||
[expcore-data]
|
[expcore-data]
|
||||||
set-preference=You data saving preference has been set to __1__. Existing data will not be effected until you rejoin.
|
set-preference=You data saving preference has been set to __1__. Existing data will not be effected until you rejoin.
|
||||||
get-preference=You data saving preference is __1__. Use /set-data-preference to change this.
|
get-preference=You data saving preference is __1__. Use /set-preference to change this. Use /save-data to get a local copy of your data.
|
||||||
|
get-data=Your player data has been writen to file, location: factorio/script_output/expgaming_player_data.json
|
||||||
|
data-failed=Your player data has failed to load. Any changes to your data will not be saved.
|
||||||
|
data-restore=Your player data has been restored and changes will now save when you leave.
|
||||||
Reference in New Issue
Block a user