diff --git a/expcore/commands.lua b/expcore/commands.lua index c96dcce4..c7698802 100644 --- a/expcore/commands.lua +++ b/expcore/commands.lua @@ -218,7 +218,7 @@ ]] local Game = require 'utils.game' -local player_return = ext_require('expcore.common','player_return') +local player_return,write_json = ext_require('expcore.common','player_return','write_json') local Commands = { defines={ -- common values are stored error like signals @@ -580,14 +580,14 @@ end -- logs command usage to file local function command_log(player,command,comment,params,raw,details) local player_name = player and player.name or '' - game.write_file('log/commands.log',game.table_to_json{ + write_json('log/commands.log',{ player_name=player_name, command_name=command.name, comment=comment, details=details, params=params, raw=raw - }..'\n',true,0) + }) end --- Main event function that is ran for all commands, used internally please avoid direct use diff --git a/expcore/common.lua b/expcore/common.lua index 38ef5276..aeab73e8 100644 --- a/expcore/common.lua +++ b/expcore/common.lua @@ -123,6 +123,13 @@ function Public.player_return(value,colour,player) else rcon.print(returnAsString) end end +--- Writes a table object to a file in json format +-- @tparam path string the path of the file to write include / to use dir +-- @tpatam tbl table the table that will be converted to a json string and wrote to file +function Public.write_json(path,tbl) + game.write_file(path,game.table_to_json(tbl)..'\n',true,0) +end + --- Calls a require that will not error if the file is not found -- @usage local file = opt_require('file.not.present') -- will not cause any error -- @tparam path string the path that you want to require diff --git a/expcore/roles.lua b/expcore/roles.lua index 11df21dc..a2dca09e 100644 --- a/expcore/roles.lua +++ b/expcore/roles.lua @@ -160,6 +160,7 @@ local Global = require 'utils.global' local Event = require 'utils.event' local Groups = require 'expcore.permission_groups' local Colours = require 'resources.color_presets' +local write_json = ext_require('expcore.common','write_json') local Roles = { config={ @@ -223,12 +224,12 @@ local function emit_player_roles_updated(player,type,roles,by_player_name,skip_g by_player_index=by_player_index, roles=roles }) - game.write_file('log/roles.log',game.table_to_json{ + write_json('log/roles.log',{ player_name=player.name, by_player_name=by_player_name, type=type, roles_changed=role_names - }..'\n',true,0) + }) end --- Returns a string which contains all roles in index order displaying all data for them diff --git a/expcore/store.lua b/expcore/store.lua index 4ab0adf3..d8834ef2 100644 --- a/expcore/store.lua +++ b/expcore/store.lua @@ -20,7 +20,7 @@ game - this will store all a single value so any sub_location string can be used, this is the general case so you really can store what ever values you want to in this and watch for external updates, this would be used when its not a local varible for example if you are watching the number of online players. - global - WIP this will store all of its data in an external source indepentent of the lua code, this means that you can store data between + global - this will store all of its data in an external source indepentent of the lua code, this means that you can store data between maps and even instances, when the value is updated it will trigger an emit where some external code should send a message to the other connected instances to update they value. lcoal set -> emit update -> local setter -> remote set -> remote setter @@ -61,7 +61,7 @@ local Global = require 'utils.global' local Event = require 'utils.event' local Game = require 'utils.game' -local Enum = ext_require('expcore.common','enum') +local Enum,write_json = ext_require('expcore.common','enum','write_json') local Store = { data={}, @@ -98,7 +98,11 @@ end --- Emits an event to the external store that a value was updated local function set_global_location_value(location,sub_location,value) - -- not yet impimented, this will emit to a file in some way to set the value in an external database + write_json('log/store.log',{ + location=location, + sub_location=sub_location, + value=value + }) end --- Register a new location to store a value, the valu returned from getter will be watched for updates diff --git a/modules/commands/interface.lua b/modules/commands/interface.lua index 6e5b89b0..98452174 100644 --- a/modules/commands/interface.lua +++ b/modules/commands/interface.lua @@ -9,7 +9,8 @@ local interface_modules = { ['Commands']=Commands, ['output']=Common.player_return, ['Group']='expcore.permission_groups', - ['Roles']='expcore.roles' + ['Roles']='expcore.roles', + ['Store']='expcore.store' } -- loads all the modules given in the above table