Class: Q3Servers::Server

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

Constant Summary collapse

STATUS =
i[request request_status response response_status destroyed].freeze
GT =
{ '0' => 'FFA', '4' => 'TS', '7' => 'CTF', '9' => 'JMP' }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip, port, info) ⇒ Server

Returns a new instance of Server.



10
11
12
13
# File 'lib/q3_servers/server.rb', line 10

def initialize(ip, port, info)
  @connection = ServerConnection.new(ip, port)
  @info = info || {}
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



5
6
7
# File 'lib/q3_servers/server.rb', line 5

def connection
  @connection
end

#infoObject

Returns the value of attribute info.



5
6
7
# File 'lib/q3_servers/server.rb', line 5

def info
  @info
end

#statusObject

Returns the value of attribute status.



5
6
7
# File 'lib/q3_servers/server.rb', line 5

def status
  @status
end

Instance Method Details

#destroy_socketObject



77
78
79
80
# File 'lib/q3_servers/server.rb', line 77

def destroy_socket
  status = :destroyed
  connection.socket.close
end

#filter_info(filter) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/q3_servers/server.rb', line 23

def filter_info(filter)
  return false if info.empty?
  return true if filter.empty?

  info['hostname'] = name_c_sanitized if info.key?('hostname')
  f = filter.select { |k, v| info.key?(k.to_s) and (info[k.to_s].downcase =~ /#{v.to_s.downcase}/) }
  !f.empty?
end

#gametypeObject



19
20
21
# File 'lib/q3_servers/server.rb', line 19

def gametype
  GT.fetch(info['gametype'], '')
end

#get_info_connectObject



32
33
34
35
# File 'lib/q3_servers/server.rb', line 32

def get_info_connect
  request_info
  read_info
end

#info_statusObject



90
91
92
# File 'lib/q3_servers/server.rb', line 90

def info_status
  info['sv_status'] || {}
end

#name_c_sanitizedObject



15
16
17
# File 'lib/q3_servers/server.rb', line 15

def name_c_sanitized
  info['hostname'].gsub(/(\^[0-9]{1})/, '') if info.key?('hostname')
end

#read_infoObject



49
50
51
52
53
54
# File 'lib/q3_servers/server.rb', line 49

def read_info
  self.status = :response
  self.info = connection.read_info_server
  touch!
  info
end

#read_statusObject



56
57
58
59
60
61
# File 'lib/q3_servers/server.rb', line 56

def read_status
  self.status = :response_status
  info['sv_status'] = connection.read_status_server
  status_touch!
  info['sv_status']
end

#request_and_get_statusObject



63
64
65
66
67
# File 'lib/q3_servers/server.rb', line 63

def request_and_get_status
  info['sv_status'] = connection.request_and_get_server_status # step 2 more info from server
  status_touch!
  info['sv_status']
end

#request_infoObject



37
38
39
40
41
# File 'lib/q3_servers/server.rb', line 37

def request_info
  self.status = :request
  puts "Requesting info to #{connection}"
  connection.request_info_server
end

#request_statusObject



43
44
45
46
47
# File 'lib/q3_servers/server.rb', line 43

def request_status
  self.status = :request_status
  puts "Requesting status to #{connection}"
  connection.request_status_server
end

#socketObject



69
70
71
# File 'lib/q3_servers/server.rb', line 69

def socket
  connection.socket
end

#status_updated_atObject



86
87
88
# File 'lib/q3_servers/server.rb', line 86

def status_updated_at
  info.dig('sv_status', 'updated_at')
end

#unique_indexObject



73
74
75
# File 'lib/q3_servers/server.rb', line 73

def unique_index
  connection.unique_index
end

#updated_atObject



82
83
84
# File 'lib/q3_servers/server.rb', line 82

def updated_at
  info['updated_at']
end