| 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 Openttd extends Protocol |
| 35: | { |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | protected $packets = [ |
| 43: | self::PACKET_ALL => "\x03\x00\x00", |
| 44: | ]; |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | protected $protocol = 'openttd'; |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | protected $name = 'openttd'; |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | protected $name_long = "Open Transport Tycoon Deluxe"; |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | protected $normalize = [ |
| 73: | |
| 74: | 'general' => [ |
| 75: | |
| 76: | 'hostname' => 'hostname', |
| 77: | 'mapname' => 'map', |
| 78: | 'maxplayers' => 'max_clients', |
| 79: | 'numplayers' => 'clients', |
| 80: | 'password' => 'password', |
| 81: | 'dedicated' => 'dedicated', |
| 82: | ], |
| 83: | ]; |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | public function processResponse() |
| 93: | { |
| 94: | |
| 95: | $buffer = new Buffer(implode('', $this->packets_response)); |
| 96: | |
| 97: | |
| 98: | $packetLength = $buffer->getLength(); |
| 99: | |
| 100: | |
| 101: | $length = $buffer->readInt16(); |
| 102: | |
| 103: | $buffer->skip(1); |
| 104: | |
| 105: | |
| 106: | |
| 107: | if ($packetLength != $length) { |
| 108: | throw new Exception(__METHOD__ . " response type '" . bin2hex($length) . "' is not valid"); |
| 109: | } |
| 110: | |
| 111: | return call_user_func_array([$this, 'processServerInfo'], [$buffer]); |
| 112: | } |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | protected function processServerInfo(Buffer $buffer) |
| 122: | { |
| 123: | |
| 124: | $result = new Result(); |
| 125: | |
| 126: | $protocol_version = $buffer->readInt8(); |
| 127: | $result->add('protocol_version', $protocol_version); |
| 128: | |
| 129: | switch ($protocol_version) { |
| 130: | case 4: |
| 131: | $num_grfs = $buffer->readInt8(); |
| 132: | $result->add('num_grfs', $num_grfs); |
| 133: | |
| 134: | |
| 135: | for ($i=0; $i<$num_grfs; $i++) { |
| 136: | $result->add('grfs_'.$i.'_ID', strtoupper(bin2hex($buffer->read(4)))); |
| 137: | $result->add('grfs_'.$i.'_MD5', strtoupper(bin2hex($buffer->read(16)))); |
| 138: | } |
| 139: | |
| 140: | case 3: |
| 141: | $result->add('game_date', $buffer->readInt32()); |
| 142: | $result->add('start_date', $buffer->readInt32()); |
| 143: | |
| 144: | |
| 145: | case 2: |
| 146: | $result->add('companies_max', $buffer->readInt8()); |
| 147: | $result->add('companies_on', $buffer->readInt8()); |
| 148: | $result->add('spectators_max', $buffer->readInt8()); |
| 149: | |
| 150: | |
| 151: | case 1: |
| 152: | $result->add('hostname', $buffer->readString()); |
| 153: | $result->add('version', $buffer->readString()); |
| 154: | |
| 155: | $language = $buffer->readInt8(); |
| 156: | $result->add('language', $language); |
| 157: | $result->add('language_icon', '//media.openttd.org/images/server/'.$language.'_lang.gif'); |
| 158: | |
| 159: | $result->add('password', $buffer->readInt8()); |
| 160: | $result->add('max_clients', $buffer->readInt8()); |
| 161: | $result->add('clients', $buffer->readInt8()); |
| 162: | $result->add('spectators', $buffer->readInt8()); |
| 163: | if ($protocol_version < 3) { |
| 164: | $days = (365 * 1920 + 1920 / 4 - 1920 / 100 + 1920 / 400); |
| 165: | $result->add('game_date', $buffer->readInt16() + $days); |
| 166: | $result->add('start_date', $buffer->readInt16() + $days); |
| 167: | } |
| 168: | $result->add('map', $buffer->readString()); |
| 169: | $result->add('map_width', $buffer->readInt16()); |
| 170: | $result->add('map_height', $buffer->readInt16()); |
| 171: | $result->add('map_type', $buffer->readInt8()); |
| 172: | $result->add('dedicated', $buffer->readInt8()); |
| 173: | |
| 174: | } |
| 175: | unset($buffer); |
| 176: | |
| 177: | return $result->fetch(); |
| 178: | } |
| 179: | } |
| 180: | |