You've already forked Epicnabbo-Catalogus-Updated-Daily
🆙 More fixes 🆙
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Services;
|
||||
|
||||
use App\Enums\CurrencyTypes;
|
||||
use App\Exceptions\RconConnectionException;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use JsonException;
|
||||
use Socket;
|
||||
@@ -14,13 +15,17 @@ class RconService
|
||||
|
||||
public private(set) bool $isConnected = false;
|
||||
|
||||
protected array $config = [];
|
||||
/** @var array{ip: string, port: int} */
|
||||
protected array $config;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$ip = setting('rcon_ip');
|
||||
$port = setting('rcon_port');
|
||||
|
||||
$this->config = [
|
||||
'ip' => setting('rcon_ip'),
|
||||
'port' => (int) setting('rcon_port'),
|
||||
'ip' => (string) $ip,
|
||||
'port' => is_numeric($port) ? (int) $port : 3001,
|
||||
];
|
||||
|
||||
$this->initialize();
|
||||
@@ -28,9 +33,9 @@ class RconService
|
||||
|
||||
private function initialize(): void
|
||||
{
|
||||
$this->socket = @socket_create(domain: AF_INET, type: SOCK_STREAM, protocol: SOL_TCP);
|
||||
$socket = @socket_create(domain: AF_INET, type: SOCK_STREAM, protocol: SOL_TCP);
|
||||
|
||||
if (! $this->socket) {
|
||||
if ($socket === false) {
|
||||
$error = socket_strerror(socket_last_error());
|
||||
Log::error("RCON initialization failed: {$error}");
|
||||
|
||||
@@ -39,6 +44,8 @@ class RconService
|
||||
return;
|
||||
}
|
||||
|
||||
$this->socket = $socket;
|
||||
|
||||
socket_set_option(
|
||||
socket: $this->socket,
|
||||
level: SOL_SOCKET,
|
||||
@@ -53,7 +60,7 @@ class RconService
|
||||
);
|
||||
|
||||
if (! @socket_connect($this->socket, $this->config['ip'], $this->config['port'])) {
|
||||
$error = socket_strerror(socket_last_error());
|
||||
$error = socket_strerror(socket_last_error($this->socket));
|
||||
Log::error("RCON connection failed: {$error}");
|
||||
|
||||
$this->closeConnection();
|
||||
@@ -80,12 +87,13 @@ class RconService
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed>|null $data
|
||||
* @throws RconConnectionException
|
||||
* @throws JsonException
|
||||
*/
|
||||
public function sendCommand(string $command, ?array $data = null): bool
|
||||
{
|
||||
if (! $this->isConnected) {
|
||||
if (! $this->isConnected || ! $this->socket) {
|
||||
Log::error('RCON command failed: Not connected');
|
||||
$this->closeConnection();
|
||||
return false;
|
||||
@@ -106,7 +114,7 @@ class RconService
|
||||
/**
|
||||
* @throws RconConnectionException|JsonException
|
||||
*/
|
||||
public function sendGift($user, int $item_id, string $message = 'Here is a gift.'): void
|
||||
public function sendGift(User $user, int $item_id, string $message = 'Here is a gift.'): void
|
||||
{
|
||||
$this->sendCommand('sendgift', [
|
||||
'user_id' => $user->id,
|
||||
@@ -118,7 +126,7 @@ class RconService
|
||||
/**
|
||||
* @throws RconConnectionException|JsonException
|
||||
*/
|
||||
public function giveCredits($user, int $credits): void
|
||||
public function giveCredits(User $user, int $credits): void
|
||||
{
|
||||
$this->sendCommand('givecredits', [
|
||||
'user_id' => $user->id,
|
||||
@@ -129,7 +137,7 @@ class RconService
|
||||
/**
|
||||
* @throws RconConnectionException|JsonException
|
||||
*/
|
||||
public function giveBadge($user, string $badge): void
|
||||
public function giveBadge(User $user, string $badge): void
|
||||
{
|
||||
$this->sendCommand('givebadge', [
|
||||
'user_id' => $user->id,
|
||||
@@ -140,7 +148,7 @@ class RconService
|
||||
/**
|
||||
* @throws RconConnectionException|JsonException
|
||||
*/
|
||||
public function setMotto($user, string $motto): void
|
||||
public function setMotto(User $user, string $motto): void
|
||||
{
|
||||
$this->sendCommand('setmotto', [
|
||||
'user_id' => $user->id,
|
||||
@@ -159,7 +167,7 @@ class RconService
|
||||
/**
|
||||
* @throws RconConnectionException|JsonException
|
||||
*/
|
||||
public function disconnectUser($user): void
|
||||
public function disconnectUser(User $user): void
|
||||
{
|
||||
$this->sendCommand('disconnect', [
|
||||
'user_id' => $user->id,
|
||||
@@ -170,7 +178,7 @@ class RconService
|
||||
/**
|
||||
* @throws RconConnectionException|JsonException
|
||||
*/
|
||||
public function givePoints($user, CurrencyTypes $type, int $amount): void
|
||||
public function givePoints(User $user, CurrencyTypes $type, int $amount): void
|
||||
{
|
||||
$this->sendCommand('givepoints', [
|
||||
'user_id' => $user->id,
|
||||
@@ -183,7 +191,7 @@ class RconService
|
||||
* @throws RconConnectionException
|
||||
* @throws JsonException
|
||||
*/
|
||||
public function giveGotw($user, int $amount): void
|
||||
public function giveGotw(User $user, int $amount): void
|
||||
{
|
||||
$this->givePoints($user, CurrencyTypes::Points, $amount);
|
||||
}
|
||||
@@ -192,7 +200,7 @@ class RconService
|
||||
* @throws RconConnectionException
|
||||
* @throws JsonException
|
||||
*/
|
||||
public function giveDiamonds($user, int $amount): void
|
||||
public function giveDiamonds(User $user, int $amount): void
|
||||
{
|
||||
$this->givePoints($user, CurrencyTypes::Diamonds, $amount);
|
||||
}
|
||||
@@ -201,16 +209,16 @@ class RconService
|
||||
* @throws RconConnectionException
|
||||
* @throws JsonException
|
||||
*/
|
||||
public function giveDuckets($user, int $amount): void
|
||||
public function giveDuckets(User $user, int $amount): void
|
||||
{
|
||||
$this->givePoints($user, CurrencyTypes::DUCKETS, $amount);
|
||||
$this->givePoints($user, CurrencyTypes::Duckets, $amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RconConnectionException
|
||||
* @throws JsonException
|
||||
*/
|
||||
public function setRank($user, int $rank): void
|
||||
public function setRank(User $user, int $rank): void
|
||||
{
|
||||
$this->sendCommand('setrank', [
|
||||
'user_id' => $user->id,
|
||||
@@ -231,7 +239,7 @@ class RconService
|
||||
* @throws RconConnectionException
|
||||
* @throws JsonException
|
||||
*/
|
||||
public function alertUser($user, string $message): void
|
||||
public function alertUser(User $user, string $message): void
|
||||
{
|
||||
$this->sendCommand('alertuser', [
|
||||
'user_id' => $user->id,
|
||||
@@ -243,7 +251,7 @@ class RconService
|
||||
* @throws RconConnectionException
|
||||
* @throws JsonException
|
||||
*/
|
||||
public function forwardUser($user, int $roomId): void
|
||||
public function forwardUser(User $user, int $roomId): void
|
||||
{
|
||||
$this->sendCommand('forwarduser', [
|
||||
'user_id' => $user->id,
|
||||
@@ -255,7 +263,7 @@ class RconService
|
||||
* @throws RconConnectionException
|
||||
* @throws JsonException
|
||||
*/
|
||||
public function updateConfig($user, string $command): void
|
||||
public function updateConfig(User $user, string $command): void
|
||||
{
|
||||
$this->sendCommand('executecommand', [
|
||||
'user_id' => $user->id,
|
||||
|
||||
Reference in New Issue
Block a user