| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | namespace GameQ\Protocols; |
| 19: | |
| 20: | use GameQ\Exception\Protocol as Exception; |
| 21: | use GameQ\Result; |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | class Tshock extends Http |
| 37: | { |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | protected $packets = [ |
| 44: | self::PACKET_STATUS => "GET /v2/server/status?players=true&rules=true HTTP/1.0\r\nAccept: */*\r\n\r\n", |
| 45: | ]; |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | protected $protocol = 'tshock'; |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | protected $name = 'tshock'; |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | protected $name_long = "Tshock"; |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | protected $normalize = [ |
| 74: | |
| 75: | 'general' => [ |
| 76: | |
| 77: | 'dedicated' => 'dedicated', |
| 78: | 'hostname' => 'hostname', |
| 79: | 'mapname' => 'world', |
| 80: | 'maxplayers' => 'maxplayers', |
| 81: | 'numplayers' => 'numplayers', |
| 82: | 'password' => 'password', |
| 83: | ], |
| 84: | |
| 85: | 'player' => [ |
| 86: | 'name' => 'nickname', |
| 87: | 'team' => 'team', |
| 88: | ], |
| 89: | ]; |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | public function processResponse() |
| 98: | { |
| 99: | if (empty($this->packets_response)) { |
| 100: | return []; |
| 101: | } |
| 102: | |
| 103: | |
| 104: | preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches); |
| 105: | |
| 106: | |
| 107: | if (!isset($matches[0]) || ($json = json_decode($matches[0])) === null) { |
| 108: | throw new Exception("JSON response from Tshock protocol is invalid."); |
| 109: | } |
| 110: | |
| 111: | |
| 112: | if ($json->status != 200) { |
| 113: | throw new Exception("JSON status from Tshock protocol response was '{$json->status}', expected '200'."); |
| 114: | } |
| 115: | |
| 116: | $result = new Result(); |
| 117: | |
| 118: | |
| 119: | $result->add('dedicated', 1); |
| 120: | |
| 121: | |
| 122: | $result->add('hostname', $json->name); |
| 123: | $result->add('game_port', $json->port); |
| 124: | $result->add('serverversion', $json->serverversion); |
| 125: | $result->add('world', $json->world); |
| 126: | $result->add('uptime', $json->uptime); |
| 127: | $result->add('password', (int)$json->serverpassword); |
| 128: | $result->add('numplayers', $json->playercount); |
| 129: | $result->add('maxplayers', $json->maxplayers); |
| 130: | |
| 131: | |
| 132: | foreach ($json->players as $player) { |
| 133: | $result->addPlayer('nickname', $player->nickname); |
| 134: | $result->addPlayer('username', $player->username); |
| 135: | $result->addPlayer('group', $player->group); |
| 136: | $result->addPlayer('active', (int)$player->active); |
| 137: | $result->addPlayer('state', $player->state); |
| 138: | $result->addPlayer('team', $player->team); |
| 139: | } |
| 140: | |
| 141: | |
| 142: | $rules = []; |
| 143: | |
| 144: | |
| 145: | foreach ($json->rules as $rule => $value) { |
| 146: | |
| 147: | $rules[$rule] = (is_bool($value)) ? (int)$value : $value; |
| 148: | } |
| 149: | |
| 150: | |
| 151: | $result->add('rules', $rules); |
| 152: | |
| 153: | unset($rules, $rule, $player, $value); |
| 154: | |
| 155: | return $result->fetch(); |
| 156: | } |
| 157: | } |
| 158: | |