Chaos_Cash

shopOrderManager

Oct 3rd, 2024 (edited)
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.30 KB | None | 0 0
  1. function startup()
  2.  
  3.     if fs.exists("disk2") then
  4.     fs.delete("disk/")
  5.     end
  6.  
  7.     for k,v in pairs(peripheral.getNames()) do
  8.         if peripheral.getType(peripheral.getNames()[k]) == "modem" then
  9.         rednet.open(peripheral.getNames()[k])
  10.         end
  11.     end
  12.  
  13.  
  14. readingOutput = {}
  15.  
  16. blinkIsOn = true
  17.  
  18. scrollmenuLen={}
  19. scrollmenuInfos={}
  20.  
  21. dropdown = {}
  22. dropdown["loaded"] = {}
  23. dropdown["loaded"]["len"] = 0
  24.  
  25.  
  26. orderQueue = {}
  27.  
  28.  
  29. shopItems = {
  30. {["name"]="Coal",["price"]=2,["category"]="ores",["slot"]=1,["chest"]=1},
  31. {["name"]="Iron Ingot",["price"]=5,["category"]="ores",["slot"]=2,["chest"]=1},
  32. {["name"]="Copper Ingot",["price"]=2,["category"]="ores",["slot"]=3,["chest"]=1},
  33. {["name"]="Gold Ingot",["price"]=6,["category"]="ores",["slot"]=4,["chest"]=1},
  34. {["name"]="Lapis Lazuli",["price"]=6,["category"]="ores",["slot"]=5,["chest"]=1},
  35. {["name"]="Redstone Dust",["price"]=6,["category"]="ores",["slot"]=6,["chest"]=1},
  36. {["name"]="Diamond",["price"]=10,["category"]="ores",["slot"]=7,["chest"]=1},
  37. {["name"]="Emerald",["price"]=13,["category"]="ores",["slot"]=8,["chest"]=1},
  38. {["name"]="Netherite Ingot",["price"]=25,["category"]="ores",["slot"]=9,["chest"]=1},
  39. {["name"]="Osmium Ingot",["price"]=5,["category"]="ores",["slot"]=10,["chest"]=1},
  40. {["name"]="Uranium Ingot",["price"]=7,["category"]="ores",["slot"]=11,["chest"]=1},
  41. {["name"]="Silver Ingot",["price"]=5,["category"]="ores",["slot"]=12,["chest"]=1},
  42. {["name"]="Aluminum Ingot",["price"]=5,["category"]="ores",["slot"]=13,["chest"]=1},
  43. {["name"]="Zinc Ingot",["price"]=5,["category"]="ores",["slot"]=14,["chest"]=1},
  44. {["name"]="Steel Ingot",["price"]=7,["category"]="ores",["slot"]=15,["chest"]=1},
  45. {["name"]="Electrum Ingot",["price"]=7,["category"]="ores",["slot"]=16,["chest"]=1},
  46.  
  47. {["name"]="Manasteel Ingot",["price"]=10,["category"]={},["slot"]=17,["chest"]=1},
  48. {["name"]="Terrasteel Ingot",["price"]=20,["category"]={},["slot"]=18,["chest"]=1},
  49. {["name"]="Lead Ingot",["price"]=5,["category"]={},["slot"]=19,["chest"]=1},
  50. {["name"]="Nickel Ingot",["price"]=5,["category"]={},["slot"]=20,["chest"]=1},
  51. {["name"]="Platinum Ingot",["price"]=5,["category"]={},["slot"]=21,["chest"]=1},
  52. {["name"]="Tin Ingot",["price"]=5,["category"]={},["slot"]=22,["chest"]=1},
  53. {["name"]="Invar Ingot",["price"]=6,["category"]={},["slot"]=23,["chest"]=1},
  54. {["name"]="Bronze Ingot",["price"]=5,["category"]={},["slot"]=24,["chest"]=1},
  55. {["name"]="Enderium Ingot",["price"]=10,["category"]={},["slot"]=25,["chest"]=1},
  56. {["name"]="Lumium Ingot",["price"]=10,["category"]={},["slot"]=26,["chest"]=1},
  57. {["name"]="Signalum Ingot",["price"]=12,["category"]={},["slot"]=27,["chest"]=1},
  58. {["name"]="Constantan Ingot",["price"]=6,["category"]={},["slot"]=28,["chest"]=1},
  59. {["name"]="Brass Ingot",["price"]=5,["category"]={},["slot"]=29,["chest"]=1},
  60. {["name"]="Allthemodium Ingot",["price"]=200,["category"]={},["slot"]=30,["chest"]=1},
  61. {["name"]="Graphite Ingot",["price"]=3,["category"]={},["slot"]=31,["chest"]=1},
  62. {["name"]="Draconium Ingot",["price"]=8,["category"]={},["slot"]=32,["chest"]=1},
  63. {["name"]="Awakened Draconium Ingot",["price"]=30,["category"]={},["slot"]=33,["chest"]=1},
  64. {["name"]="Copper Alloy Ingot",["price"]=7,["category"]={},["slot"]=34,["chest"]=1},
  65. {["name"]="Energetic Alloy Ingot",["price"]=15,["category"]={},["slot"]=35,["chest"]=1},
  66. {["name"]="Vibrant Alloy Ingot",["price"]=18,["category"]={},["slot"]=36,["chest"]=1},
  67. {["name"]="Redstone Alloy Ingot",["price"]=8,["category"]={},["slot"]=37,["chest"]=1},
  68. {["name"]="Conductive Alloy Ingot",["price"]=13,["category"]={},["slot"]=38,["chest"]=1},
  69. {["name"]="Pulsating Alloy Ingot",["price"]=7,["category"]={},["slot"]=39,["chest"]=1},
  70. {["name"]="Dark Steel Ingot",["price"]=10,["category"]={},["slot"]=40,["chest"]=1},
  71. {["name"]="Soularium Ingot",["price"]=8,["category"]={},["slot"]=41,["chest"]=1},
  72. {["name"]="End Steel Ingot",["price"]=14,["category"]={},["slot"]=42,["chest"]=1},
  73. {["name"]="Deorum Ingot",["price"]=15,["category"]={},["slot"]=43,["chest"]=1},
  74. {["name"]="Obsidian Ingot",["price"]=5,["category"]={},["slot"]=44,["chest"]=1},
  75. {["name"]="Force Ingot",["price"]=7,["category"]={},["slot"]=45,["chest"]=1},
  76. {["name"]="Refined Obsidian Ingot",["price"]=14,["category"]={},["slot"]=46,["chest"]=1},
  77. {["name"]="Refined Glowstone Ingot",["price"]=12,["category"]={},["slot"]=47,["chest"]=1},
  78. {["name"]="Prosperity Ingot",["price"]=7,["category"]={},["slot"]=48,["chest"]=1},
  79. {["name"]="Inferium Ingot",["price"]=8,["category"]={},["slot"]=49,["chest"]=1},
  80. {["name"]="Prudentium Ingot",["price"]=10,["category"]={},["slot"]=50,["chest"]=1},
  81. {["name"]="Tertium Ingot",["price"]=13,["category"]={},["slot"]=51,["chest"]=1},
  82. {["name"]="Imperium Ingot",["price"]=18,["category"]={},["slot"]=52,["chest"]=1},
  83. {["name"]="Supremium Ingot",["price"]=25,["category"]={},["slot"]=53,["chest"]=1},
  84. {["name"]="Soulium Ingot",["price"]=9,["category"]={},["slot"]=54,["chest"]=1},
  85. {["name"]="Prismalium Ingot",["price"]=14,["category"]={},["slot"]=55,["chest"]=1},
  86. {["name"]="Melodium Ingot",["price"]=16,["category"]={},["slot"]=56,["chest"]=1},
  87. {["name"]="Stellarium Ingot",["price"]=40,["category"]={},["slot"]=57,["chest"]=1},
  88. {["name"]="Soul Infused ingot",["price"]=10,["category"]={},["slot"]=58,["chest"]=1},
  89. {["name"]="Shellite ingot",["price"]=14,["category"]={},["slot"]=59,["chest"]=1},
  90. {["name"]="Twinite ingot",["price"]=12,["category"]={},["slot"]=60,["chest"]=1},
  91. {["name"]="Dragonsteel ingot",["price"]=15,["category"]={},["slot"]=61,["chest"]=1}
  92.  
  93. }
  94.  
  95.  
  96. chestIDs = {972}
  97.  
  98. chestOrders = {}
  99.  
  100. orderTimers = {}
  101.  
  102. chest = peripheral.wrap("back")
  103.  
  104. drive = peripheral.wrap("front")
  105.  
  106. fs.delete("disk/startup")
  107.  
  108. password = "sdfewkwlsfirpqasdw+ödö230182jdjwwoqqyy<<12203464s5fvb7v53545755.55,477ddlw+w##k,kkk<iiwß10wwwl3.f,s,siw000dlf.göötää5klxmy,<kkhpi33k5k6mmddn.z--+#'*!)§=)§($//$L?DJW)(CJM"
  109. end
  110.  
  111.  
  112.  
  113. function main()
  114.  
  115.     while true do
  116.    
  117.     os.startTimer(5)
  118.     info1,info2,info3,info4 = os.pullEvent()
  119.        
  120.    
  121.         if info1 == "rednet_message" then  
  122.            
  123.            
  124.                 if type(info3) == "table" then
  125.                
  126.                
  127.                     if info3["action"] == "paid" then
  128.                     responseID = pay(info3["amount"])
  129.                     rednet.send(responseID,{["action"]="paid",["amount"]=info3["amount"]})
  130.                         if orderIsDone() then
  131.                         rednet.send(lastID,{["action"]="orderIsDone"})
  132.                         end
  133.                     end
  134.                
  135.                
  136.                     if info3["action"] == "newOrder" and fs.exists("pocketIDs/"..info2) then
  137.                     rednet.send(info2,{["action"]="listedOrder",["orderPos"]=addToOrderQueue(info3["order"])})
  138.                     end
  139.                
  140.                    
  141.                     if info3["action"] == "canceledOrder" and fs.exists("pocketIDs/"..info2) then
  142.                     cancelOrder(info2)
  143.                     end
  144.                    
  145.                    
  146.                     if info3["action"] == "getItems" and fs.exists("pocketIDs/"..info2) then
  147.                    
  148.                     rednet.send(info2,{["action"]="sendingItems",["items"]=shopItems})
  149.                    
  150.                     end
  151.                    
  152.                    
  153.                     if info3["action"] == "newPocket" and info3["password"] == password then
  154.                     file = fs.open("pocketIDs/"..info2,"w")
  155.                     file.flush()
  156.                     file.close()
  157.                     end
  158.                    
  159.                 end
  160.        
  161.        
  162.         end
  163.        
  164.        
  165.         if (not rs.getInput("left")) and newPocketTimer == nil then
  166.         newPocketTimer = os.startTimer(1)
  167.         end
  168.    
  169.    
  170.        
  171.         if info1 == "timer" then
  172.        
  173.             for k,v in pairs(orderQueue) do
  174.                 if info2 == orderTimers[k] then
  175.                 cancelOrder(orderQueue[k]["id"])
  176.                 end
  177.             end
  178.            
  179.            
  180.             if info2 == newPocketTimer then
  181.             outputNewPocket()
  182.             end
  183.            
  184.            
  185.             if info2 == resetNewPocketTimer then
  186.             newPocketTimer = nil
  187.             end
  188.            
  189.         end
  190.        
  191.     end
  192.    
  193. end
  194.  
  195.  
  196.  
  197. function outputNewPocket()
  198. fs.delete("disk/startup")
  199. fs.delete("disk/password")
  200. fs.copy("pocketProgram","disk/startup")
  201. file = fs.open("disk/password","w")
  202. file.write(password)
  203. file.flush()
  204. file.close()
  205.  
  206. chest.pullItems("front",1)
  207.  
  208. resetNewPocketTimer = os.startTimer(10)
  209. end
  210.  
  211.  
  212.  
  213. function cancelOrder(id)
  214.  
  215.     for k,v in pairs(orderQueue) do
  216.         if orderQueue[k]["id"] == id then
  217.             for i=1,table.maxn(orderQueue)-k+1 do
  218.             orderQueue[k+i-1] = orderQueue[i+k]
  219.             end
  220.         end
  221.     end
  222.    
  223.    
  224.     for k,v in pairs(orderQueue) do
  225.     rednet.send(orderQueue[k]["id"],{["action"]="changedOrder",["orderPos"]=k})
  226.     end
  227.  
  228. end
  229.  
  230.  
  231.  
  232. function orderIsDone()
  233.     if type(orderQueue[1]) == "table" then
  234.         if orderQueue[1]["price"] < 0 or orderQueue[1]["price"] == 0 then
  235.         buyCurrentOrder()
  236.        
  237.         lastID = orderQueue[1]["id"]
  238.             for k,v in pairs(orderQueue) do
  239.             orderQueue[k] = orderQueue[k+1]
  240.             end
  241.        
  242.         return true
  243.         else
  244.         return false
  245.         end
  246.     else
  247.     return false
  248.     end
  249. end
  250.  
  251.  
  252.  
  253. function buyCurrentOrder()
  254.  
  255.     for k,v in pairs(chestIDs) do
  256.     chestOrders[k] = {}
  257.     end
  258.  
  259.     for k,v in pairs(orderQueue[1]["cartInfo"]) do
  260.     table.insert(chestOrders[orderQueue[1]["cartInfo"][k]["chest"]],{["slot"]=orderQueue[1]["cartInfo"][k]["slot"],["amount"]=orderQueue[1]["cartInfo"][k]["amount"]})
  261.     end
  262.  
  263.     for k,v in pairs(chestIDs) do
  264.         if chestOrders[k][1] ~= nil then
  265.         rednet.send(chestIDs[k],{["action"]="buy",["order"]=chestOrders[k]})
  266.         end
  267.     end
  268.  
  269. end
  270.  
  271.  
  272.  
  273. function pay(amount)
  274.     if type(orderQueue[1]) == "table" then
  275.     orderQueue[1]["price"] = orderQueue[1]["price"]-amount
  276.     return orderQueue[1]["id"]
  277.     else
  278.     return 0
  279.     end
  280. end
  281.  
  282.  
  283.  
  284. function addToOrderQueue(order)
  285.  
  286. table.insert(orderQueue,order)
  287.  
  288. orderTimers[table.maxn(orderQueue)] = os.startTimer(90)
  289.  
  290. return table.maxn(orderQueue)
  291. end
  292.  
  293.  
  294.  
  295.  
  296. -------------------------------------------------
  297.  
  298.  
  299.  
  300.  
  301. startup()
  302. term.clear()
  303. main()
Add Comment
Please, Sign In to add comment