socket.lua

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


if ws then
    while true do
        mon.setTextColor(1)
        mon.setBackgroundColor(2)
        sleep(0.1)
        numpadmon.clear()
        mon.clear()
        mon.setCursorPos(1,1)
        mon.write("Please drop your")
        mon.setCursorPos(1,2)
        mon.write("card to the turtle")
        local succ = turtle.suck()
        if succ then
            turtle.dropDown()
            print("disk inserted")
            if fs.exists("disk/carddata.txt") then
                print("data file found")
                local file = fs.open("disk/carddata.txt", "r")
                local fileData = file.readAll()
                ws.send("@coin/"..fileData)
                print("sent data")
                local message,binary = ws.receive()
                print("recieved data")
                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")
                local dButton = button.create("Deposit")
                local addButton = button.create("Add Money")
                exitButton.setPos(1,1)
                dButton.setPos(1,2)
                addButton.setPos(1,3)
                exitButton.onClick(function() 
                    numpadmon.clear()
                    turtle.suckDown()
                    turtle.drop()
                    mon.clear()
                    mon.setCursorPos(1,1)
                    mon.write("Thank you for")
                    mon.setCursorPos(1,2)
                    mon.write("using us!")
                    sleep(4)
                    mon.clear()
                end)
                dButton.onClick(function() 
                    numpadmon.clear()
                    mon.clear()
                    mon.setCursorPos(1,1)
                    mon.write("Numpad test started")
                    sleep(4)
                    mon.setCursorPos(1,2)
                    local num1b = button.create("1")
                    local num2b = button.create("2")
                    num2b.setPos(2,1)
                    button.await(num1b,num2b)
                    
                end)
                addButton.onClick(function() 
                    mon.clear()
                    numpadmon.clear()
                    ws.send("@addmoney/"..fileData.."/1")
                    mon.write("Added Money!")
                    local exit = button.create("Exit")
                    exit.setPos(1,1)
                    exit.onClick(function() 
                        turtle.suckDown()
                        turtle.drop()
                        sleep(4)
                        mon.clear()
                        numpadmon.clear()
                    end)
                    button.await(exit)
                end)
                button.await(exitButton, dButton, addButton)
            else
                mon.clear()
                mon.setCursorPos(1,1)
                mon.write("Creating user")
                local carddata = fs.open("disk/carddata.txt", "w")
                all = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
                e = ""
                
                for i = 1, 6 do
                    r = math.random(#all)
                    e = e..string.sub(all,r,r)
                end
                carddata.write(e)
                numpadmon.clear()
                numpadmon.setCursorPos(1,1)
                numpadmon.write(e)
                carddata.close()
                mon.clear()
                mon.setCursorPos(1,1)
                mon.write("Created!")
                turtle.suckDown()
                turtle.drop()
                sleep(4)
                mon.clear()
                numpadmon.clear()
            end
        end
    end
    
    ws.close()
end