Advertisement
Pinkishu

02-svc-order

Aug 28th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.94 KB | None | 0 0
  1. print("Loading CMD/Order Service")
  2.  
  3. -- Prototyping
  4.  
  5. local addOrder
  6.  
  7. -- Other local vars
  8.  
  9. local orders = {}
  10. local orderFuncs = {}
  11. local processingOrder = false
  12.  
  13. -- Peripheral Configs
  14.  
  15. local chatDir = "right"
  16. local tesseractDir = "item_tesseract_0"
  17. local rqpipeDir = "back"
  18.  
  19. -- Peripheral Init
  20.  
  21. local chat = peripheral.wrap(chatDir)
  22. local tesseract = peripheral.wrap(tesseractDir)
  23. local rqpipe = peripheral.wrap(rqpipeDir)
  24.  
  25. -- Helper functions
  26.  
  27. local function strsplit(str,sep)
  28.   local sep,fields = sep or ":",{}
  29.   local pattern = string.format("([^%s]+)",sep)
  30.   str:gsub(pattern,function(c) fields[#fields+1]=c end)
  31.   return fields
  32. end
  33.  
  34. function strjoin(list,delimeter)
  35.   local len = #list
  36.   if len == 0 then
  37.     return ""
  38.   end
  39.   local string = list[1]
  40.   for i = 2, len do
  41.     string = string .. delimiter .. list[i]
  42.   end
  43.   return string
  44. end
  45.  
  46. function getItemInfo(itemName)
  47.     local items = rqpipe.getAvailableItems()
  48.  
  49.     for i,v in pairs(items) do
  50.         local uName = rqpipe.getUnlocalizedName(v[1]):lower()
  51.         if uName == itemName then
  52.             return v[1],v[2]
  53.         end
  54.     end
  55.     return nil
  56. end
  57.  
  58. function getItemAmount(iid)
  59.     local items = rqpipe.getAvailableItems()
  60.  
  61.     for i,v in pairs(items) do
  62.         if v[1] == iid then
  63.             return v[2]
  64.         end
  65.     end
  66.     return nil 
  67. end
  68.  
  69. -- Load state
  70.  
  71. local defState = {}
  72. defState.registrations = {}
  73.  
  74. local state = state.new("/lib/svc-state/order",defState)
  75.  
  76. -- Service start
  77.  
  78. event.subscribe_once("service.start", function()
  79.   print("CMD/Order Service Starting")
  80.   if state.registrations == nil then state.registrations = {} end
  81.  
  82.   print("Service running")
  83. end)
  84.  
  85. -- Handler Functions
  86.  
  87. function orderFuncs.help(name,cmd)
  88.     cmd = cmd or ""
  89.     cmd= cmd:lower()
  90.  
  91.     local cmdDescs = {}
  92.     cmdDescs.help = " - Lists the available commands."
  93.     cmdDescs.register = " <channel> - Register <channel> as tesseract channel to send orders on"
  94.     cmdDescs.order = " <amount> <item> - Orders <amount> of <item> (sent through registered channel)"
  95.     cmdDescs.info = " - Show your personal registration info"
  96.     cmdDescs.unregister = " - Unregisters your channel"
  97.     cmdDescs.check = " <item> - Shows the available amount of <item>"
  98.  
  99.     if cmd == "" then
  100.         chat.tell(name,"Available commands: help, info, register, unregister, check, order")
  101.     elseif cmdDescs[cmd] then
  102.         chat.tell(name,cmd..cmdDescs[cmd])
  103.     end
  104. end
  105.  
  106. function orderFuncs.register(name,channel)
  107.     if channel == nil then
  108.         chat.tell(name,"Please specify a channel")
  109.         return
  110.     end
  111.  
  112.     channel = tonumber(channel)
  113.  
  114.     if channel == nil or channel < 0 or math.ceil(channel) ~= channel then
  115.         chat.tell(name,"Please specify a valid channel.")
  116.         return
  117.     end
  118.  
  119.     if state.registrations[name] then
  120.         chat.tell(name,"You already have a registered channel: "..state.registrations[name].channel)
  121.         return
  122.     end
  123.  
  124.     for k,v in pairs(state.registrations) do
  125.         if v.channel == channel then
  126.             chat.tell(name,k.." already has this channel registered.")
  127.             return
  128.         end
  129.     end
  130.  
  131.     state.registrations[name]={["channel"]=channel}
  132.     state:save_now()
  133.     chat.tell(name,"Channel "..channel.. " successfully registered.")
  134. end
  135.  
  136. function orderFuncs.unregister(name)
  137.     if not state.registrations[name] then
  138.         chat.tell(name,"You don't have a channel registered.")
  139.         return
  140.     end
  141.  
  142.     state.registrations[name] = nil
  143.     state:save_now()
  144.     chat.tell(name,"Successfully unregistered.")
  145. end
  146.  
  147. function orderFuncs.info(name)
  148.     if not state.registrations[name] then
  149.         chat.tell(name,"You don't have a channel registered.")
  150.         return
  151.     end
  152.  
  153.     chat.tell(name,"Registered with channel "..state.registrations[name].channel)
  154. end
  155.  
  156. function orderFuncs.order(name,amount,...)
  157.     local args = {...}
  158.     if args == nil or #args == 0 then
  159.         orderFuncs.help(name,"order")
  160.     end
  161.     local item = strjoin(args," "):lower()
  162.     local amount = tonumber(amount)
  163.  
  164.     if amount == nil or amount < 1 or math.ceil(amount) ~= amount then
  165.         chat.tell(name,"Please specify a valid amount.")
  166.         return
  167.     end
  168.  
  169.  
  170.     if type(state.registrations[name]) ~= "table" then
  171.         chat.tell(name,"Please use the 'register' command to register a channel before ordering.")
  172.         return
  173.     end
  174.  
  175.     local iid = getItemInfo(item)
  176.  
  177.     if iid == nil then
  178.         chat.tell(name,"Item "..item.." not found.")
  179.         return
  180.     else
  181.         addOrder(name,iid,item,amount)
  182.         chat.tell(name,"Order queued, will be processed momentarily.")
  183.         if not processingOrders then
  184.             os.queueEvent("newOrder")
  185.         end
  186.     end
  187.  
  188. end
  189.  
  190. -- Route chat
  191.  
  192. function onChat(ev,name,msg)
  193.     if msg:sub(1,1) == "." then
  194.         msg = msg:sub(2)
  195.  
  196.         local msgSplit = strsplit(msg," ")
  197.  
  198.         local cmd = table.remove(msgSplit,1)
  199.  
  200.         if type(orderFuncs[cmd]) == "function" then
  201.             orderFuncs[cmd](name,unpack(msgSplit))
  202.         end
  203.     end
  204. end
  205.  
  206. -- Order Processng
  207.  
  208. addOrder = function(name,iid,item,amount)
  209.     local t = {}
  210.     t.name = name
  211.     t.iid = iid
  212.     t.item = item
  213.     t.amount = amount
  214.  
  215.     table.insert(orders,t)
  216.    
  217. end
  218.  
  219. function nextOrderCheck()
  220.     if #orders > 0 then
  221.         os.queueEvent("newOrder")
  222.     else
  223.         processingOrders = false
  224.     end
  225. end
  226.  
  227. function processOrder(ev)
  228.     processingOrders = true
  229.  
  230.     local order = table.remove(orders,1)
  231.  
  232.     local amount = getItemAmount(order.iid)
  233.  
  234.     if amount < order.amount then
  235.         chat.tell(order.name,"Only "..amount.. " of " ..order.item.. " available ("..order.amount.." requested).")
  236.     else
  237.         if state.registrations[order.name] ~= nil then
  238.             tesseract.setFrequency(state.registrations[order.name].channel)
  239.             rqpipe.makeRequest(order.iid,order.amount,false)
  240.             chat.tell(order.name,"Sending "..order.amount.." "..order.item)
  241.         else
  242.             chat.tell(name,"Error: You are not registered. This shouldn't happen unless you unregister after ordering but before the order is processed.")
  243.         end
  244.     end
  245.  
  246.     timer.after(4,nextOrderCheck)
  247.  
  248. end
  249.  
  250. -- Event Subscriptions
  251.  
  252. local eGroup = event.new_group()
  253.  
  254. eGroup:subscribe("event.chat",onChat)
  255. eGroup:subscribe("event.newOrder",processOrder)
  256.  
  257. event.subscribe_once("service.stop",function()
  258.   print("Service Closing")
  259.   eGroup:done()
  260. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement