Class: BTetrisKp::NetGameState

Inherits:
Object
  • Object
show all
Defined in:
lib/btetris_kp/netgame.rb

Overview

class representing net game window state used for playin game over network (no matter if you are client or server)

Instance Method Summary collapse

Constructor Details

#initialize(window, socket) ⇒ NetGameState

Returns a new instance of NetGameState.



10
11
12
13
14
15
16
17
# File 'lib/btetris_kp/netgame.rb', line 10

def initialize(window, socket)
  @window = window
  @socket = socket
  @font = Gosu::Font.new(@window, Gosu.default_font_name, Const::FONT_BIG_SIZE)
  @game = GameState.new(@window, 10, 40)
  @o_board = Board.new(@window, 2 * @window.width / 3 - 10, 40)
  @winner = 0
end

Instance Method Details

#button_down(id) ⇒ Object

button handler



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/btetris_kp/netgame.rb', line 125

def button_down(id)
  if @winner == Const::GAME_ON
    case id
    when Gosu::KbP
      send_msg(Const::MSG_PAUSE)
      @game.pause!
    when Gosu::KbEscape
      send_msg(Const::MSG_GAME_OVER)
      sleep(0.2)
      @socket.close
      @window.state = MenuState.new(@window)
    else
      @game.button_down(id)
    end
  else
    if id == Gosu::KbEscape
      @socket.close
      @window.state = MenuState.new(@window)
    end
  end
end

#check_msgObject

checks if there is incomming message returns a string with message id, or GOT_NO_MESSAGE



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/btetris_kp/netgame.rb', line 74

def check_msg
  begin
    s = @socket.recv_nonblock(1)
    return s
  rescue Errno::EAGAIN
    # zadna zprava, hrajeme dal
  rescue
    # problem s pripojenim -- game over
    @socket.close
    @window.state = MenuState.new(@window)
  rescue
  end
  Const::GOT_NO_MESSAGE
end

#drawObject

draws net game window (contains normal game + opponents board)



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/btetris_kp/netgame.rb', line 111

def draw
  if @winner == Const::GAME_ON
    @game.draw
    @o_board.draw
  elsif @winner == Const::GAME_LOST
    @font.draw(Const::GAME_LOST_CAPTION,
               @window.width / 2 - 170, @window.height / 2 - 30, 0)
  else
    @font.draw(Const::GAME_WON_CAPTION,
               @window.width / 2 - 170, @window.height / 2 - 30, 0)
  end
end

#needs_cursor?Boolean

hides default system cursor

Returns:

  • (Boolean)


148
149
150
# File 'lib/btetris_kp/netgame.rb', line 148

def needs_cursor?
  true
end

#recv_block(cnt) ⇒ Object

blocking recv for cnt of bytes



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/btetris_kp/netgame.rb', line 42

def recv_block(cnt)
  block = nil
  begin
    block = @socket.recv(cnt, Socket::MSG_WAITALL)
  rescue
    # problem s pripojenim -- game over
    @socket.close
    @window.state = MenuState.new(@window)
  end
  block
end

#recv_msg(id) ⇒ Object

recieves and manages game updates



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/btetris_kp/netgame.rb', line 55

def recv_msg(id)
  case id
  when Const::MSG_PAUSE
    @game.pause!
  when Const::MSG_GAME_OVER
    @winner = Const::GAME_WON
  when Const::MSG_BOARD
    recv_block(1)
    board = recv_block(Const::PNR_HOR * Const::PNR_VER)
    @o_board.from_s!(board) unless board.nil?
  when Const::MSG_GARBAGE
    recv_block(1)
    garbage = recv_block(1).to_i
    @game.insert_garbage!(garbage)
  end
end

#send_msg(msg) ⇒ Object

sends message to socket



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/btetris_kp/netgame.rb', line 20

def send_msg(msg)
  s = ''
  case msg
  when Const::MSG_PAUSE
    s = "#{Const::MSG_PAUSE}"
  when Const::MSG_GAME_OVER
    s = "#{Const::MSG_GAME_OVER}"
  when Const::MSG_BOARD
    s = "#{Const::MSG_BOARD}:#{@game.get_board_s}"
  when Const::MSG_GARBAGE
    s = "#{Const::MSG_GARBAGE}:#{@game.rows_cleared}"
  end
  begin
    @socket.sendmsg_nonblock(s)
  rescue
    # problem s pripojenim -- game over
    @socket.close
    @window.state = MenuState.new(@window)
  end
end

#updateObject

updates net game state



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/btetris_kp/netgame.rb', line 90

def update
  if @winner == Const::GAME_ON
    until (m = check_msg) == Const::GOT_NO_MESSAGE
      recv_msg(m)
    end
    unless @game.paused
      @game.update
      send_msg(Const::MSG_BOARD)
      if @game.game_over
        send_msg(Const::MSG_GAME_OVER)
        @winner = Const::GAME_LOST
      end
      if @game.rows_cleared > 0
        send_msg(Const::MSG_GARBAGE)
        @game.rows_cleared = 0
      end
    end
  end
end