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