working pong-bot

This commit is contained in:
Jeena Paradies 2011-04-13 03:44:00 +02:00
parent 5bcd7260d3
commit 16045afb21
5 changed files with 237 additions and 149 deletions

View file

@ -3,14 +3,13 @@ require 'socket'
class GGSNetwork
SERVER = "Server"
CLIENT = "Client"
CLIENT = "Game"
public
attr_accessor :delegate
def initialize(delegate, host='localhost', port=9000)
connect(host, port)
def initialize(delegate)
@delegate = delegate
end
@ -22,15 +21,16 @@ class GGSNetwork
write( makeMessage(CLIENT, command, args) )
end
protected
def connect(host, port)
def connect(host='localhost', port=9000)
@socket = TCPSocket.new(host, port)
read
end
protected
def write(message)
@socket.write(message)
puts message
end
def read
@ -39,26 +39,31 @@ class GGSNetwork
size = 0
args = ""
select([@socket], nil, nil)
while (line = @socket.gets) != "\n"
break if line.nil?
key, value = line.split(": ")
headers[key] = value
headers[key] = value.strip
end
if headers.has_key?("Content-Size")
headers["Content-Size"].to_i.times do
args << @socket.recv
end
args = @socket.read(headers["Content-Size"].to_i)
end
receivedCommand(headers, args)
receivedCommand(headers, args)
end
end
def receivedCommand(headers, data)
puts [headers, data].inspect
if headers.has_key? "Client-Command"
command = headers["Client-Command"]
case command
when "hello"
@game_token = data
@delegate.ggsNetworkReady(self, true)
when "defined"
@delegate.ggsNetworkDefined(self, true)
@ -69,13 +74,12 @@ class GGSNetwork
end
def makeMessage(serverOrGame, command, args)
message =<<MESSAGE
Token: #{@game_token}
#{serverOrGame}-Command: #{command}
Content-Length: #{args.length}
message = "Token: #{@game_token}\n" +
"#{serverOrGame}-Command: #{command}\n" +
"Content-Length: #{args.length}\n\n"
message += args if args.length > 0
#{args}
MESSAGE
message
end

View file

@ -9,11 +9,26 @@ class PongBot
include GGSDelegate
def initialize
@me = nil
@player1 = Pos.new
@player2 = Pos.new
@ball = Pos.new
@game_paused = true
@send_start = false
@ggs_network = GGSNetwork.new(self)
@ggs_network.connect()
end
def ggsNetworkReady(ggs_network, ready)
@ggs_network.sendCommand("ready")
t = Thread.new {
loop do
gameTick()
sleep 0.3
end
}
end
def ggsNetworkDefined(ggs_network, defined)
@ -21,9 +36,78 @@ class PongBot
end
def ggsNetworkReceivedCommandWithArgs(ggs_network, command, args)
case command
when "welcome" then welcome(args)
when "ball" then ball(args)
when "player1_y" then player1_y(args)
when "player2_y" then player2_y(args)
when "game" then game(args)
when "player1_points" then new_round()
when "player2_points" then new_round()
end
end
protected
def gameTick()
if @game_paused
unless @send_start
@ggs_network.sendCommand("start")
@send_start = true
end
else
puts "#{@ball.y}:#{@me.y}"
if @ball.y < @me.y - 5
@ggs_network.sendCommand("up")
elsif @ball.y > @me.y - 5
@ggs_network.sendCommand("down")
end
end
end
def welcome(who_am_i)
if who_am_i == 1
@me = @player1
else
@me = @player2
end
end
def ball(pos_s)
x, y = pos_s.split(",")
@ball.x, @ball.y = x.to_i, y.to_i
end
def player1_y(y)
@player1.y = y.to_i
end
def player2_y(y)
@player2.y = y.to_i
end
def game(wait_or_start)
if wait_or_start == "wait"
puts "Other ready"
else
@game_paused = false
end
end
def new_round
@game_paused = true
@send_start = false
end
class Pos
attr_accessor :x, :y
def initialize
@x = 0
@y = 0
end
end
end
if __FILE__ == $0