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