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