Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- version = 0.5
- if not fs.isDir("/apis") then
- fs.delete("/apis/")
- fs.makeDir("/apis/")
- shell.run("pastebin get 9E5UHiqv /apis/AES")
- shell.run("pastebin get QYvNKrXE /apis/Base64")
- end
- if not fs.exists("/apis/AES") then
- shell.run("pastebin get 9E5UHiqv /apis/AES")
- end
- if not fs.exists("/apis/Base64") then
- shell.run("pastebin get QYvNKrXE /apis/Base64")
- end
- os.loadAPI("/apis/AES")
- os.loadAPI("/apis/Base64")
- modem = nil
- browserID = "storm"
- for k, v in pairs(peripheral.getNames()) do
- if peripheral.getType(v) == "modem" then
- modem = peripheral.wrap(v)
- end
- end
- if not modem then
- error("This program requires a modem!")
- else
- modem.open(24680)
- end
- function decode(msg)
- de_msg = AES.decrypt(tostring(client_id)..tostring(distance)..tostring(server_sent),Base64.decode(msg))
- server_sent = server_sent +1
- return de_msg
- end
- function eSend(tmsg,domain,msg)
- the_key = tostring(client_id) .. tostring(distance) .. tostring(sent)
- messageToSend = Base64.encode(AES.encrypt(the_key,tmsg.." "..msg))
- sent = sent+1
- modem.transmit(24680,client_id,"E"..domain.."|"..messageToSend)
- end
- function send(tmsg,msg)
- sent = sent+1
- modem.transmit(24680,client_id,tmsg.." "..msg)
- end
- function connect(address)
- server_sent = 0
- sent = 0
- client_id = math.random(1000,31337)
- send("PING",address)
- timer_id = os.startTimer(2)
- exit_loop = false
- while not exit_loop do
- evnt = {os.pullEvent()}
- if evnt[1] == "timer" and evnt[2] == timer_id then
- return false -- time out
- elseif evnt[1] == "modem_message" and evnt[4] == client_id then
- msg = evnt[5]
- msg_words = {}
- for w in msg:gmatch("%S+") do
- table.insert(msg_words,w)
- end
- if msg_words[1] and msg_words[1] == "Pong" then
- if msg_words[2] and msg_words[2] == "Pong!" then
- distance = evnt[6]
- exit_loop = true
- end
- end
- end
- end
- eSend("EPING",address,"ENCRYPTED PING")
- exit_loop = false
- timer_id = os.startTimer(2)
- while not exit_loop do
- evnt = {os.pullEvent()}
- if evnt[1] == "timer" and evnt[2] == timer_id then
- return false
- elseif evnt[1] == "modem_message" then
- if evnt[4] == client_id then
- if evnt[5]:sub(1,1) == "E" then
- msg = decode(evnt[5]:sub(2,#evnt[5]))
- msg_words = {}
- for w in msg:gmatch("%S+") do
- table.insert(msg_words,w)
- end
- if msg_words[2] and msg_words[2] == "EPONG" then
- if msg_words[3] and msg_words[4] and msg_words[4] == "ENCRYPTED" and msg_words[4] == "PONG" then
- return true
- end
- else
- return false
- end
- else
- end
- end
- end
- end
- end
- fConnect = false
- function getPage(dmn,page)
- if not fConnect then
- connect(dmn)
- fConnect = true
- end
- eSend("DATA",dmn,page)
- exit_loop = false
- timer_id = os.startTimer(2)
- while not exit_loop do
- evnt = {os.pullEvent()}
- if evnt[1] == "timer" and evnt[2] == timer_id then
- return false
- elseif evnt[1] == "modem_message" then
- if evnt[4] == client_id then
- if evnt[5]:sub(1,1) == "E" then
- msg = decode(evnt[5]:sub(2,#evnt[5]))
- if msg:sub(1,9) == "200 PDATA" then
- page_data = msg:sub(11,#msg)
- exit_loop = true
- return page_data, tonumber(msg:sub(1,3))
- elseif msg:sub(1,9) == "400 PDATA" then
- page_data = msg:sub(11,#msg)
- exit_loop = true
- return page_data, tonumber(msg:sub(1,3))
- elseif msg:sub(1,9) == "404 PDATA" then
- page_data = msg:sub(11,#msg)
- exit_llop = true
- return page_data, tonumber(msg:sub(1,3))
- end
- end
- end
- end
- end
- end
- function cWrite(text)
- curx,cury = term.getCursorPos()
- term.setCursorPos(cx - (#text/2), cury)
- write(text)
- end
- function login()
- resp_code = 400
- while resp_code == 400 do
- term.setBackgroundColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.black)
- cWrite("Login")
- term.setCursorPos(1,cy-1)
- cWrite("Username: ")
- term.setCursorPos(1,scry-1)
- cWrite("Type Exit to exit")
- term.setCursorPos(1,cy+1)
- cWrite("Password: ")
- term.setCursorPos(1,cy-1)
- cWrite("Username: ")
- username = read()
- if username:lower() == "exit" then
- action = 3
- break
- end
- term.setCursorPos(1,cy+1)
- cWrite("Password: ")
- password = read("*")
- if password:lower() == "exit" then
- action = 3
- break
- end
- resp, resp_code = getPage("roll","login " .. username .. " " .. password)
- if resp_code == 400 then
- term.setCursorPos(1,cy+3)
- cWrite("Login failed.")
- sleep(2)
- end
- end
- end
- function register()
- resp_code = 400
- while resp_code == 400 do
- term.setBackgroundColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.black)
- cWrite("Register")
- term.setCursorPos(1,cy-2)
- cWrite("Username: ")
- term.setCursorPos(1,cy)
- cWrite("Password: ")
- term.setCursorPos(1,cy+2)
- cWrite("Repeat Password: ")
- term.setCursorPos(1,scry-1)
- cWrite("Type Exit to exit")
- term.setCursorPos(1,cy-2)
- cWrite("Username: ")
- username = read()
- if username:lower() == "exit" then
- action = 3
- break
- end
- term.setCursorPos(1,cy)
- cWrite("Password: ")
- password = read("*")
- if password:lower() == "exit" then
- action = 3
- break
- end
- term.setCursorPos(1,cy+2)
- cWrite("Repeat Password: ")
- if read("*") == password then
- resp, resp_code = getPage("roll","register " .. username .. " " .. password)
- if resp_code == 400 then
- term.setCursorPos(1,cy+4)
- cWrite("Username Taken.")
- sleep(2)
- end
- else
- term.setCursorPos(1,cy+4)
- cWrite("Passwords do not match")
- sleep(2)
- end
- end
- end
- function drawBar()
- buttons[#buttons+1] = {"Home",1,8,3,3}
- buttons[#buttons+1] = {"HL",1,8,5,5}
- buttons[#buttons+1] = {"Deposit",1,8,7,7}
- buttons[#buttons+1] = {"Withdraw",1,8,9,9}
- buttons[#buttons+1] = {"Exit",1,8,scry,scry}
- term.setBackgroundColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.black)
- cWrite("Casino")
- term.setBackgroundColor(colors.red)
- term.setCursorPos(1,3)
- term.setTextColor(colors.black)
- write("Home ")
- term.setCursorPos(1,5)
- term.setBackgroundColor(colors.blue)
- write("High/Low")
- term.setCursorPos(1,7)
- term.setBackgroundColor(colors.lime)
- write("Deposit ")
- term.setCursorPos(1,9)
- term.setBackgroundColor(colors.orange)
- write("Withdraw")
- term.setCursorPos(1,scry)
- term.setBackgroundColor(colors.gray)
- write("Exit ")
- for i = 1, scry do
- term.setCursorPos(9,i)
- term.setBackgroundColor(colors.white)
- write("|")
- end
- end
- function drawMain()
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(1,3)
- cWrite(username)
- term.setCursorPos(1,4)
- bal, bal_code = getPage("roll","get_bal")
- if bal_code and bal_code == 200 then
- cWrite("Balance: " .. bal .. "kst")
- end
- end
- function drawHL()
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(10,2)
- term.setTextColor(colors.lightGray)
- --hash, hash_code = getPage("roll","get_hash")
- --if hash and hash_code == 200 then
- -- write(hash)
- --end
- term.setCursorPos(scrx-3,1)
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- write("Hash")
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(1,3)
- cWrite("High/Low")
- term.setCursorPos(1,4)
- cWrite(username)
- term.setCursorPos(10,5)
- write("To win, bet high or low")
- term.setCursorPos(10,6)
- write("If high you must get higher than 141")
- term.setCursorPos(10,7)
- write("If low you must get lower than 115")
- term.setCursorPos(1,8)
- bal, bal_code = getPage("roll","get_bal")
- if bal_code and bal_code == 200 then
- cWrite("Your balance: " .. bal .. "kst")
- end
- term.setBackgroundColor(colors.orange)
- term.setTextColor(colors.white)
- term.setCursorPos(1,11)
- cWrite("Bet High")
- term.setCursorPos(1,12)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.orange)
- cWrite("--------")
- term.setCursorPos(1,13)
- term.setBackgroundColor(colors.orange)
- term.setTextColor(colors.white)
- cWrite("Bet Low ")
- term.setCursorPos(1,15)
- term.setBackgroundColor(colors.gray)
- if not bet_amount then
- bet_amount = 1
- end
- if #tostring(bet_amount) <= 6 then
- cWrite("Amount: " .. tostring(bet_amount) .. string.rep(" ",7-#tostring(bet_amount)))
- else
- cWrite("Amount: " .. tostring(bet_amount))
- end
- term.setCursorPos(1,17)
- bet_key = rString(6)
- cWrite("Client Key: " .. bet_key)
- term.setCursorPos(10,15)
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.white)
- write("x2")
- term.setCursorPos(13,15)
- write("/2")
- buttons[#buttons+1] = {"bet_high",math.floor(cx-(#"Bet High"/2)),math.ceil(cx+(#"Bet High"/2)),11,11}
- buttons[#buttons+1] = {"bet_low",math.floor(cx-(#"Bet Low "/2)),math.ceil(cx+(#"Bet Low "/2)),13,13}
- buttons[#buttons+1] = {"bet_amount",math.floor(cx-(#"Amount: "/2)),math.ceil(cx+(#"Amount: "/2)),15,15}
- buttons[#buttons+1] = {"bet_key",math.floor(cx-(#"Client Key: 123456"/2)),math.ceil(cx+(#"Client Key: 123456"/2)),17,17}
- buttons[#buttons+1] = {"view_hash", scrx-3,scrx,1,1}
- buttons[#buttons+1] = {"*2", 10,11, 15, 15}
- buttons[#buttons+1] = {"/2", 13,14, 15, 15}
- end
- function drawHash()
- term.setCursorPos(scrx-3,1)
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.white)
- write("Back")
- term.setCursorPos(1,cy)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- cWrite("Server Key Hash: ")
- term.setCursorPos(10,cy+1)
- hash, hash_code = getPage("roll","get_hash")
- if hash and hash_code == 200 then
- term.setTextColor(colors.lightGray)
- write(hash:sub(1,scrx-11))
- term.setCursorPos(10,cy+2)
- write(hash:sub(scrx-10,#hash))
- term.setCursorPos(1,cy+3)
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.white)
- cWrite("Write to log")
- else
- print("Error")
- end
- buttons[#buttons+1] = {"HL",scrx-3,scrx,1,1}
- buttons[#buttons+1] = {"hash_log",math.floor(cx-(#"Write to log"/2)),math.ceil(cx+(#"Write to log"/2)),math.floor(cy+3),math.floor(cy+3)}
- end
- function casino()
- buttons = {}
- drawBar()
- drawMain()
- doGame = true
- while doGame do
- local event, ctype, clickx,clicky = os.pullEvent("mouse_click")
- for i = 1, #buttons do
- if clickx >= buttons[i][2] and clickx <= buttons[i][3] and clicky >= buttons[i][4] and clicky <= buttons[i][5] then
- --Clicked button named buttons[i][1]
- button_name = buttons[i][1]
- if button_name == "Home" then
- buttons = {}
- drawBar()
- drawMain()
- break
- elseif button_name == "HL" then
- --Start High/Low
- buttons = {}
- drawBar()
- drawHL()
- break
- elseif button_name == "Exit" then
- doGame = false
- break
- elseif button_name == "*2" then
- term.setCursorPos(1,15)
- term.setBackgroundColor(colors.gray)
- if not bet_amount then
- bet_amount = 1
- else
- bet_amount = bet_amount *2
- end
- if #tostring(bet_amount) <= 6 then
- cWrite("Amount: " .. tostring(bet_amount) .. string.rep(" ",7-#tostring(bet_amount)))
- else
- cWrite("Amount: " .. tostring(bet_amount))
- end
- elseif button_name == "/2" then
- term.setCursorPos(1,15)
- term.setBackgroundColor(colors.gray)
- if not bet_amount then
- bet_amount = 1
- else
- bet_amount = bet_amount /2
- end
- if #tostring(bet_amount) <= 6 then
- cWrite("Amount: " .. tostring(bet_amount) .. string.rep(" ",7-#tostring(bet_amount)))
- else
- cWrite("Amount: " .. tostring(bet_amount))
- end
- elseif button_name == "view_hash" then
- buttons = {}
- drawBar()
- drawHash()
- break
- elseif button_name == "hash_log" then
- f = fs.open("rlog","a")
- f.write("\n"..hash)
- f.close()
- break
- elseif button_name == "bet_high" or button_name == "bet_low" then
- dArgs = tostring(bet_amount)
- if button_name == "bet_high" then
- dArgs = dArgs .. " high"
- elseif button_name == "bet_low" then
- dArgs = dArgs .. " low"
- end
- dArgs = dArgs .. " "..bet_key
- roll, roll_code = getPage("roll","roll "..dArgs)
- if roll_code and roll_code == 200 then
- rDets = textutils.unserialize(roll)
- if rDets["win"] then
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.black)
- else
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.white)
- end
- term.setCursorPos(1,10)
- cWrite(tostring(rDets["roll"]))
- term.setCursorPos(10,18)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.lightGray)
- key = rDets["key_used"]
- key_a = key:sub(1,scrx-11)
- key_b = key:sub(scrx-10,#key)
- write(key_a)
- term.setCursorPos(10,19)
- write(key_b)
- f = fs.open("rlog","a")
- f.write("\n"..textutils.serialize(rDets))
- f.close()
- sleep(3)
- buttons = {}
- drawBar()
- drawHL()
- break
- else
- if not roll then
- term.setCursorPos(10,18)
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.white)
- write("Server offline!")
- else
- if roll == "bet must be a number >= 0" then
- term.setCursorPos(10,18)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- write("Invalid bet amount!")
- elseif roll == "Must guess high or low" then
- term.setCursorPos(10,18)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- write("Must guess high or low!")
- end
- end
- sleep(3)
- buttons = {}
- drawBar()
- drawHL()
- break
- end
- elseif button_name == "bet_key" then
- term.setBackgroundColor(colors.white)
- term.setCursorPos(1,17)
- write(string.rep(" ",scrx))
- term.setTextColor(colors.black)
- term.setCursorPos(9,17)
- write("|")
- term.setCursorPos(1,17)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.gray)
- cWrite("Client Key: ")
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(cx-(#"Client Key: 123456"/2)+#"Client Key: ",17)
- bet_key = read()
- break
- elseif button_name == "bet_amount" then
- term.setBackgroundColor(colors.white)
- term.setCursorPos(1,15)
- write(string.rep(" ",scrx))
- term.setTextColor(colors.black)
- term.setCursorPos(9,15)
- write("|")
- term.setCursorPos(10,15)
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.white)
- write("x2")
- term.setCursorPos(13,15)
- write("/2")
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(1,15)
- if #tostring(bet_amount) <= 6 then
- cWrite("Amount: " .. string.rep(" ",7))
- else
- cWrite("Amount: " .. tostring(" "))
- end
- term.setCursorPos(cx+1,15)
- bet_amount = read()
- term.setCursorPos(1,15)
- if #tostring(bet_amount) <= 6 then
- cWrite("Amount: " .. tostring(bet_amount) .. string.rep(" ",7-#tostring(bet_amount)))
- else
- cWrite("Amount: " .. tostring(bet_amount))
- end
- break
- end
- end
- end
- end
- end
- function rString(length)
- local chars = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"}
- random_string = ""
- for i = 1, length do
- random_string = random_string .. chars[math.random(1,#chars)]
- end
- return random_string
- end
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.clear()
- scrx,scry = term.getSize()
- cx, cy = scrx/2, scry/2
- check,check_code = getPage("roll","check")
- if check_code and check_code == 200 then
- term.setCursorPos(cx-#"Krist Casino"/2,cy-2)
- print("Krist Casino")
- term.setCursorPos(cx-4,math.floor(cy))
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- cWrite(" Login ")
- term.setCursorPos(1,math.floor(cy+2))
- cWrite(" Register")
- term.setCursorPos(1,math.floor(scry-1))
- cWrite(" Exit ")
- action = -1
- while true do
- local event, ctype, clickx, clicky = os.pullEvent("mouse_click")
- if clicky == math.floor(cy) and clickx >= math.floor(cx-4) and clicky <= math.floor(cx+4) then
- action = 1
- elseif clicky == math.floor(cy+2) and clickx >= math.floor(cx-4) and clicky <= math.floor(cx+4) then
- action = 2
- elseif clicky == math.floor(scry-1) and clickx >= math.floor(cx-4) and clicky <= math.floor(cx+4) then
- action = 3
- end
- if action > 0 then
- break
- end
- end
- if action == 1 then
- login()
- elseif action == 2 then
- register()
- end
- if action <= 2 then
- casino()
- end
- else
- print("Couldn't connect to server!")
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(1,1)
- print("Thanks for playing!")
Advertisement
Add Comment
Please, Sign In to add comment