Advertisement
Guest User

client

a guest
May 14th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.90 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 debug(msg)
  21.    modem.transmit(2, os.getComputerID(), msg)
  22. end
  23.  
  24. local function idRNG()
  25.    modem.close(_id)
  26.    _id = math.random(50000, 55000)
  27.    modem.open(_id)
  28. end
  29.  
  30. local function receive()
  31.    debug("Listening on " .. _id)
  32.    event, side, sChannel, id, data, dist = os.pullEvent("modem_message")
  33. end
  34.  
  35. local function send(to, msg)
  36.    idRNG()
  37.    modem.transmit(to, _id, msg)
  38.    debug("Sending " .. msg .. " to " .. to .. " from " .. _id)
  39.    receive()
  40. end
  41.  
  42. local function start()
  43.    debug("Starting execution")
  44.    while true do
  45.       term.setBackgroundColor(8)
  46.       term.setTextColor(16)
  47.       term.clear()
  48.       term.setCursorPos(1, 1)
  49.       print("Welcome, " .. user)
  50.       if sid ~= nil then
  51.          term.setCursorPos(3, 4)
  52.          print("[Logout]")
  53.          term.setCursorPos(3, 6)
  54.          print("[Balance]")
  55.          term.setCursorPos(3, 8)
  56.          print("[Transfer]")
  57.       else
  58.          term.setCursorPos(3, 4)
  59.          print("[Login]")
  60.          term.setCursorPos(3, 6)
  61.          print("[Create Account]")
  62.       end
  63.       event, button, x, y = os.pullEvent("mouse_click")
  64.       if x >= 3 and x <= 10 and y == 4 and sid == nil then
  65.          debug("Login")
  66.          send(server, "login")
  67.          debug("Sent login")
  68.          term.clear()
  69.          term.setCursorPos(3, 4)
  70.          write("User: ")
  71.          user = read()
  72.          term.setCursorPos(3, 5)
  73.          write("Pass: ")
  74.          pass = read("*")
  75.          debug("Sending " .. user .. ":" .. pass .. " to " .. id)
  76.          send(id, user .. ":" .. pass)
  77.          debug("Sent user and pass")
  78.          debug("Listening for return")
  79.          debug("Got return: " .. data .. " from " .. id)
  80.          if data:find("Login Successful") and data:find(":") then
  81.             if data:find(":", data:find(":") + 1) then
  82.                admin = true
  83.             else
  84.                admin = false
  85.             end
  86.             print(admin)
  87.             sid = data:sub(data:find(":") + 1)
  88.             print(data:sub(0, data:find(":") - 1))
  89.          else
  90.             user = "Guest"
  91.          end
  92.          term.setCursorPos(3, 2)
  93.          sleep(1)
  94.       elseif x >= 3 and x <= 10 and y == 4 and sid ~= nil then
  95.          user = "Guest"
  96.          send(server, "logout")
  97.          send(id, sid)
  98.          sid = nil
  99.          print(data)
  100.          sleep(1)
  101.       elseif x >= 3 and x <= 11 and y == 6 and sid ~= nil then
  102.          send(server, "balance")
  103.          send(id, sid)
  104.          term.clear()
  105.          term.setCursorPos(1, 1)
  106.          print("Balance is $" .. data)
  107.          os.pullEvent("mouse_click")
  108.       elseif x >= 3 and x <= 12 and y == 8 and sid ~= nil then
  109.          send(server, "transfer")
  110.          term.clear()
  111.          term.setCursorPos(3, 4)
  112.          write("Amount: $")
  113.          amt = read()
  114.          term.setCursorPos(3, 6)
  115.          write("To User: ")
  116.          user2 = read()
  117.          send(id, sid .. ":" .. user2 .. ":" .. amt)
  118.          term.setCursorPos(3, 2)
  119.          print(data)
  120.          os.pullEvent("mouse_click")
  121.       elseif x >= 3 and x <= 18 and y == 6 and sid == nil then
  122.          term.clear()
  123.          term.setCursorPos(3, 4)
  124.          write("Username: ")
  125.          user = read()
  126.          term.clear()
  127.          term.setCursorPos(3, 4)
  128.          write("Password: ")
  129.          pass = read("*")
  130.          term.clear()
  131.          send(id, "create")
  132.          send(id, user .. ":" .. pass)
  133.          user = "Guest"
  134.          term.setCursorPos(3, 4)
  135.          print(data)
  136.          os.pullEvent("mouse_click")
  137.       end
  138.    end
  139. end
  140.  
  141. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement