This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
dcleondc's profile picture

how to extend this program

Started by dcleondc, 28 September 2012 - 11:18 PM
dcleondc #1
Posted 29 September 2012 - 01:18 AM
here is the program i am using now

shell.run("clear")
print("ID | Distance | Message")
print("---+----------+---------
while true do
    id, msg, dis=rednet.receive(timeout)
    print(id.." | "..dis.." | "..msg
end
and i want the program to make it so if someone is trying to spam it they will get put onto a banned id list and there things will not get displayed on the monitor.
so what it would do is if someone sent 4 things in a row will all the same info in a 1 minute time period that computer id would get put on the banned id list and if they send a request again it will not get displayed.
Fatal_Exception #2
Posted 29 September 2012 - 04:38 AM
Maintain a table of banned ids, and every message you get, check that the id isn't in the table.
This tutorial on the wiki might be of use.
KaoS #3
Posted 29 September 2012 - 07:38 AM
Give this a try


rednet.open('right')
local tRecord={}
local tBanned={}
print("ID | Distance | Message")
print("---+----------+--------")
while true do
  local id,msg,dist=rednet.receive()
  if not tRecord[id] then
   tRecord[id]={}
  end
  while #tRecord[id]>=4 do
   table.remove(tRecord[id],1)
  end
  tRecord[id][#tRecord[id]+1]=msg
  if tRecord[id][1]==tRecord[id][2] and tRecord[id][1]==tRecord[id][3] and tRecord[id][1]==tRecord[id][4] then
   tBanned[id]=true
  elseif not tBanned[id] then
   id=string.sub(id,1,3) --shorten the id to 3 chars if it is too long
   dist=string.sub(dist,1,9)
   print(id..string.rep(' ',3-#id)..'| '..dist..string.rep(' ',9-#dist)..'| '..msg) --add spaces to fill up the gap
  end
end

fully tested and functional, edit modem side of course
dcleondc #4
Posted 29 September 2012 - 08:56 AM
Give this a try


rednet.open('right')
local tRecord={}
local tBanned={}
print("ID | Distance | Message")
print("---+----------+--------")
while true do
  local id,msg,dist=rednet.receive()
  if not tRecord[id] then
   tRecord[id]={}
  end
  while #tRecord[id]>=4 do
   table.remove(tRecord[id],1)
  end
  tRecord[id][#tRecord[id]+1]=msg
  if tRecord[id][1]==tRecord[id][2] and tRecord[id][1]==tRecord[id][3] and tRecord[id][1]==tRecord[id][4] then
   tBanned[id]=true
  elseif not tBanned[id] then
   id=string.sub(id,1,3) --shorten the id to 3 chars if it is too long
   dist=string.sub(dist,1,9)
   print(id..string.rep(' ',3-#id)..'| '..dist..string.rep(' ',9-#dist)..'| '..msg) --add spaces to fill up the gap
  end
end

fully tested and functional, edit modem side of course
if the server were to restart does the ban list stay the same? or could you make it so it gets saved to a file
KaoS #5
Posted 29 September 2012 - 10:31 AM
sure


rednet.open('right')
local tRecord={}
if fs.exists('banned') then
  local oFile=io.open('banned','r')
  local tBanned=textutils.unserialize(oFile:read())
  oFile:close()
else
  local tBanned={}
end
print("ID | Distance | Message")
print("---+----------+--------")
while true do
  local id,msg,dist=rednet.receive()
  if not tRecord[id] then
   tRecord[id]={}
  end
  while #tRecord[id]>=4 do
   table.remove(tRecord[id],1)
  end
  tRecord[id][#tRecord[id]+1]=msg
  if tRecord[id][1]==tRecord[id][2] and tRecord[id][1]==tRecord[id][3] and tRecord[id][1]==tRecord[id][4] then
   tBanned[id]=true
   local oFile=io.open('banned','w')
   oFile:write(textutils.serialize(tBanned))
   oFile:close()
  elseif not tBanned[id] then
   id=string.sub(id,1,3) --shorten the id to 3 chars if it is too long
   dist=string.sub(dist,1,9)
   print(id..string.rep(' ',3-#id)..'| '..dist..string.rep(' ',9-#dist)..'| '..msg) --add spaces to fill up the gap
  end
end
dcleondc #6
Posted 29 September 2012 - 04:18 PM
sure


rednet.open('right')
local tRecord={}
if fs.exists('banned') then
  local oFile=io.open('banned','r')
  local tBanned=textutils.unserialize(oFile:read())
  oFile:close()
else
  local tBanned={}
end
print("ID | Distance | Message")
print("---+----------+--------")
while true do
  local id,msg,dist=rednet.receive()
  if not tRecord[id] then
   tRecord[id]={}
  end
  while #tRecord[id]>=4 do
   table.remove(tRecord[id],1)
  end
  tRecord[id][#tRecord[id]+1]=msg
  if tRecord[id][1]==tRecord[id][2] and tRecord[id][1]==tRecord[id][3] and tRecord[id][1]==tRecord[id][4] then
   tBanned[id]=true
   local oFile=io.open('banned','w')
   oFile:write(textutils.serialize(tBanned))
   oFile:close()
  elseif not tBanned[id] then
   id=string.sub(id,1,3) --shorten the id to 3 chars if it is too long
   dist=string.sub(dist,1,9)
   print(id..string.rep(' ',3-#id)..'| '..dist..string.rep(' ',9-#dist)..'| '..msg) --add spaces to fill up the gap
  end
end
one more thing, can you make it so if the id msg, and distance are the same then they get banned instead of just the ids