From 00fded29d941ff6418d0b47191affbe82dd800a7 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 4 Aug 2022 03:31:53 +0100 Subject: chat --- chat.lua | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/chat.lua b/chat.lua index d347f68..3c9593f 100644 --- a/chat.lua +++ b/chat.lua @@ -104,6 +104,25 @@ local function draw_lb(W,scores) end end +-- persistence +local function save_scores(scores) + local file,err = fs.open(".chatscores","w") + if err then error("fs.open "..err) end + file.write(textutils.serialize(scores)) + file.flush() + file.close() +end + +local function load_scores() + local file,err = fs.open(".chatscores","r") + if err then + print("fs.open "..err.." - resetting scores") + return {} + end + local c = file.readAll() or "" + file.close() + return textutils.unserialize(c) or {} +end -- scoring @@ -113,14 +132,8 @@ local function score(msg) return s end -local userscores = setmetatable({ - aaa=10, - bbb=100, - ccc=12345, - ddd=234287, - thethethethe=69420, - harold=111222, -},{__index=function()return 0 end}) + +local userscores = setmetatable(load_scores(),{__index=function()return 0 end}) while true do draw_lb(wLb,userscores) @@ -128,7 +141,8 @@ while true do local s = score(msg) userscores[user] = userscores[user] + s if isnan(userscores[user]) then userscores[user] = 0 end - p(M,string.format("(%d) <%s> %s",s,user,msg)) + save_scores(userscores) + p(wLog,string.format("(%d) <%s> %s",s,user,msg)) end -- cgit v1.2.3