From 4286e25803f84d1aa8c417aba877d80d9836df52 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Fri, 29 May 2020 22:57:06 +0100 Subject: [PATCH] Fixed some bugs --- config/_file_loader.lua | 5 +++-- config/expcore/roles.lua | 6 +++--- expcore/datastore.lua | 20 ++++++++++---------- expcore/player_data.lua | 17 ++++------------- modules/data/bonus.lua | 4 ++-- modules/data/tag.lua | 2 +- 6 files changed, 23 insertions(+), 31 deletions(-) diff --git a/config/_file_loader.lua b/config/_file_loader.lua index 65d8f093..28d4ebde 100644 --- a/config/_file_loader.lua +++ b/config/_file_loader.lua @@ -12,7 +12,6 @@ return { 'modules.commands.me', 'modules.commands.kill', 'modules.commands.admin-chat', - 'modules.commands.tag', 'modules.commands.teleport', 'modules.commands.cheat-mode', 'modules.commands.ratio', @@ -27,7 +26,6 @@ return { 'modules.commands.spawn', 'modules.commands.warnings', 'modules.commands.find', - 'modules.commands.bonus', 'modules.commands.home', --- Addons @@ -48,6 +46,9 @@ return { 'modules.data.player-colours', 'modules.data.greetings', 'modules.data.quickbar', + 'modules.data.alt-view', + 'modules.data.tag', + 'modules.data.bonus', --- GUI 'modules.gui.readme', diff --git a/config/expcore/roles.lua b/config/expcore/roles.lua index 3b4b225d..c1904ada 100644 --- a/config/expcore/roles.lua +++ b/config/expcore/roles.lua @@ -151,8 +151,7 @@ Roles.new_role('Supporter','Sup') :allow{ 'command/jail', 'command/unjail', - 'command/join-message', - 'command/save-quickbar' + 'command/join-message' } Roles.new_role('Partner','Part') @@ -187,7 +186,8 @@ Roles.new_role('Member','Mem') 'gui/task-list/add', 'gui/task-list/edit', 'gui/warp-list/add', - 'gui/warp-list/edit' + 'gui/warp-list/edit', + 'command/save-quickbar' } Roles.new_role('Regular','Reg') diff --git a/expcore/datastore.lua b/expcore/datastore.lua index be6848e4..f13a4c50 100644 --- a/expcore/datastore.lua +++ b/expcore/datastore.lua @@ -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 diff --git a/expcore/player_data.lua b/expcore/player_data.lua index d6157f5c..576674f6 100644 --- a/expcore/player_data.lua +++ b/expcore/player_data.lua @@ -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) diff --git a/modules/data/bonus.lua b/modules/data/bonus.lua index 6cf1752f..2275d543 100644 --- a/modules/data/bonus.lua +++ b/modules/data/bonus.lua @@ -23,8 +23,8 @@ local function apply_bonus(player, amount) end --- When store is updated apply new bonus to the player -PlayerBonus.on_update(function(player_name, player_bonus) - apply_bonus(game.player[player_name], player_bonus or 0) +PlayerBonus:on_update(function(player_name, player_bonus) + apply_bonus(game.players[player_name], player_bonus or 0) end) --- Changes the amount of bonus you receive diff --git a/modules/data/tag.lua b/modules/data/tag.lua index 644fc0f3..d01b6918 100644 --- a/modules/data/tag.lua +++ b/modules/data/tag.lua @@ -16,7 +16,7 @@ local PlayerTags = PlayerData.Settings:combine('Tag') PlayerTags:on_update(function(player_name, player_tag) local player = game.players[player_name] if player_tag == nil or player_tag == '' then - player_tag.tag = '' + player.tag = '' else player.tag = '- '..player_tag end