values.json and users.json are created if not exists

This commit is contained in:
yago 2014-11-23 12:25:33 +01:00
parent 019fdd06ba
commit 48b17bd5d0
6 changed files with 26 additions and 14 deletions

1
.gitignore vendored
View File

@ -0,0 +1 @@
res/

View File

@ -3,7 +3,7 @@
URL = require("socket.url") URL = require("socket.url")
json = (loadfile "./bot/JSON.lua")() json = (loadfile "./bot/JSON.lua")()
VERSION = 'v0.7.3' VERSION = 'v0.7.4'
-- taken from http://stackoverflow.com/a/11130774/3163199 -- taken from http://stackoverflow.com/a/11130774/3163199
function scandir(directory) function scandir(directory)
@ -16,7 +16,7 @@
end end
function on_msg_receive (msg) function on_msg_receive (msg)
--vardump(msg) vardump(msg)
if msg_valid(msg) == false then if msg_valid(msg) == false then
return return
@ -240,10 +240,19 @@
end end
function load_user_stats() function load_user_stats()
local f = assert(io.open('./res/users.json', "r")) local f = io.open('res/users.json', "r+")
-- If file doesn't exists
if f == nil then
f = io.open('res/users.json', "w+")
f:write("{}") -- Write empty table
f:close()
return {}
else
local c = f:read "*a" local c = f:read "*a"
f:close()
return json:decode(c) return json:decode(c)
end end
end
function get_receiver(msg) function get_receiver(msg)
if msg.to.type == 'user' then if msg.to.type == 'user' then

View File

@ -1,6 +1,14 @@
local f = assert(io.open('./res/values.json', "r+")) local f = io.open('./res/values.json', "r+")
local c = f:read "*a" if f == nil then
_values = json:decode(c) 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)
end
function get_value( value_name ) function get_value( value_name )
-- If there is not value name, return all the values. -- If there is not value name, return all the values.

0
res/.gitkeep Normal file
View File

View File

@ -1,3 +0,0 @@
{
}

View File

@ -1,3 +0,0 @@
{
}