Advertisement
Marlingaming

Banking ATM MBA-2

Dec 4th, 2023 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.34 KB | None | 0 0
  1. os.pullEvent = os.pullEventRaw
  2. local Program_Run = true
  3.  
  4. local MachineID = os.computerID()
  5. local Deposit_Box
  6. local Store_Box
  7. local Fee_Rate = 0.1
  8. local Min_FeeCharge = 30--minimum range till fees are charged
  9. local Accepts_Cards = false
  10. local User
  11.  
  12. local Amount = 0
  13.  
  14. local Admin_Pin = "0000"
  15. local Bank
  16.  
  17. local Net_Mode = 0--0=none,1=slaved,2=master
  18. local Net = -1.0
  19.  
  20. local Menu = "Home"
  21.  
  22. local Currency = {{"createdeco:brass_coin",64},{"createdeco:iron_coin",16},{"createdeco:copper_coin",1}}
  23.  
  24. function Boot()
  25. Deposit_Box = peripheral.wrap("top")
  26. Store_Box = peripheral.wrap("left")
  27. User = nil
  28. Pin = nil
  29. local file = fs.open("Banking.txt","r")
  30. file.readLine()
  31. Admin_Pin = file.readLine()
  32. file.readLine()
  33. Net = tonumber(file.readLine())
  34. file.readLine()
  35. Fee_Rate = tonumber(file.readLine())
  36. file.readLine()
  37. Bank = file.readLine()
  38. file.readLine()
  39. Min_FeeCharge = tonumber(file.readLine())
  40. file.readLine()
  41. Net_Mode = tonumber(file.readLine())
  42. file.readLine()
  43. Accepts_Cards = file.readLine()
  44. file.close()
  45. Explorer()
  46. end
  47.  
  48. function Input(y)
  49. local n = 1
  50. local Text
  51. while true do
  52. term.setCursorPos(1,y)
  53. term.clearLine()
  54. if n == 1 then if Text ~= nil then term.write("> ["..Text.."]") else term.write("> [_]") end else if Text ~= nil then term.write("["..Text.."]") else term.write("[_]") end end
  55. term.setCursorPos(1,y+1)
  56. term.clearLine()
  57. if n == 2 then term.write("> -enter") else term.write("-enter") end
  58. term.setCursorPos(1,y+2)
  59. term.clearLine()
  60. term.write("use arrow keys")
  61. local event, a, b = os.pullEvent()
  62. if event == "key" and a ~= 265 and a ~= 264 and n == 1 then term.setCursorPos(1,y) term.clearLine() Text = read() end
  63. if event == "key" and (a == 265 or a == 264) then if n == 1 then n = 2 else n = 1 end end
  64. if n == 2 and a == keys.enter then break end
  65. end
  66. term.clear()
  67. return Text
  68. end
  69.  
  70. function List(y,options)
  71. local n = 1
  72. while true do
  73. for i = 1, #options do
  74. term.setCursorPos(1,(i-1)+y)
  75. term.clearLine()
  76. if n == i then term.write("> -"..options[i]) else term.write("-"..options[i]) end
  77. end
  78. term.setCursorPos(1,y+#options)
  79. term.clearLine()
  80. term.write("W/S, Enter")
  81. local a, b, c = os.pullEvent("key")
  82. if b == keys.w or b == 265 then if n == 1 then n = #options else n = n - 1 end end
  83. if b == keys.s or b == 264 then if n == #options then n = 1 else n = n + 1 end end
  84. if b == keys.enter then break end
  85. end
  86. term.clear()
  87. return n
  88. end
  89.  
  90. function CheckLog(Name)
  91. local Verified = false
  92. if fs.exists(fs.combine("Accounts",Name..".act")) then
  93. local file = fs.open(fs.combine("Accounts",Name..".act"),"r")
  94. local Data = textutils.unserialize(file.readAll())
  95. Verified = Data[2]
  96. if Data[5] == true then Vertified = false end
  97. file.close()
  98. end
  99. return Verified
  100. end
  101.  
  102. function NetManage(a,b,c,d)
  103. local MSG
  104. if c == "request" then
  105. MSG = textutils.serialize(ReadAccount(b))
  106. elseif c == "update" then
  107. local file = fs.open(fs.combine("Accounts",b..".act"),"w")
  108. file.write(d)
  109. file.close()
  110. MSG = "accepted"
  111. elseif c == "charge" then
  112.  
  113. end
  114. return MSG
  115. end
  116.  
  117. function NetGet(act)
  118. local ReturnNet = math.random(88,99)
  119. local Modem = peripheral.wrap("back")
  120. Modem.open(ReturnNet)
  121. local MSG = textutils.serialize({Bank,act,"request"})
  122. Modem.transmit(Net,ReturnNet,MSG)
  123. local a, b, c, d, e, f = os.pullEvent("modem_message")
  124. local file = fs.open(fs.combine("Accounts",act..".act"),"w")
  125. file.write(e)
  126. file.close()
  127. end
  128.  
  129. function NetSend(act)
  130. local ReturnNet = math.random(88,99)
  131. local Modem = peripheral.wrap("back")
  132. Modem.open(ReturnNet)
  133. local file = fs.open(fs.combine("Accounts",act..".act"),"r")
  134. local DATA = file.readAll()
  135. file.close()
  136. local MSG = textutils.serialize({Bank,act,"update",DATA})
  137. Modem.transmit(Net,ReturnNet,MSG)
  138. end
  139.  
  140. function CreateAccount(UserName,admin)
  141. if admin == nil then admin = false end
  142. local Data = {MachineID,true,0,0.01,admin,{}}--machineid,unlocked,balance,rate,admin
  143. local file = fs.open(fs.combine("Accounts",UserName..".act"),"w")
  144. file.write(textutils.serialize(Data))
  145. file.close()
  146. end
  147.  
  148. function ReadAccount(D)
  149. local Data = {}
  150. local file = fs.open(fs.combine("Accounts",User..".act"),"r")
  151. Data = textutils.unserialize(file.readAll())
  152. file.close()
  153. return Data[D]
  154. end
  155.  
  156. function UpdateAccount(U,A)
  157. local Data = {ReadAccount(1),ReadAccount(2),ReadAccount(3)+A,ReadAccount(4),ReadAccount(5),ReadAccount(6)}
  158. local file = fs.open(fs.combine("Accounts",U..".act"),"w")
  159. file.write(textutils.serialize(Data))
  160. file.close()
  161. end
  162.  
  163. function EditCards(action,card,var)
  164. local Cards = ReadAccount(6)
  165. if action == "delete" then Cards[card] = nil elseif action == "new" then Cards[#Cards+1] = math.random(11111,99999) end
  166. error("need to finish")
  167. end
  168.  
  169. function CreateTranLog(from,to,amount,reason)
  170. local file = fs.open(fs.combine("TLogs",from.."_"..os.day()..".txt"),"w")
  171. file.write(textutils.serialize({amount,reason,MachineID}))
  172. file.close()
  173. end
  174.  
  175. function Transfer(amount)
  176. local Result
  177. local Fees = 0
  178. if amount < 0 then Result = Chests(Deposit_Box,Store_Box,amount) else Result = true amount = DrainStore() end
  179. if Result == true then if amount < 0 and amount*-1 >= Min_FeeCharge and ReadAccount(User,5) == false then Fees = math.floor((amount*Fee_Rate)+0.5) amount = amount + Fees end UpdateAccount(User,amount) if Net ~= -1.0 and Net_Mode == 1 then NetSend(User) end end
  180. if Fees > 0 then UpdateAccount("_Admin",Fees) end
  181. end
  182.  
  183. function Charge(a,b,amount,reason)
  184. local Result = false
  185. local A_D = ReadAccount(a)
  186. local B_D = ReadAccount(b)
  187. if A_D[3]-amount >= 0 then
  188. UpdateAccount(a,amount*-1)
  189. UpdateAccount(b,amount)
  190. CreateTranLog(a,b,amount,reason)
  191. Result = true
  192. end
  193. return Result
  194. end
  195.  
  196. function DrainStore()
  197. local E = 0
  198. for i = 1, Deposit_Box.size() do
  199. local Slot = Deposit_Box.getItemDetail(i)
  200. for M = 1, #Currency do
  201. if Slot ~= nil then if Slot.name == Currency[M][1] then E = E + (Currency[M][2] * Slot.count) Deposit_Box.pushItems(peripheral.getName(Store_Box),i,64,M*9-8) end end
  202. end
  203. end
  204. return E
  205. end
  206.  
  207. function TransferMenu()
  208. local n = 1
  209. local C = {}
  210. for i = 1, #Currency do
  211. C[i] = 0
  212. end
  213. term.clear()
  214. while true do
  215. for i = 1, #Currency+1 do
  216. term.setCursorPos(1,i+3)
  217. term.clearLine()
  218. if n == i then if i <= #Currency then term.write("[>] "..Currency[i][1].." ["..C[i].."] | $"..(C[i]*Currency[i][2])) else term.write("[>] confirm") end else if i <= #Currency then term.write("[ ] "..Currency[i][1].." ["..C[i].."] | $"..(C[i]*Currency[i][2])) else term.write("[ ] confirm") end end
  219. end
  220. term.setCursorPos(1,#Currency+5)
  221. term.clearLine()
  222. term.write("W/S,A/D,/\ \/,< >, enter")
  223. local a, b, c = os.pullEvent("key")
  224. if b == 264 or b == keys.s then if n == #C + 1 then n = 1 else n = n + 1 end end
  225. if b == 265 or b == keys.w then if n == 1 then n = #C + 1 else n = n - 1 end end
  226. if b == 263 or b == keys.a then if n <= #C and C[n] > 0 then C[n] = C[n] - 1 end end
  227. if b == 262 or b == keys.d then if n <= #C then C[n] = C[n] + 1 end end
  228. if n == #C+1 and b == keys.enter then break end
  229. end
  230. local E = 0
  231. for i = 1, #C do
  232. E = E + (Currency[i][2]*C[i])
  233. end
  234. local Bal = ReadAccount(3)
  235. if E <= Bal then
  236. local CE = C
  237. for X = 1, #C do
  238. for i = X*9-8, 9*X do
  239. local Slot = Store_Box.getItemDetail(i)
  240. if Slot ~= nil and Slot.name == Currency[X][1] and CE[X] ~= 0 then local T = Slot.count Store_Box.pushItems(peripheral.getName(Deposit_Box),i,CE[X]) if CE[X] > T then CE[X] = CE[X] - T else CE[X] = 0 end end
  241. end
  242. end
  243. UpdateAccount(User,E*-1)
  244. return true
  245. else
  246. return false
  247. end
  248. end
  249.  
  250. function Chests(A,B,amount)
  251. local ChestA = A
  252. local ChestB = B
  253. local E = 0
  254. local S = 0
  255. for i = 1, A.size() do
  256. local Slot = A.getItemDetail(i)
  257. for M = 1, #Currency do
  258. if Slot ~= nil then if Slot.name == Currency[M][1] then E = E + (Currency[M][2] * Slot.count) end end
  259. end
  260. end
  261. if E >= amount then
  262. E = 0
  263. repeat
  264. for i = 1, ChestA.size() do
  265. local Slot = ChestA.getItemDetail(i)
  266. for M = 1, #Currency do
  267. if Slot ~= nil then if Slot.name == Currency[M][1] and Currency[M][2] <=(amount-E) then local AM = math.floor((amount/Currency[M][2])+0.6) E = E + AM ChestA.pushItems(peripheral.getName(ChestB),i,AM) end end
  268. end
  269. end
  270. until E >= amount
  271. Amount = E
  272. return true
  273. else
  274. return false
  275. end
  276. end
  277.  
  278. function UI()
  279. term.setTextColor(colors.white)
  280. term.setBackgroundColor(colors.blue)
  281. term.clear()
  282. term.setCursorPos(1,1)
  283. local Data = {}
  284. if Menu == "Home" then
  285. print("Banking OS")
  286. print("//WARNING//Expermental")
  287. local c = List(3,{"login/signup","policies"})
  288. if c == 1 then Data = {"Page","Login"} else Data = {"Page","Policies"} end
  289. elseif Menu == "Policies" then
  290. print("Policies:")
  291. print("-no refunds on Fees")
  292. print("press enter")
  293. local event, a = os.pullEvent("key")
  294. repeat until a == keys.enter
  295. Data = {"Page","Home"}
  296. elseif Menu == "Login" then
  297. print("enter Credentials")
  298. local Log = Input(3)
  299. if Log == Admin_Pin then Data = {"Page","Admin"} elseif CheckLog(Log) == true then User = Log Data = {"Page", "Account"} if Net ~= -1.0 and Net_Mode == 1 then NetGet(Log) end else local T = List(5,{"create account","cancel"}) if T == 1 then CreateAccount(Log) User = Log Data = {"Page","Account"} elseif T == 2 then Data = {"Page","Home"} end end
  300. elseif Menu == "Setup" then
  301.  
  302. elseif Menu == "Account" then
  303. local BAL = ReadAccount(3)
  304. print("Welcome, "..User)
  305. print("Balance: "..BAL)
  306. print("====================")
  307. local n = List(4,{"Transaction","Card","logout"})
  308. if n == 1 then Data = {"Page","Transaction"} elseif n == 2 then print("coming soon") os.sleep(3) elseif n == 3 then User = nil Data = {"Page","Home"} end
  309.  
  310. elseif Menu == "Admin" then
  311. print("ADMIN MENU")
  312. local n = List(3,{"stop program","open deposit","close deposit","admin account","logout"})
  313. if n == 1 then Program_Run = false elseif n == 2 then redstone.setOutput("bottom",true) elseif n == 3 then redstone.setOutput("bottom",false) elseif n == 4 then User = "_Admin" Data = {"Page","Account"} elseif n == 5 then Data = {"Page","Home"} end
  314. elseif Menu == "Transaction" then
  315. local n = List(3,{"withdraw","deposit"})
  316. if n == 1 then TransferMenu() else UpdateAccount(User,0+DrainStore()) end
  317. Data = {"Page","Account"}
  318. elseif Menu == "Cards" then
  319. Print("Cards on file")
  320. local Cards = ReadAccount(6)
  321. Cards[#Cards+1] = "new"
  322. Cards[#Cards+1] = "exit"
  323.  
  324. local n = List(3,Cards)
  325. if n == #Cards then Menu = "Account" elseif n == #Cards - 1 then else local G = List(3,{"delete","return"}) if G == 1 then EditCards("delete",n) end end
  326. end
  327. return Data
  328. end
  329.  
  330. function Explorer()
  331. while true do
  332. local a, b, c, d, e, f
  333. if Net_Mode == 2 then local modem = peripheral.wrap("back") modem.open(Net) a, b, c, d, e, f = os.pullEvent("modem_message") local msg = textutils.unserialize(e) if msg[1] == Bank then modem.transmit(d,Net,NetManage(msg)) end end
  334. local R = UI()
  335. if R[1] == "Page" then Menu = R[2] end
  336. os.queueEvent("pass")
  337. if Program_Run == false then break end
  338. end
  339. if Program_Run == true then os.reboot() end
  340. end
  341.  
  342. function Setup()
  343. fs.makeDir("Accounts")
  344. fs.makeDir("TLogs")
  345. Menu = "Setup"
  346. Admin_Pin, Net, Fee_Rate, Bank = "0000", -1, 0.05, "State Bank"
  347. local file = fs.open("Banking.txt","w")
  348. file.writeLine("Admin Pin")
  349. file.writeLine(Admin_Pin)
  350. file.writeLine("Net")
  351. file.writeLine(Net)
  352. file.writeLine("Fee")
  353. file.writeLine(Fee_Rate)
  354. file.writeLine("Bank")
  355. file.writeLine(Bank)
  356. file.writeLine("minium amount for fee to apply")
  357. file.writeLine(Min_FeeCharge)
  358. file.writeLine("Net Mode")
  359. file.writeLine(Net_Mode)
  360. file.writeLine("Accepts Cards")
  361. file.writeLine(Accepts_Cards)
  362. file.close()
  363. CreateAccount("_Admin",true)
  364. end
  365.  
  366. if fs.exists("Banking.txt") then Boot() else Setup() end
  367.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement