· 4 years ago · Jun 12, 2021, 05:26 PM
1local thread = require("thread")
2local com = require("component")
3local term = require("term")
4local gpu = term.gpu()
5local web = require("internet")
6local json = require("json")
7local event = require("event")
8local shell = require("shell")
9local data = com.data
10
11--Variables
12local apiKeyPath = "/home/key/apiKey"
13local mainURL ="https://alteran.de/api"
14local userName = nil
15local userKey = nil
16local accounts = nil
17local workAccount = nil
18local apiKey = nil
19local action = nil
20local dBack = 0x365991
21local tBack = 0x545454
22
23--Do not change!!!
24local sizeX, sizeY = term.getViewport()
25local rs = nil
26local endSeason = false
27local backToAccounts = true
28--functions
29
30function clearScreen()
31 setBack(dBack)
32 term.clear()
33 setCursor(1,1)
34end
35
36function setCursor(x,y)
37 term.setCursor(x,y)
38end
39
40function setBack(color)
41 gpu.setBackground(color, false)
42end
43
44function printBack()
45 setBack(0xde1310)
46 setCursor(sizeX-3,sizeY-3)
47 print(" ")
48 setCursor(sizeX-3,sizeY-2)
49 print(" <- ")
50 setCursor(sizeX-3,sizeY-1)
51 print(" ")
52 setBack(dBack)
53end
54
55function programSerProblem(problem)
56 local err = "There was an Error"
57 local err2 = "Please inform the bank staff"
58 local err3 = "Problem: "..problem
59 local l1 = string.len(err)/2
60 local l2 = string.len(err2)/2
61 local l3 = string.len(err3)/2
62 gpu.setBackground(0xe01c07, false)
63 term.clear()
64 setCursor(sizeX/2-l1,sizeY/2-3)
65 print(err)
66 setCursor(sizeX/2-l2,sizeY/2-1)
67 print(err2)
68 setCursor(sizeX/2-l3,sizeY/2+1)
69 print(err3)
70 while true do
71 local _,_,x,y,_,n = event.pull("touch")
72 if x == 1 and y == 1 then
73 shell.execute("reboot")
74 end
75 end
76end
77
78function getInterface()
79 for e in com.list("interface") do
80 rs = com.proxy(e)
81 break
82 end
83end
84
85function getApiData(path)
86 local url = mainURL.."/"..apiKey.."/"..path
87 local handle = web.request(url)
88-- print(url)
89-- os.sleep(2)
90 local result = ""
91 for chunk in handle do result = result..chunk end
92
93 local mt = getmetatable(handle)
94 local code, message, headers = mt.__index.response()
95 if code == 200 then
96 return json.decode(result)
97 end
98end
99
100function programProblem(problem)
101 local l = string.len(problem)/2
102 gpu.setBackground(0xe01c07, false)
103 term.clear()
104 setCursor(sizeX/2-l,sizeY/2)
105 print(problem)
106 os.sleep(8)
107 os.execute("reboot")
108end
109
110function drawKeyButton(x,y,n)
111 setCursor(x,y)
112 print(" ")
113 setCursor(x,y+1)
114 print(" "..n.." ")
115 setCursor(x,y+2)
116 print(" ")
117end
118
119function drawKeyPad(x,y)
120 setBack(0x593799)--odd Buttons
121 drawKeyButton(x,y,"1")
122 drawKeyButton(x+8,y,"3")
123 drawKeyButton(x+4,y+3,"5")
124 drawKeyButton(x,y+6,"7")
125 drawKeyButton(x+8,y+6,"9")
126 drawKeyButton(x+4,y+9,"0")
127
128 setBack(0x3e2869)--even Buttons
129 drawKeyButton(x+4,y,"2")
130 drawKeyButton(x,y+3,"4")
131 drawKeyButton(x+8,y+3,"6")
132 drawKeyButton(x+4,y+6,"8")
133
134 setBack(0xde1310)--cancle
135 drawKeyButton(x,y+9,"X")
136
137 setBack(0x0cc456)--continue
138 drawKeyButton(x+8,y+9,"O")
139end
140
141function getAccounts()
142 local out = getApiData("accounts/"..userName.."/"..userKey)
143 if out.accounts == nil then
144 programSerProblem("Your user account has no associated bank accounts.")
145 else
146 accounts = out.accounts
147 end
148end
149
150function printLine()
151 local i = sizeX
152 local s = ""
153 while i > 0 do
154 s = s.."-"
155 i = i - 1
156 end
157 print(s)
158end
159
160function selectAction()
161 setBack(dBack)
162 clearScreen()
163 local t1 = "What do you want to do?"
164 local l1 = string.len(t1)/2
165 setCursor(sizeX/2-l1,1)
166 print(t1)
167 printLine()
168 local li = "-------------- --------------"
169 local b1 = "| withdraw | | deposit |"
170 local l2 = string.len(b1)/2
171 local x = sizeX/2-l2
172 setCursor(x,sizeY/2-1)
173 print(li)
174 setCursor(x,sizeY/2)
175 print(b1)
176 setCursor(x,sizeY/2+1)
177 print(li)
178 while true do
179 local _,_,x,y,_,n = event.pull("touch")
180 if n == userName then
181 if x >= 13 and x <= 26 and y >= 11 and y <= 13 then
182 action = "withdraw"
183 break
184 elseif x >= 33 and x <= 46 and y >= 11 and y <= 13 then
185 action = "deposit"
186 break
187 end
188 end
189 end
190end
191
192function drawAccounts()
193 setBack(dBack)
194 local t1 = "Please select an account"
195 local l1 = string.len(t1)/2
196 clearScreen()
197 setCursor(sizeX/2-l1,1)
198 print(t1)
199 setCursor(1,3)
200 printLine()
201 setCursor(1,4)
202 local llist = 0
203 for _, e in ipairs(accounts) do
204 print("Number: ".. e.iban)
205 print("Name: ".. e.name)
206 print("Balance: ".. (e.balance*100).." GC")
207 printLine()
208 llist = llist +1
209 end
210 while true do
211 local _,_,px,py,_,n = event.pull("touch")
212 if n == userName then
213 if py >= 4 and py <= 6 then
214 workAccount = accounts[1]
215 break
216 elseif py >= 8 and py <= 10 then
217 if llist >= 2 then
218 workAccount = accounts[2]
219 break
220 end
221 elseif py >= 12 and py <= 14 then
222 if llist >= 3 then
223 workAccount = accounts[3]
224 break
225 end
226 end
227 end
228 end
229end
230
231function drawLogin()
232 local text1 = "Please login to your account"
233 local len1 = string.len(text1)
234 local name = ""
235 local pw = ""
236 while true do
237 local lenN = string.len(name)
238 local Nlen = 18-lenN
239 local lenP = string.len(pw)
240 local Plen = 9-lenP
241
242 clearScreen()
243 printBack()
244 setCursor(sizeX/2-len1/2,sizeY/2-8)
245 print(text1)
246 setBack(tBack)
247 setCursor(sizeX/2-9,sizeY/2-4)
248
249 if userName == nil then
250 local name = "_"
251 local i = Nlen - 1
252
253 while i > 0 do
254 name = name .. " "
255 i = i-1
256 end
257 print(name)
258 setCursor(sizeX/2-4,sizeY/2-2)
259 i = Plen
260 local pw2 = ""
261
262 while i > 0 do
263 pw2 = pw2 .. " "
264 i = i-1
265 end
266 print(pw2)
267 else
268 local i = Nlen
269
270 while i > 0 do
271 name = name .. " "
272 i = i -1
273 end
274 print(name)
275 setCursor(sizeX/2-4, sizeY/2-2)
276 i = Plen
277 local pw2 = pw
278 while i > 0 do
279 pw2 = pw2 .. " "
280 i = i-1
281 end
282 print(pw2)
283 drawKeyPad(sizeX/2-5,sizeY-13)
284 end
285 local _,_,px,py,_,n = event.pull("touch")
286
287 if userName == nil then
288 userName = n
289 name = n
290 else
291 if n == userName then
292 setCursor(sizeX/2, sizeY-1)
293 --print(px,py)
294 if px >= 25 and px <= 27 and py >= 12 and py <= 14 then
295 pw = pw .. "1"
296 elseif px >= 29 and px <= 31 and py >= 12 and py <= 14 then
297 pw = pw .. "2"
298 elseif px >= 33 and px <= 35 and py >= 12 and py <= 14 then
299 pw = pw .. "3"
300 elseif px >= 25 and px <= 27 and py >= 15 and py <= 17 then
301 pw = pw .. "4"
302 elseif px >= 29 and px <= 31 and py >= 15 and py <= 17 then
303 pw = pw .. "5"
304 elseif px >= 33 and px <= 35 and py >= 15 and py <= 17 then
305 pw = pw .. "6"
306 elseif px >= 25 and px <= 27 and py >= 18 and py <= 20 then
307 pw = pw .. "7"
308 elseif px >= 29 and px <= 31 and py >= 18 and py <= 20 then
309 pw = pw .. "8"
310 elseif px >= 33 and px <= 35 and py >= 18 and py <= 20 then
311 pw = pw .. "9"
312 elseif px >= 25 and px <= 27 and py >= 21 and py <= 23 then
313 if pw == "" then
314 --print("log off")
315 os.sleep(2)
316 shell.execute("/home/readyScreen.lua")
317 else
318 pw = ""
319 end
320 elseif px >= 29 and px <= 31 and py >= 21 and py <= 23 then
321 pw = pw .. "0"
322 elseif px >= 33 and px <= 35 and py >= 21 and py <= 23 then
323 local resp = getApiData("login/"..userName.."/".. pw)
324 if resp.key == false then
325 programSerProblem("The API Key is not/no longer valid")
326 end
327 if resp.account == nil then
328 programProblem("You do not have an account yet!")
329 elseif resp.account == "wrongPin" then
330 local err = " Wrong Pin "
331 local er2 = " "
332 local lerr = string.len(err)/2
333 setCursor(sizeX/2-lerr, sizeY/2-1)
334 print(er2)
335 setCursor(sizeX/2-lerr, sizeY/2)
336 print(err)
337 setCursor(sizeX/2-lerr, sizeY/2+1)
338 print(er2)
339 os.sleep(2)
340 pw = ""
341 elseif resp.account:find("^U000") ~= nil then
342 userKey = resp.accAuth
343 break
344 end
345 elseif px >= sizeX-3 and py >= sizeY-3 then
346 endSeason = true
347 break
348 end
349 end
350 end
351 end
352end
353
354function backToChest()
355 for e in rs.getItems() do
356 local size = e.size()
357 while size > 0 do
358 local b = rs.extractItem(e,size,sides.up)
359 size = size - b
360 end
361 end
362end
363
364function deposit()
365 local emptyBox = thread.create(function()
366 os.sleep(8)
367 shell.execute("/home/emptyBox.lua")
368 end)
369
370 setBack(dBack)
371 clearScreen()
372 local t1 = "Depositing money - "..workAccount.name.." ("..workAccount.iban..")"
373 local t2 = "Please put your money in the chest"
374 local t21 = "Importing"
375 local t3 = "Calculating the amount"
376 local l1 = sizeX/2-string.len(t1)/2
377 local l2 = sizeX/2-string.len(t2)/2
378 local l21 = sizeX/2-string.len(t21)/2
379 local l3 = sizeX/2-string.len(t3)/2
380 setCursor(l1,1)
381 print(t1)
382 setCursor(l2,2)
383 print(t2)
384 setCursor(1,3)
385 printLine()
386 setCursor(l21, sizeY/2)
387 print(t21)
388 thread.waitForAny({emptyBox})
389 emptyBox:kill()
390 clearScreen()
391 setCursor(l1,1)
392 print(t1)
393 setCursor(1,2)
394 printLine()
395 setCursor(l3,sizeY/2)
396 print(t3)
397 local list = json.decode('{"Copper":0, "Silver":0, "Gold":0, "Steel":0, "Platinum":0, "Enderium":0}')
398 local amount = 0
399 for _,e in ipairs(rs.getItems()) do
400 if e.label=="Copper Coin" then
401 list.Copper = e.size
402 amount = amount + e.size * 0.0001
403 elseif e.label=="Silver Coin" then
404 list.Silver = e.size
405 amount = amount + e.size * 0.001
406 elseif e.label=="Gold Coin" then
407 list.Gold = e.size
408 amount = amount + e.size * 0.01
409 elseif e.label=="Steel Coin" then
410 list.Steel = e.size
411 amount = amount + e.size * 0.2
412 elseif e.label=="Platinum Coin" then
413 list.Platinum = e.size
414 amount = amount + e.size * 1
415 elseif e.label=="Enderium Coin" then
416 list.Enderium = e.size
417 amount = amount + e.size * 10
418 end
419 end
420 os.sleep(1)
421 t1 = "Found the following coins:"
422 l1 = sizeX/2-string.len(t1)/2
423 local ct1 = "Copper Coins: "..list.Copper..", Silver Coins: "..list.Silver..", Gold Coins: "..list.Gold
424 local ct2 = "Platinum Coins: "..list.Platinum..", Enderium Coins: "..list.Enderium
425 local amount2 = amount*100
426 t2 = "Total Value: "..amount2.." GC"
427 local lct1 = sizeX/2-string.len(ct1)/2
428 local lct2 = sizeX/2-string.len(ct2)/2
429 local lt2 = sizeX/2-string.len(t2)/2
430 setCursor(l1,sizeY/2-2)
431 print(t1)
432 setCursor(lct1, sizeY/2)
433 print(ct1)
434 setCursor(lct2, sizeY/2+1)
435 print(ct2)
436 setCursor(lt2, sizeY/2+3)
437 print(t2)
438 setBack(0x0cc456)
439 drawKeyButton(sizeX/2-1,sizeY/2+6, "0")
440 printBack()
441 while true do
442 local _,_,x,y,_,n = event.pull("touch")
443 if n == userName then
444 if x >= 29 and x <= 31 and y >= 18 and y <= 20 then
445 local resp = getApiData("deposit/"..userName.."/"..amount.."/"..workAccount.accessKey)
446 -- print(resp.deposit)
447 -- print(amount)
448 -- os.sleep(4)
449 if tostring(resp.deposit) == "noTransaction" then
450 programSerProblem("Deposit couldn't be executed")
451 elseif tostring(resp.deposit) == tostring(amount) then
452 local t2 = "Deposit was succsessful."
453 local l2 = sizeX/2-string.len(t2)/2
454 clearScreen()
455 setCursor(l2,sizeY/2)
456 print(t2)
457 os.sleep(3)
458 shell.execute("/home/emptySystem.lua")
459 break
460 else
461 programSerProblem("Error while executing API request. (Deposit)")
462 end
463 end
464 elseif px >= sizeX-3 and py >= sizeY-3 then
465 backToChest()
466 backToAccounts = true
467 break
468 end
469 end
470end
471
472function withdrawMoney(amount)
473 local list = json.decode('{"Copper":0, "Silver":0, "Gold":0, "Steel":0, "Platinum":0, "Enderium":0}')
474 local counter = amount
475 while counter >= 10 do
476 list.Enderium = list.Enderium + 1
477 counter = counter - 10
478 end
479 while counter >= 1 do
480 list.Platinum = list.Platinum +1
481 counter = counter - 1
482 end
483 while counter >= 0.2 do
484 list.Steel = list.Steel + 1
485 counter = counter - 0.2
486 end
487 while counter >= 0.01 do
488 list.Gold = list.Gold + 1
489 counter = counter - 0.01
490 end
491 while counter >= 0.001 do
492 list.Silver = list.Silver + 1
493 counter = counter - 0.001
494 end
495 while counter >= 0.0001 do
496 list.Copper = list.Copper + 1
497 counter = counter - 0.0001
498 end
499 --Do the money export here###############
500 shell.execute("/home/openVault.lua")
501 local send = 0
502 local redo = false
503 while redo do
504 redo = false
505 for e in rs.getItems() do
506 local size = e.size()
507 if e.label=="Copper Coin" then
508 while list.Copper > 0 and size > 0 do
509 local b = rs.extractItem(e,list.Copper,sides.up)
510 list.Copper = list.Copper - b
511 size = size - b
512 send = send + b* 0.0001
513 end
514 elseif e.label=="Silver Coin" then
515 while list.Silver > 0 and size > 0 do
516 local b = rs.extractItem(e,list.Silver,sides.up)
517 list.Silver = list.Silver - b
518 size = size - b
519 send = send + b* 0.001
520 end
521 elseif e.label=="Gold Coin" then
522 while list.Gold > 0 and size > 0 do
523 local b = rs.extractItem(e,list.Gold,sides.up)
524 list.Gold = list.Gold - b
525 size = size - b
526 send = send + b* 0.01
527 end
528 elseif e.label=="Steel Coin" then
529 while list.Steel > 0 and size > 0 do
530 local b = rs.extractItem(e,list.Steel,sides.up)
531 list.Steel = list.Steel - b
532 size = size - b
533 send = send + b* 0.2
534 end
535 if list.Steel > 0 then
536 list.Gold = list.Gold + list.Steel * 20
537 list.Steel = 0
538 redo = true
539 end
540 elseif e.label=="Platinum Coin" then
541 while list.Platinum > 0 and size > 0 do
542 local b = rs.extractItem(e,list.Platinum,sides.up)
543 list.Platinum = list.Platinum - b
544 size = size - b
545 send = send + b --* 1
546 end
547 if list.Platinum > 0 then
548 list.Steel = list.Steel + list.Platinum * 5
549 list.Platinum = 0
550 redo = true
551 end
552 elseif e.label=="Enderium Coin" then
553 while list.Enderium > 0 and size > 0 do
554 local b = rs.extractItem(e,list.Enderium,sides.up)
555 list.Enderium = list.Enderium - b
556 size = size - b
557 send = send + b* 10
558 end
559 if list.Enderium > 0 then
560 list.Platinum = list.Platinum + list.Enderium * 10
561 list.Enderium = 0
562 redo = true
563 end
564 end
565 end
566 end
567 shell.execute("/home/closeVault.lua")
568 return amount - send
569end
570
571function withdraw()
572 setBack(dBack)
573 clearScreen()
574 local t1 = "How much do you want"
575 local l1 = sizeX/2-string.len(t1)/2
576 setCursor(l1,1)
577 print(t1)
578 setCursor(1,2)
579 printLine()
580 setCursor(2,4)
581 print(workAccount.name)
582 setCursor(2,5)
583 print(workAccount.iban)
584 if (string.len(workAccount.balance*100)+4) > 33 then
585 setCursor(2,sizeY)
586 else
587 setCursor(2,sizeY/2)
588 end
589 print("=>"..(workAccount.balance*100).." GC")
590 drawKeyPad(sizeX/2+8,sizeY/2-5)
591 setBack(0x3e2869)
592 drawKeyButton(sizeX/2+12, sizeY-6, ".")
593 local amount = ""
594 local hasDot = false
595 setBack(tBack)
596 printBack()
597 while true do
598 local amount2 = amount
599 while string.len(amount2) < 20 do
600 amount2 = amount2 .. " "
601 end
602 setCursor(sizeX/2+3,sizeY/2-7)
603 print(amount2)
604 local _,_,x,y,_,n = event.pull("touch")
605 --setCursor(1,3)
606 --print(" "..x.." "..y.." "..n.." ")
607 if n ==userName then
608 if x >= 38 and x <= 40 then
609 if y >= 7 and y <= 9 then
610 amount = amount .. "1"
611 elseif y >= 10 and y <= 12 then
612 amount = amount .."4"
613 elseif y >= 13 and y <= 15 then
614 amount = amount .. "7"
615 elseif y >= 16 and y <= 18 then
616 amount = ""
617 hasDot = false
618 end
619 elseif x >= 42 and x <= 44 then
620 if y >= 7 and y <= 9 then
621 amount = amount .. "2"
622 elseif y >= 10 and y <= 12 then
623 amount = amount .. "5"
624 elseif y >= 13 and y <= 15 then
625 amount = amount .. "8"
626 elseif y >= 16 and y <= 18 then
627 amount = amount .. "0"
628 elseif y >= 19 and y <= 21 then
629 if hasDot == false then
630 amount = amount .. "."
631 hasDot = true
632 end
633 end
634 elseif x >= 46 and x <= 48 then
635 if y >= 7 and y <= 9 then
636 amount = amount .. "3"
637 elseif y >= 10 and y <= 12 then
638 amount = amount .. "6"
639 elseif y >= 13 and y <= 15 then
640 amount = amount .. "9"
641 elseif y >= 16 and y <= 18 then
642 if amount ~= "" then
643 amount = tonumber(amount)/100
644 local resp = getApiData("withdraw/"..userName.."/"..amount.."/"..workAccount.accessKey)
645 --print(resp.withdraw)
646 --os.sleep(4)
647 if tostring(resp.withdraw) == "noTransaction" then
648 programProblem("This action could not be executed. (withdraw)")
649 elseif tostring(resp.withdraw) == "insufficientFunds" then
650 amount = ""
651 setBack(0xfcba03)
652 setCursor(sizeX/2-11,sizeY/2-5)
653 print(" ")
654 setCursor(sizeX/2-11,sizeY/2-4)
655 print(" Insufficent Funds ")
656 setCursor(sizeX/2-11,sizeY/2-3)
657 print(" ")
658 os.sleep(2)
659 setBack(dBack)
660 setCursor(sizeX/2-11,sizeY/2-5)
661 print(" ")
662 setCursor(sizeX/2-11,sizeY/2-4)
663 print(" ")
664 setCursor(sizeX/2-11,sizeY/2-3)
665 print(" ")
666 setBack(tBack)
667 elseif tostring(resp.withdraw) == tostring(amount) then
668 local rest = withdrawMoney(amount)
669 if rest > 0 then
670 local resp = getApiData("deposit/"..userName.."/"..amount.."/"..workAccount.accessKey)
671 --print(resp.deposit)
672 --print(amount)
673 --os.sleep(4)
674 if tostring(resp.deposit) == "noTransaction" then
675 programSerProblem("Redeposit couldn't be executed")
676 elseif tostring(resp.deposit) == tostring(amount) then
677 local t2 = "Redeposit was succsessful."
678 local l2 = sizeX/2-string.len(t2)/2
679 clearScreen()
680 setCursor(l2,sizeY/2)
681 print(t2)
682 os.sleep(3)
683 shell.execute("/home/emptySystem.lua")
684 end
685 end
686 local t2 = "Redeposit was succsessful."
687 local l2 = sizeX/2-string.len(t2)/2
688 clearScreen()
689 setCursor(l2,sizeY/2)
690 print(t2)
691 os.sleep(3)
692 end
693 end
694 end
695 end
696 elseif px >= sizeX-3 and py >= sizeY-3 then
697 backToAccounts = true
698 break
699 end
700 end
701end
702
703function printGoodbye()
704 clearScreen()
705 local t1 = "Have a nice day."
706 local l1 = sizeX/2-string.len(t1)/2
707 setCursor(l1,sizeY/2)
708 print(t1)
709end
710
711function loadApiKey()
712 local f = io.open(apiKeyPath)
713 if f == nil then
714 programSerProblem("API key not found")
715 else
716 apiKey = f:read()
717 f:close()
718 end
719end
720
721--main loop
722loadApiKey()
723getInterface()
724shell.execute("/home/closeDoor.lua")
725drawLogin()
726if endSeason == false then
727 getAccounts()
728 while backToAccounts do
729 backToAccounts = false
730 drawAccounts()
731 selectAction()
732 if action == "deposit" then
733 deposit()
734 elseif action == "withdraw" then
735 withdraw()
736 else
737 programSerProblem("There was a problem with the action selection")
738 end
739 end
740end
741
742shell.execute("/home/openDoor.lua")
743printGoodbye()
744os.sleep(1)
745setCursor(1,1)
746print("Program end")
747--shell.execute("reboot")