Skip to content

Commit

Permalink
v1.0.4
Browse files Browse the repository at this point in the history
- Multiple rcon commands support
- Vip time support
  • Loading branch information
FlamesONE committed Jun 1, 2024
1 parent b696b33 commit d0ced23
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Contracts/DriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface DriverInterface
*
* @throws \Exception
*/
public function deliver( User $user, Server $server, array $additional = [] ) : bool;
public function deliver( User $user, Server $server, array $additional = [], ?int $timeId = null ) : bool;

/**
* Get the alias name for the system
Expand Down
2 changes: 1 addition & 1 deletion Give/Drivers/AdminDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class AdminDriver implements DriverInterface
{
public function deliver(User $user, Server $server, array $additional = []): bool
public function deliver(User $user, Server $server, array $additional = [], ?int $timeId = null): bool
{
return true;
}
Expand Down
39 changes: 19 additions & 20 deletions Give/Drivers/RconDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@

class RconDriver implements DriverInterface
{
public function deliver(User $user, Server $server, array $additional = []): bool
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');

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

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

if (!$steam)
Expand All @@ -32,14 +32,24 @@ public function deliver(User $user, Server $server, array $additional = []): boo
$steam = $steam->value;
}

try {
$query = $this->sendCommand($server->ip, $server->port, $server->rcon, $this->replace($command, $steam, $user));
$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));
}
return true;
} catch (\Exception $e) {
throw new GiveDriverException($e->getMessage());
} finally {
$query['query']->Disconnect();
$query->Disconnect();
}

return false;
Expand Down Expand Up @@ -82,19 +92,8 @@ protected function replace(string $command, $steam, User $user): string
], $command);
}

protected function sendCommand(string $ip, int $port, string $rcon, string $command): array
protected function sendCommand(SourceQuery $query, string $command): void
{
$Query = new SourceQuery;

$Query->Connect($ip, $port);
$Query->SetRconPassword($rcon);

// We don't need a get result from the server
$RconResult = $Query->Rcon($command);

return [
'query' => $Query,
'rcon' => $RconResult
];
$query->Rcon($command);
}
}
}
87 changes: 44 additions & 43 deletions Give/Drivers/VipDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,8 @@

class VipDriver extends AbstractDriver
{
private function validateAdditionalParams(array $additional, Server $server): array
{
if (empty($additional['group'])) {
throw new BadConfigurationException('group');
}

$dbConnection = $server->getDbConnection('VIP');
if (!$dbConnection) {
throw new BadConfigurationException('db connection VIP is not exists');
}

$dbParams = Json::decode($dbConnection->additional);
if (empty($dbParams->sid)) {
throw new BadConfigurationException("SID {$server->name} for db connection is empty");
}

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

private function updateOrInsertUser($db, $accountId, $sid, $group, $time, $user, $currentGroup = null)
{
$expiresTime = ($time === 0) ? 0 : ($currentGroup ? $currentGroup['expires'] + $time : time() + $time);
if ($currentGroup) {
$db->table('users')
->update(['expires' => $expiresTime])
->where('account_id', $accountId)
->andWhere('sid', $sid)
->run();
} else {
$db->insert('users')
->values([
'expires' => $expiresTime,
'group' => $group,
'account_id' => $accountId,
'lastvisit' => time(),
'sid' => $sid,
'name' => $user->name,
])
->run();
}
}

public function deliver(User $user, Server $server, array $additional = []): bool
public function deliver(User $user, Server $server, array $additional = [], ?int $timeId = null): bool
{
[$dbConnection, $sid] = $this->validateAdditionalParams($additional, $server);

Expand All @@ -64,7 +23,7 @@ public function deliver(User $user, Server $server, array $additional = []): boo

$accountId = steam()->steamid($steam->value)->GetAccountID();
$group = $additional['group'];
$time = $additional['time'] ?? 0;
$time = !$timeId ? ($additional['time'] ?? 0) : $timeId;

$db = dbal()->database($dbConnection->dbname);
$dbusers = $db->table("users")->select()
Expand Down Expand Up @@ -97,4 +56,46 @@ public function alias(): string
{
return 'vip';
}

private function validateAdditionalParams(array $additional, Server $server): array
{
if (empty($additional['group'])) {
throw new BadConfigurationException('group');
}

$dbConnection = $server->getDbConnection('VIP');
if (!$dbConnection) {
throw new BadConfigurationException('db connection VIP is not exists');
}

$dbParams = Json::decode($dbConnection->additional);
if (empty($dbParams->sid)) {
throw new BadConfigurationException("SID {$server->name} for db connection is empty");
}

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

private function updateOrInsertUser($db, $accountId, $sid, $group, $time, $user, $currentGroup = null)
{
$expiresTime = ($time === 0) ? 0 : ($currentGroup ? $currentGroup['expires'] + $time : time() + $time);
if ($currentGroup) {
$db->table('users')
->update(['expires' => $expiresTime])
->where('account_id', $accountId)
->andWhere('sid', $sid)
->run();
} else {
$db->insert('users')
->values([
'expires' => $expiresTime,
'group' => $group,
'account_id' => $accountId,
'lastvisit' => time(),
'sid' => $sid,
'name' => $user->name,
])
->run();
}
}
}
4 changes: 2 additions & 2 deletions Give/GiveFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public function getAll(): array
return $this->drivers;
}

public function make(string $name, User $user, Server $server, array $additional = []): bool
public function make(string $name, User $user, Server $server, array $additional = [], ?int $timeId = null): bool
{
if( !$this->exists($name) )
throw new \Exception("Driver '$name' is not exists");

return (new $this->drivers[$name])->deliver($user, $server, $additional);
return (new $this->drivers[$name])->deliver($user, $server, $additional, $timeId);
}

public function add(string $class) : self
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.3",
"version": "1.0.4",
"description": "A core module for what provides a ability to to give privileges",
"authors": [
"Flames"
Expand Down

0 comments on commit d0ced23

Please sign in to comment.