-
Notifications
You must be signed in to change notification settings - Fork 0
/
RCON.php
101 lines (82 loc) · 3.64 KB
/
RCON.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/*
* This file is a part of the PZ Bot project.
*
* Copyright (c) 2024-present Valithor Obsidion <[email protected]>
*/
namespace PZ;
class RCON
{
public string $serverIp;
public int $rconPort;
private readonly string $rconPassword;
//private string $mcrconDir = '';
private string $mcrconPath = '';
private string $batDir = '';
private string $batPath = '';
public bool $initialized = false;
public array $previousPlayers = [];
public array $currentPlayers = [];
public function __construct(string $serverIp, int $rconPort, string $rconPassword, string $mcrconDir, ?string $mcronPath = 'mcrcon.exe', ?string $batDir = __DIR__, ?string $batFile = 'pz.bat')
{
if (! filter_var($serverIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE))
throw new \InvalidArgumentException('Invalid server IP address');
$this->serverIp = $serverIp;
if ($rconPort < 1 || $rconPort > 65535)
throw new \InvalidArgumentException('Invalid RCON port number');
$this->rconPort = $rconPort;
$this->rconPassword = $rconPassword;
if (! is_dir($mcrconDir))
throw new \InvalidArgumentException('Invalid directory for mcrcon: ' . $mcrconDir);
//$this->mcrconDir = $mcrconDir;
if (! file_exists($mcrconPath = $mcrconDir . '\\' . $mcronPath))
throw new \InvalidArgumentException('Invalid file for mcrcon:' . $mcrconPath);
$this->mcrconPath = $mcrconPath;
if (! is_dir($batDir))
throw new \InvalidArgumentException('Invalid directory for pz.bat:' . $batDir);
$this->batDir = $batDir;
if (! file_exists($batDir . '\\' . $batFile))
throw new \InvalidArgumentException('Invalid file for pz.bat:' . $batFile);
$this->batPath = $batDir . '\\' . $batFile;
$this->__populatePlayers();
$this->initialized = true;
}
public function getPlayerCount($populate = false): int
{
if ($populate) $this->__populatePlayers();
return count($this->currentPlayers);
}
public function getPlayersWhoJoined($populate = false): array
{
if ($populate) $this->__populatePlayers();
return array_diff($this->currentPlayers, $this->previousPlayers);
}
public function getPlayersWhoLeft($populate = false): array
{
if ($populate) $this->__populatePlayers();
return array_diff($this->previousPlayers, $this->currentPlayers);
}
public function getPlayers($populate = false): array
{
if ($populate) $this->__populatePlayers();
return $this->currentPlayers;
}
private function __populatePlayers(): array
{
$this->previousPlayers = $this->currentPlayers;
$playersString = $this->__getPlayersString();
$playersString = is_string($playersString) ? str_replace("Error: Failed to execute mcrcon command.", "", $playersString) : '';
return $this->currentPlayers = $playersString ? explode("\n", trim($playersString)) : [];
}
/*
* Function to execute pz.bat in the current directory
* Creats a txt file containing the output of the batch file, formatted as a list of players
* Usage: pz.bat SERVER_IP [RCON_PASSWORD] [RCON_PORT] [MCRCON_PATH]
*/
private function __getPlayersString(): string|false|null
{
$launchOptions = sprintf('"%s" "%s" "%d" "%s"', $this->serverIp, $this->rconPassword, $this->rconPort, $this->mcrconPath);
$cmd = "cd /d \"{$this->batDir}\" && \"{$this->batPath}\" $launchOptions";
return shell_exec($cmd);
}
}