Advertisement
Pinkishu

order

Aug 25th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.99 KB | None | 0 0
  1. local registrations = {}
  2. local tesseracts = {}
  3. local maxTesseracts=3
  4. local cTesseracts=0
  5. local chat = peripheral.wrap("right")
  6.  
  7. function strsplit(self,sep)
  8.         local sep, fields = sep or ":", {}
  9.         local pattern = string.format("([^%s]+)", sep)
  10.         self:gsub(pattern, function(c) fields[#fields+1] = c end)
  11.         return fields
  12. end
  13.  
  14.  
  15. function strjoin(list,delimiter)
  16.   local len = #list
  17.   if len == 0 then
  18.     return ""
  19.   end
  20.   local string = list[1]
  21.   for i = 2, len do
  22.     string = string .. delimiter .. list[i]
  23.   end
  24.   return string
  25. end
  26.  
  27. function tryRegister(name,channel)
  28.     if channel == nil or channel == 0 then
  29.       chat.tell(name, "Can't register this channel")
  30.       return
  31.     end
  32.     if registrations[name] ~= nil then
  33.         chat.tell(name,"You already registered a tesseract channel.")
  34.     else
  35.         if cTesseracts >= maxTesseracts then
  36.             chat.tell(name,"Sorry, all tesseracts are in use.")
  37.             return
  38.         end
  39.  
  40.         local regT = 0
  41.         for i=1,maxTesseracts,1 do
  42.             if tesseracts[i] == nil then
  43.                 tesseracts[i] = name
  44.                 local tesseract = peripheral.wrap("item_tesseract_"..(i-1))
  45.                 tesseract.setFrequency(channel)
  46.                 cTesseracts = cTesseracts + 1
  47.                 regT = i
  48.             end
  49.         end
  50.  
  51.         if regT ~= 0 then
  52.             registrations[name] = {channel,regT}
  53.             chat.tell(name,"Sucesfully registered channel "..channel)
  54.         else
  55.             chat.tell(name,"Unknown error")
  56.         end
  57.     end
  58. end
  59.  
  60. function getIID(rqpipe,item)
  61.     local items = rqpipe.getAvailableItems()
  62.  
  63.     for i,v in pairs(items) do
  64.         local uName = rqpipe.getUnlocalizedName(v[1])
  65.         if uName == item then
  66.           return v[1],v[2]
  67.         end
  68.     end
  69.     return nil
  70. end
  71.  
  72. function tryUnregister(name)
  73.   if registrations[name] == nil then
  74.     chat.tell(name,"You aren't registered.")
  75.     return
  76.   end
  77.  
  78.   cTesseracts = cTesseracts-1
  79.   tesseracts[registrations[name][2]] = nil
  80.   registrations[name] = nil
  81.  
  82.   chat.tell(name,"Sucessfully unregistered.")
  83. end
  84.  
  85. function tryOrder(name,amount,item)
  86.     if registrations[name] == nil then
  87.         chat.tell(name,"Please register before ordering.")
  88.         return
  89.     end
  90.  
  91.     local tesseractNumber = registrations[name][2]
  92.  
  93.     local rqpipe = peripheral.wrap("LogisticsPipes:Request_"..(tesseractNumber-1))
  94.  
  95.     local iid,avAmount = getIID(rqpipe,item)
  96.  
  97.     if iid == nil then
  98.       chat.tell(name,"Item not found.")
  99.       return
  100.     end
  101.  
  102.     if avAmount < amount then
  103.       chat.tell("You tried to order "..amount.." "..item.. ", but only "..avAmount.. " are available.")
  104.       return
  105.     end
  106.  
  107.     rqpipe.makeRequest(iid,amount,false)
  108.     chat.tell(name,"Order sucessful")
  109.  
  110. end
  111.  
  112. while true do
  113.   local evData = { os.pullEvent("chat") }
  114.   local name = evData[2]
  115.   local msg = evData[3]
  116.   msgSplit = strsplit(msg," ")
  117.  
  118.   local cmd = table.remove(msgSplit,1)
  119.   if cmd == "register" and #msgSplit == 1 then
  120.     tryRegister(name,tonumber(msgSplit[1]))
  121.   elseif cmd=="order" and #msgSplit >= 2 then
  122.     tryOrder(name,tonumber(table.remove(msgSplit,1)),strjoin(msgSplit," "))
  123.   elseif cmd=="unregister" and #msgSplit == 0 then
  124.     tryUnregister(name)
  125.   end
  126. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement