From 370a6572ad2f2e01beba8cc50087a172f52100da Mon Sep 17 00:00:00 2001 From: Er2 Date: Mon, 7 Mar 2022 18:59:41 +0300 Subject: [PATCH] online list, bugfixes --- main.lua | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/main.lua b/main.lua index 57c3fd5..546996e 100644 --- a/main.lua +++ b/main.lua @@ -53,15 +53,21 @@ end function forc(fn, t, c) if c == nil or c == true then - for k, v in pairs(cls) do + local k = 1 + while k <= #cls do + local v = cls[k] fn(v, k) - if v.closed then table.remove(cls, k) end + if v.closed then table.remove(cls, k) + else k = k + 1 end end end if t then - for k, v in pairs(tmp) do + local k = 1 + while k <= #tmp do + local v = tmp[k] fn(v, k) - if v.closed then table.remove(tmp, k) end + if v.closed then table.remove(tmp, k) + else k = k + 1 end end end end @@ -80,9 +86,12 @@ while runs do elseif v.st == 1 then write (v, 'Username: ') elseif v.st == 2 then write (v, 'Password: ') elseif v.st == 3 then writeln(v, 'Welcome, '.. v.user ..'!') + local us = '' for _, w in pairs(cls) do writeln(w, '', '-- '.. v.user ..' joined --') + us = us.. w.user ..', ' end + writeln(v, 'Online: '.. us ..v.user) v.st, v.reads = nil, nil table.remove(tmp, k) table.insert(cls, v) @@ -123,14 +132,16 @@ while runs do elseif l == '/leave' then close(v) elseif (l or ''):sub(1, 1) == '/' then writeln(v, 'Unknown command') + end - elseif not (l or ''):match '^%s*$' - then forc(function(w) - if w == v then -- ignore yourself + if not (l or ''):match '^%s*$' + and (not l:sub(1, 1) == '/' or v.closed) + then for j, w in pairs(cls) do + if j == k then -- ignore yourself elseif v.closed then writeln(w, '', '-- '.. v.user ..' leaves --') else writeln(w, '', v.user ..': '.. l) end - end) end + end end end, false, true) end