summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2021-08-08 19:45:02 -0400
committerrealtradam <[email protected]>2021-08-08 19:45:02 -0400
commit112a1de4774c13ad2433286a79c3c4d6e910a1b1 (patch)
treee3a2dbe09e5b64a14a78914738ca4d64854948ec
downloadbad-networking-112a1de4774c13ad2433286a79c3c4d6e910a1b1.tar.gz
bad-networking-112a1de4774c13ad2433286a79c3c4d6e910a1b1.zip
.
-rw-r--r--.ruby-version1
-rw-r--r--client.rb73
-rw-r--r--server.rb93
3 files changed, 167 insertions, 0 deletions
diff --git a/.ruby-version b/.ruby-version
new file mode 100644
index 0000000..2c9b4ef
--- /dev/null
+++ b/.ruby-version
@@ -0,0 +1 @@
+2.7.3
diff --git a/client.rb b/client.rb
new file mode 100644
index 0000000..61c44ef
--- /dev/null
+++ b/client.rb
@@ -0,0 +1,73 @@
+require 'socket'
+require 'ruby2d'
+
+s = UDPSocket.new
+s.bind(nil, ARGV[0])
+s.send("0 #{ARGV[0]}", 0, 'localhost', 4000)
+# ignore first message from server
+s.recvfrom(16)
+#sema = Mutex.new
+
+# Stores the current location of player
+location = "#{ARGV[0]} 0-0"
+# stores where squares last known locations are
+state = {}
+threads = []
+
+# this gurantees something you choose will not be executed more then 60 times a second
+def once_per_frame
+ last = Time.now
+ while true
+ yield
+ now = Time.now
+ _next = [last + (1 / 60), now].max
+ sleep(_next - now)
+ last = _next
+ end
+end
+
+# tell server where you are, once per frame
+threads.push(Thread.new do
+ once_per_frame do
+ s.send("1 #{ARGV[0]} #{location}", 0, 'localhost', 4000)
+ end
+end)
+
+# Ctrl-c will kill threads
+at_exit do
+ puts 'trapping'
+ threads.each do |t|
+ puts 'killing'
+ Thread.kill t
+ end
+end
+
+#reciever
+threads.push(Thread.new do
+ loop do
+ text, _sender = s.recvfrom(16)
+ #sema.synchronize do
+ ary = text.split('-')
+ state[ary[0]] = [ary[1], ary[2]]
+ #end
+ end
+end)
+
+update do
+ puts get(:fps)
+ #sema.synchronize do
+ state.each do |_color, square|
+ Square.draw(color: [[1.0, 0.0, 0.0, 1.0],
+ [1.0, 0.0, 0.0, 1.0],
+ [1.0, 0.0, 0.0, 1.0],
+ [1.0, 0.0, 0.0, 1.0]],
+ x: square[0].to_i,
+ y: square[1].to_i,
+ size: 25)
+ end
+ #end
+ # update location of square
+ location = "#{get :mouse_x}-#{get :mouse_y}"
+end
+
+show
diff --git a/server.rb b/server.rb
new file mode 100644
index 0000000..139ea9b
--- /dev/null
+++ b/server.rb
@@ -0,0 +1,93 @@
+require 'socket'
+
+class Player
+ attr_accessor :color, :x, :y, :port
+
+ def initialize(port, color)
+ @port = port
+ @color = color
+ @x = 0
+ @y = 0
+ end
+end
+change = false
+threads = []
+
+# Ctrl-c will kill threads
+at_exit do
+ puts 'trapping'
+ threads.each do |t|
+ puts 'killing'
+ Thread.kill t
+ end
+end
+
+# this gurantees something you choose will not be executed more then 60 times a second
+def once_per_frame
+ last = Time.now
+ while true
+ yield
+ now = Time.now
+ _next = [last + (1 / 60), now].max
+ sleep(_next - now)
+ last = _next
+ end
+end
+
+semaphore = Mutex.new
+
+players = []
+
+#Socket.do_not_reverse_lookup = true
+s = UDPSocket.new
+s.bind(nil, 4000)
+
+#reciever
+threads.push(Thread.new do
+ loop do
+ text, _sender = s.recvfrom(16)
+ temp = text.split(' ')
+ # if new connection
+ # format: "0 #{port}"
+ if temp[0] == '0'
+ semaphore.synchronize do
+ players.push Player.new(temp[1], 'red')#colors.delete(colors.sample))
+ s.send("0 #{players.last.color}", 0, 'localhost', temp[1])
+ end
+ #else if player updating their position
+ # format: "1 #{port} #{x}-#{y}"
+ elsif temp[0] == '1'
+ semaphore.synchronize do
+ user = players.select { |player| player.port == temp[1] }
+ # something changed, inform sender to send update
+ if (user.first.x != temp[2].split('-')[0]) && (user.first.x != temp[2].split('-')[0])
+ user.first.x = temp[2].split('-')[0]
+ user.first.y = temp[2].split('-')[1]
+ change = true
+ end
+ end
+ end
+ end
+end)
+
+#sender
+threads.push(Thread.new do
+ once_per_frame do
+ semaphore.synchronize do
+ # only if something changed should you send an update
+ if change
+ change = false
+ players.each do |target|
+ players.each do |player, _str|
+ s.send("#{player.color}-#{player.x}-#{player.y}", 0, 'localhost', target.port)
+ end
+ end
+ end
+ end
+ end
+end)
+
+#prevents threads of going to background
+loop do
+ sleep 2
+end