working pong-bot
This commit is contained in:
parent
5bcd7260d3
commit
16045afb21
5 changed files with 237 additions and 149 deletions
|
@ -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,15 +39,17 @@ 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)
|
||||
|
@ -55,10 +57,13 @@ class GGSNetwork
|
|||
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
|
||||
|
||||
|
|
|
@ -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,7 +36,76 @@ 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
|
||||
|
|
|
@ -209,11 +209,11 @@
|
|||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>50</integer>
|
||||
<integer>46</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>54</integer>
|
||||
<integer>50</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
|
@ -449,7 +449,7 @@
|
|||
<string>PrimaryDocumentVisibleCharacterRange</string>
|
||||
<string>HideAllIssues</string>
|
||||
<string>PrimaryDocumentSelectedCharacterRange</string>
|
||||
<real>324249006.22126502</real>
|
||||
<real>324349224.47908998</real>
|
||||
<string>{0, 910}</string>
|
||||
<string>{797, 0}</string>
|
||||
<dict>
|
||||
|
@ -462,106 +462,6 @@
|
|||
<key>$classname</key>
|
||||
<string>NSMutableDictionary</string>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>$class</key>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>41</integer>
|
||||
</dict>
|
||||
<key>NS.keys</key>
|
||||
<array>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>43</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>44</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>45</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>46</integer>
|
||||
</dict>
|
||||
</array>
|
||||
<key>NS.objects</key>
|
||||
<array>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>47</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>48</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>15</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>49</integer>
|
||||
</dict>
|
||||
</array>
|
||||
</dict>
|
||||
<string>PrimaryDocumentTimestamp</string>
|
||||
<string>PrimaryDocumentVisibleCharacterRange</string>
|
||||
<string>HideAllIssues</string>
|
||||
<string>PrimaryDocumentSelectedCharacterRange</string>
|
||||
<real>324253828.95541298</real>
|
||||
<string>{2248, 1438}</string>
|
||||
<string>{953, 0}</string>
|
||||
<dict>
|
||||
<key>$class</key>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>41</integer>
|
||||
</dict>
|
||||
<key>NS.keys</key>
|
||||
<array>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>43</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>44</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>45</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>46</integer>
|
||||
</dict>
|
||||
</array>
|
||||
<key>NS.objects</key>
|
||||
<array>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>51</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>52</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>15</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>53</integer>
|
||||
</dict>
|
||||
</array>
|
||||
</dict>
|
||||
<real>324255376.074705</real>
|
||||
<string>{351, 2071}</string>
|
||||
<string>{2942, 0}</string>
|
||||
<dict>
|
||||
<key>$class</key>
|
||||
<dict>
|
||||
|
@ -588,6 +488,102 @@
|
|||
</dict>
|
||||
</array>
|
||||
<key>NS.objects</key>
|
||||
<array>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>43</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>44</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>15</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>45</integer>
|
||||
</dict>
|
||||
</array>
|
||||
</dict>
|
||||
<real>324351348.42624801</real>
|
||||
<string>{1190, 1757}</string>
|
||||
<string>{953, 0}</string>
|
||||
<dict>
|
||||
<key>$class</key>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>41</integer>
|
||||
</dict>
|
||||
<key>NS.keys</key>
|
||||
<array>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>34</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>35</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>36</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>37</integer>
|
||||
</dict>
|
||||
</array>
|
||||
<key>NS.objects</key>
|
||||
<array>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>47</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>48</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>15</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>49</integer>
|
||||
</dict>
|
||||
</array>
|
||||
</dict>
|
||||
<real>324349919.87820297</real>
|
||||
<string>{1453, 2127}</string>
|
||||
<string>{2942, 0}</string>
|
||||
<dict>
|
||||
<key>$class</key>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>41</integer>
|
||||
</dict>
|
||||
<key>NS.keys</key>
|
||||
<array>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>51</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>52</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>53</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>54</integer>
|
||||
</dict>
|
||||
</array>
|
||||
<key>NS.objects</key>
|
||||
<array>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
|
@ -607,6 +603,10 @@
|
|||
</dict>
|
||||
</array>
|
||||
</dict>
|
||||
<string>PrimaryDocumentTimestamp</string>
|
||||
<string>PrimaryDocumentVisibleCharacterRange</string>
|
||||
<string>HideAllIssues</string>
|
||||
<string>PrimaryDocumentSelectedCharacterRange</string>
|
||||
<real>324248959.37454498</real>
|
||||
<string>{0, 531}</string>
|
||||
<string>{0, 0}</string>
|
||||
|
@ -655,8 +655,8 @@
|
|||
</dict>
|
||||
</array>
|
||||
</dict>
|
||||
<real>324248237.36148798</real>
|
||||
<string>{423, 2314}</string>
|
||||
<real>324347076.57831001</real>
|
||||
<string>{328, 2297}</string>
|
||||
<dict>
|
||||
<key>$class</key>
|
||||
<dict>
|
||||
|
@ -667,19 +667,19 @@
|
|||
<array>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>34</integer>
|
||||
<integer>51</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>35</integer>
|
||||
<integer>52</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>36</integer>
|
||||
<integer>53</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>37</integer>
|
||||
<integer>54</integer>
|
||||
</dict>
|
||||
</array>
|
||||
<key>NS.objects</key>
|
||||
|
@ -1059,7 +1059,7 @@
|
|||
<integer>102</integer>
|
||||
</dict>
|
||||
<key>NS.time</key>
|
||||
<real>324255371.33403099</real>
|
||||
<real>324351343.54201001</real>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>$classes</key>
|
||||
|
@ -1070,7 +1070,7 @@
|
|||
<key>$classname</key>
|
||||
<string>NSDate</string>
|
||||
</dict>
|
||||
<string>Today at 00:56</string>
|
||||
<string>Today at 03:35</string>
|
||||
<dict>
|
||||
<key>$classes</key>
|
||||
<array>
|
||||
|
@ -1147,7 +1147,7 @@
|
|||
<integer>109</integer>
|
||||
</dict>
|
||||
</dict>
|
||||
<string>file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/GGSNetwork.m</string>
|
||||
<string>file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/PongViewController.m</string>
|
||||
<dict>
|
||||
<key>$class</key>
|
||||
<dict>
|
||||
|
@ -1165,7 +1165,7 @@
|
|||
<integer>111</integer>
|
||||
</dict>
|
||||
</dict>
|
||||
<string>file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/PongViewController.m</string>
|
||||
<string>file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/GGSNetwork.m</string>
|
||||
<dict>
|
||||
<key>$class</key>
|
||||
<dict>
|
||||
|
@ -1201,7 +1201,7 @@
|
|||
<integer>115</integer>
|
||||
</dict>
|
||||
</dict>
|
||||
<string>file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/GGSDelegate.h</string>
|
||||
<string>file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/PongAppDelegate.m</string>
|
||||
<dict>
|
||||
<key>$class</key>
|
||||
<dict>
|
||||
|
@ -1219,7 +1219,7 @@
|
|||
<integer>117</integer>
|
||||
</dict>
|
||||
</dict>
|
||||
<string>file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/PongAppDelegate.m</string>
|
||||
<string>file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/GGSDelegate.h</string>
|
||||
<dict>
|
||||
<key>$class</key>
|
||||
<dict>
|
||||
|
@ -1390,7 +1390,7 @@
|
|||
<string>IDEWorkspaceTabControllerDesignAreaSplitView</string>
|
||||
<string>IDEShowUtilities</string>
|
||||
<string>AssistantEditorsLayout</string>
|
||||
<string>GGSNetwork.m</string>
|
||||
<string>PongViewController.m</string>
|
||||
<true/>
|
||||
<dict>
|
||||
<key>$class</key>
|
||||
|
@ -1587,7 +1587,7 @@
|
|||
<integer>153</integer>
|
||||
</dict>
|
||||
</dict>
|
||||
<string>GGSNetwork.m</string>
|
||||
<string>PongViewController.m</string>
|
||||
<dict>
|
||||
<key>$classes</key>
|
||||
<array>
|
||||
|
@ -1641,7 +1641,7 @@
|
|||
<key>documentURL</key>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>26</integer>
|
||||
<integer>24</integer>
|
||||
</dict>
|
||||
<key>timestamp</key>
|
||||
<dict>
|
||||
|
@ -1996,19 +1996,19 @@
|
|||
<array>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>43</integer>
|
||||
<integer>34</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>44</integer>
|
||||
<integer>35</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>45</integer>
|
||||
<integer>36</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>CF$UID</key>
|
||||
<integer>46</integer>
|
||||
<integer>37</integer>
|
||||
</dict>
|
||||
</array>
|
||||
<key>NS.objects</key>
|
||||
|
@ -2031,9 +2031,9 @@
|
|||
</dict>
|
||||
</array>
|
||||
</dict>
|
||||
<real>324255376.07524002</real>
|
||||
<string>{351, 2071}</string>
|
||||
<string>{2942, 0}</string>
|
||||
<real>324351348.42674202</real>
|
||||
<string>{1190, 1757}</string>
|
||||
<string>{953, 0}</string>
|
||||
<dict>
|
||||
<key>$classes</key>
|
||||
<array>
|
||||
|
@ -2043,7 +2043,7 @@
|
|||
<key>$classname</key>
|
||||
<string>NSDictionary</string>
|
||||
</dict>
|
||||
<string>-onSocket:didReadData:withTag:</string>
|
||||
<string>@implementation PongViewController</string>
|
||||
<string>Xcode.IDEKit.EditorDocument.SourceCode</string>
|
||||
<dict>
|
||||
<key>$class</key>
|
||||
|
@ -2062,7 +2062,7 @@
|
|||
<integer>199</integer>
|
||||
</dict>
|
||||
</dict>
|
||||
<string>file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/GGSNetwork.m</string>
|
||||
<string>file://localhost/Users/jeena/Student/GGS/games/Pong/Classes/PongViewController.m</string>
|
||||
<dict>
|
||||
<key>$class</key>
|
||||
<dict>
|
||||
|
|
|
@ -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) ->
|
||||
|
|
|
@ -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.
|
||||
|
|
Reference in a new issue