From e2403a6e3722e43ce463544e10c9cd43949164f0 Mon Sep 17 00:00:00 2001 From: yago Date: Sun, 23 Nov 2014 00:31:22 +0100 Subject: [PATCH] get, set, and stats persistence --- bot/JSON.lua | 42 +++++++++++++++++++++++++++++------------- bot/bot.lua | 20 +++++++++++++------- bot/config.json | 3 +-- plugins/get.lua | 8 ++++++-- plugins/set.lua | 8 +++++--- plugins/stats.lua | 8 ++++++++ res/users.json | 3 +++ res/values.json | 3 +++ 8 files changed, 68 insertions(+), 27 deletions(-) create mode 100644 res/users.json create mode 100644 res/values.json diff --git a/bot/JSON.lua b/bot/JSON.lua index 53065bb..8723771 100644 --- a/bot/JSON.lua +++ b/bot/JSON.lua @@ -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 diff --git a/bot/bot.lua b/bot/bot.lua index 60d72d6..dce382f 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -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 diff --git a/bot/config.json b/bot/config.json index 7c20281..feff6fc 100644 --- a/bot/config.json +++ b/bot/config.json @@ -9,6 +9,5 @@ "access_token_secret": "", "consumer_key": "", "consumer_secret": "" - }, - "values": { } + } } diff --git a/plugins/get.lua b/plugins/get.lua index 52a2dd4..d42c4da 100644 --- a/plugins/get.lua +++ b/plugins/get.lua @@ -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 diff --git a/plugins/set.lua b/plugins/set.lua index da0dedd..6b27911 100644 --- a/plugins/set.lua +++ b/plugins/set.lua @@ -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 diff --git a/plugins/stats.lua b/plugins/stats.lua index 75e21b6..e0133a7 100644 --- a/plugins/stats.lua +++ b/plugins/stats.lua @@ -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" diff --git a/res/users.json b/res/users.json new file mode 100644 index 0000000..0e0dcd2 --- /dev/null +++ b/res/users.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file diff --git a/res/values.json b/res/values.json new file mode 100644 index 0000000..0e0dcd2 --- /dev/null +++ b/res/values.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file