Advertisement
mattysmith22

liststore.lua

Aug 30th, 2019
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.38 KB | None | 0 0
  1. os.loadAPI("/liststore")
  2.  
  3. --Find wireless modem
  4. print("Configuring networking")
  5. rednet.open("left")
  6. print("Done!")
  7.  
  8. -- Setup database
  9. print ("Configuring database")
  10. db = liststore.TodoDB:new()
  11. db:setDbPath("db.txt")
  12. print ("Done!")
  13.  
  14. function defaultResponse(succ, ret)
  15.     if succ then
  16.         if ret == true then
  17.             return {status = "Success"}
  18.         else
  19.             return {status = "Error", msg = ret}
  20.         end
  21.     else
  22.         return {status = "Error", msg = "Catastrophic runtime error", err = ret}
  23.     end
  24. end
  25.  
  26. handlers = {}
  27. function handlers.ping(message)
  28.     return {status = "Success", msg = "pong"}
  29. end
  30.  
  31. function handlers.addItem(message)
  32.     local succ, ret = pcall(db.addItem, db, message.name, message.priority)
  33.     return defaultResponse(succ, ret)
  34. end
  35.  
  36. function handlers.renameItem(message)
  37.     local succ, ret = pcall(db.addItem, db, message.from, message.to)
  38.     return defaultResponse(succ, ret)
  39. end
  40.  
  41. function handlers.modifyAttribute(message)
  42.     local succ, ret = pcall(db.addItemAttribute, db, message.attribute, message.value)
  43.     return defaultResponse(succ, ret)
  44. end
  45.  
  46. function handlers.removeItem(message)
  47.     local succ, ret = pcall(db.removeItem, db, message.name)
  48.     return defaultResponse(succ, ret)
  49. end
  50.  
  51. function handlers.listItems(message)
  52.     local succ, ret = pcall(db.getItemNames, db, message.opts)
  53.     if succ then
  54.         return {status = "Success", list = ret}
  55.     else
  56.         return {status = "Error", msg = "Catastrophic runtime error"}
  57.     end
  58. end
  59.  
  60. function handlers.getItem(message)
  61.     local succ, ret = pcall(db.removeItem, db, message.name)
  62.     if succ then
  63.         if ret == true then
  64.             return {status = "Success", msg = ret}
  65.         else
  66.             return {status = "Error", msg = ret}
  67.         end
  68.     else
  69.         return {status = "Error", msg = "Catastrophic runtime error"}
  70.     end
  71. end
  72.  
  73. function handleMessage(message)
  74.     if handlers[message.command] == nil then
  75.         return {status = "Error", msg = "Command \"" .. "\" is unknown" }
  76.     end
  77.     return handlers[message.command](message)
  78. end
  79.  
  80. while true do
  81.     -- Main loop
  82.     local senderId, messageRaw, protocol = rednet.receive("todo")
  83.     local message = textutils.unserialise(messageRaw)
  84.  
  85.     response = handleMessage(message)
  86.  
  87.     rednet.send(senderId, textutils.serialise(response))
  88.     print("Handled message")
  89. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement