You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.7 KiB

  1. local running = true
  2. local socket = require 'socket'
  3. local udp = socket.udp()
  4. local player1ip, player2ip, player1port, player2port = "none", "none", nil, nil
  5. udp:settimeout(0)
  6. udp:setsockname('*', 12345)
  7. local p1ping = 0
  8. local p2ping = 0
  9. while running do
  10. local data, msg_or_ip, port_or_nil
  11. local p1data, p2data
  12. repeat
  13. data, msg_or_ip, port_or_nil = udp:receivefrom()
  14. if data then
  15. print(string.sub(data,1,1) .. "Playerlist: " .. player1ip .. " " .. player2ip)
  16. if (player1ip == msg_or_ip) then
  17. p1ping = 0
  18. p1data = data
  19. elseif player2ip == msg_or_ip then
  20. p2data = data
  21. p2ping = 0
  22. else
  23. if (player1ip == "none") then
  24. player1ip = msg_or_ip
  25. p1data = data
  26. p1ping = 0
  27. player1port = port_or_nil
  28. print("CONNECTED: PLAYER 1 FROM: " .. player1ip)
  29. elseif player2ip == "none" and msg_or_ip ~= player1ip then
  30. player2ip = msg_or_ip
  31. p2data = data
  32. p2ping = 0
  33. player2port = port_or_nil
  34. print("CONNECTED: PLAYER 2 FROM: " .. player2ip)
  35. elseif (player1ip ~= msg_or_ip and player2ip ~= msg_or_ip) then
  36. print("Lobby Full!" .. player1ip .. player2ip)
  37. end
  38. end
  39. else
  40. end
  41. until not data
  42. if player1ip ~= "none" then
  43. p1ping = p1ping + 1
  44. if p1ping > 100 then
  45. if p2data then
  46. udp:sendto(p2data .. '|' .. p1ping, player1ip, player1port)
  47. end
  48. print("PLAYER 1 DISCONNECTED")
  49. p1data = nil
  50. player1ip = "none"
  51. player1port = nil
  52. end
  53. end
  54. if player2ip ~= "none" then
  55. p2ping = p2ping + 1
  56. if p2ping > 100 then
  57. if p1data then
  58. udp:sendto(p1data .. '|' .. p2ping, player2ip, player2port)
  59. end
  60. print("PLAYER 2 DISCONNECTED")
  61. p2data = nil
  62. player2ip = "none"
  63. player2port = nil
  64. end
  65. end
  66. if p1data and player2port then
  67. udp:sendto(p1data .. '|' .. p2ping, player2ip, player2port)
  68. print("SENT TO " .. player2ip .. ":" .. player2port .. " : " .. string.sub(p1data,1,1))
  69. end
  70. if p2data and player1port then
  71. udp:sendto(p2data .. '|' .. p1ping, player1ip, player1port)
  72. print("SENT TO " .. player1ip .. ":" .. player1port .. " : " .. string.sub(p2data,1,1))
  73. --print("1::" .. p1data)
  74. --print("2::" .. p2data)
  75. --print("SENT1: " .. player2ip .. " " .. player2port .. " " .. p1data)
  76. --print("SENT2: " .. player1ip .. " " .. player1port .. " " .. p2data)
  77. end
  78. socket.sleep(0.015)
  79. end