Advertisement
BasketMC

ComputerCraft: BasketNet API

Dec 6th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.57 KB | None | 0 0
  1. -- apis/BasketNet
  2.  
  3. local iMasterControlID
  4.  
  5. local iConversationPartnerID
  6. local sConversationKey
  7.  
  8. function broadcast(sMessage, sProtocol)
  9.     if sProtocol == nil then
  10.         sProtocol = "BasketNet"
  11.     end
  12.     if not rednet.isOpen("left") then
  13.         rednet.open("left")
  14.     end
  15.     rednet.broadcast(sMessage, sProtocol)
  16. end
  17.  
  18. function send(id, sMessage, sProtocol)
  19.     if sProtocol == nil then
  20.         sProtocol = "BasketNet"
  21.     end
  22.     if not rednet.isOpen("left") then
  23.         rednet.open("left")
  24.     end
  25.     id = id + 0
  26.     if id ~= nil and id ~= 0 then
  27.         return rednet.send(id, sMessage, sProtocol)
  28.     end
  29.     return false
  30. end
  31.  
  32. function sendToMasterControl(sMessage, sProtocol)  
  33.     if sProtocol == nil then
  34.         sProtocol = "BasketNet"
  35.     end
  36.     if not rednet.isOpen("left") then
  37.         rednet.open("left")
  38.     end
  39.     return rednet.send(getMasterControlID(), sMessage, sProtocol)
  40. end
  41.  
  42. -- Return local iSenderId, sMessage, sProtocol
  43. -- on timeout returns nil
  44. function receive(iTimeout, sProtocol)
  45.     if sProtocol == nil then
  46.         sProtocol = "BasketNet"
  47.     end
  48.     return rednet.receive(sProtocol, iTimeout)
  49. end
  50.  
  51. -- Return local iSenderId, sMessage, sProtocol
  52. -- on timeout returns nil
  53. function receiveFromMasterControl(iTimeout, sProtocol)
  54.     local iSenderID, sMessage, sReceivedProtocol
  55.     while true do
  56.         if sProtocol == nil then
  57.             sProtocol = "BasketNet"
  58.         end
  59.         iSenderID, sMessage, sReceivedProtocol= rednet.receive(sProtocol, iTimeout)
  60.         if iSenderID == nil then
  61.             return nil
  62.         end
  63.         if iSenderID == getMasterControlID() then
  64.             break
  65.         end
  66.     end
  67.  
  68.     return iSenderID, sMessage, sReceivedProtocol
  69. end
  70.  
  71. function getMasterControlID()
  72.     if iMasterControlID == nil or not iMasterControlID then
  73.         while true do
  74.             iMasterControlID = rednet.lookup("BasketNet", "MasterControl")
  75.             if not iMasterControlID then
  76.                 print("Unable to find Master Control.")
  77.                 sleep(5)
  78.             else
  79.                 break
  80.             end
  81.         end
  82.     end
  83.     return iMasterControlID
  84. end
  85.  
  86. function registerAsMasterControl()
  87.     rednet.host("BasketNet", "MasterControl")
  88. end
  89.  
  90. function startConversation(iComputerID)
  91.     sConversationKey = getConversationKey(10)
  92.     iConversationPartnerID = iComputerID
  93.     local iAttempts = 0
  94.     while iAttempts < 5 do
  95.         send(iComputerID, "StartConversation " .. sConversationKey, "BasketNet")
  96.         local iSenderID, sMessage, sProtocol = receiveFromConversation(1)
  97.         if iSenderID ~= nil and sMessage == "JoinedConversation" then
  98.             iAttempts = 0
  99.             break
  100.         end
  101.         iAttempts = iAttempts + 1
  102.     end
  103.     if iAttempts == 0 then
  104.         return true
  105.     else
  106.         return false
  107.     end
  108. end
  109.  
  110. function startConversationWithMasterControl()
  111.     return startConversation(getMasterControlID())
  112. end
  113.  
  114. function joinConversation(iNewConversationPartnerID, sNewConversationKey)
  115.     iConversationPartnerID = iNewConversationPartnerID
  116.     sConversationKey = sNewConversationKey
  117.     sendToConversation("JoinedConversation")
  118. end
  119.  
  120. function endConversation()
  121.     sendToConversation("EndConversation")
  122.     iConversationPartnerID = 0
  123.     sConversationKey = ""
  124. end
  125.  
  126. -- Return local iSenderID, sMessage, sProtocol
  127. -- on timeout returns nil
  128. function receiveFromConversation(iTimeout)
  129.     if iTimeout == nil then
  130.         iTimeout = 2
  131.     end
  132.     return rednet.receive("BasketNet" .. sConversationKey, iTimeout)
  133. end
  134.  
  135. function sendToConversation(sMessage)
  136.     if iConversationPartnerID ~= 0 then
  137.         send(iConversationPartnerID, sMessage, "BasketNet" .. sConversationKey)
  138.     end
  139. end
  140.  
  141. function getConversationKey(l)
  142.         if l < 1 then
  143.         return nil
  144.     end
  145.         local sKey = ""
  146.         for i = 1, l do
  147.                 sKey = sKey .. string.char(math.random(string.byte("a"), string.byte("z")))
  148.         end
  149.         return sKey
  150. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement