Advertisement
Pinkishu

order2

Aug 25th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.95 KB | None | 0 0
  1. local registrations = {}
  2. local orders = {}
  3. local chat = peripheral.wrap("right")
  4. local rqpipe = peripheral.wrap("back")
  5. local tesseract = peripheral.wrap("item_tesseract_4")
  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.         registrations[name] = {channel}
  36.         chat.tell(name,"Sucesfully registered channel "..channel)
  37.     end
  38. end
  39.  
  40. function getIID(item)
  41.     local items = rqpipe.getAvailableItems()
  42.  
  43.     for i,v in pairs(items) do
  44.         local uName = rqpipe.getUnlocalizedName(v[1])
  45.         if uName == item then
  46.           return v[1],v[2]
  47.         end
  48.     end
  49.     return nil
  50. end
  51.  
  52. function tryUnregister(name)
  53.   if registrations[name] == nil then
  54.     chat.tell(name,"You aren't registered.")
  55.     return
  56.   end
  57.  
  58.   registrations[name] = nil
  59.  
  60.   chat.tell(name,"Sucessfully unregistered.")
  61. end
  62.  
  63. function addOrder(name,amount,item)
  64.     if registrations[name] == nil then
  65.         chat.tell(name,"Please register before ordering.")
  66.         return
  67.     end
  68.  
  69.     if amount ~= nil and amount > 0 and math.floor(amount) == amount then
  70.         table.insert(orders,{name,amount,item})
  71.         os.queueEvent("newOrder")
  72.     else
  73.         chat.tell(name,"Please specify a valid amount.")
  74.     end
  75. end
  76.  
  77. function tryOrder(name,amount,item)
  78.  
  79.  
  80.     local iid,avAmount = getIID(item)
  81.  
  82.     if iid == nil then
  83.       chat.tell(name,"Item not found.")
  84.       return
  85.     end
  86.  
  87.     if avAmount < amount then
  88.       chat.tell(name,"You tried to order "..amount.." "..item.. ", but only "..avAmount.. " are available.")
  89.       return
  90.     end
  91.  
  92.     tesseract.setFrequency(registrations[name][1])
  93.     rqpipe.makeRequest(iid,amount,false)
  94.     sleep(2)
  95.     chat.tell(name,"Order sucessful")
  96.  
  97. end
  98.  
  99. function listen()
  100.     while true do
  101.       local evData = { os.pullEvent("chat") }
  102.       local name = evData[2]
  103.       local msg = evData[3]
  104.       msgSplit = strsplit(msg," ")
  105.  
  106.       local cmd = table.remove(msgSplit,1)
  107.       if cmd == "register" and #msgSplit == 1 then
  108.         tryRegister(name,tonumber(msgSplit[1]))
  109.       elseif cmd=="order" and #msgSplit >= 2 then
  110.         addOrder(name,tonumber(table.remove(msgSplit,1)),strjoin(msgSplit," "))
  111.       elseif cmd=="unregister" and #msgSplit == 0 then
  112.         tryUnregister(name)
  113.       end
  114.     end
  115. end
  116.  
  117. function processOrders()
  118.     while true do
  119.         if #orders == 0 then
  120.             os.pullEvent("newOrder")
  121.         end
  122.  
  123.         local order = table.remove(orders,1)
  124.         tryOrder(order[1],order[2],order[3])
  125.     end
  126. end
  127. parallel.waitForAny(listen,processOrders)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement