Skip to content

Commit

Permalink
Added Palworld Direct Query (test)
Browse files Browse the repository at this point in the history
You need to add JSON string in comment field. Example: {"login":"admin","password":"23ewer"}
  • Loading branch information
tltneon committed Jun 22, 2024
1 parent 0704bd6 commit 1756c7e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/lgsl_admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
$_POST['form_s_port'] [$form_key],
$_POST['form_zone'] [$form_key],
$_POST['form_disabled'][$form_key],
$_POST['form_comment'] [$form_key]) = explode(":", "{$form_line}:::::::");
$_POST['form_comment'] [$form_key]) = explode(":", "{$form_line}", 8);
}
}

Expand Down Expand Up @@ -328,7 +328,7 @@
<td>[ S Port ] </td>
<td>[ Zone ] </td>
<td>[ {$lgsl_config['text']['dsb']} ]</td>
<td>[ Comment ] </td>
<td>[ Comment | Additional Data ] </td>
</tr>";

$result = $db->get_all();
Expand Down
24 changes: 23 additions & 1 deletion src/lgsl_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ public function getTimestamp($type = Timestamp::SERVER) {
}
public function getTimestampFormatted($type = Timestamp::SERVER) {
$time = $this->getTimestamp($type);
global $lgsl_config;
global $lgsl_config;
if ($time > 0) {
return Date($lgsl_config['text']['tzn'], $time);
}
Expand All @@ -1002,6 +1002,28 @@ public function setTimestamp($type, $time) {
public function getTimestamps() {
return $this->_server['cache_time']->toString();
}
public function getAdditionalData(): array {
$json = json_decode(str_replace("&quot;", '"', $this->_other['comment']), true);
try {
if (json_last_error() > 0) {
throw new \Exception('JSON decode error code '. json_last_error() . " for server {$this->getId()}");
}
return $json;
} catch (\Exception $e) {
return [0 => $json, 1 => $e->getMessage()];
}
}
public function setAdditionalData($data) : null {
$json = json_encode($this->_other['comment']);
try {
if (json_last_error() > 0) {
throw new \Exception();
}
$this->_other['comment'] = $json;
} catch (\Exception $e) {
$this->_other['comment'] = $data;
}
}
public function checkTimestamps($request = "") {
global $lgsl_config;
$needed = "";
Expand Down
27 changes: 26 additions & 1 deletion src/lgsl_protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class Protocol {
const OPENTTD = "openttd";
const PAINKILLER = "painkiller";
const PALWORLD = "palworld";
const PALWORLDDIRECT = "palworlddirect";
const PLAINSIGHT = "plainsight";
const PREY = "prey";
const QUAKEWORLD = "quakeworld";
Expand Down Expand Up @@ -179,6 +180,7 @@ static public function lgslConnectionType($type) {
self::ECO => self::HTTP,
self::FIVEM => self::HTTP,
self::PALWORLD => self::HTTP,
self::PALWORLDDIRECT=> self::HTTP,
self::RAGEMP => self::HTTP,
self::SCUM => self::HTTP,
self::TERRARIA => self::HTTP,
Expand Down Expand Up @@ -274,6 +276,7 @@ static public function lgslList($type = null) {
self::OPENTTD => ["Query22", "Open Transport Tycoon Deluxe"],
self::PAINKILLER => ["Query08", "PainKiller"],
self::PALWORLD => ["Query51", "Palworld"],
self::PALWORLDDIRECT=> ["Query55", "Palworld Direct"],
self::PLAINSIGHT => ["Query32", "Plain Sight"],
self::PREY => ["Query10", "Prey"],
self::QUAKEWORLD => ["Query07", "Quake World"],
Expand Down Expand Up @@ -2883,6 +2886,25 @@ public function process() {
return $this::SUCCESS;
}
}
class Query55 extends QueryJSON { // Palworld Direct
public function process() {
$cred = $this->_server->getAdditionalData();
if (empty($cred['login']) || empty($cred['password'])) {
$this->_data['e']['_error'] = "No login or password presented";
return $this::WITH_ERROR;
}
$auth = base64_encode("{$cred['login']}:{$cred['password']}");
$this->_fp->setOpt(CURLOPT_HTTPHEADER, ['Accept: application/json', "Authorization: Basic {$auth}"]);
$buffer = $this->fetch("http://{$this->_server->getIp()}:{$this->_server->getQueryPort()}/v1/api/settings");
if (!$buffer) return $this::NO_RESPOND;
$this->_data['s']['name'] = $buffer['ServerName'];
$this->_data['s']['password'] = 0;
$this->_data['s']['players'] = 0;
$this->_data['s']['playersmax'] = $buffer['ServerPlayerMaxNum'];
$this->_data['e']['description'] = isset($buffer['ServerDescription']) ? $buffer['ServerDescription'] : "";
return $this::SUCCESS;
}
}

class QueryTest extends Query {
public function process() {
Expand Down Expand Up @@ -3057,7 +3079,7 @@ public function readRaw($length = 4096) {
} else {
return $msg;
}
}
}
return $result;
} else {
return fread($this->_stream, $length);
Expand Down Expand Up @@ -3086,6 +3108,9 @@ public function write($data) {
public function &getStream() {
return $this->_stream;
}
public function setOpt($name, $value) {
curl_setopt($this->_stream, $name, $value);
}
public function close() {
if (!$this->_stream) return;
if ($this->_isHttp()) {
Expand Down

0 comments on commit 1756c7e

Please sign in to comment.