Class: Protocol

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Logging

#logger, logger

Constructor Details

#initialize(network, client) ⇒ Protocol

Returns a new instance of Protocol.



30
31
32
33
34
35
36
# File 'lib/software_challenge_client/protocol.rb', line 30

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

#clientClientInterface (readonly)

Returns current client.

Returns:



28
29
30
# File 'lib/software_challenge_client/protocol.rb', line 28

def client
  @client
end

#gamestateGamestate (readonly)

Returns current gamestate.

Returns:

  • (Gamestate)

    current gamestate



22
23
24
# File 'lib/software_challenge_client/protocol.rb', line 22

def gamestate
  @gamestate
end

#roomIdString

Returns current room id.

Returns:

  • (String)

    current room id



25
26
27
# File 'lib/software_challenge_client/protocol.rb', line 25

def roomId
  @roomId
end

Instance Method Details

#move_to_xml(move) ⇒ Object

Converts a move to XML for sending to the server.

Parameters:

  • move (Move)

    The move to convert to XML.



220
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 220

def move_to_xml(move)
  builder = Builder::XmlMarkup.new(indent: 2)
  builder.data(class: 'move') do |data|
    move.actions.each_with_index do |action, index|
      # Converting every action type here instead of requiring the Action
      # class interface to supply a method which returns the action hash
      # because XML-generation should be decoupled from internal data
      # structures.
      attribute = case action.type
                  when :advance
                    { distance: action.distance }
                  when :skip, :eat_salad, :fall_back
                    {}
                  when :card
                    { type: action.card_type.key.to_s, value: action.value }
                  when :exchange_carrots
                    { value: action.value }
                  else
                    raise "unknown action type: #{action.type.inspect}. "\
                          "Can't convert to XML!"
                  end
      attribute[:order] = index
      data.tag!(snake_case_to_lower_camel_case(action.type.to_s), attribute)
    end
  end
  move.hints.each do |hint|
    data.hint(content: hint.content)
  end
  builder.target!
end

#parsePlayer(attributes) ⇒ Player

Converts XML attributes for a Player to a new Player object

Parameters:

  • attributes (Hash)

    Attributes for the new Player.

Returns:

  • (Player)

    The created Player object.



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/software_challenge_client/protocol.rb', line 183

def parsePlayer(attributes)
  player = Player.new(
    PlayerColor.find_by_key(attributes['color'].to_sym),
    attributes['displayName']
  )
  player.points = attributes['points'].to_i
  player.index = attributes['index'].to_i
  player.carrots = attributes['carrots'].to_i
  player.salads = attributes['salads'].to_i
  player.cards = []
  player
end

#process_string(text) ⇒ Object

starts xml-string parsing

Parameters:

  • text (String)

    the xml-string that will be parsed



41
42
43
44
# File 'lib/software_challenge_client/protocol.rb', line 41

def process_string(text)
  logger.debug "Parse XML:\n#{text}\n----END XML"
  REXML::Document.parse_stream(text, self)
end

#sendString(string) ⇒ Object

send a string

Parameters:

  • document (String)

    The string that will be send to the connected server.



206
207
208
# File 'lib/software_challenge_client/protocol.rb', line 206

def sendString(string)
  @network.sendString("<room roomId=\"#{@roomId}\">#{string}</room>")
end

#sendXml(document) ⇒ Object

send a xml document

Parameters:

  • document (REXML::Document)

    the document, that will be send to the connected server



199
200
201
# File 'lib/software_challenge_client/protocol.rb', line 199

def sendXml(document)
  @network.sendXML(document)
end

#snake_case_to_lower_camel_case(string) ⇒ Object

converts “this_snake_case” to “thisSnakeCase”



211
212
213
214
215
# File 'lib/software_challenge_client/protocol.rb', line 211

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

Parameters:

  • name (String)

    the end-tag name, that was read



55
56
57
58
59
60
61
62
# File 'lib/software_challenge_client/protocol.rb', line 55

def tag_end(name)
  case name
  when 'board'
    logger.debug @gamestate.board.to_s
  when 'type'
    @context[:player].cards << CardType.find_by_key(@context[:last_text].to_sym)
  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

Parameters:

  • name (String)

    the start-tag, that was read

  • attrs (Dictionary<String, String>)

    Attributes attached to the tag



70
71
72
73
74
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
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
# File 'lib/software_challenge_client/protocol.rb', line 70

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
    end
  when 'state'
    logger.debug 'new gamestate'
    @gamestate = GameState.new
    @gamestate.turn = attrs['turn'].to_i
    @gamestate.start_player_color = attrs['startPlayer'] == 'RED' ? PlayerColor::RED : PlayerColor::BLUE
    @gamestate.current_player_color = attrs['currentPlayer'] == 'RED' ? PlayerColor::RED : PlayerColor::BLUE
    logger.debug "Turn: #{@gamestate.turn}"
  when 'red'
    logger.debug 'new red player'
    player = parsePlayer(attrs)
    if player.color != PlayerColor::RED
      throw new IllegalArgumentException("expected #{PlayerColor::RED} Player but got #{player.color}")
    end
    @gamestate.add_player(player)
    @context[:player] = player
  when 'blue'
    logger.debug 'new blue player'
    player = parsePlayer(attrs)
    if player.color != PlayerColor::BLUE
      throw new IllegalArgumentException("expected #{PlayerColor::BLUE} Player but got #{player.color}")
    end
    @gamestate.add_player(player)
    @context[:player] = player
  when 'board'
    logger.debug 'new board'
    @gamestate.board = Board.new
    @context[:current_tile_index] = nil
    @context[:current_tile_direction] = nil
  when 'fields'
    type = FieldType.find_by_key(attrs['type'].to_sym)
    index = attrs['index'].to_i
    raise "unexpected field type: #{attrs['type']}. Known types are #{FieldType.map { |t| t.key.to_s }}" if type.nil?
    @gamestate.board.fields[index] = Field.new(type, index)
  when 'lastMove'
    @gamestate.last_move = Move.new
  when 'advance'
    @gamestate.last_move.add_action_with_order(
        Advance.new(attrs['distance'].to_i), attrs['order'].to_i
    )
  when 'card'
    @gamestate.last_move.add_action_with_order(
        Card.new(CardType.find_by_key(attrs['type'].to_sym), attrs['value'].to_i),
        attrs['order'].to_i
    )
  when 'skip'
    @gamestate.last_move.add_action_with_order(
        Skip.new, attrs['order'].to_i
    )
  when 'eatSalad'
    @gamestate.last_move.add_action_with_order(
        EatSalad.new, attrs['order'].to_i
    )
  when 'fallBack'
    @gamestate.last_move.add_action_with_order(
        FallBack.new, attrs['order'].to_i
    )
  when 'exchangeCarrots'
    @gamestate.last_move.add_action_with_order(
        ExchangeCarrots.new(attrs['value'].to_i), attrs['order'].to_i
    )
  when 'winner'
    winning_player = parsePlayer(attrs)
    @gamestate.condition = Condition.new(winning_player)
    @context[:player] = winning_player
  when 'left'
    logger.debug 'got left event, terminating'
    @network.disconnect
  when 'lastNonSkipAction'
    @context[:player].last_non_skip_action =
      case attrs['class']
      when 'advance'
        Advance.new(attrs['distance'].to_i)
      when 'card'
        Card.new(CardType.find_by_key(attrs['type'].to_sym), attrs['value'].to_i)
      when 'skip'
        Skip.new
      when 'eatSalad'
        EatSalad.new
      when 'fallBack'
        FallBack.new
      when 'exchangeCarrots'
        ExchangeCarrots.new(attrs['value'].to_i)
      else
        raise "Unknown action type #{attrs['class']}"
      end
  end
end

#text(text) ⇒ Object

called when text is encountered



48
49
50
# File 'lib/software_challenge_client/protocol.rb', line 48

def text(text)
  @context[:last_text] = text
end