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\Buffer; |
22: | use GameQ\Exception\Protocol as Exception; |
23: | use GameQ\Helpers\Str; |
24: | use GameQ\Protocol; |
25: | use GameQ\Result; |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | class Gamespy extends Protocol |
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: | |
71: | public function processResponse() |
72: | { |
73: | |
74: | $processed = []; |
75: | |
76: | |
77: | foreach ($this->packets_response as $response) { |
78: | |
79: | if (($match = preg_match("#^(.*)\\\\queryid\\\\([^\\\\]+)(\\\\|$)#", $response, $matches)) === false |
80: | || $match != 1 |
81: | ) { |
82: | throw new Exception(__METHOD__ . " An error occurred while parsing the packets for 'queryid'"); |
83: | } |
84: | |
85: | |
86: | $key = (int)(floatval($matches[2]) * 1000); |
87: | |
88: | |
89: | $processed[$key] = $matches[1]; |
90: | } |
91: | |
92: | |
93: | ksort($processed, SORT_NUMERIC); |
94: | |
95: | |
96: | return $this->processStatus(new Buffer(implode('', $processed))); |
97: | } |
98: | |
99: | |
100: | |
101: | |
102: | |
103: | |
104: | |
105: | |
106: | |
107: | |
108: | protected function processStatus(Buffer $buffer) |
109: | { |
110: | |
111: | $result = new Result(); |
112: | |
113: | |
114: | $result->add('dedicated', 1); |
115: | |
116: | |
117: | if ($buffer->lookAhead(1) == '\\') { |
118: | |
119: | $buffer->skip(1); |
120: | } |
121: | |
122: | |
123: | $data = explode('\\', $buffer->getBuffer()); |
124: | |
125: | |
126: | unset($buffer); |
127: | |
128: | |
129: | $numPlayers = 0; |
130: | $numTeams = 0; |
131: | |
132: | $itemCount = count($data); |
133: | |
134: | |
135: | if (count($data) > 1) { |
136: | |
137: | for ($x = 0; $x < $itemCount; $x += 2) { |
138: | |
139: | $key = $data[$x]; |
140: | $val = $data[$x + 1]; |
141: | |
142: | |
143: | if (($suffix = strrpos($key, '_')) !== false && is_numeric(substr($key, $suffix + 1))) { |
144: | |
145: | if (substr($key, 0, $suffix) == 'teamname') { |
146: | $result->addTeam('teamname', $val); |
147: | $numTeams++; |
148: | } else { |
149: | |
150: | if (substr($key, 0, $suffix) == 'playername') { |
151: | $numPlayers++; |
152: | } |
153: | $result->addPlayer(substr($key, 0, $suffix), Str::isoToUtf8($val)); |
154: | } |
155: | } else { |
156: | |
157: | $result->add($key, $val); |
158: | } |
159: | } |
160: | } |
161: | |
162: | |
163: | $result->add('num_players', $numPlayers); |
164: | $result->add('num_teams', $numTeams); |
165: | |
166: | |
167: | unset($data, $key, $val, $suffix, $x, $itemCount); |
168: | |
169: | |
170: | return $result->fetch(); |
171: | } |
172: | } |
173: | |