old_gamespy_query 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/old_gamespy_query.rb +2 -1
- data/lib/old_gamespy_query/parser.rb +102 -15
- data/lib/old_gamespy_query/server_data.rb +28 -6
- data/lib/old_gamespy_query/version.rb +1 -1
- metadata +15 -22
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8f54d93bfc818e02160f94962481e85af83be531
|
4
|
+
data.tar.gz: 9c8edf4c0b4c6e756a0a963801245b56d5b649c2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9061eb11c935e656e59ae6553cdd0b27ecc67be9e4e966b4ca442fa85a2151762341c24c1027b22d8caa9188b9e2fe0cc2ef7d0ccf64fada5db35a62881ea843
|
7
|
+
data.tar.gz: fe240cca045c514ea64268c4cd8537ee5cf4e174ddb90f3e0cd066ffddcd5e7756c65213f08d9e36d1a48997a9f399f53f6f1f20db115823682408e1c009f5c1
|
data/lib/old_gamespy_query.rb
CHANGED
@@ -1,12 +1,34 @@
|
|
1
1
|
class OldGameSpyQuery
|
2
|
+
class QueryObject
|
3
|
+
attr_accessor :data
|
4
|
+
def initialize(status, players, general, data)
|
5
|
+
@data = OpenStruct.new(:status => status, :players => players, :general => general, :data => data)
|
6
|
+
return @data
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class PlayerObject
|
11
|
+
attr_accessor :data
|
12
|
+
def initialize(hash)
|
13
|
+
@data = OpenStruct.new(hash)
|
14
|
+
return @data
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
2
18
|
class Parser
|
3
|
-
def initialize(
|
4
|
-
@
|
19
|
+
def initialize(socket_array, mode = 'status', multi_packet = false)
|
20
|
+
@socket_array = socket_array
|
5
21
|
@mode = mode
|
22
|
+
@multi_packet = multi_packet
|
6
23
|
@data = {}
|
7
|
-
|
8
|
-
@
|
24
|
+
|
25
|
+
@players = []
|
26
|
+
@status = {}
|
27
|
+
@general = {}
|
28
|
+
|
9
29
|
parse
|
30
|
+
|
31
|
+
@struct = OldGameSpyQuery::QueryObject.new(@status, @players, @general, @data)
|
10
32
|
end
|
11
33
|
|
12
34
|
def parse
|
@@ -16,57 +38,122 @@ class OldGameSpyQuery
|
|
16
38
|
when 'players'
|
17
39
|
players_parse
|
18
40
|
when 'rules'
|
19
|
-
|
41
|
+
general_parse
|
20
42
|
when 'info'
|
21
|
-
|
43
|
+
general_parse
|
22
44
|
end
|
23
45
|
|
46
|
+
@data["status"] = @status
|
47
|
+
@data["players"] = @players
|
48
|
+
@data["general"] = @general
|
24
49
|
@data["_gamespy"] = {
|
25
|
-
"hostname" => "#{@
|
50
|
+
"hostname" => "#{@socket_array[0][1][3]}",
|
51
|
+
"gamespy_port" => @socket_array[0][1][1]
|
26
52
|
}
|
27
53
|
end
|
28
54
|
|
29
|
-
def
|
55
|
+
def array_object(index = 0)
|
56
|
+
if @socket_array.count >= 2
|
57
|
+
@socket_array[index][0].force_encoding('utf-8').strip.split("\\")
|
58
|
+
else
|
59
|
+
@socket_array[0].force_encoding('utf-8').strip.split("\\")
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Generic key -> value
|
64
|
+
def general_parse(_index = 0)
|
65
|
+
array = @socket_array[_index][0].force_encoding('utf-8').strip.split("\\")
|
66
|
+
array.delete(array.first) # Remove blank first item
|
67
|
+
|
30
68
|
keys = []
|
31
69
|
values = []
|
32
70
|
|
33
|
-
|
71
|
+
array.each_with_index do |object, index|
|
34
72
|
keys << object if index.even?
|
35
73
|
values << object if index.odd?
|
36
74
|
end
|
37
75
|
|
38
76
|
keys.each_with_index do |key, index|
|
39
77
|
value = values[index]
|
40
|
-
@
|
78
|
+
@status["#{key}"] = value if @mode == 'status'
|
79
|
+
@general["#{key}"]= value if @mode != 'status'
|
41
80
|
end
|
42
81
|
end
|
43
82
|
|
44
|
-
def
|
83
|
+
def status_parse
|
84
|
+
if @multi_packet
|
85
|
+
@socket_array.each_with_index do |packet, index|
|
86
|
+
array = @socket_array[index][0].force_encoding('utf-8').strip.split("\\")
|
87
|
+
array.delete(array.first) # Remove blank first item
|
88
|
+
|
89
|
+
if array.find{|string| true if string == "gamename"}
|
90
|
+
general_parse(index)
|
91
|
+
end
|
92
|
+
if array.find {|string| true if string.include?("player_")}
|
93
|
+
players_parse(index)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
else
|
97
|
+
general_parse
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def players_parse(_index = 0)
|
102
|
+
array = array_object(_index)
|
103
|
+
array.delete(array.first) # Remove blank first item
|
104
|
+
|
45
105
|
sub_hash = {}
|
46
106
|
keys = []
|
47
107
|
values = []
|
48
|
-
|
108
|
+
if array.first.include?("_")
|
109
|
+
_num = Integer(array.first.split('_')[1])
|
110
|
+
else
|
111
|
+
_num = 0
|
112
|
+
end
|
49
113
|
|
50
|
-
|
114
|
+
num = _num
|
115
|
+
|
116
|
+
array.each_with_index do |object, index|
|
51
117
|
keys << object if index.even?
|
52
118
|
values << object if index.odd?
|
53
119
|
end
|
54
120
|
|
55
121
|
keys.each_with_index do |key, index|
|
56
122
|
value = values[index]
|
123
|
+
|
57
124
|
if key.end_with?("#{num}")
|
58
125
|
sub_hash["#{key}"] = value
|
59
126
|
elsif key.end_with?("#{num+1}")
|
60
|
-
|
127
|
+
_hash = clean_player_hash(sub_hash)
|
128
|
+
_player = OldGameSpyQuery::PlayerObject.new(_hash)
|
129
|
+
@players << _player
|
61
130
|
sub_hash = {}
|
62
131
|
num+=1
|
63
132
|
sub_hash["#{key}"] = value
|
133
|
+
else
|
134
|
+
unless key == "queryid"
|
135
|
+
puts "Hi, umm, an error: 'key' did not end with 'num' or 'num+1'"
|
136
|
+
puts "Here is the output of: Key:Value-Num"
|
137
|
+
p "#{key}:#{value}-#{num}"
|
138
|
+
puts "-----------------------------------------------------------"
|
139
|
+
end
|
64
140
|
end
|
65
141
|
end
|
66
142
|
end
|
67
143
|
|
144
|
+
def clean_player_hash(hash)
|
145
|
+
temp_hash = {}
|
146
|
+
|
147
|
+
hash.each_pair do |key, value|
|
148
|
+
array = key.split("_")
|
149
|
+
temp_hash[array[0]] = value
|
150
|
+
end
|
151
|
+
|
152
|
+
return temp_hash
|
153
|
+
end
|
154
|
+
|
68
155
|
def data
|
69
|
-
@data
|
156
|
+
@struct.data
|
70
157
|
end
|
71
158
|
end
|
72
159
|
end
|
@@ -2,6 +2,7 @@ class OldGameSpyQuery
|
|
2
2
|
class ServerData
|
3
3
|
def initialize(address = "hostname:port")
|
4
4
|
@address = address
|
5
|
+
@data = []
|
5
6
|
end
|
6
7
|
|
7
8
|
def get_server_data(request = "status")
|
@@ -14,20 +15,41 @@ class OldGameSpyQuery
|
|
14
15
|
raise "#{self.class}: can't parse \\#{request}\\"
|
15
16
|
end
|
16
17
|
|
18
|
+
retrieve_data(request)
|
19
|
+
|
20
|
+
if @data.count >= 2 # Received multiple packets
|
21
|
+
@gamespy_query = OldGameSpyQuery::Parser.new(@data, request, true).data
|
22
|
+
else
|
23
|
+
@gamespy_query = OldGameSpyQuery::Parser.new(@data, request).data
|
24
|
+
end
|
25
|
+
|
26
|
+
return data
|
27
|
+
end
|
28
|
+
|
29
|
+
def retrieve_data(request)
|
17
30
|
addr = @address.split(':')
|
18
31
|
@socket = UDPSocket.new
|
19
32
|
@socket.connect("#{addr[0]}", addr[1].to_i)
|
33
|
+
|
20
34
|
begin
|
21
|
-
timeout(
|
35
|
+
timeout(8) do
|
22
36
|
@socket.send("\\#{request}\\", 0)
|
23
|
-
|
24
|
-
|
37
|
+
loop do
|
38
|
+
data = @socket.recvfrom(4096)
|
39
|
+
_data = data[0].sub("\\final\\", '')
|
40
|
+
a = data.dup
|
41
|
+
a[0] = _data
|
42
|
+
@data << a
|
43
|
+
|
44
|
+
if data[0].include?("\\final\\")
|
45
|
+
break
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
25
50
|
rescue Timeout::Error
|
26
51
|
raise Timeout::Error, "#{self.class}: The Server At '#{@address}' Did Not Respond In Time (Within 5 Seconds)"
|
27
52
|
end
|
28
|
-
|
29
|
-
@data[0].sub!("\\final\\", '')
|
30
|
-
@gamespy_query = OldGameSpyQuery::Parser.new(@data, request).data
|
31
53
|
end
|
32
54
|
|
33
55
|
def data
|
metadata
CHANGED
@@ -1,62 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: old_gamespy_query
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Cyberarm
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2015-
|
11
|
+
date: 2015-03-27 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: gamespy_query
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: bundler
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- - ~>
|
31
|
+
- - "~>"
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '1.7'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- - ~>
|
38
|
+
- - "~>"
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '1.7'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rake
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- - ~>
|
45
|
+
- - "~>"
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '10.0'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- - ~>
|
52
|
+
- - "~>"
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '10.0'
|
62
55
|
description: Query old GameSpy servers.
|
@@ -66,7 +59,7 @@ executables: []
|
|
66
59
|
extensions: []
|
67
60
|
extra_rdoc_files: []
|
68
61
|
files:
|
69
|
-
- .gitignore
|
62
|
+
- ".gitignore"
|
70
63
|
- Gemfile
|
71
64
|
- Gemfile.lock
|
72
65
|
- Rakefile
|
@@ -81,26 +74,26 @@ files:
|
|
81
74
|
homepage: https://github.com/cyberarm/old_gamespy_query
|
82
75
|
licenses:
|
83
76
|
- MIT
|
77
|
+
metadata: {}
|
84
78
|
post_install_message:
|
85
79
|
rdoc_options: []
|
86
80
|
require_paths:
|
87
81
|
- lib
|
88
82
|
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
83
|
requirements:
|
91
|
-
- -
|
84
|
+
- - ">="
|
92
85
|
- !ruby/object:Gem::Version
|
93
86
|
version: '0'
|
94
87
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
-
none: false
|
96
88
|
requirements:
|
97
|
-
- -
|
89
|
+
- - ">="
|
98
90
|
- !ruby/object:Gem::Version
|
99
91
|
version: '0'
|
100
92
|
requirements: []
|
101
93
|
rubyforge_project:
|
102
|
-
rubygems_version:
|
94
|
+
rubygems_version: 2.2.2
|
103
95
|
signing_key:
|
104
|
-
specification_version:
|
96
|
+
specification_version: 4
|
105
97
|
summary: Query old GameSpy servers.
|
106
98
|
test_files: []
|
99
|
+
has_rdoc:
|