Advertisement
mattysmith22

todoserver.lua

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