sxrgini

CC: Casino Card (WIP)

Nov 2nd, 2024 (edited)
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.38 KB | None | 0 0
  1. -- Casino Card for Casino Machines --
  2. -- Created by iiAmPanda --
  3. -- pastebin get cDaurxvD casinocard(wip) --
  4.  
  5. local collectorFee = 10
  6. local creds = 0
  7. local credsPerDia = 75
  8. local credsPerGoldIngot = 45
  9. local credsPerGoldNugget = 5
  10. local playerName = ""
  11. local originalDirection = 1 -- 1: north, 2: east, 3: south, 4: west
  12.  
  13. -- Function to center text on the screen
  14. function centerText(y, text)
  15. local width = term.getSize()
  16. term.setCursorPos(math.floor((width - #text) / 2), y)
  17. term.write(text)
  18. end
  19.  
  20. -- Function to wrap and display text within the screen width
  21. function wrapText(y, text)
  22. local width = term.getSize()
  23. for line in text:gmatch(".{1," .. width .. "}") do
  24. term.setCursorPos(1, y)
  25. term.write(line)
  26. y = y + 1
  27. end
  28. return y -- Return the new y position after writing
  29. end
  30.  
  31. -- Function to check if the disk is a Casino Card
  32. function checkForCasinoCard()
  33. return fs.exists("disk/creds.lua")
  34. end
  35.  
  36. -- Function to write card data to disk and update the disk label with credits
  37. function writeCard()
  38. local Card = fs.open("disk/creds.lua", "w")
  39. if not Card then
  40. centerText(8, "Error writing to disk!")
  41. sleep(2)
  42. return
  43. end
  44.  
  45. local data = (tostring(math.random(1, 163456)) .. "11066011" .. tostring(creds) .. "11077011" .. tostring(math.random(1, 163456)))
  46. local file = fs.open("disk/name.txt", "w")
  47. file.write(playerName)
  48. file.close()
  49. Card.write(data)
  50. Card.close()
  51.  
  52. -- Update the disk label to include current credits and player's name
  53. disk.setLabel("right",playerName .. "'s Casino Card - " .. tostring(creds) .. "C")
  54. end
  55.  
  56. -- Function to read card data from the disk
  57. function readCard()
  58. if not fs.exists("disk/creds.lua") then
  59. return false
  60. end
  61.  
  62. local Card = fs.open("disk/creds.lua", "r")
  63. if not Card then
  64. centerText(8, "Error reading from disk!")
  65. sleep(2)
  66. return false
  67. end
  68.  
  69. local data = Card.readAll()
  70. Card.close()
  71.  
  72. local a, b = string.find(data, "11066011")
  73. local c, d = string.find(data, "11077011")
  74. creds = tonumber(string.sub(data, b + 1, c - 1))
  75. return true
  76. end
  77.  
  78. -- Function to insert a card
  79. function insertCard()
  80. while true do
  81. term.clear()
  82. centerText(6, "Please insert your Casino Card.")
  83. sleep(1)
  84.  
  85. if fs.exists("disk/") then
  86. term.clear()
  87. if readCard() then
  88. -- Rename the disk to reflect player credits and name
  89. disk.setLabel("right",playerName .. "'s Casino Card - " .. tostring(creds) .. "C")
  90. break -- Exit the loop if a valid card is inserted
  91. else
  92. centerText(8, "This isn't a Casino Card.")
  93. sleep(2)
  94. centerText(10, "Would you like to create one? (Y/N)")
  95. local event, key = os.pullEvent("key")
  96. if key == keys.y or key == keys.enter then
  97. term.clear()
  98. centerText(7, "Enter your name:")
  99. playerName = read() -- Prompt for player's name
  100. centerText(8, "Creating a new Casino Card...")
  101. creds = 0 -- Start with 0 credits
  102. writeCard()
  103. centerText(9, "Casino Card created.")
  104. sleep(2)
  105. else
  106. centerText(10, "Please remove the disk.")
  107. sleep(2)
  108. end
  109. end
  110. end
  111. end
  112. end
  113.  
  114. -- Function to check credit balance
  115. function checkBalance()
  116. term.clear()
  117. centerText(4, "Your Balance: " .. creds .. " Credits")
  118. centerText(6, "Press any key to continue...")
  119. os.pullEvent("key")
  120. end
  121.  
  122. -- Function to deposit credits
  123. function depositCredits()
  124. if not checkForCasinoCard() then
  125. centerText(10, "No Casino Card Found.")
  126. sleep(2)
  127. return
  128. end
  129.  
  130. term.clear()
  131. readCard()
  132. centerText(4, "Deposit Credits")
  133. centerText(6, "Diamonds, Gold Ingots, or Gold Nuggets.")
  134.  
  135. local totalCreds = 0
  136.  
  137. -- Function to find the chest behind the turtle
  138. local function findChest()
  139. originalDirection = 1 -- Reset to original direction
  140. for _ = 1, 4 do
  141. if turtle.suck(0) then
  142. return true -- Chest found
  143. end
  144. turtle.turnLeft() -- Keep turning until a chest is found
  145. end
  146. -- Restore original direction
  147. turtle.turnRight() -- Restore to original facing direction
  148. return false -- No chest found
  149. end
  150.  
  151. if not findChest() then
  152. centerText(8, "No chest found behind the turtle!")
  153. sleep(2)
  154. return
  155. end
  156.  
  157. for i = 1, 16 do
  158. if turtle.getItemCount(i) > 0 then
  159. local slot = turtle.getItemDetail(i)
  160. if slot.name == "minecraft:diamond" then
  161. totalCreds = totalCreds + (slot.count * credsPerDia)
  162. turtle.select(i)
  163. turtle.drop(slot.count) -- Drop diamonds to deposit
  164. elseif slot.name == "minecraft:gold_ingot" then
  165. totalCreds = totalCreds + (slot.count * credsPerGoldIngot)
  166. turtle.select(i)
  167. turtle.drop(slot.count) -- Drop gold ingots to deposit
  168. elseif slot.name == "minecraft:gold_nugget" then
  169. totalCreds = totalCreds + (slot.count * credsPerGoldNugget)
  170. turtle.select(i)
  171. turtle.drop(slot.count) -- Drop gold nuggets to deposit
  172. end
  173. end
  174. end
  175.  
  176. if totalCreds > 0 then
  177. creds = creds + totalCreds
  178. writeCard() -- Update card data and disk label
  179. centerText(8, totalCreds .. " Credits deposited.")
  180. else
  181. centerText(8, "No items to deposit!")
  182. end
  183.  
  184. centerText(10, "Press any key to continue...")
  185. local event, key = os.pullEvent("key")
  186. if key == keys.b then
  187. return
  188. end
  189. end
  190.  
  191. -- Function to withdraw credits
  192. function withdrawCredits()
  193. if not checkForCasinoCard() then
  194. centerText(10, "No Casino Card Found.")
  195. sleep(2)
  196. return
  197. end
  198.  
  199. term.clear()
  200. readCard()
  201. if creds <= 0 then -- Check if there are no credits available
  202. centerText(8, "The Bank is Empty!")
  203. centerText(10, "Press any key to return...")
  204. os.pullEvent("key") -- Wait for user input
  205. return
  206. end
  207.  
  208. centerText(4, "Withdraw Credits")
  209. centerText(6, "Enter amount to withdraw:")
  210.  
  211. local amount = tonumber(read())
  212.  
  213. if amount and amount > 0 and amount <= creds then
  214. creds = creds - amount
  215. writeCard() -- Update card data and disk label
  216.  
  217. -- Calculate how many items to withdraw based on credits
  218. local diamondsToWithdraw = math.floor(amount / credsPerDia)
  219. local goldIngotsToWithdraw = math.floor((amount % credsPerDia) / credsPerGoldIngot)
  220. local goldNuggetsToWithdraw = math.floor((amount % credsPerGoldIngot) / credsPerGoldNugget)
  221.  
  222. -- Function to find the chest behind the turtle
  223. local function findChest()
  224. originalDirection = 1 -- Reset to original direction
  225. for _ = 1, 4 do
  226. if turtle.suck(0) then
  227. return true -- Chest found
  228. end
  229. turtle.turnLeft() -- Keep turning until a chest is found
  230. end
  231. -- Restore original direction
  232. turtle.turnRight() -- Restore to original facing direction
  233. return false -- No chest found
  234. end
  235.  
  236. if not findChest() then
  237. centerText(8, "No chest found behind the turtle!")
  238. sleep(2)
  239. return
  240. end
  241.  
  242. -- Withdraw diamonds
  243. for i = 1, diamondsToWithdraw do
  244. turtle.select(1) -- Ensure the turtle is ready to suck
  245. turtle.suck(1) -- Try to pull a diamond from the chest
  246. end
  247.  
  248. -- Withdraw gold ingots
  249. for i = 1, goldIngotsToWithdraw do
  250. turtle.select(2) -- Ensure the turtle is ready to suck
  251. turtle.suck(1) -- Try to pull a gold ingot from the chest
  252. end
  253.  
  254. -- Withdraw gold nuggets
  255. for i = 1, goldNuggetsToWithdraw do
  256. turtle.select(3) -- Ensure the turtle is ready to suck
  257. turtle.suck(1) -- Try to pull a gold nugget from the chest
  258. end
  259.  
  260. centerText(8, "Withdrawn: " .. diamondsToWithdraw .. " Diamonds, " .. goldIngotsToWithdraw .. " Gold Ingots, " .. goldNuggetsToWithdraw .. " Gold Nuggets.")
  261. else
  262. centerText(8, "Invalid amount!")
  263. end
  264.  
  265. centerText(10, "Press 'B' to go back or any key to continue...")
  266. local event, key = os.pullEvent("key")
  267. if key == keys.b then
  268. return
  269. end
  270. end
  271.  
  272. -- Main program loop
  273. insertCard()
  274.  
  275. while true do
  276. term.clear()
  277. centerText(4, "Welcome to the Casino!")
  278. centerText(6, "1. Check Balance")
  279. centerText(7, "2. Deposit Credits")
  280. centerText(8, "3. Withdraw Credits")
  281. centerText(9, "4. Exit")
  282. local event, key = os.pullEvent("key")
  283.  
  284. if key == keys.one then
  285. checkBalance()
  286. elseif key == keys.two then
  287. depositCredits()
  288. elseif key == keys.three then
  289. withdrawCredits()
  290. elseif key == keys.four then
  291. insertCard() -- Direct to insert card screen
  292. end
  293. end
Advertisement
Add Comment
Please, Sign In to add comment