rewritten with socket.udp directly

This commit is contained in:
Tangent 2019-05-16 18:41:03 -07:00
parent f1f7e2060c
commit 6afb06c936
2 changed files with 76 additions and 44 deletions

View File

@ -1,42 +1,64 @@
w, h = love.graphics.getDimensions!
sock = require "sock"
socket = require "socket"
Ship = require "Ship"
address, port = "127.0.0.1", 22109
udp = socket.udp!
udp\settimeout 0 -- do not block
print udp\setpeername address, port
-- TODO sync moving ships
ships = {}
ships[1] = Ship!
client = sock.newClient("localhost", 22122)
client\on "u", (contacts) ->
ships = contacts -- very naive
tmp_flag = false
id = os.time! -- shitty method
ships[id] = Ship!
request_frequency = 1/10
time = 0
request_time = 0
update_frequency = 1/30
update_time = 0
update_needed = false
love.update = (dt) ->
client\update!
for _, ship in pairs ships
ship\update dt
unless tmp_flag
if client\getState! == "connected"
tmp_flag = true
client\send "n", ships[1]
-- unless tmp_flag
-- if client\getState! == "connected"
-- tmp_flag = true
-- client\send "n", ships[1]
ship = ships[id]
if love.keyboard.isDown "w"
ships[1].vy -= 100 * dt
ship.vy -= 100 * dt
update_needed = true
if love.keyboard.isDown "a"
ships[1].vx -= 100 * dt
ship.vx -= 100 * dt
update_needed = true
if love.keyboard.isDown "s"
ships[1].vy += 100 * dt
ship.vy += 100 * dt
update_needed = true
if love.keyboard.isDown "d"
ships[1].vx += 100 * dt
ship.vx += 100 * dt
update_needed = true
time += dt
if time >= request_frequency
time -= request_frequency
client\send "u"
update_time += dt
if update_time >= update_frequency and update_needed
update_time -= update_frequency
print udp\send "#{ship.id} #{ship.x} #{ship.y} #{ship.vx} #{ship.vy}" -- to be replaced with a proper method
update_needed = false
-- request_time += dt
-- if request_time >= request_frequency
-- request_time -= request_frequency
-- udp\send "u" -- request update TODO have server push updates instead
data, msg = udp\receive!
while data
-- TODO do something with updates
print data
data, msg = udp\receive!
print msg if msg ~= "timeout"
love.draw = ->
love.graphics.translate w / 2, h / 2
for _, ship in pairs ships
love.graphics.circle "fill", ship.x, ship.y, 2

View File

@ -1,28 +1,38 @@
sock = require "sock"
socket = require "socket"
Ship = require "Ship"
udp = socket.udp!
udp\settimeout 0
print udp\setsockname "*", 22109
ships = {}
clients = {}
server = sock.newServer "*", 22122
status_frequency = 5
status_time = 0
update_frequency = 1/30
update_time = 0
server\on "connect", (data, client) ->
print data, client
server\on "u", (data, client) ->
print data, client
client\send "u", ships
-- TODO filter to in-range, return only needed data (how will bitser parse classes?)
server\on "n", (data, client) ->
table.insert ships, data
time, status_frequency = 0, 5
love.update = (dt) ->
server\update!
for ship in *ships
for _, ship in pairs ships
ship\update dt
time += dt
if time >= status_frequency
time -= status_frequency
print server\getMaxChannels!, server\getMaxPeers!, server\getMessageTimeout!
update_time += dt
if update_time >= update_frequency
update_time -= update_frequency
-- TODO send update to all players!!
for ip, port in pairs clients
print udp\sendto "update", ip, port
data, ip, port = udp\receivefrom!
while data
clients[ip] = port unless clients[ip]
-- TODO parse
print data, ip, port
data, ip, port = udp\receivefrom!
print ip unless ip == "timeout"
-- time += dt
-- if time >= status_frequency
-- time -= status_frequency
-- print server\getMaxChannels!, server\getMaxPeers!, server\getMessageTimeout!