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.
-
#roomId ⇒ String
Current room id.
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.
31 32 33 34 35 36 37 |
# File 'lib/software_challenge_client/protocol.rb', line 31 def initialize(network, client) @gamestate = GameState.new @network = network @client = client @context = {} # for saving context when stream-parsing the XML @client.gamestate = @gamestate end |
Instance Attribute Details
#client ⇒ ClientInterface (readonly)
Returns current client.
29 30 31 |
# File 'lib/software_challenge_client/protocol.rb', line 29 def client @client end |
#gamestate ⇒ Gamestate (readonly)
Returns current gamestate.
23 24 25 |
# File 'lib/software_challenge_client/protocol.rb', line 23 def gamestate @gamestate end |
#roomId ⇒ String
Returns current room id.
26 27 28 |
# File 'lib/software_challenge_client/protocol.rb', line 26 def roomId @roomId end |
Instance Method Details
#move_to_xml(move) ⇒ Object
Converts a move to XML for sending to the server.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/software_challenge_client/protocol.rb', line 239 def move_to_xml(move) builder = Builder::XmlMarkup.new(indent: 2) # 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. case move when SetMove builder.data(class: 'sc.plugin2021.SetMove') do |data| data.piece(color: move.piece.color, kind: move.piece.kind, rotation: move.piece.rotation, isFlipped: move.piece.is_flipped) do |piece| piece.position(x: move.piece.position.x, y: move.piece.position.y) end move.hints.each do |hint| data.hint(content: hint.content) end end when SkipMove builder.data(class: 'sc.plugin2021.SkipMove') do |data| data.color(@gamestate.current_color.key.to_s) move.hints.each do |hint| data.hint(content: hint.content) end end end builder.target! end |
#process_string(text) ⇒ Object
starts xml-string parsing
42 43 44 45 46 47 48 49 50 |
# File 'lib/software_challenge_client/protocol.rb', line 42 def process_string(text) #logger.debug "Parse XML:\n#{text}\n----END XML" begin REXML::Document.parse_stream(text, 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
225 226 227 |
# File 'lib/software_challenge_client/protocol.rb', line 225 def sendString(string) @network.sendString("<room roomId=\"#{@roomId}\">#{string}</room>") end |
#sendXml(document) ⇒ Object
send a xml document
218 219 220 |
# File 'lib/software_challenge_client/protocol.rb', line 218 def sendXml(document) @network.sendXML(document) end |
#snake_case_to_lower_camel_case(string) ⇒ Object
converts “this_snake_case” to “thisSnakeCase”
230 231 232 233 234 |
# File 'lib/software_challenge_client/protocol.rb', line 230 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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/software_challenge_client/protocol.rb', line 60 def tag_end(name) case name when 'board' logger.debug @gamestate.board.to_s when 'color' if @context[:color] == :ordered_colors @gamestate.ordered_colors << Color.to_a.find {|s| s.key == @context[:last_text].to_sym } end when 'shape' case @context[:piece_target] when :blue_shapes last = @context[:last_text] arr = PieceShape.to_a shape = arr.find {|s| s.key == @context[:last_text].to_sym } @gamestate.undeployed_blue_pieces << shape when :yellow_shapes shape = PieceShape.to_a.find {|s| s.key == @context[:last_text].to_sym } @gamestate.undeployed_yellow_pieces << shape when :red_shapes shape = PieceShape.to_a.find {|s| s.key == @context[:last_text].to_sym } @gamestate.undeployed_red_pieces << shape when :green_shapes shape = PieceShape.to_a.find {|s| s.key == @context[:last_text].to_sym } @gamestate.undeployed_green_pieces << shape end when 'yellowShapes' when 'redShapes' when 'greenShapes' 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
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 152 153 154 155 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/software_challenge_client/protocol.rb', line 100 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'] == 'sc.framework.plugins.protocol.MoveRequest' @client.gamestate = gamestate 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.current_color_index = attrs['currentColorIndex'].to_i @gamestate.turn = attrs['turn'].to_i @gamestate.round = attrs['round'].to_i @gamestate.start_piece = PieceShape.to_a.find {|s| s.key == attrs['startPiece'].to_sym } logger.debug "Round: #{@gamestate.round}, Turn: #{@gamestate.turn}" when 'first' logger.debug 'new first player' player = Player.new(PlayerType::ONE, attrs['displayName']) @gamestate.add_player(player) @context[:player] = player @context[:color] = :one when 'second' logger.debug 'new second player' player = Player.new(PlayerType::TWO, attrs['displayName']) @gamestate.add_player(player) @context[:player] = player @context[:color] = :two when 'orderedColors' @context[:color] = :ordered_colors @gamestate.ordered_colors = [] when 'board' logger.debug 'new board' @gamestate.board = Board.new() when 'field' x = attrs['x'].to_i y = attrs['y'].to_i color = Color.find_by_key(attrs['content'].to_sym) field = Field.new(x, y, color) @gamestate.board.add_field(field) @context[:piece_target] = :field @context[:field] = field when 'blueShapes' @context[:piece_target] = :blue_shapes @gamestate.undeployed_blue_pieces = [] when 'yellowShapes' @context[:piece_target] = :yellow_shapes @gamestate.undeployed_yellow_pieces = [] when 'redShapes' @context[:piece_target] = :red_shapes @gamestate.undeployed_red_pieces = [] when 'greenShapes' @context[:piece_target] = :green_shapes @gamestate.undeployed_green_pieces = [] when 'piece' color = Color.find_by_key(attrs['color'].to_sym) kind = PieceShape.find_by_key(attrs['kind'].to_sym) rotation = Rotation.find_by_key(attrs['rotation'].to_sym) is_flipped = attrs['isFlipped'].downcase == "true" piece = Piece.new(color, kind, rotation, is_flipped, Coordinates.origin) case @context[:piece_target] when :blue_shapes @gamestate.undeployed_blue_pieces << piece when :yellow_shapes @gamestate.undeployed_yellow_pieces << piece when :red_shapes @gamestate.green_red_pieces << piece when :green_shapes @gamestate.undeployed_green_pieces << piece when :last_move @context[:last_move_piece] = piece else raise "unknown piece target #{@context[:piece_target]}" end when 'lastMove' type = attrs['class'] if type == 'skipmove' @gamestate.last_move = SkipMove.new else @context[:last_move_type] = type @context[:piece_target] = :last_move end when 'startColor' @gamestate.start_color = Color::BLUE 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
53 54 55 |
# File 'lib/software_challenge_client/protocol.rb', line 53 def text(text) @context[:last_text] = text end |