Advertisement
Guest User

client

a guest
May 14th, 2016
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.71 KB | None | 0 0
  1. os.pullEvent = os.pullEventRaw
  2.  
  3. local file = fs.open(".server", "r")
  4.  
  5. if file == nil then
  6.    print("No server configured")
  7.    return
  8. end
  9.  
  10. local server = tonumber(file.readLine())
  11. file.close()
  12.  
  13. local sid
  14. local user = "Guest"
  15. local modem = peripheral.find("modem")
  16. local event, side, sChannel, id, data, dist
  17. local _id = os.getComputerID()
  18. local admin = false
  19.  
  20. local function idRNG()
  21.    modem.close(_id)
  22.    _id = math.random(50000, 55000)
  23.    modem.open(_id)
  24. end
  25.  
  26. local function receive()
  27.    event, side, sChannel, id, data, dist = os.pullEvent("modem_message")
  28. end
  29.  
  30. local function send(to, msg)
  31.    idRNG()
  32.    modem.transmit(to, _id, msg)
  33.    receive()
  34. end
  35.  
  36. local function addmoney(usr, amt)
  37.    send(server,"add")
  38.    send(id,sid..":"..usr..":"..amt)
  39. end
  40.  
  41. local function remusr(usr)
  42.    send(server,"delete")
  43.    send(id,sid..":"..usr)
  44. end
  45.  
  46. local function chmod(usr,bool)
  47.    send(server,"chmod")
  48.    send(id,sid..":"..usr..":"..bool)
  49. end
  50.  
  51. local function PClick()
  52.    local evnt, button, x, y = os.pullEvent("mouse_click")
  53.    local A = x
  54.    local B = y
  55.    local C = button
  56.    return A, B, C
  57. end
  58.  
  59. local function start()
  60.    while true do
  61.       term.setBackgroundColor(8)
  62.       term.setTextColor(16)
  63.       term.clear()
  64.       term.setCursorPos(1, 1)
  65.       print("Welcome, " .. user)
  66.       if sid ~= nil then
  67.          term.setCursorPos(3, 4)
  68.          print("[Logout]")
  69.          term.setCursorPos(3, 6)
  70.          print("[Balance]")
  71.          term.setCursorPos(3, 8)
  72.          print("[Transfer]")
  73.          if admin then
  74.             term.setCursorPos(3, 10)
  75.             print("[R/A $]")
  76.             term.setCursorPos(3,12)
  77.             print("[Remove User]")
  78.             term.setCursorPos(3,14)
  79.             print("[Change User Status]")
  80.             x, y, button = PClick()
  81.             if button == 1 and x >= 3 and x <= 9 and y == 10 then
  82.                term.setBackgroundColor(8)
  83.                term.setTextColor(16)
  84.                term.clear()
  85.                term.setCursorPos(1, 1)
  86.                print("Welcome, " .. user)
  87.                term.setCursorPos(3, 4)
  88.                write("User: ")
  89.                USR = read()
  90.                term.setCursorPos(3, 5)
  91.                write("Amount: $")
  92.                ammt = read()
  93.                addmoney(USR,ammt)
  94.             elseif button == 1 and x >= 3 and x <= 15 and y == 12 then
  95.                term.setBackgroundColor(8)
  96.                term.setTextColor(16)
  97.                term.clear()
  98.                term.setCursorPos(1, 1)
  99.                print("Welcome, " .. user)
  100.                term.setCursorPos(3, 4)
  101.                write("User: ")
  102.                USR = read()
  103.                remusr(USR)
  104.             elseif button == 1 and x >= 3 and x <= 22 and y == 14 then
  105.                term.setBackgroundColor(8)
  106.                term.setTextColor(16)
  107.                term.clear()
  108.                term.setCursorPos(1, 1)
  109.                print("Welcome, " .. user)
  110.                term.setCursorPos(3, 4)
  111.                write("User: ")
  112.                USR = read()
  113.                term.setCursorPos(3, 5)
  114.                write("Admin (true/false): ")
  115.                BOOL = read()
  116.                chmod(USR,BOOL)
  117.             end
  118.          end
  119.       else
  120.          term.setCursorPos(3, 4)
  121.          print("[Login]")
  122.          term.setCursorPos(3, 6)
  123.          print("[Create Account]")
  124.       end
  125.       event, button, x, y = os.pullEvent("mouse_click")
  126.       if x >= 3 and x <= 10 and y == 4 and sid == nil then
  127.          send(server, "login")
  128.          term.clear()
  129.          term.setCursorPos(3, 4)
  130.          write("User: ")
  131.          user = read()
  132.          term.setCursorPos(3, 5)
  133.          write("Pass: ")
  134.          pass = read("*")
  135.          send(id, user .. ":" .. pass)
  136.           if data:find("Login Successful") and data:find(":") then
  137.             if data:find(":", data:find(":") + 1) then
  138.                admin = true
  139.                local c1 = data:find(":")
  140.                local c2 = data:find(":", c1 + 1)
  141.                sid = data:sub(c1 + 1, c2 - 1)
  142.             else
  143.                admin = false
  144.                sid = data:sub(data:find(":") + 1)
  145.             end
  146.             print(data:sub(0, data:find(":") - 1))
  147.          else
  148.             user = "Guest"
  149.             print(data)
  150.          end
  151.          term.setCursorPos(3, 2)
  152.          sleep(1)
  153.       elseif x >= 3 and x <= 10 and y == 4 and sid ~= nil then
  154.          user = "Guest"
  155.          send(server, "logout")
  156.          send(id, sid)
  157.          sid = nil
  158.          print(data)
  159.          sleep(1)
  160.       elseif x >= 3 and x <= 11 and y == 6 and sid ~= nil then
  161.          send(server, "balance")
  162.          send(id, sid)
  163.          term.clear()
  164.          term.setCursorPos(1, 1)
  165.          print("Balance is $" .. data)
  166.          os.pullEvent("mouse_click")
  167.       elseif x >= 3 and x <= 12 and y == 8 and sid ~= nil then
  168.          send(server, "transfer")
  169.          term.clear()
  170.          term.setCursorPos(3, 4)
  171.          write("Amount: $")
  172.          amt = read()
  173.          term.setCursorPos(3, 6)
  174.          write("To User: ")
  175.          user2 = read()
  176.          send(id, sid .. ":" .. user2 .. ":" .. amt)
  177.          term.setCursorPos(3, 2)
  178.          print(data)
  179.          os.pullEvent("mouse_click")
  180.       elseif x >= 3 and x <= 18 and y == 6 and sid == nil then
  181.          term.clear()
  182.          term.setCursorPos(3, 4)
  183.          write("Username: ")
  184.          user = read()
  185.          term.clear()
  186.          term.setCursorPos(3, 4)
  187.          write("Password: ")
  188.          pass = read("*")
  189.          term.clear()
  190.          send(server, "create")
  191.          send(id, user .. ":" .. pass)
  192.          user = "Guest"
  193.          term.setCursorPos(3, 4)
  194.          print(data)
  195.          os.pullEvent("mouse_click")
  196.       end
  197.    end
  198. end
  199.  
  200. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement