The3vilM0nk3y

ChatListener

Sep 15th, 2021 (edited)
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.40 KB | None | 0 0
  1. function hasValue(tab,val,keySearch)
  2.     for k,v in pairs(tab) do
  3.         if keySearch then
  4.             if k == val then
  5.                 return true
  6.             end
  7.         else
  8.             if v == val then
  9.                 return true
  10.             end
  11.         end
  12.     end
  13. end
  14. function split(s, delimiter)
  15.     result = {}
  16.     for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  17.         table.insert(result, match)
  18.     end
  19.     return result
  20. end
  21. function showValidCommands()
  22.   s = ""
  23.     for _,_command in pairs(validCommands) do
  24.       s = s .. _command .. ","
  25.     end
  26.     return s
  27. end
  28. function config(action)
  29.     if action == "load" then
  30.         if fs.exists("chatConfig.json") then
  31.             file = fs.open("chatConfig.json","r")
  32.             _data = textutils.unserialize(file.readAll())
  33.             print(_data)
  34.             owners = _data.owners or {"YourNameHere"}
  35.             file.close()
  36.         else
  37.             owners = {"YourNameHere"}
  38.             config("save")
  39.         end
  40.     elseif action == "save" then
  41.         _data = {}
  42.         _data.owners = owners
  43.         file = fs.open("chatConfig.json","w")
  44.         file.write(textutils.serialize(_data))
  45.         file.close()
  46.     end
  47.  
  48. end
  49. owners = {}
  50. config("load")
  51. requesterListFull = {"Alchemist", "Archer in Training", "Baker", "Beekeeper", "Blacksmith", "Builder", "Carpenter",
  52. "Chicken Farmer", "Composter", "Concrete Mixer", "Cook", "Assistant Cook", "Courier", "Cowhand",
  53. "Crusher", "Dyer", "Enchanter", "Farmer", "Fisher", "Fletcher", "Flourist", "Forester", "Glassblower",
  54. "Doctor", "Knight", "Archer", "Druid", "Knight in Training", "Library Student", "Mechanic", "Miner", "Planter",
  55. "Pupil", "Rabbit Herder", "Carpenter", "Student", "Shepherd", "Sifter", "Smelter", "Stonemason", "Stone Smelter", "Swineherd",
  56. "Teacher", "Undertaker", "Researcher", "Quarrier", "Request System"}
  57. validCommands = {
  58.     "add",
  59.     "rm",
  60.     "list",
  61.     "jobs",
  62.     "output",
  63.     --"view",
  64.     "restart",
  65.     "arShowAll",
  66.     "doRequests",
  67.     "update",
  68.     "help"}
  69. validMainCommand = {"$rq", "rq", "requestmanager", "$requestmanager"}
  70. chat = peripheral.find("chatBox")
  71. while true do
  72.     event, player, msg = os.pullEvent("chat")
  73.     print(player)
  74.     --Check owners list
  75.     if hasValue(owners,player) then
  76.         passes = true
  77.         inputs = split(msg, " ")
  78.         --check if its a command for this program
  79.         if hasValue(validMainCommand,inputs[1]) then
  80.             print("found command")
  81.             command = inputs[2]
  82.             --check if it has a proper sub command
  83.             if command == "whoisoutthere" then
  84.                 chat.sendMessage("Colony Owner - " .. owners[1], "Request Manager")
  85.                 passes = false
  86.             end
  87.             if command == nil or hasValue(validCommands,command) ~= true or command == "help" then
  88.                 chat.sendMessageToPlayer(tostring(command) .. " is not a valid command. Valid Commands are: " .. showValidCommands(),player)
  89.                 passes = false
  90.             end
  91.             if command == "restart" then
  92.                 chat.sendMessageToPlayer("Restarting the Computer",player)
  93.                 os.reboot()
  94.             end
  95.             if command == "update" then
  96.                 chat.sendMessageToPlayer("Updating, then Restarting the Computer",player)
  97.                 os.run({},"update.lua")
  98.                 --os.reboot()
  99.                 -- if fs.exists("chatListener.lua") then fs.delete("chatListener.lua") end
  100.                 -- if fs.exists("colonyInfo.lua") then fs.delete("colonyInfo.lua") end
  101.                 -- if fs.exists("requestManager.lua") then fs.delete("requestManager.lua") end
  102.                 -- if fs.exists("monitorManager.lua") then fs.delete("monitorManager.lua") end
  103.                 -- shell.run("pastebin get Taa1i9Tn chatListener.lua")
  104.                 -- shell.run("pastebin get wVz0Kg0N colonyInfo.lua")
  105.                 -- shell.run("pastebin get rSDtwYBe requestManager.lua")
  106.                 -- shell.run("pastebin get bVQbY1qL monitorManager.lua")
  107.                 -- if not fs.exists("startup.lua") then
  108.                 --  file = fs.open("startup.lua", "w");
  109.                 --  file.write("shell.execute('requestManager.lua')")
  110.                 --  file.close()
  111.                 -- end
  112.                 -- os.reboot()
  113.             end
  114.             -- extract job from the arguments
  115.             arg = inputs[3] or ""
  116.             job = ""
  117.             if command == "add" or command == "rm" then
  118.                 for i=3, #inputs, 1 do
  119.                     job = job .. inputs[i]
  120.                     if i ~= #inputs then
  121.                       job = job .. " "
  122.                     end
  123.                 end
  124.                 -- check if job name if valid
  125.                 if hasValue(requesterListFull,job) ~= true then
  126.                 -- check if its already on the list.
  127.                     jobList = ""
  128.                     for i=1,#requesterListFull do
  129.                         jobList = jobList .. requesterListFull[i] .. ", "
  130.                     end
  131.                     chat.sendMessageToPlayer(job .. " is not a valid job. Valid Jobs are: " .. jobList,player)
  132.                     passes = false
  133.                 else
  134.                     arg = job
  135.                 end
  136.             elseif command == "output" then
  137.                 if #inputs < 6 then
  138.                     chat.sendMessageToPlayer("Command Usage: rq output inventory x y z   Example: rq output minecraft:chest_1 20 65 -10",player)
  139.                     passes = false
  140.                 else
  141.                     arg = textutils.serialize({{x=inputs[4],y=inputs[5],z=inputs[6]},inputs[3]})
  142.                 end
  143.             else
  144.                 arg = inputs[3]
  145.             end
  146.             -- pass on command to requestManager to handle.
  147.             if passes then
  148.                 print("Sending request " .. command .. " " .. tostring(arg) .. " for " .. player)
  149.                 os.queueEvent("request",command, arg, player, subArg)
  150.                 -- start a timer and wait for a responce back
  151.                 myTimer = os.startTimer(0.5)
  152.                 while true do
  153.                     event,k1 = os.pullEvent()
  154.                     --resend event/timer if timed out
  155.                     if event == "timer" and k1 == myTimer then
  156.                         os.queueEvent("request",command, arg, player, subArg)
  157.                         myTimer = os.startTimer(0.5)
  158.                         --print("sending re-request")
  159.                     end
  160.                     --break out of confirm loop on proper confirmation
  161.                     if event == "confirm" then
  162.                         print("Transaction Complete")
  163.                         break
  164.                     end
  165.                 end
  166.             end
  167.         end
  168.     end
  169. end
  170.  
Advertisement
Add Comment
Please, Sign In to add comment