1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | namespace GameQ\Protocols; |
20: | |
21: | use GameQ\Protocol; |
22: | use GameQ\Buffer; |
23: | use GameQ\Result; |
24: | use \GameQ\Exception\Protocol as Exception; |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | class Gamespy extends Protocol |
32: | { |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | protected $packets = [ |
41: | self::PACKET_STATUS => "\x5C\x73\x74\x61\x74\x75\x73\x5C", |
42: | ]; |
43: | |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | protected $protocol = 'gamespy'; |
50: | |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | protected $name = 'gamespy'; |
57: | |
58: | |
59: | |
60: | |
61: | |
62: | |
63: | protected $name_long = "GameSpy Server"; |
64: | |
65: | |
66: | |
67: | |
68: | |
69: | |
70: | protected $join_link = null; |
71: | |
72: | |
73: | |
74: | |
75: | |
76: | |
77: | |
78: | public function processResponse() |
79: | { |
80: | |
81: | $processed = []; |
82: | |
83: | |
84: | foreach ($this->packets_response as $response) { |
85: | |
86: | if (($match = preg_match("#^(.*)\\\\queryid\\\\([^\\\\]+)(\\\\|$)#", $response, $matches)) === false |
87: | || $match != 1 |
88: | ) { |
89: | throw new Exception(__METHOD__ . " An error occurred while parsing the packets for 'queryid'"); |
90: | } |
91: | |
92: | |
93: | $key = (int)(floatval($matches[2]) * 1000); |
94: | |
95: | |
96: | $processed[$key] = $matches[1]; |
97: | } |
98: | |
99: | |
100: | ksort($processed, SORT_NUMERIC); |
101: | |
102: | |
103: | return $this->processStatus(new Buffer(implode('', $processed))); |
104: | } |
105: | |
106: | |
107: | |
108: | |
109: | |
110: | |
111: | |
112: | |
113: | |
114: | |
115: | |
116: | |
117: | protected function processStatus(Buffer $buffer) |
118: | { |
119: | |
120: | $result = new Result(); |
121: | |
122: | |
123: | $result->add('dedicated', 1); |
124: | |
125: | |
126: | if ($buffer->lookAhead(1) == '\\') { |
127: | |
128: | $buffer->skip(1); |
129: | } |
130: | |
131: | |
132: | $data = explode('\\', $buffer->getBuffer()); |
133: | |
134: | |
135: | unset($buffer); |
136: | |
137: | |
138: | $numPlayers = 0; |
139: | $numTeams = 0; |
140: | |
141: | $itemCount = count($data); |
142: | |
143: | |
144: | if (count($data) > 1) { |
145: | |
146: | for ($x = 0; $x < $itemCount; $x += 2) { |
147: | |
148: | $key = $data[$x]; |
149: | $val = $data[$x + 1]; |
150: | |
151: | |
152: | if (($suffix = strrpos($key, '_')) !== false && is_numeric(substr($key, $suffix + 1))) { |
153: | |
154: | if (substr($key, 0, $suffix) == 'teamname') { |
155: | $result->addTeam('teamname', $val); |
156: | $numTeams++; |
157: | } else { |
158: | |
159: | if (substr($key, 0, $suffix) == 'playername') { |
160: | $numPlayers++; |
161: | } |
162: | $result->addPlayer(substr($key, 0, $suffix), utf8_encode($val)); |
163: | } |
164: | } else { |
165: | |
166: | $result->add($key, $val); |
167: | } |
168: | } |
169: | } |
170: | |
171: | |
172: | $result->add('num_players', $numPlayers); |
173: | $result->add('num_teams', $numTeams); |
174: | |
175: | |
176: | unset($data, $key, $val, $suffix, $x, $itemCount); |
177: | |
178: | |
179: | return $result->fetch(); |
180: | } |
181: | } |
182: | |