| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | namespace GameQ; |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | abstract class Protocol |
| 31: | { |
| 32: | |
| 33: | |
| 34: | |
| 35: | const STATE_TESTING = 1; |
| 36: | |
| 37: | const STATE_BETA = 2; |
| 38: | |
| 39: | const STATE_STABLE = 3; |
| 40: | |
| 41: | const STATE_DEPRECATED = 4; |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | const PACKET_ALL = 'all'; |
| 47: | |
| 48: | const PACKET_BASIC = 'basic'; |
| 49: | |
| 50: | const PACKET_CHALLENGE = 'challenge'; |
| 51: | |
| 52: | const PACKET_CHANNELS = 'channels'; |
| 53: | |
| 54: | const PACKET_DETAILS = 'details'; |
| 55: | |
| 56: | const PACKET_INFO = 'info'; |
| 57: | |
| 58: | const PACKET_PLAYERS = 'players'; |
| 59: | |
| 60: | const PACKET_STATUS = 'status'; |
| 61: | |
| 62: | const PACKET_RULES = 'rules'; |
| 63: | |
| 64: | const PACKET_VERSION = 'version'; |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | const TRANSPORT_UDP = 'udp'; |
| 70: | |
| 71: | const TRANSPORT_TCP = 'tcp'; |
| 72: | |
| 73: | const TRANSPORT_SSL = 'ssl'; |
| 74: | |
| 75: | const TRANSPORT_TLS = 'tls'; |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | protected $name = 'unknown'; |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | protected $name_long = 'unknown'; |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | protected $port_diff = 0; |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | protected $transport = self::TRANSPORT_UDP; |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | protected $protocol = 'unknown'; |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | protected $packets = []; |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | protected $responses = []; |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | protected $process_methods = []; |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | protected $packets_response = []; |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | protected $result = null; |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | protected $options = []; |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | protected $state = self::STATE_STABLE; |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | protected $normalize = [ |
| 171: | |
| 172: | 'general' => [ |
| 173: | |
| 174: | 'dedicated' => [ |
| 175: | 'listenserver', |
| 176: | 'dedic', |
| 177: | 'bf2dedicated', |
| 178: | 'netserverdedicated', |
| 179: | 'bf2142dedicated', |
| 180: | 'dedicated', |
| 181: | ], |
| 182: | 'gametype' => ['ggametype', 'sigametype', 'matchtype'], |
| 183: | 'hostname' => ['svhostname', 'servername', 'siname', 'name'], |
| 184: | 'mapname' => ['map', 'simap'], |
| 185: | 'maxplayers' => ['svmaxclients', 'simaxplayers', 'maxclients', 'max_players'], |
| 186: | 'mod' => ['game', 'gamedir', 'gamevariant'], |
| 187: | 'numplayers' => ['clients', 'sinumplayers', 'num_players'], |
| 188: | 'password' => ['protected', 'siusepass', 'sineedpass', 'pswrd', 'gneedpass', 'auth', 'passsord'], |
| 189: | ], |
| 190: | |
| 191: | 'player' => [ |
| 192: | 'name' => ['nick', 'player', 'playername', 'name'], |
| 193: | 'kills' => ['kills'], |
| 194: | 'deaths' => ['deaths'], |
| 195: | 'score' => ['kills', 'frags', 'skill', 'score'], |
| 196: | 'ping' => ['ping'], |
| 197: | ], |
| 198: | |
| 199: | 'team' => [ |
| 200: | 'name' => ['name', 'teamname', 'team_t'], |
| 201: | 'score' => ['score', 'score_t'], |
| 202: | ], |
| 203: | ]; |
| 204: | |
| 205: | |
| 206: | |
| 207: | |
| 208: | |
| 209: | |
| 210: | protected $join_link = null; |
| 211: | |
| 212: | |
| 213: | |
| 214: | |
| 215: | public function __construct(array $options = []) |
| 216: | { |
| 217: | |
| 218: | $this->options = $options; |
| 219: | } |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: | |
| 226: | public function __toString() |
| 227: | { |
| 228: | return $this->name; |
| 229: | } |
| 230: | |
| 231: | |
| 232: | |
| 233: | |
| 234: | |
| 235: | |
| 236: | public function portDiff() |
| 237: | { |
| 238: | return $this->port_diff; |
| 239: | } |
| 240: | |
| 241: | |
| 242: | |
| 243: | |
| 244: | |
| 245: | |
| 246: | |
| 247: | |
| 248: | |
| 249: | |
| 250: | public function findQueryPort($clientPort) |
| 251: | { |
| 252: | return $clientPort + $this->port_diff; |
| 253: | } |
| 254: | |
| 255: | |
| 256: | |
| 257: | |
| 258: | |
| 259: | |
| 260: | public function joinLink() |
| 261: | { |
| 262: | return $this->join_link; |
| 263: | } |
| 264: | |
| 265: | |
| 266: | |
| 267: | |
| 268: | |
| 269: | |
| 270: | public function name() |
| 271: | { |
| 272: | return $this->name; |
| 273: | } |
| 274: | |
| 275: | |
| 276: | |
| 277: | |
| 278: | |
| 279: | |
| 280: | public function nameLong() |
| 281: | { |
| 282: | return $this->name_long; |
| 283: | } |
| 284: | |
| 285: | |
| 286: | |
| 287: | |
| 288: | |
| 289: | |
| 290: | public function state() |
| 291: | { |
| 292: | return $this->state; |
| 293: | } |
| 294: | |
| 295: | |
| 296: | |
| 297: | |
| 298: | |
| 299: | |
| 300: | public function getProtocol() |
| 301: | { |
| 302: | return $this->protocol; |
| 303: | } |
| 304: | |
| 305: | |
| 306: | |
| 307: | |
| 308: | |
| 309: | |
| 310: | |
| 311: | |
| 312: | public function transport($type = null) |
| 313: | { |
| 314: | |
| 315: | if (!is_null($type)) { |
| 316: | $this->transport = $type; |
| 317: | } |
| 318: | |
| 319: | return $this->transport; |
| 320: | } |
| 321: | |
| 322: | |
| 323: | |
| 324: | |
| 325: | |
| 326: | |
| 327: | |
| 328: | |
| 329: | public function options($options = []) |
| 330: | { |
| 331: | |
| 332: | if (!empty($options)) { |
| 333: | $this->options = $options; |
| 334: | } |
| 335: | |
| 336: | return $this->options; |
| 337: | } |
| 338: | |
| 339: | |
| 340: | |
| 341: | |
| 342: | |
| 343: | |
| 344: | |
| 345: | |
| 346: | |
| 347: | |
| 348: | |
| 349: | public function getPacket($type = []) |
| 350: | { |
| 351: | $packets = []; |
| 352: | |
| 353: | |
| 354: | |
| 355: | if (is_array($type) && !empty($type)) { |
| 356: | |
| 357: | foreach ($this->packets as $packet_type => $packet_data) { |
| 358: | |
| 359: | if (in_array($packet_type, $type)) { |
| 360: | $packets[$packet_type] = $packet_data; |
| 361: | } |
| 362: | } |
| 363: | } elseif ($type == '!challenge') { |
| 364: | |
| 365: | foreach ($this->packets as $packet_type => $packet_data) { |
| 366: | |
| 367: | if ($packet_type != self::PACKET_CHALLENGE) { |
| 368: | $packets[$packet_type] = $packet_data; |
| 369: | } |
| 370: | } |
| 371: | } elseif (is_string($type)) { |
| 372: | |
| 373: | $packets = $this->packets[$type]; |
| 374: | } else { |
| 375: | |
| 376: | $packets = $this->packets; |
| 377: | } |
| 378: | |
| 379: | |
| 380: | return $packets; |
| 381: | } |
| 382: | |
| 383: | |
| 384: | |
| 385: | |
| 386: | |
| 387: | |
| 388: | |
| 389: | |
| 390: | public function packetResponse(array $response = []) |
| 391: | { |
| 392: | |
| 393: | if (!empty($response)) { |
| 394: | $this->packets_response = $response; |
| 395: | } |
| 396: | |
| 397: | return $this->packets_response; |
| 398: | } |
| 399: | |
| 400: | |
| 401: | |
| 402: | |
| 403: | |
| 404: | |
| 405: | |
| 406: | |
| 407: | |
| 408: | public function hasChallenge() |
| 409: | { |
| 410: | return (isset($this->packets[self::PACKET_CHALLENGE]) && !empty($this->packets[self::PACKET_CHALLENGE])); |
| 411: | } |
| 412: | |
| 413: | |
| 414: | |
| 415: | |
| 416: | |
| 417: | |
| 418: | |
| 419: | |
| 420: | |
| 421: | |
| 422: | |
| 423: | |
| 424: | public function challengeParseAndApply(Buffer $challenge_buffer) |
| 425: | { |
| 426: | return true; |
| 427: | } |
| 428: | |
| 429: | |
| 430: | |
| 431: | |
| 432: | |
| 433: | |
| 434: | |
| 435: | |
| 436: | protected function challengeApply($challenge_string) |
| 437: | { |
| 438: | |
| 439: | foreach ($this->packets as $packet_type => $packet) { |
| 440: | $this->packets[$packet_type] = sprintf($packet, $challenge_string); |
| 441: | } |
| 442: | |
| 443: | return true; |
| 444: | } |
| 445: | |
| 446: | |
| 447: | |
| 448: | |
| 449: | |
| 450: | |
| 451: | public function getNormalize() |
| 452: | { |
| 453: | return $this->normalize; |
| 454: | } |
| 455: | |
| 456: | |
| 457: | |
| 458: | |
| 459: | |
| 460: | |
| 461: | |
| 462: | |
| 463: | |
| 464: | |
| 465: | |
| 466: | public function beforeSend(Server $server) |
| 467: | { |
| 468: | } |
| 469: | |
| 470: | |
| 471: | |
| 472: | |
| 473: | |
| 474: | |
| 475: | abstract public function processResponse(); |
| 476: | } |
| 477: | |