Class: Protocol
- Inherits:
-
Object
- Object
- Protocol
- Includes:
- Logging, REXML::StreamListener
- Defined in:
- lib/software_challenge_client/protocol.rb
Overview
This class handles communication to the server over the XML communication protocol. Messages from the server are parsed and moves are serialized and send back.
Instance Attribute Summary collapse
-
#client ⇒ ClientInterface
readonly
Current client.
-
#gamestate ⇒ Gamestate
readonly
Current gamestate.
-
#i ⇒ Integer
I.
-
#roomId ⇒ String
Current room id.
-
#x ⇒ Integer
X.
-
#y ⇒ Integer
Y.
Instance Method Summary collapse
-
#initialize(network, client) ⇒ Protocol
constructor
A new instance of Protocol.
-
#move_to_xml(move) ⇒ Object
Converts a move to XML for sending to the server.
-
#process_string(text) ⇒ Object
starts xml-string parsing.
-
#sendString(string) ⇒ Object
send a string.
-
#sendXml(document) ⇒ Object
send a xml document.
-
#snake_case_to_lower_camel_case(string) ⇒ Object
converts “this_snake_case” to “thisSnakeCase”.
-
#tag_end(name) ⇒ Object
called if an end-tag is read.
-
#tag_start(name, attrs) ⇒ Object
called if a start tag is read Depending on the tag the gamestate is updated or the client will be asked for a move.
-
#text(text) ⇒ Object
called when text is encountered.
Methods included from Logging
Constructor Details
#initialize(network, client) ⇒ Protocol
Returns a new instance of Protocol.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/software_challenge_client/protocol.rb', line 40 def initialize(network, client) @gamestate = GameState.new @network = network @client = client @context = {} # for saving context when stream-parsing the XML @client.gamestate = @gamestate @x = 0 @y = 0 @i = 0 end |
Instance Attribute Details
#client ⇒ ClientInterface (readonly)
Returns current client.
28 29 30 |
# File 'lib/software_challenge_client/protocol.rb', line 28 def client @client end |
#gamestate ⇒ Gamestate (readonly)
Returns current gamestate.
22 23 24 |
# File 'lib/software_challenge_client/protocol.rb', line 22 def gamestate @gamestate end |
#i ⇒ Integer
Returns i.
38 39 40 |
# File 'lib/software_challenge_client/protocol.rb', line 38 def i @i end |
#roomId ⇒ String
Returns current room id.
25 26 27 |
# File 'lib/software_challenge_client/protocol.rb', line 25 def roomId @roomId end |
#x ⇒ Integer
Returns x.
32 33 34 |
# File 'lib/software_challenge_client/protocol.rb', line 32 def x @x end |
#y ⇒ Integer
Returns y.
35 36 37 |
# File 'lib/software_challenge_client/protocol.rb', line 35 def y @y end |
Instance Method Details
#move_to_xml(move) ⇒ Object
Converts a move to XML for sending to the server.
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/software_challenge_client/protocol.rb', line 221 def move_to_xml(move) builder = Builder::XmlMarkup.new(indent: 2) if move.nil? raise 'nil moves are not sendable!' end # Converting every the move here instead of requiring the Move # class interface to supply a method which returns the XML # because XML-generation should be decoupled from internal data # structures. to_d = Coordinates.oddr_to_doubled(move.to) if move.from.nil? builder.data(class: 'move') do |d| d.to(x: to_d.x, y: to_d.y) end else from_d = Coordinates.oddr_to_doubled(move.from) builder.data(class: 'move') do |d| d.from(x: from_d.x, y: from_d.y) d.to(x: to_d.x, y: to_d.y) end end builder.target! end |
#process_string(text) ⇒ Object
starts xml-string parsing
54 55 56 57 58 59 60 61 62 |
# File 'lib/software_challenge_client/protocol.rb', line 54 def process_string(text) #logger.debug "Parse XML:\n#{text}\n----END XML" begin REXML::Document.parse_stream(text.encode('UTF-8', :invalid => :replace, :undef => :replace), self) rescue REXML::ParseException => e # to parse incomplete xml, ignore missing end tag exceptions raise e unless e. =~ /Missing end tag/ end end |
#sendString(string) ⇒ Object
send a string
207 208 209 |
# File 'lib/software_challenge_client/protocol.rb', line 207 def sendString(string) @network.sendString("<room roomId=\"#{@roomId}\">#{string}</room>") end |
#sendXml(document) ⇒ Object
send a xml document
200 201 202 |
# File 'lib/software_challenge_client/protocol.rb', line 200 def sendXml(document) @network.sendXML(document) end |
#snake_case_to_lower_camel_case(string) ⇒ Object
converts “this_snake_case” to “thisSnakeCase”
212 213 214 215 216 |
# File 'lib/software_challenge_client/protocol.rb', line 212 def snake_case_to_lower_camel_case(string) string.split('_').inject([]) do |result, e| result + [result.empty? ? e : e.capitalize] end.join end |
#tag_end(name) ⇒ Object
called if an end-tag is read
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/software_challenge_client/protocol.rb', line 156 def tag_end(name) case name when 'board' logger.debug @gamestate.board.to_s when 'startTeam' @gamestate.add_player(Player.new(Team::ONE, "ONE", 0)) @gamestate.add_player(Player.new(Team::TWO, "TWO", 0)) if @context[:last_text] == "ONE" @gamestate.start_player = @gamestate.player_one else @gamestate.start_player = @gamestate.player_two end when 'int' @i += 1 if i == 1 logger.info 'Got player one fishes' @gamestate.player_one.fishes = @context[:last_text].to_i elsif i == 2 logger.info 'Got player two fishes' @gamestate.player_two.fishes = @context[:last_text].to_i else logger.info 'We got a problemo' end when 'list' @y += 1 @x = 0 when 'field' if @context[:last_text] == "ONE" field = Field.new(@x, @y, Piece.new(Team::ONE, Coordinates.new(@x, @y))) elsif @context[:last_text] == "TWO" field = Field.new(@x, @y, Piece.new(Team::TWO, Coordinates.new(@x, @y))) else field = Field.new(@x, @y, nil, @context[:last_text].to_i) end @gamestate.board.add_field(field) @x += 1 else why = 'tho' end end |
#tag_start(name, attrs) ⇒ Object
called if a start tag is read Depending on the tag the gamestate is updated or the client will be asked for a move
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/software_challenge_client/protocol.rb', line 75 def tag_start(name, attrs) case name when 'room' @roomId = attrs['roomId'] logger.info 'roomId : ' + @roomId when 'data' logger.debug "data(class) : #{attrs['class']}" @context[:data_class] = attrs['class'] if attrs['class'] == 'moveRequest' @client.gamestate = gamestate if gamestate.turn == 0 gamestate.myself_player = gamestate.start_player logger.debug "I am #{gamestate.myself_player}" elsif gamestate.turn == 1 gamestate.myself_player = gamestate.not_player(gamestate.start_player) logger.debug "I am #{gamestate.myself_player}" end gamestate.current_player = gamestate.myself_player move = @client.move_requested sendString(move_to_xml(move)) end if attrs['class'] == 'error' logger.info "Game ended - ERROR: #{attrs['message']}" @network.disconnect end if attrs['class'] == 'result' logger.info 'Got game result' @network.disconnect @gamestate.condition = Condition.new(nil, '') end when 'state' logger.debug 'new gamestate' #@gamestate = GameState.new @gamestate.turn = attrs['turn'].to_i logger.debug "Round: #{@gamestate.round}, Turn: #{@gamestate.turn}" when 'board' @x = 0 @y = 0 @i = 0 logger.debug 'new board' @gamestate.board = Board.new() # when 'pieces' # @context[:entry] = :pieces # when 'coordinates' # @context[:x] = attrs['x'].to_i # @context[:y] = attrs['y'].to_i # when 'piece' # x = @context[:x] # y = @context[:y] # team = Team.find_by_key(attrs['team'].to_sym) # type = PieceType.find_by_key(attrs['type'].to_sym) # count = attrs['count'].to_i # field = Field.new(x, y, Piece.new(team.to_c, type, Coordinates.new(x, y), count)) # @gamestate.board.add_field(field) # when 'from' # @context[:from] = Coordinates.new(attrs['x'].to_i, attrs['y'].to_i) # when 'to' # from = @context[:from] # @gamestate.last_move = Move.new(Coordinates.new(from.x, from.y), Coordinates.new(attrs['x'].to_i, attrs['y'].to_i)) # when 'ambers' # @context[:entry] = :ambers when 'winner' # TODO # winning_player = parsePlayer(attrs) # @gamestate.condition = Condition.new(winning_player, @gamestate.condition.reason) when 'score' # TODO # there are two score tags in the result, but reason attribute should be equal on both # @gamestate.condition = Condition.new(@gamestate.condition.winner, attrs['reason']) when 'left' logger.debug 'got left event, terminating' @network.disconnect when 'sc.protocol.responses.CloseConnection' logger.debug 'got left close connection event, terminating' @network.disconnect end end |
#text(text) ⇒ Object
called when text is encountered
65 66 67 |
# File 'lib/software_challenge_client/protocol.rb', line 65 def text(text) @context[:last_text] = text end |