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.
--
-- Copyright 2010-2013 Jeffrey Friedl
-- Copyright 2010-2014 Jeffrey Friedl
-- http://regex.info/blog/
--
-- Latest version: http://regex.info/blog/lua/json
@ -10,11 +10,22 @@
-- This code is released under a Creative Commons CC-BY "Attribution" License:
-- http://creativecommons.org/licenses/by/3.0/deed.en_US
--
-- It can be used for any purpose so long as the copyright notice and
-- web-page links above are maintained. Enjoy.
-- It can be used for any purpose so long as the copyright notice above,
-- 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 OBJDEF = { VERSION = VERSION }
local VERSION = 20140920.13 -- version history at end of file
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 isObject = { __tostring = function() return "JSON object" end } isObject.__index = isObject
@ -386,7 +395,7 @@ end
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
return match_end + 1
else
@ -397,7 +406,7 @@ end
local grok_one -- assigned later
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)
end
@ -420,9 +429,9 @@ local function grok_object(self, text, start, etc)
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.
@ -446,7 +455,7 @@ local function grok_object(self, text, start, etc)
end
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)
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.
--
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)
end
@ -841,6 +850,13 @@ return OBJDEF:new()
--
-- 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
-- ["1",null,null,null,null,null,"seven"],
-- would return

View File

@ -3,7 +3,7 @@
URL = require("socket.url")
json = (loadfile "./bot/JSON.lua")()
VERSION = 'v0.7.2'
VERSION = 'v0.7.3'
-- taken from http://stackoverflow.com/a/11130774/3163199
function scandir(directory)
@ -227,16 +227,24 @@
function update_user_stats(msg)
-- Save user to _users table
if (_users[msg.from.id] == nil) then
_users[msg.from.id] = {
local from_id = tostring(msg.from.id)
if (_users[from_id] == nil) then
_users[from_id] = {
name = get_name(msg),
msg_num = 1
}
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
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)
if msg.to.type == 'user' then
return 'user#id'..msg.from.id
@ -269,12 +277,10 @@
started = 1
end
-- Start and load values
config = load_config()
_users = load_user_stats()
our_id = 0
_users = {}
now = os.time()
-- load plugins

View File

@ -9,6 +9,5 @@
"access_token_secret": "",
"consumer_key": "",
"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 )
-- If there is not value name, return all the values.
if (value_name == nil ) then
local text = ""
for key,value in pairs(config.values) do
for key,value in pairs(_values) do
text = text..key.." = "..value.."\n"
end
return text
end
local value = config.values[value_name]
local value = _values[value_name]
if ( value == nil) then
return "Can't find "..value_name
end

View File

@ -3,11 +3,13 @@ function save_value( text )
if (var_name == nil or var_value == nil) then
return "Usage: !set var_name value"
end
config.values[var_name] = var_value
local json_text = json:encode_pretty(config)
file = io.open ("./bot/config.json", "w+")
_values[var_name] = var_value
local json_text = json:encode_pretty(_values)
file = io.open ("./res/values.json", "w+")
file:write(json_text)
file:close()
return "Saved "..var_name.." = "..var_value
end

View File

@ -1,4 +1,12 @@
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 = ""
for id, user in pairs(_users) do
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 @@
{
}