diff --git a/atm.lua b/atm.lua index 3dc7428..ee0308b 100644 --- a/atm.lua +++ b/atm.lua @@ -18,35 +18,47 @@ local function drawFrame() g.drawText(term, 2, 1, "ATM", colors.white, colors.black) end +local function tryReadDiskCredentials(name) + if disk.hasData(name) then + local dataFile = fs.combine(disk.getMountPath(name), "bank-credentials.json") + if fs.exists(dataFile) then + local f = io.open(dataFile, "r") + local content = textutils.unserializeJSON(f:read("*a")) + f:close() + if ( + content ~= nil and + content.username and + type(content.username) == "string" and + content.password and + type(content.password) == "string" + ) then + return content + end + end + end + return nil +end + local function showLoginUI() drawFrame() g.drawTextCenter(term, W/2, 3, "Welcome to HandieBank ATM!", colors.green, colors.white) g.drawTextCenter(term, W/2, 5, "Insert your card below, or click to login.", colors.black, colors.white) - g.fillRect(term, W/2 - 3, 7, 9, 3, colors.green) + g.fillRect(term, 22, 7, 9, 3, colors.green) g.drawTextCenter(term, W/2, 8, "Login", colors.white, colors.green) while true do local event, p1, p2, p3 = os.pullEvent() if event == "disk" then - local side = p1 - if disk.hasData(side) then - local mountPath = disk.getMountPath(side) - local dataFile = fs.combine(disk.getMountPath(side), "bank-credentials.json") - if fs.exists(dataFile) then - local f = io.open(dataFile, "r") - local content = f:read("*a") - f:close() - return textutils.unserializeJSON(content) - else - disk.eject(side) - end + local credentials = tryReadDiskCredentials(p1) + if credentials then + return credentials else - disk.eject(side) + disk.eject(p1) end elseif event == "mouse_click" then local button = p1 local x = p2 local y = p3 - if button == 1 and x >= (W/2 - 3) and x <= (W/2 + 4) and y >= 7 and y <= 9 then + if button == 1 and x >= 22 and x <= 30 and y >= 7 and y <= 9 then -- TODO: Show login input elements. return {username = "bleh", password = "bleh"} end @@ -57,5 +69,6 @@ end while true do local credentials = showLoginUI() g.clear(term, colors.black) + print("Credentials: " .. textutils.serialize(credentials)) return end \ No newline at end of file