| 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\Protocol; |
| 24: | use GameQ\Result; |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | class Bf3 extends Protocol |
| 35: | { |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | protected $packets = [ |
| 42: | self::PACKET_STATUS => "\x00\x00\x00\x21\x1b\x00\x00\x00\x01\x00\x00\x00\x0a\x00\x00\x00serverInfo\x00", |
| 43: | self::PACKET_VERSION => "\x00\x00\x00\x22\x18\x00\x00\x00\x01\x00\x00\x00\x07\x00\x00\x00version\x00", |
| 44: | self::PACKET_PLAYERS => |
| 45: | "\x00\x00\x00\x23\x24\x00\x00\x00\x02\x00\x00\x00\x0b\x00\x00\x00listPlayers\x00\x03\x00\x00\x00\x61ll\x00", |
| 46: | ]; |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | protected $responses = [ |
| 54: | 1627389952 => "processDetails", |
| 55: | 1644167168 => "processVersion", |
| 56: | 1660944384 => "processPlayers", |
| 57: | ]; |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | protected $transport = self::TRANSPORT_TCP; |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | protected $protocol = 'bf3'; |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | protected $name = 'bf3'; |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | protected $name_long = "Battlefield 3"; |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | protected $port_diff = 22000; |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | protected $normalize = [ |
| 101: | |
| 102: | 'general' => [ |
| 103: | |
| 104: | 'dedicated' => 'dedicated', |
| 105: | 'hostname' => 'hostname', |
| 106: | 'mapname' => 'map', |
| 107: | 'maxplayers' => 'max_players', |
| 108: | 'numplayers' => 'num_players', |
| 109: | 'password' => 'password', |
| 110: | ], |
| 111: | 'player' => [ |
| 112: | 'name' => 'name', |
| 113: | 'score' => 'score', |
| 114: | 'ping' => 'ping', |
| 115: | ], |
| 116: | 'team' => [ |
| 117: | 'score' => 'tickets', |
| 118: | ], |
| 119: | ]; |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | public function processResponse() |
| 128: | { |
| 129: | |
| 130: | $results = []; |
| 131: | |
| 132: | |
| 133: | $processed = []; |
| 134: | |
| 135: | |
| 136: | $sequence_id_last = 0; |
| 137: | |
| 138: | foreach ($this->packets_response as $packet) { |
| 139: | |
| 140: | $buffer = new Buffer($packet); |
| 141: | |
| 142: | |
| 143: | $sequence_id = $buffer->readInt32(); |
| 144: | |
| 145: | |
| 146: | if (array_key_exists($sequence_id, $this->responses)) { |
| 147: | $processed[$sequence_id] = $buffer->getBuffer(); |
| 148: | $sequence_id_last = $sequence_id; |
| 149: | } else { |
| 150: | |
| 151: | $buffer->jumpto(0); |
| 152: | |
| 153: | |
| 154: | $processed[$sequence_id_last] .= $buffer->getBuffer(); |
| 155: | } |
| 156: | } |
| 157: | |
| 158: | unset($buffer, $sequence_id_last, $sequence_id); |
| 159: | |
| 160: | |
| 161: | foreach ($processed as $sequence_id => $data) { |
| 162: | |
| 163: | $buffer = new Buffer($data); |
| 164: | |
| 165: | |
| 166: | $packetLength = $buffer->getLength(); |
| 167: | |
| 168: | |
| 169: | |
| 170: | if ($packetLength != ($buffer->readInt32() - 4)) { |
| 171: | throw new Exception(__METHOD__ . " packet length does not match expected length!"); |
| 172: | } |
| 173: | |
| 174: | |
| 175: | $results = array_merge( |
| 176: | $results, |
| 177: | call_user_func_array([$this, $this->responses[$sequence_id]], [$buffer]) |
| 178: | ); |
| 179: | } |
| 180: | |
| 181: | return $results; |
| 182: | } |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | |
| 193: | |
| 194: | protected function decode(Buffer $buffer) |
| 195: | { |
| 196: | $items = []; |
| 197: | |
| 198: | |
| 199: | $itemCount = $buffer->readInt32(); |
| 200: | |
| 201: | |
| 202: | for ($i = 0; $i < $itemCount; $i++) { |
| 203: | |
| 204: | $buffer->readInt32(); |
| 205: | |
| 206: | |
| 207: | $items[$i] = $buffer->readString(); |
| 208: | } |
| 209: | |
| 210: | return $items; |
| 211: | } |
| 212: | |
| 213: | |
| 214: | |
| 215: | |
| 216: | |
| 217: | |
| 218: | |
| 219: | |
| 220: | protected function processDetails(Buffer $buffer) |
| 221: | { |
| 222: | |
| 223: | $items = $this->decode($buffer); |
| 224: | |
| 225: | |
| 226: | $result = new Result(); |
| 227: | |
| 228: | |
| 229: | $result->add('dedicated', 1); |
| 230: | |
| 231: | |
| 232: | $result->add('hostname', $items[1]); |
| 233: | $result->add('num_players', (int)$items[2]); |
| 234: | $result->add('max_players', (int)$items[3]); |
| 235: | $result->add('gametype', $items[4]); |
| 236: | $result->add('map', $items[5]); |
| 237: | $result->add('roundsplayed', (int)$items[6]); |
| 238: | $result->add('roundstotal', (int)$items[7]); |
| 239: | $result->add('num_teams', (int)$items[8]); |
| 240: | |
| 241: | |
| 242: | $index_current = 9; |
| 243: | |
| 244: | |
| 245: | $teamCount = $result->get('num_teams'); |
| 246: | |
| 247: | |
| 248: | for ($id = 1; $id <= $teamCount; $id++, $index_current++) { |
| 249: | |
| 250: | $result->addTeam('tickets', $items[$index_current]); |
| 251: | |
| 252: | $result->addTeam('id', $id); |
| 253: | } |
| 254: | |
| 255: | |
| 256: | $result->add('targetscore', (int)$items[$index_current]); |
| 257: | $result->add('online', 1); |
| 258: | $result->add('ranked', (int)$items[$index_current + 2]); |
| 259: | $result->add('punkbuster', (int)$items[$index_current + 3]); |
| 260: | $result->add('password', (int)$items[$index_current + 4]); |
| 261: | $result->add('uptime', (int)$items[$index_current + 5]); |
| 262: | $result->add('roundtime', (int)$items[$index_current + 6]); |
| 263: | |
| 264: | $result->add('ip_port', $items[$index_current + 7]); |
| 265: | $result->add('punkbuster_version', $items[$index_current + 8]); |
| 266: | $result->add('join_queue', (int)$items[$index_current + 9]); |
| 267: | $result->add('region', $items[$index_current + 10]); |
| 268: | $result->add('pingsite', $items[$index_current + 11]); |
| 269: | $result->add('country', $items[$index_current + 12]); |
| 270: | |
| 271: | $result->add('quickmatch', (int)$items[$index_current + 13]); |
| 272: | |
| 273: | unset($items, $index_current, $teamCount, $buffer); |
| 274: | |
| 275: | return $result->fetch(); |
| 276: | } |
| 277: | |
| 278: | |
| 279: | |
| 280: | |
| 281: | |
| 282: | |
| 283: | |
| 284: | |
| 285: | |
| 286: | protected function processVersion(Buffer $buffer) |
| 287: | { |
| 288: | |
| 289: | $items = $this->decode($buffer); |
| 290: | |
| 291: | |
| 292: | $result = new Result(); |
| 293: | |
| 294: | $result->add('version', $items[2]); |
| 295: | |
| 296: | unset($buffer, $items); |
| 297: | |
| 298: | return $result->fetch(); |
| 299: | } |
| 300: | |
| 301: | |
| 302: | |
| 303: | |
| 304: | |
| 305: | |
| 306: | |
| 307: | |
| 308: | |
| 309: | protected function processPlayers(Buffer $buffer) |
| 310: | { |
| 311: | |
| 312: | $items = $this->decode($buffer); |
| 313: | |
| 314: | |
| 315: | $result = new Result(); |
| 316: | |
| 317: | |
| 318: | $numTags = $items[1]; |
| 319: | |
| 320: | |
| 321: | $tags = array_slice($items, 2, $numTags); |
| 322: | |
| 323: | |
| 324: | $playerCount = $items[$numTags + 2]; |
| 325: | |
| 326: | |
| 327: | for ($i = 0, $x = $numTags + 3; $i < $playerCount; $i++, $x += $numTags) { |
| 328: | |
| 329: | foreach ($tags as $index => $tag) { |
| 330: | $result->addPlayer($tag, $items[($x + $index)]); |
| 331: | } |
| 332: | } |
| 333: | |
| 334: | return $result->fetch(); |
| 335: | } |
| 336: | } |
| 337: | |