diff --git a/.gitignore b/.gitignore index 76174e7..e150a82 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ res/ +data/ bot/config.lua \ No newline at end of file diff --git a/bot/bot.lua b/bot/bot.lua index 20c139a..108af81 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -5,7 +5,7 @@ json = (loadfile "./libs/JSON.lua")() serpent = (loadfile "./libs/serpent.lua")() require("./bot/utils") -VERSION = '0.8.1' +VERSION = '0.8.2' function on_msg_receive (msg) vardump(msg) diff --git a/res/.gitkeep b/data/.gitkeep similarity index 100% rename from res/.gitkeep rename to data/.gitkeep diff --git a/plugins/get.lua b/plugins/get.lua index 7f4631a..c254dc5 100644 --- a/plugins/get.lua +++ b/plugins/get.lua @@ -1,15 +1,21 @@ -local f = io.open('./res/values.json', "r+") -if f == nil then - f = io.open('./res/values.json', "w+") - f:write("{}") -- Write empty table - f:close() - _values = {} -else - local c = f:read "*a" - f:close() - _values = json:decode(c) +local _file_values = './data/values.lua' + +function read_file_values( ) + local f = io.open(_file_values, "r+") + -- If file doesn't exists + if f == nil then + -- Create a new empty table + print ('Created value file '.._file_values) + serialize_to_file({}, _file_values) + else + print ('Stats loaded: '.._file_values) + f:close() + end + return loadfile (_file_values)() end +_values = read_file_values() + function fetch_value(chat, value_name) if (_values[chat] == nil) then return nil diff --git a/plugins/set.lua b/plugins/set.lua index c706f98..d971b9c 100644 --- a/plugins/set.lua +++ b/plugins/set.lua @@ -1,3 +1,5 @@ +local _file_values = './data/values.lua' + function save_value(chat, text ) var_name, var_value = string.match(text, "!set (%a+) (.+)") if (var_name == nil or var_value == nil) then @@ -8,10 +10,8 @@ function save_value(chat, text ) end _values[chat][var_name] = var_value - local json_text = json:encode_pretty(_values) - file = io.open ("./res/values.json", "w+") - file:write(json_text) - file:close() + -- Save values to file + serialize_to_file(_values, _file_values) return "Saved "..var_name.." = "..var_value end diff --git a/plugins/stats.lua b/plugins/stats.lua index 70ce32e..e9fcf7e 100644 --- a/plugins/stats.lua +++ b/plugins/stats.lua @@ -4,7 +4,7 @@ do local socket = require('socket') -local _file_stats = './res/stats.lua' +local _file_stats = './data/stats.lua' local _stats function update_user_stats(msg)