· 4 years ago · Jun 04, 2021, 09:28 PM
1local com = require("component")
2local term = require("term")
3local gpu = term.gpu()
4local web = require("internet")
5local json = require("json")
6local event = require("event")
7local shell = require("shell")
8
9--Variables
10local apiKeyPath = "home/key/apiKey"
11local mainURL ="https://alteran.de"
12local userName = nil
13local userKey = nil
14local accounts = nil
15local apiKey = nil
16local dBack = 0x365991
17local tBack = 0x545454
18local sizeX, sizeY = term.getViewport()
19
20--functions
21function programSerProblem(problem)
22 local err = "There was an Error"
23 local err2 = "Please inform the bank staff"
24 local err3 = "Problem: "..problem
25 local l1 = string.len(err)/2
26 local l2 = string.len(err2)/2
27 local l3 = string.len(err3)/2
28 gpu.setBackground(0xe01c07, false)
29 term.clear()
30 setCursor(sizeX/2-l1,sizeY/2-3)
31 print(err)
32 setCursor(sizeX/2-l2,sizeY/2-1)
33 print(err2)
34 setCursor(sizeX/2-l3,sizeY/2+1)
35 print(err3)
36 os.sleep(8)
37 os.execute("reboot")
38end
39
40function programProblem(problem)
41 local l = string.len(problem)/2
42 gpu.setBackground(0xe01c07, false)
43 term.clear()
44 setCursor(sizeX/2-l,sizeY/2)
45 print(problem)
46 os.sleep(5)
47 os.execute("reboot")
48end
49
50function getApiData(path)
51 local handle = internet.request(mainURL.."/"..apiKey.."/"..path)
52 local result = ""
53 for chunk in handle do result = result..chunk end
54
55 local mt = getmetatable(handle)
56 local code, message, headers = mt.__index.response()
57 if code == 200 then
58 return json.decode(result)
59 end
60end
61
62function setCursor(x,y)
63 term.setCursor(x,y)
64end
65
66function loadApiKey()
67 local f = io.open(apiKeyPath)
68 if f == nil then
69 programProblem("API key not found")
70 else
71 apiKey = f:read()
72 f:close()
73 end
74end
75
76function setBack(color)
77 gpu.setBackground(color, false)
78end
79
80function drawKeyButton(x,y,n)
81 setCursor(x,y)
82 print(" ")
83 setCursor(x,y+1)
84 print(" "..n.." ")
85 setCursor(x,y+2)
86 print(" ")
87end
88
89function drawKeyPad(x,y)
90 setBack(0x593799)--odd Buttons
91 drawKeyButton(x,y,"1")
92 drawKeyButton(x+8,y,"3")
93 drawKeyButton(x+4,y+3,"5")
94 drawKeyButton(x,y+6,"7")
95 drawKeyButton(x+8,y+6,"9")
96 drawKeyButton(x+4,y+9,"0")
97
98 setBack(0x3e2869)--even Buttons
99 drawKeyButton(x+4,y,"2")
100 drawKeyButton(x,y+3,"4")
101 drawKeyButton(x+8,y+3,"6")
102 drawKeyButton(x+4,y+6,"8")
103
104 setBack(0xde1310)--cancle
105 drawKeyButton(x,y+9,"X")
106
107 setBack(0x0cc456)--continue
108 drawKeyButton(x+8,y+9,"O")
109end
110
111function clearScreen()
112 setBack(dBack)
113 term.clear()
114 setCursor(1,1)
115end
116
117function getAccounts()
118 local out = getApiData("accounts/"..userName.."/"..userKey)
119 if out.accounts == nil then
120 programSerProblem("Your user account has no associated bank accounts.")
121 elif out.accounts ~= nil then
122 accounts = out.accounts
123 else
124 programSerProblem("There was an error receiving your account data")
125 end
126end
127
128function drawLogin()
129 local text1 = "Please login to your account"
130 local len1 = string.len(text1)
131 local name = ""
132 local pw = ""
133 while true do
134 local lenN = string.len(name)
135 local Nlen = 18-lenN
136 local lenP = string.len(pw)
137 local Plen = 9-lenP
138
139 clearScreen()
140 setCursor(sizeX/2-len1/2,sizeY/2-8)
141 print(text1)
142 setBack(tBack)
143 setCursor(sizeX/2-9,sizeY/2-4)
144
145 if userName == nil then
146 local name = "_"
147 local i = Nlen - 1
148
149 while i > 0 do
150 name = name .. " "
151 i = i-1
152 end
153 print(name)
154 setCursor(sizeX/2-4,sizeY/2-2)
155 i = Plen
156 local pw2 = ""
157
158 while i > 0 do
159 pw2 = pw2 .. " "
160 i = i-1
161 end
162 print(pw2)
163 else
164 local i = Nlen
165
166 while i > 0 do
167 name = name .. " "
168 i = i -1
169 end
170 print(name)
171 setCursor(sizeX/2-4, sizeY/2-2)
172 i = Plen
173 local pw2 = pw
174 while i > 0 do
175 pw2 = pw2 .. " "
176 i = i-1
177 end
178 print(pw2)
179 drawKeyPad(sizeX/2-5,sizeY-13)
180 end
181
182 local _,_,px,py,_,n = event.pull("touch")
183
184 if userName == nil then
185 userName = n
186 name = n
187 else
188 setCursor(sizeX/2, sizeY-1)
189 print(px,py)
190 if px >= 25 and px <= 27 and py >= 12 and py <= 14 then
191 pw = pw .. "1"
192 elseif px >= 29 and px <= 31 and py >= 12 and py <= 14 then
193 pw = pw .. "2"
194 elseif px >= 33 and px <= 35 and py >= 12 and py <= 14 then
195 pw = pw .. "3"
196 elseif px >= 25 and px <= 27 and py >= 15 and py <= 17 then
197 pw = pw .. "4"
198 elseif px >= 29 and px <= 31 and py >= 15 and py <= 17 then
199 pw = pw .. "5"
200 elseif px >= 33 and px <= 35 and py >= 15 and py <= 17 then
201 pw = pw .. "6"
202 elseif px >= 25 and px <= 27 and py >= 18 and py <= 20 then
203 pw = pw .. "7"
204 elseif px >= 29 and px <= 31 and py >= 18 and py <= 20 then
205 pw = pw .. "8"
206 elseif px >= 33 and px <= 35 and py >= 18 and py <= 20 then
207 pw = pw .. "9"
208 elseif px >= 25 and px <= 27 and py >= 21 and py <= 23 then
209 if pw == "" then
210 print("log off")
211 os.sleep(2)
212 os.execute("reboot")
213 else
214 pw = ""
215 end
216 elseif px >= 29 and px <= 31 and py >= 21 and py <= 23 then
217 pw = pw .. "0"
218 elseif px >= 33 and px <= 35 and py >= 21 and py <= 23 then
219 local resp = getApiData("login/"..userName.."/"..pw)
220 if resp.key == false then
221 programSerProblem("The API Key is not/no longer valid")
222 end
223 if resp.account == nil then
224 programProblem("You do not have an account yet!")
225 elif resp.account == "wrongPin" then
226 local err = " Wrong Pin "
227 local er2 = " "
228 local lerr = string.len(err)/2
229 setCursor(sizeX/2-lerr, sizeY/2-1)
230 print(er2)
231 setCursor(sizeX/2-lerr, sizeY/2)
232 print(err)
233 setCursor(sizeX/2-lerr, sizeY/2+1)
234 print(er2)
235 os.sleep(2)
236 elif resp.account:find("^U000") ~= nil then
237 userKey = resp.accAuth
238 break
239 end
240 end
241 end
242 end
243end
244
245
246
247--main loop
248loadApiKey()
249drawLogin()
250getAccounts()
251