Skip to content

Commit

Permalink
v1.0.7
Browse files Browse the repository at this point in the history
- Small refactoring
- Vip / IKS group time fix
- VIP Rcon exception fix.
  • Loading branch information
FlamesONE committed Jul 22, 2024
1 parent 6f01cb5 commit 8e893ff
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 113 deletions.
122 changes: 70 additions & 52 deletions Give/Drivers/RconDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,85 +14,103 @@ class RconDriver implements DriverInterface
{
public function deliver(User $user, Server $server, array $additional = [], ?int $timeId = null): bool
{
if (!$server->rcon)
throw new BadConfigurationException("Server $server->name rcon empty");

if (!isset($additional['command']))
throw new BadConfigurationException('command');
$this->validateServerAndCommand($server, $additional);

$commands = explode(';', $additional['command']);
$steam = false;

if (preg_match('/{{steam32}}|{{steam64}}|{{accountId}}/i', $additional['command'])) {
$steam = $user->getSocialNetwork('Steam') ?? $user->getSocialNetwork('HttpsSteam');

if (!$steam)
throw new UserSocialException("Steam");

$steam = $steam->value;
}
$steam = $this->getSteamId($user, $additional['command']);

$query = new SourceQuery();

try {
$query->Connect($server->ip, $server->port, 3, ($server->mod == 10) ? SourceQuery::GOLDSOURCE : SourceQuery::SOURCE);
$query->SetRconPassword($server->rcon);

foreach ($commands as $command) {
$command = trim($command);
if (empty($command)) {
continue;
}
$this->sendCommand($query, $this->replace($command, $steam, $user));
}
$this->connectToServer($query, $server);
$this->executeCommands($query, $commands, $steam, $user);
return true;
} catch (\Exception $e) {
throw new GiveDriverException($e->getMessage());
} finally {
$query->Disconnect();
}

return false;
}

public function alias(): string
{
return 'rcon';
}

protected function replace(string $command, $steam, User $user): string
private function validateServerAndCommand(Server $server, array $additional): void
{
if (!$server->rcon) {
throw new BadConfigurationException("Server $server->name rcon empty");
}

if (!isset($additional['command'])) {
throw new BadConfigurationException('command');
}
}

private function getSteamId(User $user, string $command): ?string
{
if (preg_match('/{{steam32}}|{{steam64}}|{{accountId}}/i', $command)) {
$steam = $user->getSocialNetwork('Steam') ?? $user->getSocialNetwork('HttpsSteam');

if (!$steam) {
throw new UserSocialException("Steam");
}

return $steam->value;
}

return null;
}

private function connectToServer(SourceQuery $query, Server $server): void
{
$query->Connect(
$server->ip,
$server->port,
3,
($server->mod == 10) ? SourceQuery::GOLDSOURCE : SourceQuery::SOURCE
);
$query->SetRconPassword($server->rcon);
}

private function executeCommands(SourceQuery $query, array $commands, ?string $steam, User $user): void
{
foreach ($commands as $command) {
$command = trim($command);
if (empty($command)) {
continue;
}
$this->sendCommand($query, $this->replacePlaceholders($command, $steam, $user));
}
}

private function replacePlaceholders(string $command, ?string $steam, User $user): string
{
$steam32 = '';
$steam64 = '';
$accountId = '';
$steamDetails = $this->getSteamDetails($steam);

return str_replace(
['{{steam32}}', '{{steam64}}', '{{accountId}}', '{{login}}', '{{name}}', '{{email}}', '{{uri}}'],
[$steamDetails['steam32'], $steamDetails['steam64'], $steamDetails['accountId'], $user->login, $user->name, $user->email, $user->uri],
$command
);
}

private function getSteamDetails(?string $steam): array
{
if ($steam) {
$steamClass = steam()->steamid($steam);
$steam32 = $steamClass->RenderSteam2();
$steam64 = $steamClass->ConvertToUInt64();
$accountId = $steamClass->GetAccountID();
return [
'steam32' => $steamClass->RenderSteam2(),
'steam64' => $steamClass->ConvertToUInt64(),
'accountId' => $steamClass->GetAccountID()
];
}

return str_replace([
'{{steam32}}',
'{{steam64}}',
'{{accountId}}',
'{{login}}',
'{{name}}',
'{{email}}',
'{{uri}}'
], [
$steam32,
$steam64,
$accountId,
$user->login,
$user->name,
$user->email,
$user->uri
], $command);
return ['steam32' => '', 'steam64' => '', 'accountId' => ''];
}

protected function sendCommand(SourceQuery $query, string $command): void
private function sendCommand(SourceQuery $query, string $command): void
{
$query->Rcon($command);
}
Expand Down
14 changes: 10 additions & 4 deletions Give/Drivers/VipDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,17 @@ public function deliver(User $user, Server $server, array $additional = [], ?int
return true;
}

private function updateVips(Server $server)
private function updateVips(Server $server)
{
$query = new SourceQuery();

try {
$query->Connect($server->ip, $server->port, 3, ($server->mod == 10) ? SourceQuery::GOLDSOURCE : SourceQuery::SOURCE);
$query->SetRconPassword($server->rcon);
$this->sendCommand($query, "vip_reload");
} catch (\Exception $e) {
throw new GiveDriverException($e->getMessage());
logs()->error($e);
// throw new GiveDriverException($e->getMessage());
} finally {
$query->Disconnect();
}
Expand Down Expand Up @@ -103,7 +104,12 @@ private function validateAdditionalParams(array $additional, Server $server): ar

private function updateOrInsertUser($db, $accountId, $sid, $group, $time, $user, $currentGroup = null)
{
$expiresTime = ($time === 0) ? 0 : ($currentGroup ? $currentGroup['expires'] + $time : time() + $time);
if ($currentGroup['group'] !== $group) {
$expiresTime = ($time === 0) ? 0 : time() + $time;
} else {
$expiresTime = ($time === 0) ? 0 : ($currentGroup ? $currentGroup['expires'] + $time : time() + $time);
}

if ($currentGroup) {
$db->table('users')
->update([
Expand Down
140 changes: 86 additions & 54 deletions Give/Drivers/iksAdminDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,113 @@ class iksAdminDriver extends AbstractDriver
{
public function deliver(User $user, Server $server, array $additional = [], ?int $timeId = null): bool
{
$steamid64 = $user->getSocialNetwork('Steam') ?? $user->getSocialNetwork('HttpsSteam');

if (!$steamid64->value) {
throw new UserSocialException("Steam");
}
$steamid64 = $this->getSteamId($user);

[$dbConnection, $sid] = $this->validateAdditionalParams($additional, $server);

$group_id = $additional['group_id'];
$time = !$timeId ? ($additional['time'] ?? 0) : $timeId;
$time = $timeId ?? ($additional['time'] ?? 0);

$db = dbal()->database($dbConnection->dbname);
$dbusers = $db->table("admins")->select()
->where('sid', $steamid64->value)
->andWhere('server_id', $sid)
->fetchAll();
$dbusers = $this->getDbUsers($db, $steamid64->value, $sid);

if (!empty($dbusers)) {
$dbuser = $dbusers[0];

if ($dbuser['group_id'] === $group_id)
$this->confirm(__("givecore.add_time", [
':server' => $server->name
]));
else
$this->confirm(__("givecore.replace_group", [
':group_id' => $dbuser['group_id'],
':newGroup' => $group_id
]));

$this->confirmUserGroup($dbuser, $group_id, $server);
$this->updateOrInsertUser($db, $steamid64->value, $sid, $group_id, $time, $user, $dbuser);
} else {
$this->updateOrInsertUser($db, $steamid64->value, $sid, $group_id, $time, $user);
}

if ($server->rcon)
if ($server->rcon) {
$this->updateAdmins($server);
}

return true;
}

private function updateAdmins(Server $server)
private function getSteamId(User $user)
{
$steamid64 = $user->getSocialNetwork('Steam') ?? $user->getSocialNetwork('HttpsSteam');

if (!$steamid64 || !$steamid64->value) {
throw new UserSocialException("Steam");
}

return $steamid64;
}

private function getDbUsers($db, $steamid64, $sid)
{
return $db->table("admins")->select()
->where('sid', $steamid64)
->andWhere('server_id', $sid)
->fetchAll();
}

private function confirmUserGroup($dbuser, $group_id, $server)
{
if ($dbuser['group_id'] === $group_id) {
$this->confirm(__("givecore.add_time", [':server' => $server->name]));
} else {
$this->confirm(__("givecore.replace_group", [
':group_id' => $dbuser['group_id'],
':newGroup' => $group_id
]));
}
}

private function updateOrInsertUser($db, $steamid64, $sid, $group_id, $time, $user, $currentGroup = null): void
{
$currentUnixTime = time();
$expiresTime = $this->calculateExpiresTime($time, $currentUnixTime, $currentGroup, $group_id);

if ($currentGroup) {
$this->updateUser($db, $steamid64, $sid, $group_id, $expiresTime);
} else {
$this->insertUser($db, $steamid64, $sid, $group_id, $expiresTime, $user);
}
}

private function calculateExpiresTime($time, $currentUnixTime, $currentGroup, $group_id): int
{
if ($currentGroup['group_id'] === $group_id) {
return ($time === 0) ? 0 : ($currentGroup ? $currentGroup['end'] + $time : $currentUnixTime + $time);
}
return ($time === 0) ? 0 : $currentUnixTime + $time;
}

private function updateUser($db, $steamid64, $sid, $group_id, $expiresTime): void
{
$db->table('admins')
->update([
'end' => $expiresTime,
'group_id' => $group_id
])
->where('sid', $steamid64)
->andWhere('server_id', $sid)
->run();
}

private function insertUser($db, $steamid64, $sid, $group_id, $expiresTime, $user): void
{
$db->insert('admins')
->values([
'end' => $expiresTime,
'group_id' => $group_id,
'sid' => $steamid64,
'flags' => "",
'immunity' => -1,
'server_id' => $sid,
'name' => $user->name,
])
->run();
}

private function updateAdmins(Server $server): void
{
$query = new SourceQuery();

try {
$query->Connect($server->ip, $server->port, 3, ($server->mod == 10) ? SourceQuery::GOLDSOURCE : SourceQuery::SOURCE);
$query->SetRconPassword($server->rcon);
Expand Down Expand Up @@ -99,34 +161,4 @@ private function validateAdditionalParams(array $additional, Server $server): ar

return [$dbConnection, $dbParams->sid];
}

private function updateOrInsertUser($db, $steamid64, $sid, $group_id, $time, $user, $currentGroup = null)
{
$currentUnixTime = time();

$expiresTime = ($time === 0) ? 0 : ($currentGroup ? $currentGroup['end'] + $time : $currentUnixTime + $time);

if ($currentGroup) {
$db->table('admins')
->update([
'end' => $expiresTime,
'group_id' => $group_id
])
->where('sid', $steamid64)
->andWhere('server_id', $sid)
->run();
} else {
$db->insert('admins')
->values([
'end' => $expiresTime,
'group_id' => $group_id,
'sid' => $steamid64,
'flags' => "",
'immunity' => -1,
'server_id' => $sid,
'name' => $user->name,
])
->run();
}
}
}
2 changes: 0 additions & 2 deletions Give/GiveFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Flute\Core\Database\Entities\Server;
use Flute\Core\Database\Entities\User;
use Flute\Modules\GiveCore\Contracts\DriverInterface;
use Flute\Modules\GiveCore\Give\Drivers\AdminDriver;
use Flute\Modules\GiveCore\Give\Drivers\iksAdminDriver;
use Flute\Modules\GiveCore\Give\Drivers\RconDriver;
use Flute\Modules\GiveCore\Give\Drivers\VipDriver;
Expand All @@ -14,7 +13,6 @@ class GiveFactory
{
protected array $drivers = [
'vip' => VipDriver::class,
// 'admin' => AdminDriver::class,
'iksAdmin' => iksAdminDriver::class,
'rcon' => RconDriver::class
];
Expand Down
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "GiveCore",
"version": "1.0.6",
"version": "1.0.7",
"description": "A core module for what provides a ability to to give privileges",
"authors": [
"Flames"
Expand Down

0 comments on commit 8e893ff

Please sign in to comment.