Advertisement
Coti5432

Lotto Cashier Startup

Nov 27th, 2021 (edited)
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.33 KB | None | 0 0
  1. local monitor, drive, surface, screen, width, height, font, buttons
  2.  
  3. MAINFRAME_ID = 57
  4. PAYOUT_FEE = 5
  5.  
  6. local currencyValues = {
  7. ["minecraft:diamond"]=1
  8. }
  9.  
  10. function waitForButtonPress()
  11. local pressed = false
  12. while not pressed do
  13. local event, button, px, py = os.pullEvent("monitor_touch")
  14. for text,button in pairs(buttons) do
  15. if px >= button.x and px <= button.x + button.width and py >= button.y and py <= button.y + button.height then
  16. button.cb()
  17. buttons = {}
  18. pressed = true
  19. end
  20. end
  21. end
  22. end
  23.  
  24. function getButtonSurface(text, bg)
  25. local textSize = surface.getTextSize(text, font)
  26. local button = surface.create(textSize + 2, 7)
  27. button:fillRect(0,0,textSize+2, 7, bg)
  28. button:drawText(text, font, 1, 1, colors.black)
  29. return button
  30. end
  31.  
  32. function button(surface, text, bg, x, y, func, center)
  33. local button = getButtonSurface(text, bg)
  34. if center then
  35. x = math.floor(x - button.width / 2)
  36. end
  37. surface:drawSurface(button, x, y)
  38. buttons[text] = {x=x, y=y, width=button.width, height=button.height, cb=func}
  39. return button
  40. end
  41.  
  42. function getPlayerBalance(player)
  43. rednet.send(MAINFRAME_ID, {type="getPlayerBalance", player=player}, "otto")
  44. local _, data = rednet.receive("otto")
  45. if not data then
  46. return nil
  47. end
  48. return data.name, data.balance
  49. end
  50.  
  51. function setPlayerBalance(player, balance)
  52. rednet.send(MAINFRAME_ID, {type="setPlayerBalance", player=player, balance=balance}, "otto")
  53. rednet.receive("otto")
  54. local filePath = fs.combine(drive.getMountPath(), "bal")
  55. file = fs.open(filePath, "w")
  56. file.write(tostring(balance))
  57. file.close()
  58. return
  59. end
  60.  
  61. function centerText(text, y, color)
  62. local tWidth = surface.getTextSize(text, font)
  63. screen:drawText(text, font, math.floor((width - tWidth) / 2), y, color)
  64. end
  65.  
  66. function dropInventory()
  67. for i=1,16 do
  68. turtle.select(i)
  69. turtle.drop()
  70. end
  71. end
  72.  
  73. function countMoney()
  74. turtle.turnRight()
  75. local sum = 0
  76. for i=1,2 do
  77. for slot=1,16 do
  78. turtle.select(slot)
  79. local item = turtle.getItemDetail(slot)
  80. local isValid = false
  81. for currency,value in pairs(currencyValues) do
  82. if item and item.name == currency then
  83. isValid = true
  84. sum = sum + value * item.count
  85. end
  86. end
  87. if isValid then
  88. turtle.drop()
  89. elseif item then
  90. turtle.turnLeft()
  91. turtle.drop()
  92. turtle.turnRight()
  93. end
  94. end
  95. end
  96. turtle.select(1)
  97. turtle.turnLeft()
  98. return sum
  99. end
  100.  
  101. function dropMoney(amount)
  102. turtle.turnRight()
  103. turtle.select(1)
  104. while amount > 0 do
  105. turtle.suck()
  106. local item = turtle.getItemDetail(1)
  107. if item == nil then
  108. turtle.turnLeft()
  109. return nil
  110. end
  111. local amountDropping = math.min(item.count, amount)
  112. turtle.turnLeft()
  113. turtle.drop(amountDropping)
  114. turtle.turnRight()
  115. amount = amount - amountDropping
  116. turtle.drop()
  117. end
  118. turtle.turnLeft()
  119. end
  120.  
  121. function setup()
  122. buttons = {}
  123. surface = dofile("surface")
  124. monitor = peripheral.wrap("monitor_0")
  125. drive = peripheral.wrap("bottom")
  126. monitor.setTextScale(0.5)
  127. term.redirect(monitor)
  128. width, height = term.getSize()
  129. screen = surface.create(width, height)
  130. font = surface.loadFont(surface.load("font"))
  131. rednet.open("back")
  132. redstone.setOutput("top", true)
  133. end
  134.  
  135. function sleepTick()
  136. os.sleep(0.05)
  137. end
  138.  
  139. function detectHack(actualBalance)
  140. local filePath = fs.combine(drive.getMountPath(), "bal")
  141. if not fs.exists(filePath) then
  142. return false
  143. end
  144. local file = fs.open(filePath, "r")
  145. local fakeBalance = tonumber(file.readAll())
  146. file.close()
  147. file = fs.open(filePath, "w")
  148. file.write(tostring(actualBalance))
  149. file.close()
  150. if fakeBalance ~= actualBalance then
  151. return true
  152. end
  153. return false
  154. end
  155.  
  156. setup()
  157. while true do
  158. screen:clear()
  159. centerText("Insert", 0, colors.white)
  160. centerText("Card", 6, colors.white)
  161. centerText("Use Q", 12, colors.yellow)
  162. centerText("Muscle", 18, colors.yellow)
  163. screen:output()
  164. turtle.select(1)
  165. local item = turtle.getItemDetail()
  166. if item and item.name == "computercraft:disk" then
  167. redstone.setOutput("top", false)
  168. turtle.dropDown()
  169. local player = drive.getDiskID()
  170. local name,balance = getPlayerBalance(player)
  171. if detectHack(balance) then
  172. screen:clear()
  173. centerText("Nice Try", 8, colors.red)
  174. centerText("Nerd.", 14, colors.red)
  175. screen:output()
  176. os.sleep(5)
  177. turtle.suckDown()
  178. turtle.drop()
  179. os.sleep(2)
  180. redstone.setOutput("top", true)
  181. else
  182. if balance == nil then
  183. turtle.suckDown()
  184. turtle.drop()
  185. screen:clear()
  186. centerText("INVALID", 0, colors.red)
  187. centerText("CARD", 6, colors.red)
  188. centerText("Try", 12, colors.white)
  189. centerText("Again", 18, colors.white)
  190. screen:output()
  191. os.sleep(2)
  192. redstone.setOutput("top", true)
  193. else
  194. local userAction
  195. while userAction ~= "done" do
  196. screen:clear()
  197. centerText("WELCOME", 1, colors.green)
  198. centerText("$"..tostring(balance), 8, colors.lightBlue)
  199. button(screen, "DEPOSIT", colors.lime, width / 2, 15, function() userAction="deposit" end, true)
  200. button(screen, "PAYOUT", colors.red, width / 2, 23, function() userAction="withdraw" end, true)
  201. button(screen, "DONE", colors.white, width / 2, 31, function() userAction="done" end, true)
  202. screen:output()
  203. waitForButtonPress()
  204.  
  205. if userAction == "deposit" then
  206. redstone.setOutput("top", true)
  207. screen:clear()
  208. centerText("Insert", 0, colors.white)
  209. centerText("$$$", 6, colors.lightBlue)
  210. centerText("Use Q", 12, colors.yellow)
  211. centerText("Muscle", 18, colors.yellow)
  212. button(screen, "DONE", colors.red, width / 2, 24, function() end, true)
  213. screen:output()
  214. waitForButtonPress()
  215. redstone.setOutput("top", false)
  216. screen:clear()
  217. centerText("Counting", 2, colors.white)
  218. centerText("...", 6, colors.white)
  219. screen:output()
  220. local sum = countMoney()
  221. setPlayerBalance(player, balance + sum)
  222. balance = balance + sum
  223. elseif userAction == "withdraw" then
  224. screen:clear()
  225. centerText("PAYOUT", 1, colors.white)
  226. centerText("$"..tostring(balance), 7, colors.lightBlue)
  227. local payoutAmount
  228. if balance < 8 then
  229. centerText("MINIMUM", 14, colors.red)
  230. centerText("$8", 20, colors.red)
  231. button(screen, "DONE", colors.lime, width / 2, 28, function() end, true)
  232. screen:output()
  233. waitForButtonPress()
  234. else
  235. button(screen, "ALL", colors.yellow, width / 2, 14, function() payoutAmount="all" end, true)
  236. if balance >= 16 then
  237. button(screen, "HALF", colors.yellow, width / 2, 22, function() payoutAmount="half" end, true)
  238. button(screen, "DONE", colors.lime, width / 2, 30, function() payoutAmount="cancel" end, true)
  239. else
  240. button(screen, "DONE", colors.lime, width / 2, 22, function() payoutAmount="cancel" end, true)
  241. end
  242. screen:drawString("Withdrawal fee: "..tostring(PAYOUT_FEE).."%", 0, height - 1, colors.black, colors.gray)
  243. screen:output()
  244. waitForButtonPress()
  245. local diamondsToDrop = 0
  246. if payoutAmount == "all" then
  247. diamondsToDrop = balance
  248. elseif payoutAmount == "half" then
  249. diamondsToDrop = balance / 2
  250. end
  251. setPlayerBalance(player, math.floor(balance - diamondsToDrop))
  252. balance = math.floor(balance - diamondsToDrop)
  253. diamondsToDrop = math.floor(diamondsToDrop * (1 - PAYOUT_FEE / 100))
  254. dropMoney(diamondsToDrop)
  255. end
  256. end
  257. end
  258. screen:clear()
  259. centerText("Thanks!", 0, colors.white)
  260. centerText("Good", 12, colors.yellow)
  261. centerText("Luck", 18, colors.yellow)
  262. screen:output()
  263.  
  264. name, balance = getPlayerBalance(player)
  265. if balance ~= nil then
  266. drive.setDiskLabel(name.."'s L'Otto Card - $"..tostring(balance))
  267. end
  268. turtle.suckDown()
  269. turtle.drop()
  270. os.sleep(4)
  271. redstone.setOutput("top", true)
  272. end
  273. end
  274. elseif item then
  275. redstone.setOutput("top", false)
  276. turtle.drop()
  277. os.sleep(2)
  278. redstone.setOutput("top", true)
  279. end
  280. -- if count > 0 then
  281. -- redstone.setOutput("right", false)
  282. -- turtle.dropDown()
  283. -- local player = drive.getDiskID()
  284. -- setPlayerBalance(player, count)
  285. -- if balance ~= nil then
  286. -- drive.setDiskLabel("L'Otto Card - $"..tostring(balance))
  287. -- end
  288. -- end
  289. dropInventory()
  290. end
  291.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement