set Values arent "shared" any more. I hope nobody will miss that "feature"

This commit is contained in:
yago 2014-12-14 21:44:19 +01:00
parent 5bbf754d73
commit e475659978
2 changed files with 17 additions and 8 deletions

View File

@ -10,16 +10,22 @@ else
_values = json:decode(c) _values = json:decode(c)
end end
function get_value( value_name ) function get_value(chat, value_name)
-- If chat values is empty
if (_values[chat] == nil) then
return "There isn't any data"
end
-- If there is not value name, return all the values. -- If there is not value name, return all the values.
if (value_name == nil ) then if (value_name == nil ) then
local text = "" local text = ""
for key,value in pairs(_values) do for key,value in pairs(_values[chat]) do
text = text..key.." = "..value.."\n" text = text..key.." = "..value.."\n"
end end
return text return text
end end
local value = _values[value_name] local value = _values[chat][value_name]
if ( value == nil) then if ( value == nil) then
return "Can't find "..value_name return "Can't find "..value_name
end end
@ -28,9 +34,9 @@ end
function run(msg, matches) function run(msg, matches)
if matches[1] == "!get" then if matches[1] == "!get" then
return get_value(nil) return get_value(msg.to.id, nil)
end end
return get_value(matches[1]) return get_value(msg.to.id, matches[1])
end end
return { return {

View File

@ -1,9 +1,12 @@
function save_value( text ) function save_value(chat, text )
var_name, var_value = string.match(text, "!set (%a+) (.+)") var_name, var_value = string.match(text, "!set (%a+) (.+)")
if (var_name == nil or var_value == nil) then if (var_name == nil or var_value == nil) then
return "Usage: !set var_name value" return "Usage: !set var_name value"
end end
_values[var_name] = var_value if _values[chat] == nil then
_values[chat] = {}
end
_values[chat][var_name] = var_value
local json_text = json:encode_pretty(_values) local json_text = json:encode_pretty(_values)
file = io.open ("./res/values.json", "w+") file = io.open ("./res/values.json", "w+")
@ -14,7 +17,7 @@ function save_value( text )
end end
function run(msg, matches) function run(msg, matches)
local text = save_value(msg.text) local text = save_value(msg.to.id, msg.text)
return text return text
end end