Class: ServerConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/game_2d/server_connection.rb

Overview

An instance of this class is created by ServerPort whenever an incoming connection is accepted.

Instance Method Summary collapse

Constructor Details

#initialize(port, game, server, id, remote_addr) ⇒ ServerConnection

Returns a new instance of ServerConnection.



9
10
11
12
# File 'lib/game_2d/server_connection.rb', line 9

def initialize(port, game, server, id, remote_addr)
  @port, @game, @server, @id, @remote_addr = port, game, server, id, remote_addr
  puts "ServerConnection: New connection #{id} from #{remote_addr}"
end

Instance Method Details

#add_npc(npc, at_tick) ⇒ Object



45
46
47
# File 'lib/game_2d/server_connection.rb', line 45

def add_npc(npc, at_tick)
  send_record :add_npcs => [ npc ], :at_tick => at_tick
end

#add_player(player, at_tick) ⇒ Object



49
50
51
# File 'lib/game_2d/server_connection.rb', line 49

def add_player(player, at_tick)
  send_record :add_players => [ player ], :at_tick => at_tick
end

#answer_handshake(handshake) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/game_2d/server_connection.rb', line 14

def answer_handshake(handshake)
  player_name = handshake[:player_name]
  player = @game.add_player(player_name)
  @player_id = player.registry_id
  @port.register_player @player_id, self

  response = {
    :you_are => @player_id,
    :world => {
      :world_name => @game.world_name,
      :world_id => @game.world_id,
      :highest_id => @game.world_highest_id,
      :cell_width => @game.world_cell_width,
      :cell_height => @game.world_cell_height,
    },
    :add_players => @game.get_all_players,
    :add_npcs => @game.get_all_npcs,
    :at_tick => @game.tick,
  }
  puts "#{player} logs in from #{@remote_addr} at <#{@game.tick}>"
  send_record response, true # answer handshake reliably
end

#answer_ping(ping) ⇒ Object



41
42
43
# File 'lib/game_2d/server_connection.rb', line 41

def answer_ping(ping)
  send_record :pong => ping
end

#closeObject



66
67
68
69
70
71
# File 'lib/game_2d/server_connection.rb', line 66

def close
  @port.deregister_player @player_id
  toast = player
  puts "#{toast} -- #{@remote_addr} disconnected at <#{@game.tick}>"
  @game.delete_entity toast
end

#debug_packet(direction, hash) ⇒ Object



98
99
100
101
102
103
# File 'lib/game_2d/server_connection.rb', line 98

def debug_packet(direction, hash)
  return unless $debug_traffic
  at_tick = hash[:at_tick] || 'NO TICK'
  keys = hash.keys - [:at_tick]
  puts "#{direction} #{keys.join(', ')} <#{at_tick}>"
end

#delete_entity(entity, at_tick) ⇒ Object



53
54
55
# File 'lib/game_2d/server_connection.rb', line 53

def delete_entity(entity, at_tick)
  send_record :delete_entities => [ entity.registry_id ], :at_tick => at_tick
end

#on_packet(data, channel) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/game_2d/server_connection.rb', line 73

def on_packet(data, channel)
  hash = JSON.parse(data).fix_keys
  debug_packet('Received', hash)
  if (handshake = hash[:handshake])
    answer_handshake(handshake)
  elsif (hash[:save])
    @game.save
  elsif (ping = hash[:ping])
    answer_ping ping
  else
    @game.add_player_action @player_id, hash
    @port.broadcast_player_action @id,
      hash.merge(:player_id => @player_id),
      channel
  end
end

#playerObject



37
38
39
# File 'lib/game_2d/server_connection.rb', line 37

def player
  @game[@player_id]
end

#send_record(hash, reliable = false, channel = 0) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/game_2d/server_connection.rb', line 90

def send_record(hash, reliable=false, channel=0)
  debug_packet('Sending', hash)
  send_str = hash.to_json
  # Send data to the client (client ID, data, reliable or not, channel ID)
  @server.send_packet(@id, send_str, reliable, channel)
  @server.flush
end

#update_entities(entities, at_tick) ⇒ Object



57
58
59
# File 'lib/game_2d/server_connection.rb', line 57

def update_entities(entities, at_tick)
  send_record :update_entities => entities, :at_tick => at_tick
end

#update_score(player, at_tick) ⇒ Object

Not called yet…



62
63
64
# File 'lib/game_2d/server_connection.rb', line 62

def update_score(player, at_tick)
  send_record :update_score => { player.registry_id => player.score }, :at_tick => at_tick
end