Skip to content

Commit

Permalink
Base of the ruby server is good :)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjeux committed Jan 13, 2012
1 parent a42573d commit ac4e084
Showing 1 changed file with 81 additions and 22 deletions.
103 changes: 81 additions & 22 deletions server/server.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,96 @@
require_relative 'createserver.rb'

require 'rubygems'
require 'cool.io'
require_relative 'listener.rb'

ADDR = '127.0.0.1'
PORT = 1337


client_id = 0
clients = []

createServer(ADDR, PORT, lambda { |socket|
createServer = lambda { |addr, port, callback|
cool.io.server addr, port do
on_connect do
callback.call(self)
end
on_close do
Listener.call(self, 'close')
end
on_read do |data|
Listener.call(self, 'data', data)
end
end
cool.io.run
}

send = lambda { |clients, *msg|
msg = msg.join(' ')
puts "> #{msg}"
clients.each { |client|
client['socket'].write msg + "\r\n"
}
}

query = lambda { |clients, msg, init, update, callback|
msg = msg.join(' ')
puts "> #{msg}"

n = clients.length

answer = lambda {
n -= 1
if n == 0
callback[]
end
}

clients.each { |client|
init[client]

Listener.once(client['socket'], 'data', lambda { |data|
data = data.slice(0, data.length - 2)
puts "< #{data}"
update[client, data]
answer[]
})

client['socket'].write msg + "\r\n"
}
}


game = lambda {
players = clients

query[players, ['Start'],
lambda { |client|
client['name'] = "Unnamed-#{client['id']}"
client['score'] = 0
},
lambda { |client, name|
client['name'] = "#{name}-#{client['id']}"
send[[client], client['name']]
},
lambda {
puts "End!"
}
]
}


createServer[ADDR, PORT, lambda { |socket|
client = {"id" => client_id, "socket" => socket, "alive" => true}
client_id += 1
Listener.add(socket, 'close', lambda {
client.alive = false
client['alive'] = false
})
clients << client
a = [client]
sendMessage("vjeux", "Welcome! Please wait for a new game to start.")

puts "#{socket.remote_addr}:#{socket.remote_port} connected"

Listener.once(socket, 'data', lambda { |data|
puts "Data Received #{data}"
})

Listener.add(socket, 'close', lambda { |data|
client.alive = false
puts "Closed"
})
send[[client], "Welcome! Please wait for a new game to start."]

socket.write "Hello\r\n"
socket.write "Start\r\n"
})
if client_id == 2
game[]
end
}]

def sendMessage(a, b)
puts "send - #{a} #{b}"
end

0 comments on commit ac4e084

Please sign in to comment.