Advertisement
BasketMC

ComputerCraft: Terminal Daemon

Dec 7th, 2015
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.75 KB | None | 0 0
  1. os.loadAPI("apis/Terminal")
  2. os.loadAPI("apis/BasketNet")
  3.  
  4. Terminal.init()
  5.  
  6. local choice = "mainmenu"
  7.  
  8. local sLoggedInUser = "BasketMC"
  9.  
  10. while true do
  11.  
  12.     if choice == "mainmenu" then
  13.  
  14.         term.clear()
  15.  
  16.         print("Main Menu:")
  17.  
  18.         print(" 1. Get a list of Storage Cells")
  19.  
  20.         print(" 2. Request Items")
  21.  
  22.         print(" 3. Reboot the system")
  23.  
  24.         print(" 0. Exit")
  25.  
  26.         choice = read()
  27.  
  28.         if choice == "0" then
  29.             return
  30.         elseif choice == "1" then
  31.             choice = "storagecelllist"
  32.  
  33.         elseif choice == "2" then
  34.             choice = "requestitem"
  35.  
  36.         elseif choice == "3" then
  37.             choice = "reboot"
  38.         end
  39.  
  40.     elseif choice == "requestitem" then
  41.  
  42.         term.clear()
  43.         print("Loading...")
  44.  
  45.         local tInventory = {}
  46.  
  47.         local iAttempts = 0
  48.         while iAttempts < 5 do
  49.             BasketNet.sendToMasterControl("GetInventory " .. sLoggedInUser, "BasketNet")
  50.             local iSenderID, sMessage, sProtocol = BasketNet.receiveFromMasterControl(10, "BasketNet")
  51.             if iSenderID ~= nil then
  52.                 tInventory = sMessage                  
  53.                 iAttempts = 0
  54.                 break
  55.             end
  56.             iAttempts = iAttempts + 1
  57.         end
  58.            
  59.         if iAttempts >= 5 then
  60.             print("Unable to contact Master Control")
  61.             choice = "mainmenu"
  62.             sleep(3)
  63.         else
  64.  
  65.             local iOffset = 0
  66.             local iHowManyPerPage = 15
  67.             local sFilter = ""
  68.  
  69.             while true do
  70.  
  71.                 term.clear()
  72.  
  73.                 local iCounter = 0
  74.                 local iListCounter = 1
  75.                 local iLimit = iOffset + iHowManyPerPage
  76.  
  77.                 local tItemShortList = {}
  78.  
  79.                 local tFilteredResults = {}
  80.  
  81.                 for k, tItem in ipairs(tInventory) do
  82.                     table.insert(tFilteredResults, tItem)
  83.                 end
  84.  
  85.                 for k, tItem in ipairs(tFilteredResults) do
  86.                     if sFilter == "" or string.match(tItem.name, sFilter) then                     
  87.                         if iCounter < iLimit and iCounter >= iOffset then
  88.                             tItemShortList[iListCounter] = tItem
  89.                             print(iListCounter .. ".\t" ..tItem.count .. "x\t" .. tItem.name)
  90.                             iListCounter = iListCounter + 1
  91.                         end
  92.                         iCounter = iCounter + 1
  93.                         if iCounter > iLimit then
  94.                             break
  95.                         end
  96.                     end
  97.                 end
  98.  
  99.                 print("Usage:")
  100.                 print("'search [keyword]'; 'p [page number]'; '[number in front of item]'; exit")
  101.  
  102.                 choice = read()
  103.  
  104.                 local tParsedMessage = {}
  105.                
  106.                 for sPart in string.gmatch(choice, "%S+") do
  107.                     table.insert(tParsedMessage, sPart)
  108.                 end
  109.  
  110.                 if tParsedMessage[1] == "p" then
  111.                     iOffset = (tParsedMessage[2] - 1) * iHowManyPerPage
  112.                 elseif tParsedMessage[1] == "search" then
  113.                     if tParsedMessage[2] == nil then
  114.                         sFilter = ""
  115.                     else
  116.                         sFilter = tParsedMessage[2]
  117.                     end
  118.                 elseif tParsedMessage[1] == "exit" then
  119.                     choice = "mainmenu"
  120.                     break
  121.                 else
  122.                     choice = choice + 0
  123.                     if choice > 0 and choice <= iHowManyPerPage then
  124.                         if not BasketNet.startConversationWithMasterControl() then
  125.                             print("Unable to connect to master control")
  126.                             BasketNet.endConversation()
  127.                             sleep(2)
  128.                             break
  129.                         end
  130.  
  131.                         BasketNet.sendToConversation("Retrieve " .. sLoggedInUser .. " 64")
  132.                         local iSenderID, sMessage, sProtocol = BasketNet.receiveFromConversation(3)
  133.                         if sMessage ~= "Ready" then
  134.                             print("Master Control is not ready")
  135.                             BasketNet.endConversation()
  136.                             sleep(2)
  137.                             break
  138.                         end
  139.  
  140.                         BasketNet.sendToConversation(tItemShortList[choice])
  141.  
  142.                         if iSenderID ~= nil and sMessage == "Retrieving" then
  143.                             print("Unable to retrieve items right now")
  144.                             BasketNet.endConversation()
  145.                             sleep(4)
  146.                             break
  147.                         end
  148.  
  149.                         print("Retrieving...")
  150.                         BasketNet.endConversation()
  151.                         sleep(4)
  152.                     end
  153.                 end
  154.  
  155.             end
  156.         end
  157.  
  158.     elseif choice == "reboot" then
  159.  
  160.         BasketNet.sendToMasterControl("Reboot", "BasketNet")
  161.  
  162.         print("Rebooting...")
  163.         sleep(5)
  164.  
  165.         choice = "mainmenu"
  166.  
  167.         -- send reboot command
  168.  
  169.     end
  170.  
  171. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement