socket.lua

os.loadAPI("./LuaUtils.lua")
os.loadAPI("./button.lua")
print("Util loaded")
local ws,err = http.websocket("ws://b64af8815a46.ngrok.io")
local mon = peripheral.wrap("top")
print("Websocket and monitor cakked")
mon.clear()
button.setMonitor(mon)
print("monitor cleared")

if ws then
    while true do
        mon.setCursorPos(1,1)
        mon.write("Please drop your card to the turtle")
        local succ = turtle.suck()
        if succ then
            turtle.dropDown()
            if fs.exists("disk/carddata.txt") then
                local file = fs.open("disk/carddata.txt", "r")
                local fileData = file.readAll()
                ws.send("@coin/"..fileData)
                local message,binary = ws.receive()
                local data = string.split(message,",")
                print("Coin Data:")
                print("User: "..data[1].."/ Coins: "..data[2])
                mon.clear()
                mon.setCursorPos(1,1)
                mon.write("User: "..data[1])
                mon.setCursorPos(1,2)
                mon.write("Coins: "..data[2])
                disk.setLabel("bottom","Credit Card - "..data[2].."$")
                local exitButton = button.create("Exit")
                exitButton.setPos(1,3)
                exitButton.onClick(function() 
                    turtle.suckDown()
                    turtle.drop()
                    mon.clear()
                    mon.setCursorPos(1,1)
                    mon.write("Thank you for using us!")
                    sleep(4)
                    mon.clear()
                end)
                button.await(exitButton)
            else
                turtle.suckDown()
                turtle.drop()
                sleep(4)
            end
        end
    end
    
    ws.close()
end