From 16045afb212d5a340d249c347c452243dee0a1f3 Mon Sep 17 00:00:00 2001 From: Jeena Paradies Date: Wed, 13 Apr 2011 03:44:00 +0200 Subject: [PATCH] working pong-bot --- games/Pong-bots/ggs-network.rb | 40 +-- games/Pong-bots/pong-bot.rb | 88 +++++- .../UserInterfaceState.xcuserstate | 254 +++++++++--------- src/ggs_player.erl | 2 +- src/ggs_protocol.erl | 2 +- 5 files changed, 237 insertions(+), 149 deletions(-) diff --git a/games/Pong-bots/ggs-network.rb b/games/Pong-bots/ggs-network.rb index 7b61322..b8cac8d 100644 --- a/games/Pong-bots/ggs-network.rb +++ b/games/Pong-bots/ggs-network.rb @@ -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 =< 0 -#{args} -MESSAGE message end diff --git a/games/Pong-bots/pong-bot.rb b/games/Pong-bots/pong-bot.rb index de24dde..98b172f 100755 --- a/games/Pong-bots/pong-bot.rb +++ b/games/Pong-bots/pong-bot.rb @@ -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 diff --git a/games/Pong/Pong.xcodeproj/project.xcworkspace/xcuserdata/jeena.xcuserdatad/UserInterfaceState.xcuserstate b/games/Pong/Pong.xcodeproj/project.xcworkspace/xcuserdata/jeena.xcuserdatad/UserInterfaceState.xcuserstate index 2503cac..4d0530f 100644 --- a/games/Pong/Pong.xcodeproj/project.xcworkspace/xcuserdata/jeena.xcuserdatad/UserInterfaceState.xcuserstate +++ b/games/Pong/Pong.xcodeproj/project.xcworkspace/xcuserdata/jeena.xcuserdatad/UserInterfaceState.xcuserstate @@ -209,11 +209,11 @@ CF$UID - 50 + 46 CF$UID - 54 + 50 CF$UID @@ -449,7 +449,7 @@ PrimaryDocumentVisibleCharacterRange HideAllIssues PrimaryDocumentSelectedCharacterRange - 324249006.22126502 + 324349224.47908998 {0, 910} {797, 0} @@ -462,106 +462,6 @@ $classname NSMutableDictionary - - $class - - CF$UID - 41 - - NS.keys - - - CF$UID - 43 - - - CF$UID - 44 - - - CF$UID - 45 - - - CF$UID - 46 - - - NS.objects - - - CF$UID - 47 - - - CF$UID - 48 - - - CF$UID - 15 - - - CF$UID - 49 - - - - PrimaryDocumentTimestamp - PrimaryDocumentVisibleCharacterRange - HideAllIssues - PrimaryDocumentSelectedCharacterRange - 324253828.95541298 - {2248, 1438} - {953, 0} - - $class - - CF$UID - 41 - - NS.keys - - - CF$UID - 43 - - - CF$UID - 44 - - - CF$UID - 45 - - - CF$UID - 46 - - - NS.objects - - - CF$UID - 51 - - - CF$UID - 52 - - - CF$UID - 15 - - - CF$UID - 53 - - - - 324255376.074705 - {351, 2071} - {2942, 0} $class @@ -588,6 +488,102 @@ NS.objects + + + CF$UID + 43 + + + CF$UID + 44 + + + CF$UID + 15 + + + CF$UID + 45 + + + + 324351348.42624801 + {1190, 1757} + {953, 0} + + $class + + CF$UID + 41 + + NS.keys + + + CF$UID + 34 + + + CF$UID + 35 + + + CF$UID + 36 + + + CF$UID + 37 + + + NS.objects + + + CF$UID + 47 + + + CF$UID + 48 + + + CF$UID + 15 + + + CF$UID + 49 + + + + 324349919.87820297 + {1453, 2127} + {2942, 0} + + $class + + CF$UID + 41 + + NS.keys + + + CF$UID + 51 + + + CF$UID + 52 + + + CF$UID + 53 + + + CF$UID + 54 + + + NS.objects CF$UID @@ -607,6 +603,10 @@ + PrimaryDocumentTimestamp + PrimaryDocumentVisibleCharacterRange + HideAllIssues + PrimaryDocumentSelectedCharacterRange 324248959.37454498 {0, 531} {0, 0} @@ -655,8 +655,8 @@ - 324248237.36148798 - {423, 2314} + 324347076.57831001 + {328, 2297} $class @@ -667,19 +667,19 @@ CF$UID - 34 + 51 CF$UID - 35 + 52 CF$UID - 36 + 53 CF$UID - 37 + 54 NS.objects @@ -1059,7 +1059,7 @@ 102 NS.time - 324255371.33403099 + 324351343.54201001 $classes @@ -1070,7 +1070,7 @@ $classname NSDate - Today at 00:56 + Today at 03:35 $classes @@ -1147,7 +1147,7 @@ 109 - file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/GGSNetwork.m + file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/PongViewController.m $class @@ -1165,7 +1165,7 @@ 111 - file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/PongViewController.m + file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/GGSNetwork.m $class @@ -1201,7 +1201,7 @@ 115 - file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/GGSDelegate.h + file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/PongAppDelegate.m $class @@ -1219,7 +1219,7 @@ 117 - file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/PongAppDelegate.m + file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/GGSDelegate.h $class @@ -1390,7 +1390,7 @@ IDEWorkspaceTabControllerDesignAreaSplitView IDEShowUtilities AssistantEditorsLayout - GGSNetwork.m + PongViewController.m $class @@ -1587,7 +1587,7 @@ 153 - GGSNetwork.m + PongViewController.m $classes @@ -1641,7 +1641,7 @@ documentURL CF$UID - 26 + 24 timestamp @@ -1996,19 +1996,19 @@ CF$UID - 43 + 34 CF$UID - 44 + 35 CF$UID - 45 + 36 CF$UID - 46 + 37 NS.objects @@ -2031,9 +2031,9 @@ - 324255376.07524002 - {351, 2071} - {2942, 0} + 324351348.42674202 + {1190, 1757} + {953, 0} $classes @@ -2043,7 +2043,7 @@ $classname NSDictionary - -onSocket:didReadData:withTag: + @implementation PongViewController Xcode.IDEKit.EditorDocument.SourceCode $class @@ -2062,7 +2062,7 @@ 199 - file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/GGSNetwork.m + file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/PongViewController.m $class diff --git a/src/ggs_player.erl b/src/ggs_player.erl index b62c232..68c0a01 100644 --- a/src/ggs_player.erl +++ b/src/ggs_player.erl @@ -102,7 +102,7 @@ handle_cast({game_cmd, Command, _Headers, Data}, #state { table = Table } = Stat ggs_table:notify(Table, self(), {game, Command, Data}), {noreply, State}; -handle_cast(_Request, St) -> +handle_cast(Request, St) -> {stop, unimplemented1, St}. handle_info({tcp, _Socket, Data}, #state { protocol = Protocol } = State) -> diff --git a/src/ggs_protocol.erl b/src/ggs_protocol.erl index 3176429..92b7362 100644 --- a/src/ggs_protocol.erl +++ b/src/ggs_protocol.erl @@ -116,7 +116,7 @@ prettify(Args, Data) -> case lists:keyfind("Game-Command", 1, Args) of {_, Value} -> {game_cmd, Value, Args, Data}; - _ -> + Other -> ok end end.