| 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: | |
| 33: | |
| 34: | class Lhmp extends Protocol |
| 35: | { |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | protected $packets = [ |
| 43: | self::PACKET_DETAILS => "LHMPo", |
| 44: | self::PACKET_PLAYERS => "LHMPp", |
| 45: | ]; |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | protected $responses = [ |
| 53: | "LHMPo" => "processDetails", |
| 54: | "LHMPp" => "processPlayers", |
| 55: | ]; |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | protected $protocol = 'lhmp'; |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | protected $name = 'lhmp'; |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | protected $name_long = "Lost Heaven"; |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | protected $port_diff = 1; |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | protected $normalize = [ |
| 91: | |
| 92: | 'general' => [ |
| 93: | |
| 94: | 'gametype' => 'gamemode', |
| 95: | 'hostname' => 'servername', |
| 96: | 'mapname' => 'mapname', |
| 97: | 'maxplayers' => 'maxplayers', |
| 98: | 'numplayers' => 'numplayers', |
| 99: | 'password' => 'password', |
| 100: | ], |
| 101: | |
| 102: | 'player' => [ |
| 103: | 'name' => 'name', |
| 104: | ], |
| 105: | ]; |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | public function processResponse() |
| 114: | { |
| 115: | |
| 116: | $packets = []; |
| 117: | |
| 118: | |
| 119: | foreach ($this->packets_response as $response) { |
| 120: | $buffer = new Buffer($response); |
| 121: | |
| 122: | |
| 123: | $header = $buffer->read(5); |
| 124: | |
| 125: | |
| 126: | $packets[$header][] = $buffer->getBuffer(); |
| 127: | } |
| 128: | |
| 129: | unset($buffer); |
| 130: | |
| 131: | $results = []; |
| 132: | |
| 133: | |
| 134: | foreach ($packets as $header => $packetGroup) { |
| 135: | |
| 136: | if (!array_key_exists($header, $this->responses)) { |
| 137: | throw new Exception(__METHOD__ . " response type '{$header}' is not valid"); |
| 138: | } |
| 139: | |
| 140: | |
| 141: | $results = array_merge( |
| 142: | $results, |
| 143: | call_user_func_array([$this, $this->responses[$header]], [new Buffer(implode($packetGroup))]) |
| 144: | ); |
| 145: | } |
| 146: | |
| 147: | unset($packets); |
| 148: | |
| 149: | return $results; |
| 150: | } |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | protected function processDetails(Buffer $buffer) |
| 163: | { |
| 164: | |
| 165: | $result = new Result(); |
| 166: | |
| 167: | $result->add('protocol', $buffer->readString()); |
| 168: | $result->add('password', $buffer->readString()); |
| 169: | $result->add('numplayers', $buffer->readInt16()); |
| 170: | $result->add('maxplayers', $buffer->readInt16()); |
| 171: | $result->add('servername', Str::isoToUtf8($buffer->readPascalString())); |
| 172: | $result->add('gamemode', $buffer->readPascalString()); |
| 173: | $result->add('website', Str::isoToUtf8($buffer->readPascalString())); |
| 174: | $result->add('mapname', Str::isoToUtf8($buffer->readPascalString())); |
| 175: | |
| 176: | unset($buffer); |
| 177: | |
| 178: | return $result->fetch(); |
| 179: | } |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | protected function processPlayers(Buffer $buffer) |
| 189: | { |
| 190: | |
| 191: | $result = new Result(); |
| 192: | |
| 193: | |
| 194: | $result->add('numplayers', $buffer->readInt16()); |
| 195: | |
| 196: | |
| 197: | while ($buffer->getLength()) { |
| 198: | |
| 199: | if (($id = $buffer->readInt16()) !== 0) { |
| 200: | |
| 201: | $result->addPlayer('id', $id); |
| 202: | $result->addPlayer('name', Str::isoToUtf8($buffer->readPascalString())); |
| 203: | } |
| 204: | } |
| 205: | |
| 206: | unset($buffer, $id); |
| 207: | |
| 208: | return $result->fetch(); |
| 209: | } |
| 210: | } |
| 211: | |