Marlingaming

CC Tweaked Shopping Terminal Main Script

Jan 10th, 2022 (edited)
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.88 KB | None | 0 0
  1. --this is a vending system for shops
  2. --Client settings
  3. local Shopping_Mode = "Dispenser"--Dispenser mode only allows up to 3 different Product types, Digi mode is more expensive but allows for almost infinite Product types
  4. Products_Name = {}
  5. Products_Tag = {}
  6. Products_Price = {}
  7. SalesTax = 0.02
  8. --System Var
  9. CompanyName = "example"
  10. TerminalExitCode = "0000"
  11. local w, h = term.getSize()
  12.  
  13.  
  14. local function Clear()
  15. term.clear()
  16. term.setCursorPos(1,1)
  17. end
  18.  
  19. local function CenterText(y,text)
  20.     local x = math.floor((w - string.len(text)) /2)
  21.     term.setCursorPos(x,y)
  22.     term.clearLine()
  23.     term.write(text)
  24. end
  25.  
  26. local function Setup()
  27. settings.load(".settings")
  28. Shopping_Mode = settings.get("ShopMode")
  29. Products_Name = {settings.get("ShopProducts_Name")}
  30. Products_Tag = {settings.get("ShopProducts_Tag")}
  31. Products_Price = {settings.get("ShopProducts_Price")}
  32. TerminalExitCode = settings.get("exitCode")
  33. CompanyName = settings.get("CompanyName")
  34. end
  35.  
  36. function PurchaseRequest(CardData,item)
  37. local Details = {CardData}
  38. local TransferDetails
  39. local ClientID = Details[1]
  40. local ClientProtocol = Details[2]
  41. local Costs = Products_Price[item] * -1
  42. TransferDetails = {Costs,Products_Name[item],"Banking"}
  43. rednet.send(ClientID,TransferDetails,ClientProtocol)
  44. local Response
  45. repeat
  46.     Response = {rednet.recieve(ClientProtocol)}
  47. until Response[1] == ClientID and (Response[2] == "Accepted" or Response[2] == "Declined") == true
  48. if Response[2] == true then
  49.     return(true)
  50. else
  51.     return(false)
  52. end
  53. end
  54.  
  55. function DispenseProduct(item)
  56. if Shopping_Mode == "Dispenser" then
  57.     if item == 1 then redstone.setOutput("top",true) redstone.setOutput("top",false) end
  58.     if item == 2 then redstone.setOutput("right",true) redstone.setOutput("right",false) end
  59.     if item == 3 then redstone.setOutput("bottom",true) redstone.setOutput("bottom",false) end
  60. elseif Shopping_Mode == "Digi" then
  61.  
  62. end
  63. Display_MMenu()
  64. end
  65.  
  66. function Display_MMenu()
  67. Clear()
  68. CenterText(1,"===Shopping=Terminal===")
  69. CenterText(2,"Provided by "..CompanyName)
  70. CenterText(4,"--Products--")
  71. local Column = 1
  72. local Width = w/3
  73. for i = 1, #Products_Name do
  74.     term.setCursorPos(Column*Width,i+4)
  75.     term.write(i..">"..Products_Name[i].." $"..Products_Price[i])
  76. end
  77. Interaction_MMenu()
  78. end
  79.  
  80. local function TransferCredit(item)
  81. local GovCredits = Products_Price[item] * SalesTax
  82. local OwnerCredits = Products_Price[item] - GovCredits
  83. local GovFile = fs.open(".Gov_24124","a")
  84. local TotalGov = GovFile.readLine()
  85. TotalGov = TotalGov + GovCredits
  86. GovFile.Clear()
  87. GovFile.writeLine(TotalGov)
  88. GovFile.writeLine(os.date())
  89. GovFile.close()
  90. local OwnerFile = fs.open("OwnerFile","a")
  91. local OwnerTotal = OwnerFile.readLine()
  92. OwnerFile.clear()
  93. OwnerFile.writeLine(OwnerTotal)
  94. OwnerFile.writeLine(os.date())
  95. OwnerFile.close()
  96. end
  97.  
  98.  
  99. function Display_PurchaseMenu(item)
  100. Clear()
  101. CenterText(3,"Purchasing "..Products_Name[item])
  102. CenterText(4,"$"..Products_Price[item])
  103. CenterText(5,"y/n")
  104. local event
  105. repeat
  106.     event = {os.pullEvent("key")}
  107. until event[2] == keys.y or event[2] == keys.n
  108. if event[2] == keys.n then
  109.     Display_MMenu()
  110. else
  111.     Clear()
  112.     CenterText(h/2,"Please Swipe Card")
  113.     repeat
  114.         event = {os.pullEvent()}
  115.     until (event[1] == "key" and event[2] == keys.c) == true or (event[1] == "mag_swipe") == true
  116.     if event[1] == "key" and event[2] == keys.c then
  117.        
  118.     else
  119.         if PurchaseRequest(event[4],Products_Price[item]) == true then
  120.             TransferCredit(item)
  121.             DispenseProduct(item)
  122.         else
  123.             Display_MMenu()
  124.         end
  125.     end
  126. end
  127. end
  128.      
  129. function OwnerCollectMenu()
  130. Clear()
  131. local TotalProfits
  132. local file = fs.open("OwnerFile","a")
  133. TotalProfits = file.readLine()
  134. print("Profits = $"..TotalProfits)
  135. print("Please Swipe Card to Collect")
  136. local event
  137. repeat
  138.     event = {os.pullEvent("mag_swipe")}
  139. until event[1] == "mag_swipe"
  140. if PurchaseRequest(event[4],Products_Price[item]) == true then
  141. file.clear()
  142. file.writeLine(0)
  143. file.writeLine(os.date())
  144. file.close()
  145. AdminMenu()
  146. end
  147. end
  148.  
  149. function AdminMenu()
  150. Clear()
  151. print("=======ADMINMENU========")
  152. print("--Options--")
  153. print("> 1 Collect Profits")
  154. print("> 2 Edit Products - WIP")
  155. print("> 3 exit")
  156. local event
  157. repeat
  158.     event = {os.pullEvent("key")}
  159. until event[2] == keys.one or event[2] == keys.two or event[2] == keys.three
  160. if event[2] == keys.one then
  161.     OwnerCollectMenu()
  162. elseif event[2] == keys.two then
  163.     ProductEdit()
  164. elseif event[2] == keys.three then
  165.     Display_MMenu()
  166. end
  167. end  
  168.  
  169. function Interaction_MMenu()
  170. local event
  171. local input
  172. repeat
  173.     input = read()
  174.     event = {os.pullEvent("mouse_click")}
  175. until event[3] > w-4 and event[4] > h - 4 or input == TerminalExitCode
  176. if input == TerminalExitCode then
  177.     AdminMenu()
  178. elseif input > 0 and input < #Products_Name+1 then
  179.     Display_PurchaseMenu(input)
  180. else
  181.     Display_MMenu()
  182. end
  183. end
  184.  
  185.  
  186. Setup()
  187. Display_MMenu()
Add Comment
Please, Sign In to add comment