Class: Protocol
- Inherits:
-
Object
- Object
- Protocol
- Includes:
- REXML::StreamListener
- Defined in:
- lib/software_challenge_client/protocol.rb
Overview
This class handles the parsing of xml strings according to the network protocol of twixt
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.
-
#processString(text) ⇒ Object
starts xml-string parsing.
-
#sendXml(document) ⇒ Object
send a xml document.
-
#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.
Constructor Details
#initialize(network, client) ⇒ Protocol
27 28 29 30 31 |
# File 'lib/software_challenge_client/protocol.rb', line 27 def initialize(network, client) @gamestate = GameState.new @network, @client = network, client self.client.gamestate = self.gamestate end |
Instance Attribute Details
#client ⇒ ClientInterface (readonly)
24 25 26 |
# File 'lib/software_challenge_client/protocol.rb', line 24 def client @client end |
#gamestate ⇒ Gamestate (readonly)
18 19 20 |
# File 'lib/software_challenge_client/protocol.rb', line 18 def gamestate @gamestate end |
#roomID ⇒ String
21 22 23 |
# File 'lib/software_challenge_client/protocol.rb', line 21 def roomID @roomID end |
Instance Method Details
#processString(text) ⇒ Object
starts xml-string parsing
36 37 38 39 40 |
# File 'lib/software_challenge_client/protocol.rb', line 36 def processString(text) list = self #puts "Parse XML:\n#{text}\n----END XML" REXML::Document.parse_stream(text, list) end |
#sendXml(document) ⇒ Object
send a xml document
145 146 147 |
# File 'lib/software_challenge_client/protocol.rb', line 145 def sendXml(document) @network.sendXML(document) end |
#tag_end(name) ⇒ Object
called if an end-tag is read
45 46 47 48 49 50 51 52 53 |
# File 'lib/software_challenge_client/protocol.rb', line 45 def tag_end(name) case name when "board" puts @gamestate.board.to_s when "condition" puts "Game ended" @network.disconnect 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
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 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 |
# File 'lib/software_challenge_client/protocol.rb', line 61 def tag_start(name, attrs) case name when "room" @roomID = attrs['roomId'] puts "roomId : "+@roomID when "data" puts "data(class) : "+attrs['class'] if attrs['class'] == "sc.framework.plugins.protocol.MoveRequest" @client.gamestate = self.gamestate move = @client.getMove document = REXML::Document.new document.add_element('room',{'roomId' => @roomID}) data = REXML::Element.new('data') data.add_attribute('class', 'move') data.add_attribute('x', move.x) data.add_attribute('y', move.y) document.root.add_element(data) for h in move.hints hint = REXML::Element.new('hint') hint.add_attribute('content', h.content) document.root.elements['data'].elements << hint end self.sendXml(document) end if attrs['class'] == "error" puts "Game ended - ERROR" puts attrs['message'] @network.disconnect end when "state" puts 'new gamestate' @gamestate.turn = attrs['turn'].to_i @gamestate.startPlayerColor = attrs['startPlayer'] == 'RED' ? PlayerColor::RED : PlayerColor::BLUE @gamestate.currentPlayerColor = attrs['currentPlayer'] == 'RED' ? PlayerColor::RED : PlayerColor::BLUE puts "Turn: #{@gamestate.turn}" when "red" puts 'new red player' @gamestate.addPlayer(Player.new(attrs['color'] == 'RED' ? PlayerColor::RED : PlayerColor::BLUE)) @gamestate.red.points = attrs['points'].to_i when "blue" puts 'new blue player' @gamestate.addPlayer(Player.new(attrs['color'] == 'RED' ? PlayerColor::RED : PlayerColor::BLUE)) @gamestate.blue.points = attrs['points'].to_i when "board" puts 'new board' @gamestate.board = Board.new(true) when "field" type = FieldType::NORMAL ownerColor = PlayerColor::NONE case attrs['type'] when 'SWAMP' type = FieldType::SWAMP when 'RED' type = FieldType::RED when 'BLUE' type = FieldType::BLUE when "winner" puts "Game ended" @network.disconnect end case attrs['owner'] when 'RED' ownerColor = PlayerColor::RED when 'BLUE' ownerColor = PlayerColor::BLUE end x = attrs['x'].to_i y = attrs['y'].to_i @gamestate.board.fields[x][y] = Field.new(type, x, y) @gamestate.board.fields[x][y].ownerColor = ownerColor when "connection" @gamestate.board.connections.push(Connection.new(attrs['x1'].to_i, attrs['y1'].to_i, attrs['x2'].to_i, attrs['y2'].to_i, attrs['owner'])) when "lastMove" @gamestate.lastMove = Move.new(attrs['x'], attrs['y']) when "condition" @gamestate.condition = Condition.new(attrs['winner'], attrs['reason']) end end |