Alyssa

Krist Casino Client

Feb 4th, 2016
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.90 KB | None | 0 0
  1. version = 0.5
  2. if not fs.isDir("/apis") then
  3.         fs.delete("/apis/")
  4.         fs.makeDir("/apis/")
  5.         shell.run("pastebin get 9E5UHiqv /apis/AES")
  6.         shell.run("pastebin get QYvNKrXE /apis/Base64")
  7. end
  8. if not fs.exists("/apis/AES") then
  9.         shell.run("pastebin get 9E5UHiqv /apis/AES")
  10. end
  11. if not fs.exists("/apis/Base64") then
  12.         shell.run("pastebin get QYvNKrXE /apis/Base64")
  13. end
  14. os.loadAPI("/apis/AES")
  15. os.loadAPI("/apis/Base64")
  16. modem = nil
  17. browserID = "storm"
  18. for k, v in pairs(peripheral.getNames()) do
  19.         if peripheral.getType(v) == "modem" then
  20.                 modem = peripheral.wrap(v)
  21.         end
  22. end
  23. if not modem then
  24.         error("This program requires a modem!")
  25. else
  26.         modem.open(24680)
  27. end
  28. function decode(msg)
  29.     de_msg = AES.decrypt(tostring(client_id)..tostring(distance)..tostring(server_sent),Base64.decode(msg))
  30.     server_sent = server_sent +1
  31.     return de_msg
  32. end
  33. function eSend(tmsg,domain,msg)
  34.     the_key = tostring(client_id) .. tostring(distance) .. tostring(sent)
  35.     messageToSend = Base64.encode(AES.encrypt(the_key,tmsg.." "..msg))
  36.     sent = sent+1
  37.     modem.transmit(24680,client_id,"E"..domain.."|"..messageToSend)
  38. end
  39. function send(tmsg,msg)
  40.     sent = sent+1
  41.     modem.transmit(24680,client_id,tmsg.." "..msg)
  42. end
  43. function connect(address)
  44.     server_sent = 0
  45.     sent = 0
  46.     client_id = math.random(1000,31337)
  47.     send("PING",address)
  48.     timer_id = os.startTimer(2)
  49.     exit_loop = false
  50.     while not exit_loop do
  51.         evnt = {os.pullEvent()}
  52.         if evnt[1] == "timer" and evnt[2] == timer_id then
  53.             return false -- time out
  54.         elseif evnt[1] == "modem_message" and evnt[4] == client_id then
  55.             msg = evnt[5]
  56.             msg_words = {}
  57.             for w in msg:gmatch("%S+") do
  58.                 table.insert(msg_words,w)
  59.             end
  60.             if msg_words[1] and msg_words[1] == "Pong" then
  61.                 if msg_words[2] and msg_words[2] == "Pong!" then
  62.                     distance = evnt[6]
  63.                     exit_loop = true
  64.                 end
  65.             end
  66.         end
  67.     end
  68.     eSend("EPING",address,"ENCRYPTED PING")
  69.     exit_loop = false
  70.     timer_id = os.startTimer(2)
  71.     while not exit_loop do
  72.         evnt = {os.pullEvent()}
  73.         if evnt[1] == "timer" and evnt[2] == timer_id then
  74.             return false
  75.         elseif evnt[1] == "modem_message" then
  76.             if evnt[4] == client_id then
  77.                 if evnt[5]:sub(1,1) == "E" then
  78.                     msg = decode(evnt[5]:sub(2,#evnt[5]))
  79.                     msg_words = {}
  80.                     for w in msg:gmatch("%S+") do
  81.                         table.insert(msg_words,w)
  82.                     end
  83.                     if msg_words[2] and msg_words[2] == "EPONG" then
  84.                         if msg_words[3] and msg_words[4] and msg_words[4] == "ENCRYPTED" and msg_words[4] == "PONG" then
  85.                             return true
  86.                         end
  87.                     else
  88.                         return false
  89.                     end
  90.                 else
  91.                 end
  92.             end
  93.         end
  94.     end
  95. end
  96. fConnect = false
  97. function getPage(dmn,page)
  98.     if not fConnect then
  99.         connect(dmn)
  100.         fConnect = true
  101.     end
  102.     eSend("DATA",dmn,page)
  103.     exit_loop = false
  104.     timer_id = os.startTimer(2)
  105.     while not exit_loop do
  106.         evnt = {os.pullEvent()}
  107.         if evnt[1] == "timer" and evnt[2] == timer_id then
  108.             return false
  109.         elseif evnt[1] == "modem_message" then
  110.             if evnt[4] == client_id then
  111.                 if evnt[5]:sub(1,1) == "E" then
  112.                     msg = decode(evnt[5]:sub(2,#evnt[5]))
  113.                     if msg:sub(1,9) == "200 PDATA" then
  114.                         page_data = msg:sub(11,#msg)
  115.                         exit_loop = true
  116.                         return page_data, tonumber(msg:sub(1,3))
  117.                     elseif msg:sub(1,9) == "400 PDATA" then
  118.                         page_data = msg:sub(11,#msg)
  119.                         exit_loop = true
  120.                         return page_data, tonumber(msg:sub(1,3))
  121.                     elseif msg:sub(1,9) == "404 PDATA" then
  122.                         page_data = msg:sub(11,#msg)
  123.                         exit_llop = true
  124.                         return page_data, tonumber(msg:sub(1,3))
  125.                     end
  126.                 end
  127.             end
  128.         end
  129.     end
  130. end
  131. function cWrite(text)
  132.     curx,cury = term.getCursorPos()
  133.     term.setCursorPos(cx - (#text/2), cury)
  134.     write(text)
  135. end
  136. function login()
  137.     resp_code = 400
  138.     while resp_code == 400 do
  139.         term.setBackgroundColor(colors.white)
  140.         term.clear()
  141.         term.setCursorPos(1,1)
  142.         term.setTextColor(colors.black)
  143.         cWrite("Login")
  144.         term.setCursorPos(1,cy-1)
  145.         cWrite("Username: ")
  146.         term.setCursorPos(1,scry-1)
  147.         cWrite("Type Exit to exit")
  148.         term.setCursorPos(1,cy+1)
  149.         cWrite("Password: ")
  150.         term.setCursorPos(1,cy-1)
  151.         cWrite("Username: ")
  152.         username = read()
  153.         if username:lower() == "exit" then
  154.             action = 3
  155.             break
  156.         end
  157.         term.setCursorPos(1,cy+1)
  158.         cWrite("Password: ")
  159.         password = read("*")
  160.         if password:lower() == "exit" then
  161.             action = 3
  162.             break
  163.         end
  164.         resp, resp_code = getPage("roll","login " .. username .. " " .. password)
  165.         if resp_code == 400 then
  166.             term.setCursorPos(1,cy+3)
  167.             cWrite("Login failed.")
  168.             sleep(2)
  169.         end
  170.     end
  171. end
  172. function register()
  173.     resp_code = 400
  174.     while resp_code == 400 do
  175.         term.setBackgroundColor(colors.white)
  176.         term.clear()
  177.         term.setCursorPos(1,1)
  178.         term.setTextColor(colors.black)
  179.         cWrite("Register")
  180.         term.setCursorPos(1,cy-2)
  181.         cWrite("Username: ")
  182.         term.setCursorPos(1,cy)
  183.         cWrite("Password: ")
  184.         term.setCursorPos(1,cy+2)
  185.         cWrite("Repeat Password: ")
  186.         term.setCursorPos(1,scry-1)
  187.         cWrite("Type Exit to exit")
  188.         term.setCursorPos(1,cy-2)
  189.         cWrite("Username: ")
  190.         username = read()
  191.         if username:lower() == "exit" then
  192.             action = 3
  193.             break
  194.         end
  195.         term.setCursorPos(1,cy)
  196.         cWrite("Password: ")
  197.         password = read("*")
  198.         if password:lower() == "exit" then
  199.             action = 3
  200.             break
  201.         end
  202.         term.setCursorPos(1,cy+2)
  203.         cWrite("Repeat Password: ")
  204.         if read("*") == password then
  205.             resp, resp_code = getPage("roll","register " .. username .. " " .. password)
  206.             if resp_code == 400 then
  207.                 term.setCursorPos(1,cy+4)
  208.                 cWrite("Username Taken.")
  209.                 sleep(2)
  210.             end
  211.         else
  212.             term.setCursorPos(1,cy+4)
  213.             cWrite("Passwords do not match")
  214.             sleep(2)
  215.         end
  216.     end
  217. end
  218. function drawBar()
  219.     buttons[#buttons+1] = {"Home",1,8,3,3}
  220.     buttons[#buttons+1] = {"HL",1,8,5,5}
  221.     buttons[#buttons+1] = {"Deposit",1,8,7,7}
  222.     buttons[#buttons+1] = {"Withdraw",1,8,9,9}
  223.     buttons[#buttons+1] = {"Exit",1,8,scry,scry}
  224.     term.setBackgroundColor(colors.white)
  225.     term.clear()
  226.     term.setCursorPos(1,1)
  227.     term.setTextColor(colors.black)
  228.     cWrite("Casino")
  229.     term.setBackgroundColor(colors.red)
  230.     term.setCursorPos(1,3)
  231.     term.setTextColor(colors.black)
  232.     write("Home    ")
  233.     term.setCursorPos(1,5)
  234.     term.setBackgroundColor(colors.blue)
  235.     write("High/Low")
  236.     term.setCursorPos(1,7)
  237.     term.setBackgroundColor(colors.lime)
  238.     write("Deposit ")
  239.     term.setCursorPos(1,9)
  240.     term.setBackgroundColor(colors.orange)
  241.     write("Withdraw")
  242.     term.setCursorPos(1,scry)
  243.     term.setBackgroundColor(colors.gray)
  244.     write("Exit    ")
  245.     for i = 1, scry do
  246.         term.setCursorPos(9,i)
  247.         term.setBackgroundColor(colors.white)
  248.         write("|")
  249.     end
  250. end
  251. function drawMain()
  252.     term.setBackgroundColor(colors.white)
  253.     term.setTextColor(colors.black)
  254.     term.setCursorPos(1,3)
  255.     cWrite(username)
  256.     term.setCursorPos(1,4)
  257.     bal, bal_code = getPage("roll","get_bal")
  258.     if bal_code and bal_code == 200 then
  259.         cWrite("Balance: " .. bal .. "kst")
  260.     end
  261. end
  262. function drawHL()
  263.     term.setBackgroundColor(colors.white)
  264.     term.setTextColor(colors.black)
  265.     term.setCursorPos(10,2)
  266.     term.setTextColor(colors.lightGray)
  267.     --hash, hash_code = getPage("roll","get_hash")
  268.     --if hash and hash_code == 200 then
  269.     --  write(hash)
  270.     --end
  271.     term.setCursorPos(scrx-3,1)
  272.     term.setBackgroundColor(colors.blue)
  273.     term.setTextColor(colors.white)
  274.     write("Hash")
  275.     term.setBackgroundColor(colors.white)
  276.     term.setTextColor(colors.black)
  277.     term.setCursorPos(1,3)
  278.     cWrite("High/Low")
  279.     term.setCursorPos(1,4)
  280.     cWrite(username)
  281.     term.setCursorPos(10,5)
  282.     write("To win, bet high or low")
  283.     term.setCursorPos(10,6)
  284.     write("If high you must get higher than 141")
  285.     term.setCursorPos(10,7)
  286.     write("If low you must get lower than 115")
  287.     term.setCursorPos(1,8)
  288.     bal, bal_code = getPage("roll","get_bal")
  289.     if bal_code and bal_code == 200 then
  290.         cWrite("Your balance: " .. bal .. "kst")
  291.     end
  292.     term.setBackgroundColor(colors.orange)
  293.     term.setTextColor(colors.white)
  294.     term.setCursorPos(1,11)
  295.     cWrite("Bet High")
  296.     term.setCursorPos(1,12)
  297.     term.setBackgroundColor(colors.white)
  298.     term.setTextColor(colors.orange)
  299.     cWrite("--------")
  300.     term.setCursorPos(1,13)
  301.     term.setBackgroundColor(colors.orange)
  302.     term.setTextColor(colors.white)
  303.     cWrite("Bet Low ")
  304.     term.setCursorPos(1,15)
  305.     term.setBackgroundColor(colors.gray)
  306.     if not bet_amount then
  307.         bet_amount = 1
  308.     end
  309.     if #tostring(bet_amount) <= 6 then
  310.         cWrite("Amount: " .. tostring(bet_amount) .. string.rep(" ",7-#tostring(bet_amount)))
  311.     else
  312.         cWrite("Amount: " .. tostring(bet_amount))
  313.     end
  314.     term.setCursorPos(1,17)
  315.     bet_key = rString(6)
  316.     cWrite("Client Key: " .. bet_key)
  317.     term.setCursorPos(10,15)
  318.     term.setBackgroundColor(colors.red)
  319.     term.setTextColor(colors.white)
  320.     write("x2")
  321.     term.setCursorPos(13,15)
  322.     write("/2")
  323.     buttons[#buttons+1] = {"bet_high",math.floor(cx-(#"Bet High"/2)),math.ceil(cx+(#"Bet High"/2)),11,11}
  324.     buttons[#buttons+1] = {"bet_low",math.floor(cx-(#"Bet Low "/2)),math.ceil(cx+(#"Bet Low "/2)),13,13}
  325.     buttons[#buttons+1] = {"bet_amount",math.floor(cx-(#"Amount:         "/2)),math.ceil(cx+(#"Amount:         "/2)),15,15}
  326.     buttons[#buttons+1] = {"bet_key",math.floor(cx-(#"Client Key: 123456"/2)),math.ceil(cx+(#"Client Key: 123456"/2)),17,17}
  327.     buttons[#buttons+1] = {"view_hash", scrx-3,scrx,1,1}
  328.     buttons[#buttons+1] = {"*2", 10,11, 15, 15}
  329.     buttons[#buttons+1] = {"/2", 13,14, 15, 15}
  330. end
  331. function drawHash()
  332.     term.setCursorPos(scrx-3,1)
  333.     term.setBackgroundColor(colors.red)
  334.     term.setTextColor(colors.white)
  335.     write("Back")
  336.     term.setCursorPos(1,cy)
  337.     term.setBackgroundColor(colors.white)
  338.     term.setTextColor(colors.black)
  339.     cWrite("Server Key Hash: ")
  340.     term.setCursorPos(10,cy+1)
  341.     hash, hash_code = getPage("roll","get_hash")
  342.     if hash and hash_code == 200 then
  343.         term.setTextColor(colors.lightGray)
  344.         write(hash:sub(1,scrx-11))
  345.         term.setCursorPos(10,cy+2)
  346.         write(hash:sub(scrx-10,#hash))
  347.         term.setCursorPos(1,cy+3)
  348.         term.setBackgroundColor(colors.green)
  349.         term.setTextColor(colors.white)
  350.         cWrite("Write to log")
  351.     else
  352.         print("Error")
  353.     end
  354.     buttons[#buttons+1] = {"HL",scrx-3,scrx,1,1}
  355.     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)}
  356. end
  357. function casino()
  358.     buttons = {}
  359.     drawBar()
  360.     drawMain()
  361.     doGame = true
  362.     while doGame do
  363.         local event, ctype, clickx,clicky = os.pullEvent("mouse_click")
  364.         for i = 1, #buttons do
  365.             if clickx >= buttons[i][2] and clickx <= buttons[i][3] and clicky >= buttons[i][4] and clicky <= buttons[i][5] then
  366.                 --Clicked button named buttons[i][1]
  367.                 button_name = buttons[i][1]
  368.                 if button_name == "Home" then
  369.                     buttons = {}
  370.                     drawBar()
  371.                     drawMain()
  372.                     break
  373.                 elseif button_name == "HL" then
  374.                     --Start High/Low
  375.                     buttons = {}
  376.                     drawBar()
  377.                     drawHL()
  378.                     break
  379.                 elseif button_name == "Exit" then
  380.                     doGame = false
  381.                     break
  382.                 elseif button_name == "*2" then
  383.                     term.setCursorPos(1,15)
  384.                     term.setBackgroundColor(colors.gray)
  385.                     if not bet_amount then
  386.                         bet_amount = 1
  387.                     else
  388.                         bet_amount = bet_amount *2
  389.                     end
  390.                     if #tostring(bet_amount) <= 6 then
  391.                         cWrite("Amount: " .. tostring(bet_amount) .. string.rep(" ",7-#tostring(bet_amount)))
  392.                     else
  393.                         cWrite("Amount: " .. tostring(bet_amount))
  394.                     end
  395.                 elseif button_name == "/2" then
  396.                     term.setCursorPos(1,15)
  397.                     term.setBackgroundColor(colors.gray)
  398.                     if not bet_amount then
  399.                         bet_amount = 1
  400.                     else
  401.                         bet_amount = bet_amount /2
  402.                     end
  403.                     if #tostring(bet_amount) <= 6 then
  404.                         cWrite("Amount: " .. tostring(bet_amount) .. string.rep(" ",7-#tostring(bet_amount)))
  405.                     else
  406.                         cWrite("Amount: " .. tostring(bet_amount))
  407.                     end
  408.                 elseif button_name == "view_hash" then
  409.                     buttons = {}
  410.                     drawBar()
  411.                     drawHash()
  412.                     break
  413.                 elseif button_name == "hash_log" then
  414.                     f = fs.open("rlog","a")
  415.                     f.write("\n"..hash)
  416.                     f.close()
  417.                     break
  418.                 elseif button_name == "bet_high" or button_name == "bet_low" then
  419.                     dArgs = tostring(bet_amount)
  420.                     if button_name == "bet_high" then
  421.                         dArgs = dArgs .. " high"
  422.                     elseif button_name == "bet_low" then
  423.                         dArgs = dArgs .. " low"
  424.                     end
  425.                     dArgs = dArgs .. " "..bet_key
  426.                     roll, roll_code = getPage("roll","roll "..dArgs)
  427.                     if roll_code and roll_code == 200 then
  428.                         rDets = textutils.unserialize(roll)
  429.                         if rDets["win"] then
  430.                             term.setBackgroundColor(colors.green)
  431.                             term.setTextColor(colors.black)
  432.                         else
  433.                             term.setBackgroundColor(colors.red)
  434.                             term.setTextColor(colors.white)
  435.                         end
  436.                         term.setCursorPos(1,10)
  437.                         cWrite(tostring(rDets["roll"]))
  438.                         term.setCursorPos(10,18)
  439.                         term.setBackgroundColor(colors.white)
  440.                         term.setTextColor(colors.lightGray)
  441.                         key = rDets["key_used"]
  442.                         key_a = key:sub(1,scrx-11)
  443.                         key_b = key:sub(scrx-10,#key)
  444.                         write(key_a)
  445.                         term.setCursorPos(10,19)
  446.                         write(key_b)
  447.                         f = fs.open("rlog","a")
  448.                         f.write("\n"..textutils.serialize(rDets))
  449.                         f.close()
  450.                         sleep(3)
  451.                         buttons = {}
  452.                         drawBar()
  453.                         drawHL()
  454.                         break
  455.                     else
  456.                         if not roll then
  457.                                 term.setCursorPos(10,18)
  458.                                 term.setBackgroundColor(colors.red)
  459.                                 term.setTextColor(colors.white)
  460.                                 write("Server offline!")
  461.                         else
  462.                             if roll == "bet must be a number >= 0" then
  463.                                 term.setCursorPos(10,18)
  464.                                 term.setBackgroundColor(colors.white)
  465.                                 term.setTextColor(colors.black)
  466.                                 write("Invalid bet amount!")
  467.                             elseif roll == "Must guess high or low" then
  468.                                 term.setCursorPos(10,18)
  469.                                 term.setBackgroundColor(colors.white)
  470.                                 term.setTextColor(colors.black)
  471.                                 write("Must guess high or low!")
  472.                             end
  473.                         end
  474.                         sleep(3)
  475.                         buttons = {}
  476.                         drawBar()
  477.                         drawHL()
  478.                         break
  479.                     end
  480.                 elseif button_name == "bet_key" then
  481.                     term.setBackgroundColor(colors.white)
  482.                     term.setCursorPos(1,17)
  483.                     write(string.rep(" ",scrx))
  484.                     term.setTextColor(colors.black)
  485.                     term.setCursorPos(9,17)
  486.                     write("|")
  487.                     term.setCursorPos(1,17)
  488.                     term.setTextColor(colors.white)
  489.                     term.setBackgroundColor(colors.gray)
  490.                     cWrite("Client Key:       ")
  491.                     term.setTextColor(colors.white)
  492.                     term.setBackgroundColor(colors.gray)
  493.                     term.setCursorPos(cx-(#"Client Key: 123456"/2)+#"Client Key: ",17)
  494.                     bet_key = read()
  495.                     break
  496.                 elseif button_name == "bet_amount" then
  497.                     term.setBackgroundColor(colors.white)
  498.                     term.setCursorPos(1,15)
  499.                     write(string.rep(" ",scrx))
  500.                     term.setTextColor(colors.black)
  501.                     term.setCursorPos(9,15)
  502.                     write("|")
  503.                     term.setCursorPos(10,15)
  504.                     term.setBackgroundColor(colors.red)
  505.                     term.setTextColor(colors.white)
  506.                     write("x2")
  507.                     term.setCursorPos(13,15)
  508.                     write("/2")
  509.                     term.setTextColor(colors.white)
  510.                     term.setBackgroundColor(colors.gray)
  511.                     term.setCursorPos(1,15)
  512.                     if #tostring(bet_amount) <= 6 then
  513.                         cWrite("Amount: " .. string.rep(" ",7))
  514.                     else
  515.                         cWrite("Amount: " .. tostring("       "))
  516.                     end
  517.                     term.setCursorPos(cx+1,15)
  518.                     bet_amount = read()
  519.                     term.setCursorPos(1,15)
  520.                     if #tostring(bet_amount) <= 6 then
  521.                         cWrite("Amount: " .. tostring(bet_amount) .. string.rep(" ",7-#tostring(bet_amount)))
  522.                     else
  523.                         cWrite("Amount: " .. tostring(bet_amount))
  524.                     end
  525.                     break
  526.                 end
  527.             end
  528.         end
  529.     end
  530. end
  531. function rString(length)
  532.     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"}
  533.     random_string = ""
  534.     for i = 1, length do
  535.         random_string = random_string .. chars[math.random(1,#chars)]
  536.     end
  537.     return random_string
  538. end
  539. term.setBackgroundColor(colors.white)
  540. term.setTextColor(colors.black)
  541. term.clear()
  542. scrx,scry = term.getSize()
  543. cx, cy = scrx/2, scry/2
  544.  
  545. check,check_code = getPage("roll","check")
  546. if check_code and check_code == 200 then
  547.     term.setCursorPos(cx-#"Krist Casino"/2,cy-2)
  548.     print("Krist Casino")
  549.     term.setCursorPos(cx-4,math.floor(cy))
  550.     term.setBackgroundColor(colors.gray)
  551.     term.setTextColor(colors.white)
  552.     cWrite("  Login  ")
  553.     term.setCursorPos(1,math.floor(cy+2))
  554.     cWrite(" Register")
  555.     term.setCursorPos(1,math.floor(scry-1))
  556.     cWrite("  Exit   ")
  557.     action = -1
  558.     while true do
  559.         local event, ctype, clickx, clicky  = os.pullEvent("mouse_click")
  560.         if clicky == math.floor(cy) and clickx >= math.floor(cx-4) and clicky <= math.floor(cx+4) then
  561.             action = 1
  562.         elseif clicky == math.floor(cy+2) and clickx >= math.floor(cx-4) and clicky <= math.floor(cx+4) then
  563.             action  = 2
  564.         elseif clicky == math.floor(scry-1) and clickx >= math.floor(cx-4) and clicky <= math.floor(cx+4) then
  565.             action = 3
  566.         end
  567.         if action > 0 then
  568.             break
  569.         end
  570.     end
  571.     if action == 1 then
  572.         login()
  573.     elseif action == 2 then
  574.         register()
  575.     end
  576.     if action <= 2 then
  577.         casino()
  578.     end
  579. else
  580.     print("Couldn't connect to server!")
  581. end
  582. term.setBackgroundColor(colors.black)
  583. term.setTextColor(colors.white)
  584. term.setCursorPos(1,1)
  585. print("Thanks for playing!")
Advertisement
Add Comment
Please, Sign In to add comment