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.

103 lines
3.3 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. local requesterip
  10. local requresterport
  11. while running do
  12. local data, msg_or_ip, port_or_nil
  13. local p1data, p2data
  14. repeat
  15. data, msg_or_ip, port_or_nil = udp:receivefrom()
  16. if data then
  17. if data == "HELLO" then
  18. requesterip = msg_or_ip
  19. requesterport = port_or_nil
  20. else
  21. print(string.sub(data,1,1) .. "Playerlist: " .. player1ip .. " " .. p1ping .. " " .. player2ip .. " " .. p2ping)
  22. if (player1ip == msg_or_ip) then
  23. p1ping = 0
  24. p1data = data
  25. elseif player2ip == msg_or_ip then
  26. p2data = data
  27. p2ping = 0
  28. else
  29. if (player1ip == "none") then
  30. player1ip = msg_or_ip
  31. p1data = data
  32. p1ping = 0
  33. player1port = port_or_nil
  34. print("CONNECTED: PLAYER 1 FROM: " .. player1ip)
  35. elseif player2ip == "none" and msg_or_ip ~= player1ip then
  36. player2ip = msg_or_ip
  37. p2data = data
  38. p2ping = 0
  39. player2port = port_or_nil
  40. print("CONNECTED: PLAYER 2 FROM: " .. player2ip)
  41. elseif (player1ip ~= msg_or_ip and player2ip ~= msg_or_ip) then
  42. print("Lobby Full!" .. player1ip .. player2ip)
  43. end
  44. end
  45. end
  46. end
  47. until not data
  48. if player1ip ~= "none" then
  49. p1ping = p1ping + 1
  50. if p1ping > 100 then
  51. if p2data then
  52. udp:sendto(p2data .. '|' .. p1ping, player1ip, player1port)
  53. end
  54. print("PLAYER 1 DISCONNECTED")
  55. p1data = nil
  56. player1ip = "none"
  57. player1port = nil
  58. end
  59. end
  60. if player2ip ~= "none" then
  61. p2ping = p2ping + 1
  62. if p2ping > 100 then
  63. if p1data then
  64. udp:sendto(p1data .. '|' .. p2ping, player2ip, player2port)
  65. end
  66. print("PLAYER 2 DISCONNECTED")
  67. p2data = nil
  68. player2ip = "none"
  69. player2port = nil
  70. end
  71. end
  72. if p1data and player2port then
  73. udp:sendto(p1data .. '|' .. p2ping, player2ip, player2port)
  74. --rint("SENT TO " .. player2ip .. ":" .. player2port .. " : " .. string.sub(p1data,1,1))
  75. end
  76. if p2data and player1port then
  77. udp:sendto(p2data .. '|' .. p1ping, player1ip, player1port)
  78. --print("SENT TO " .. player1ip .. ":" .. player1port .. " : " .. string.sub(p2data,1,1))
  79. --print("1::" .. p1data)
  80. --print("2::" .. p2data)
  81. --print("SENT1: " .. player2ip .. " " .. player2port .. " " .. p1data)
  82. --print("SENT2: " .. player1ip .. " " .. player1port .. " " .. p2data)
  83. end
  84. if requesterip then
  85. print("getting pnged!")
  86. if player1ip == "none" then
  87. udp:sendto("nettest",requesterip, requesterport)
  88. print("nettest av to: " .. requesterip)
  89. elseif player2ip == "none" then
  90. udp:sendto("clienttest", requesterip, requesterport)
  91. print("clienttest av to: " .. requesterip)
  92. else
  93. udp:sendto("full", requesterip, requesterport)
  94. print("full to: " .. msg_or_ip)
  95. end
  96. requesterip, requesterport = nil
  97. end
  98. socket.sleep(0.015)
  99. end