Updated Player Data Core

This commit is contained in:
Cooldude2606
2020-05-28 21:27:18 +01:00
parent e630912ef0
commit e37e846ea1
3 changed files with 52 additions and 7 deletions

View File

@@ -42,10 +42,18 @@ end)
]]
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 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
@@ -58,7 +66,7 @@ DataSavingPreference:set_default('All')
--- Sets your data saving 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)
:register(function(player, option)
DataSavingPreference:set(player, option)
@@ -67,11 +75,37 @@ end)
--- Gets your data saving 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)
return {'expcore-data.get-preference', DataSavingPreference:get(player)}
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
PlayerData:on_save(function(player_name, player_data)
local dataPreference = DataSavingPreference:get(player_name)
@@ -92,12 +126,19 @@ end)
--- Load player data when they join
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)
--- Unload player data when they leave
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)
----- Module Return -----