get, set, and stats persistence

This commit is contained in:
yago 2014-11-23 00:31:22 +01:00
parent 5657663c63
commit e2403a6e37
8 changed files with 68 additions and 27 deletions

View File

@ -2,7 +2,7 @@
-- --
-- Simple JSON encoding and decoding in pure Lua. -- Simple JSON encoding and decoding in pure Lua.
-- --
-- Copyright 2010-2013 Jeffrey Friedl -- Copyright 2010-2014 Jeffrey Friedl
-- http://regex.info/blog/ -- http://regex.info/blog/
-- --
-- Latest version: http://regex.info/blog/lua/json -- Latest version: http://regex.info/blog/lua/json
@ -10,11 +10,22 @@
-- This code is released under a Creative Commons CC-BY "Attribution" License: -- This code is released under a Creative Commons CC-BY "Attribution" License:
-- http://creativecommons.org/licenses/by/3.0/deed.en_US -- http://creativecommons.org/licenses/by/3.0/deed.en_US
-- --
-- It can be used for any purpose so long as the copyright notice and -- It can be used for any purpose so long as the copyright notice above,
-- web-page links above are maintained. Enjoy. -- the web-page links above, and the 'AUTHOR_NOTE' string below are
-- maintained. Enjoy.
-- --
local VERSION = 20140418.11 -- version history at end of file local VERSION = 20140920.13 -- version history at end of file
local OBJDEF = { VERSION = VERSION } local AUTHOR_NOTE = "-[ JSON.lua package by Jeffrey Friedl (http://regex.info/blog/lua/json) version 20140920.13 ]-"
--
-- The 'AUTHOR_NOTE' variable exists so that information about the source
-- of the package is maintained even in compiled versions. It's included in
-- OBJDEF mostly to quiet warnings about unused variables.
--
local OBJDEF = {
VERSION = VERSION,
AUTHOR_NOTE = AUTHOR_NOTE,
}
-- --
@ -170,8 +181,6 @@ local OBJDEF = { VERSION = VERSION }
-- --
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
local author = "-[ JSON.lua package by Jeffrey Friedl (http://regex.info/blog/lua/json), version " .. tostring(VERSION) .. " ]-"
local isArray = { __tostring = function() return "JSON array" end } isArray.__index = isArray local isArray = { __tostring = function() return "JSON array" end } isArray.__index = isArray
local isObject = { __tostring = function() return "JSON object" end } isObject.__index = isObject local isObject = { __tostring = function() return "JSON object" end } isObject.__index = isObject
@ -386,7 +395,7 @@ end
local function skip_whitespace(text, start) local function skip_whitespace(text, start)
local match_start, match_end = text:find("^[ \n\r\t]+", start) -- [http://www.ietf.org/rfc/rfc4627.txt] Section 2 local _, match_end = text:find("^[ \n\r\t]+", start) -- [http://www.ietf.org/rfc/rfc4627.txt] Section 2
if match_end then if match_end then
return match_end + 1 return match_end + 1
else else
@ -397,7 +406,7 @@ end
local grok_one -- assigned later local grok_one -- assigned later
local function grok_object(self, text, start, etc) local function grok_object(self, text, start, etc)
if not text:sub(start,start) == '{' then if text:sub(start,start) ~= '{' then
self:onDecodeError("expected '{'", text, start, etc) self:onDecodeError("expected '{'", text, start, etc)
end end
@ -420,9 +429,9 @@ local function grok_object(self, text, start, etc)
i = skip_whitespace(text, i + 1) i = skip_whitespace(text, i + 1)
local val, new_i = grok_one(self, text, i) local new_val, new_i = grok_one(self, text, i)
VALUE[key] = val VALUE[key] = new_val
-- --
-- Expect now either '}' to end things, or a ',' to allow us to continue. -- Expect now either '}' to end things, or a ',' to allow us to continue.
@ -446,7 +455,7 @@ local function grok_object(self, text, start, etc)
end end
local function grok_array(self, text, start, etc) local function grok_array(self, text, start, etc)
if not text:sub(start,start) == '[' then if text:sub(start,start) ~= '[' then
self:onDecodeError("expected '['", text, start, etc) self:onDecodeError("expected '['", text, start, etc)
end end
@ -649,7 +658,7 @@ local function object_or_array(self, T, etc)
-- It's not ideal, but we'll turn the numbers into strings so that we can at least create a JSON object. -- It's not ideal, but we'll turn the numbers into strings so that we can at least create a JSON object.
-- --
if JSON.noKeyConversion then if self.noKeyConversion then
self:onEncodeError("a table with both numeric and string keys could be an object or array; aborting", etc) self:onEncodeError("a table with both numeric and string keys could be an object or array; aborting", etc)
end end
@ -841,6 +850,13 @@ return OBJDEF:new()
-- --
-- Version history: -- Version history:
-- --
-- 20140920.13 Put back (in a way that doesn't cause warnings about unused variables) the author string,
-- so that the source of the package, and its version number, are visible in compiled copies.
--
-- 20140911.12 Minor lua cleanup.
-- Fixed internal reference to 'JSON.noKeyConversion' to reference 'self' instead of 'JSON'.
-- (Thanks to SmugMug's David Parry for these.)
--
-- 20140418.11 JSON nulls embedded within an array were being ignored, such that -- 20140418.11 JSON nulls embedded within an array were being ignored, such that
-- ["1",null,null,null,null,null,"seven"], -- ["1",null,null,null,null,null,"seven"],
-- would return -- would return

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.2' VERSION = 'v0.7.3'
-- taken from http://stackoverflow.com/a/11130774/3163199 -- taken from http://stackoverflow.com/a/11130774/3163199
function scandir(directory) function scandir(directory)
@ -227,16 +227,24 @@
function update_user_stats(msg) function update_user_stats(msg)
-- Save user to _users table -- Save user to _users table
if (_users[msg.from.id] == nil) then local from_id = tostring(msg.from.id)
_users[msg.from.id] = { if (_users[from_id] == nil) then
_users[from_id] = {
name = get_name(msg), name = get_name(msg),
msg_num = 1 msg_num = 1
} }
else else
_users[msg.from.id].msg_num = _users[msg.from.id].msg_num + 1 local actual_num = _users[from_id].msg_num
_users[from_id].msg_num = actual_num + 1
end end
end end
function load_user_stats()
local f = assert(io.open('./res/users.json', "r"))
local c = f:read "*a"
return json:decode(c)
end
function get_receiver(msg) function get_receiver(msg)
if msg.to.type == 'user' then if msg.to.type == 'user' then
return 'user#id'..msg.from.id return 'user#id'..msg.from.id
@ -269,12 +277,10 @@
started = 1 started = 1
end end
-- Start and load values -- Start and load values
config = load_config() config = load_config()
_users = load_user_stats()
our_id = 0 our_id = 0
_users = {}
now = os.time() now = os.time()
-- load plugins -- load plugins

View File

@ -9,6 +9,5 @@
"access_token_secret": "", "access_token_secret": "",
"consumer_key": "", "consumer_key": "",
"consumer_secret": "" "consumer_secret": ""
}, }
"values": { }
} }

View File

@ -1,13 +1,17 @@
local f = assert(io.open('./res/values.json', "r+"))
local c = f:read "*a"
_values = json:decode(c)
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.
if (value_name == nil ) then if (value_name == nil ) then
local text = "" local text = ""
for key,value in pairs(config.values) do for key,value in pairs(_values) do
text = text..key.." = "..value.."\n" text = text..key.." = "..value.."\n"
end end
return text return text
end end
local value = config.values[value_name] local value = _values[value_name]
if ( value == nil) then if ( value == nil) then
return "Can't find "..value_name return "Can't find "..value_name
end end

View File

@ -3,11 +3,13 @@ function save_value( text )
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
config.values[var_name] = var_value _values[var_name] = var_value
local json_text = json:encode_pretty(config)
file = io.open ("./bot/config.json", "w+") local json_text = json:encode_pretty(_values)
file = io.open ("./res/values.json", "w+")
file:write(json_text) file:write(json_text)
file:close() file:close()
return "Saved "..var_name.." = "..var_value return "Saved "..var_name.." = "..var_value
end end

View File

@ -1,4 +1,12 @@
function run(msg, matches) function run(msg, matches)
vardump(_users)
-- Save stats to file
local json_users = json:encode_pretty(_users)
vardump(json_users)
file_users = io.open ("./res/users.json", "w")
file_users:write(json_users)
file_users:close()
local text = "" local text = ""
for id, user in pairs(_users) do for id, user in pairs(_users) do
text = text..user.name..": "..user.msg_num.."\n" text = text..user.name..": "..user.msg_num.."\n"

3
res/users.json Normal file
View File

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

3
res/values.json Normal file
View File

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