summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2022-08-04 03:31:53 +0100
committerubq323 <ubq323@ubq323.website>2022-08-04 03:31:53 +0100
commit00fded29d941ff6418d0b47191affbe82dd800a7 (patch)
tree05c4a8bda6fe4b6ed5b173d0c712951c38400c0d
parent885b9a7e81b34ab6a530f705ba70b98c11d57156 (diff)
chat
-rw-r--r--chat.lua32
1 files 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