online list, bugfixes

This commit is contained in:
Er2 2022-03-07 18:59:41 +03:00
parent 1e05869bd8
commit 370a6572ad

View file

@ -53,15 +53,21 @@ end
function forc(fn, t, c) function forc(fn, t, c)
if c == nil or c == true then 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) 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
end end
if t then if t then
for k, v in pairs(tmp) do local k = 1
while k <= #tmp do
local v = tmp[k]
fn(v, 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 end
end end
@ -80,9 +86,12 @@ while runs do
elseif v.st == 1 then write (v, 'Username: ') elseif v.st == 1 then write (v, 'Username: ')
elseif v.st == 2 then write (v, 'Password: ') elseif v.st == 2 then write (v, 'Password: ')
elseif v.st == 3 then writeln(v, 'Welcome, '.. v.user ..'!') elseif v.st == 3 then writeln(v, 'Welcome, '.. v.user ..'!')
local us = ''
for _, w in pairs(cls) do for _, w in pairs(cls) do
writeln(w, '', '-- '.. v.user ..' joined --') writeln(w, '', '-- '.. v.user ..' joined --')
us = us.. w.user ..', '
end end
writeln(v, 'Online: '.. us ..v.user)
v.st, v.reads = nil, nil v.st, v.reads = nil, nil
table.remove(tmp, k) table.remove(tmp, k)
table.insert(cls, v) table.insert(cls, v)
@ -123,14 +132,16 @@ while runs do
elseif l == '/leave' then close(v) elseif l == '/leave' then close(v)
elseif (l or ''):sub(1, 1) == '/' elseif (l or ''):sub(1, 1) == '/'
then writeln(v, 'Unknown command') then writeln(v, 'Unknown command')
end
elseif not (l or ''):match '^%s*$' if not (l or ''):match '^%s*$'
then forc(function(w) and (not l:sub(1, 1) == '/' or v.closed)
if w == v then -- ignore yourself then for j, w in pairs(cls) do
if j == k then -- ignore yourself
elseif v.closed elseif v.closed
then writeln(w, '', '-- '.. v.user ..' leaves --') then writeln(w, '', '-- '.. v.user ..' leaves --')
else writeln(w, '', v.user ..': '.. l) else writeln(w, '', v.user ..': '.. l)
end end
end) end end end
end, false, true) end, false, true)
end end