Guest User

todoserver

a guest
Jan 2nd, 2013
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.84 KB | None | 0 0
  1. local tTodoList = {}
  2. local saveFile = "disk/todo"
  3.  
  4. local function addToList(todo)
  5.    table.insert( tTodList, todo)
  6.    local q = fs.open( saveFile, "w")
  7.    q.write( textutils.serialize(tTodoList))
  8.    q.close()
  9. end
  10.  
  11. local function readFromFile()
  12.    local a = fs.open( saveFile, "r")
  13.    tTodoList = textutils.unserialize( a.readAll( "*a"))
  14.    a.close()
  15. end
  16.  
  17. local function writeToFile()
  18.    local b = fs.open( saveFile, "w")
  19.    b.write(textutils.serialize( tTodoList ))
  20.    b.close()
  21. end
  22.  
  23. local function removeFromList(todo)
  24.    if type(todo) == "number" then
  25.     table.remove( tTodoList, todo)
  26.    elseif type(todo) == "string" then
  27.      if tonumber( todo ) then
  28.       table.remove(tTodoList, todo)
  29.      else
  30.        for i = 1, #tTodoList do
  31.         if tTodoList[i] == todo then
  32.           table.remove( tTodoList, i )
  33.           break
  34.        end
  35.      end
  36.    end
  37.     else
  38.     return false
  39.     end
  40.     writeToFile()
  41.     return true
  42. end
  43.  
  44. local function checkList( id )
  45.   rednet.send( id, textutils.serialize( tTodoList))
  46. end
  47.    
  48.  
  49.  
  50. rednet.open("top")
  51. readFromFile()
  52.  
  53. while true do
  54.   local id, cmd = rednet.receive()
  55.      if cmd == "create" then
  56.        print("Add: "..id)
  57.          rednet.send(id, "What would you like to add?")
  58.           idb, txtb = rednet.receive()
  59.           print(txtb)
  60.         print("------------------------")    
  61.         addToList(txtb)
  62.      elseif cmd == "delete" then
  63.         print("Delete: "..id)
  64.         rednet.send(idb, "What would you like to delete?")
  65.          ida, txta = rednet.receive()
  66.           print(txta)
  67.           print("----------------------")
  68.         if not removeFromList(txta) then
  69.           print("   Failed: Does not Exist.")
  70.        end
  71.        elseif cmd == "read" then
  72.         print("Read: "..id)
  73.         print("------------------------")
  74.        checkList(ida)
  75.      end
  76.    end
Advertisement
Add Comment
Please, Sign In to add comment