Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local registrations = {}
- local orders = {}
- local chat = peripheral.wrap("right")
- local rqpipe = peripheral.wrap("back")
- local tesseract = peripheral.wrap("item_tesseract_4")
- function strsplit(self,sep)
- local sep, fields = sep or ":", {}
- local pattern = string.format("([^%s]+)", sep)
- self:gsub(pattern, function(c) fields[#fields+1] = c end)
- return fields
- end
- function strjoin(list,delimiter)
- local len = #list
- if len == 0 then
- return ""
- end
- local string = list[1]
- for i = 2, len do
- string = string .. delimiter .. list[i]
- end
- return string
- end
- function tryRegister(name,channel)
- if channel == nil or channel == 0 then
- chat.tell(name, "Can't register this channel")
- return
- end
- if registrations[name] ~= nil then
- chat.tell(name,"You already registered a tesseract channel.")
- else
- registrations[name] = {channel}
- chat.tell(name,"Sucesfully registered channel "..channel)
- end
- end
- function getIID(item)
- local items = rqpipe.getAvailableItems()
- for i,v in pairs(items) do
- local uName = rqpipe.getUnlocalizedName(v[1])
- if uName == item then
- return v[1],v[2]
- end
- end
- return nil
- end
- function tryUnregister(name)
- if registrations[name] == nil then
- chat.tell(name,"You aren't registered.")
- return
- end
- registrations[name] = nil
- chat.tell(name,"Sucessfully unregistered.")
- end
- function addOrder(name,amount,item)
- if registrations[name] == nil then
- chat.tell(name,"Please register before ordering.")
- return
- end
- if amount ~= nil and amount > 0 and math.floor(amount) == amount then
- table.insert(orders,{name,amount,item})
- os.queueEvent("newOrder")
- else
- chat.tell(name,"Please specify a valid amount.")
- end
- end
- function tryOrder(name,amount,item)
- local iid,avAmount = getIID(item)
- if iid == nil then
- chat.tell(name,"Item not found.")
- return
- end
- if avAmount < amount then
- chat.tell(name,"You tried to order "..amount.." "..item.. ", but only "..avAmount.. " are available.")
- return
- end
- tesseract.setFrequency(registrations[name][1])
- rqpipe.makeRequest(iid,amount,false)
- sleep(2)
- chat.tell(name,"Order sucessful")
- end
- function listen()
- while true do
- local evData = { os.pullEvent("chat") }
- local name = evData[2]
- local msg = evData[3]
- msgSplit = strsplit(msg," ")
- local cmd = table.remove(msgSplit,1)
- if cmd == "register" and #msgSplit == 1 then
- tryRegister(name,tonumber(msgSplit[1]))
- elseif cmd=="order" and #msgSplit >= 2 then
- addOrder(name,tonumber(table.remove(msgSplit,1)),strjoin(msgSplit," "))
- elseif cmd=="unregister" and #msgSplit == 0 then
- tryUnregister(name)
- end
- end
- end
- function processOrders()
- while true do
- if #orders == 0 then
- os.pullEvent("newOrder")
- end
- local order = table.remove(orders,1)
- tryOrder(order[1],order[2],order[3])
- end
- end
- parallel.waitForAny(listen,processOrders)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement