Class: GameServer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ GameServer

Returns a new instance of GameServer.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/game_server.rb', line 15

def initialize(server)
  @server = server
  @ack_game_tick = -1
  @pred_game_tick = 0
  @map = Map.new(
    name: 'dm1',
    crc: '98a0a4c50c', # decimal 64548818
    size: 6793,
    sha256: '491af17a510214506270904f147a4c30ae0a85b91bb854395bef8c397fc078c3'
  )
end

Instance Attribute Details

#ack_game_tickObject

Returns the value of attribute ack_game_tick.



13
14
15
# File 'lib/game_server.rb', line 13

def ack_game_tick
  @ack_game_tick
end

#mapObject

Returns the value of attribute map.



13
14
15
# File 'lib/game_server.rb', line 13

def map
  @map
end

#pred_game_tickObject

Returns the value of attribute pred_game_tick.



13
14
15
# File 'lib/game_server.rb', line 13

def pred_game_tick
  @pred_game_tick
end

Instance Method Details

#on_client_drop(client, reason = nil) ⇒ Object



105
106
107
108
# File 'lib/game_server.rb', line 105

def on_client_drop(client, reason = nil)
  reason = reason.nil? ? '' : " (#{reason})"
  puts "'#{client.player.name}' left the game#{reason}"
end

#on_emoticon(chunk, _packet) ⇒ Object



27
28
29
30
# File 'lib/game_server.rb', line 27

def on_emoticon(chunk, _packet)
  message = ClEmoticon.new(chunk.data[1..])
  p message
end

#on_enter_game(_chunk, packet) ⇒ Object



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

def on_enter_game(_chunk, packet)
  # vanilla server responds to enter game with two packets
  # first:
  #  - server info
  # second:
  #  - game info
  #  - client info
  #  - snap single
  packet.client.in_game = true
  @server.send_server_info(packet.client, ServerInfo.new.to_a)
  @server.send_game_info(packet.client, GameInfo.new.to_a)

  puts "'#{packet.client.player.name}' joined the game"
end

#on_info(chunk, packet) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/game_server.rb', line 32

def on_info(chunk, packet)
  u = Unpacker.new(chunk.data[1..])
  net_version = u.get_string
  password = u.get_string
  client_version = u.get_int
  puts "vers=#{net_version} vers=#{client_version} pass=#{password}"

  # TODO: check version and password

  @server.send_map(packet.client)
end

#on_input(chunk, packet) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/game_server.rb', line 96

def on_input(chunk, packet)
  # vanilla server responds to input with 2 chunks
  #  - input_timing
  #  - snap (empty)

  # we do nothing for now
  # TODO: do something
end

#on_rcon_cmd(chunk, _packet) ⇒ Object



90
91
92
93
94
# File 'lib/game_server.rb', line 90

def on_rcon_cmd(chunk, _packet)
  u = Unpacker.new(chunk.data[1..])
  cmd = u.get_string
  puts "got rcon_cmd=#{cmd}"
end

#on_ready(_chunk, packet) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/game_server.rb', line 44

def on_ready(_chunk, packet)
  # vanilla server sends 3 chunks here usually
  #  - motd
  #  - server settings
  #  - ready
  #
  @server.send_server_settings(packet.client, ServerSettings.new.to_a)
  @server.send_ready(packet.client)
end

#on_say(chunk, packet) ⇒ Object



68
69
70
71
72
73
# File 'lib/game_server.rb', line 68

def on_say(chunk, packet)
  say = ClSay.new(chunk.data[1..])
  author = packet.client.player
  msg = ChatMesage.new(say.to_h.merge(client_id: author.id, author:))
  puts msg.to_s
end

#on_start_info(chunk, packet) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/game_server.rb', line 54

def on_start_info(chunk, packet)
  # vanilla server sends 3 chunks here usually
  #  - vote clear options
  #  - tune params
  #  - ready to enter
  #
  # We only send ready to enter for now
  info = StartInfo.new(chunk.data[1..])
  packet.client.player.set_start_info(info)
  info_str = info.to_s
  puts "got start info: #{info_str}" if @verbose
  @server.send_ready_to_enter(packet.client)
end

#on_tickObject



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

def on_tick
  now = Time.now
  timeout_ids = []
  @server.clients.each do |id, client|
    diff = now - client.last_recv_time
    timeout_ids.push(id) if diff > 10
  end

  timeout_ids.each do |id|
    @server.drop_client(@server.clients[id], 'Timeout')
  end
end