Skip to content

Commit

Permalink
bansToCollection memory usage optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
valzargaming committed Sep 2, 2024
1 parent 0b96f61 commit 11ebd33
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/Civ13/Civ13.php
Original file line number Diff line number Diff line change
Expand Up @@ -1221,12 +1221,12 @@ public function ckeyinfo(string $ckey): array

return [
'ckeys' => $ckeys,
'discords' => $discords,
'ips' => $ips,
'cids' => $cids,
'banned' => $this->bancheck($ckey),
'altbanned' => $altbanned,
'verified' => $verified,
'discords' => $discords
'verified' => $verified
];
}
/**
Expand Down Expand Up @@ -1280,11 +1280,11 @@ public function ckeyinfoEmbed(string $ckey, ?array $ckeyinfo = null): Embed
$embed = $this->createEmbed()->setTitle($ckey);
if (isset($this->verifier) && $user = $this->verifier->getVerifiedUser($ckey)) $embed->setAuthor("{$user->username} ({$user->id})", $user->avatar);
if (! empty($ckeyinfo['ckeys'])) $embed->addFieldValues('Ckeys', implode(', ', array_map(fn($ckey) => isset($this->ages[$ckey]) ? "$ckey ({$this->ages[$ckey]})" : $ckey, $ckeyinfo['ckeys'])));
if (! empty($ckeyinfo['discords'])) $embed->addfieldValues('Discord', implode(', ', array_map(fn($id) => $id ? "<@{$id}>" : $id, $ckeyinfo['discords'])), true);
if (! empty($ckeyinfo['ips'])) $embed->addFieldValues('IPs', implode(', ', $ckeyinfo['ips']), true);
if (! empty($ckeyinfo['cids'])) $embed->addFieldValues('CIDs', implode(', ', $ckeyinfo['cids']), true);
if (! empty($ckeyinfo['ips'])) $embed->addFieldValues('Regions', implode(', ', array_unique(array_map(fn($ip) => IPToCountryResolver::Offline($ip), $ckeyinfo['ips']))), true);
$embed->addfieldValues('verified', $ckeyinfo['verified'] ? 'Yes' : 'No');
if (! empty($ckeyinfo['discords'])) $embed->addfieldValues('Discord', implode(', ', array_map(fn($id) => $id ? "<@{$id}>" : $id, $ckeyinfo['discords'])), true);
$embed->addfieldValues('Currently Banned', $ckeyinfo['banned'] ? 'Yes' : 'No', true);
$embed->addfieldValues('Alt Banned', $ckeyinfo['altbanned'] ? 'Yes' : 'No', true);
$embed->addfieldValues('Ignoring banned alts or new account age', isset($this->permitted[$ckey]) ? 'Yes' : 'No', true);
Expand Down Expand Up @@ -1319,22 +1319,22 @@ public function softban(string $id, bool $allow = true): array
return $this->softbanned;
}

public function bansToCollection($ban_collection = new Collection([], 'increment')): Collection
public function bansToCollection($ban_collection = new Collection([], 'increment'), int $increment = 0): Collection
{
$file_contents = '';
foreach ($this->enabled_gameservers as &$gameserver) {
if (! @file_exists($gameserver->basedir . self::bans) || ! $fc = @file_get_contents($gameserver->basedir . self::bans)) {
$this->logger->warning("Unable to open '{$gameserver->basedir}" . self::bans . '`');
if (! @file_exists($file_path = $gameserver->basedir . self::bans) || ! $file_contents = @file_get_contents($file_path)) {
$this->logger->warning("Unable to open '{$file_path}'");
continue;
}
$file_contents .= $fc;
}
if (! $file_contents = str_replace(PHP_EOL, '', $file_contents)) return $ban_collection;
$increment = 0;
foreach (explode('|||', $file_contents) as $item) if ($ban = $this->banArrayToAssoc(explode(';', $item))) {
$ban['increment'] = ++$increment;
$ban_collection->pushItem($ban);

foreach (explode('|||', str_replace(PHP_EOL, '', $file_contents)) as $item) {
if ($ban = $this->banArrayToAssoc(explode(';', $item))) {
$ban['increment'] = ++$increment;
$ban_collection->pushItem($ban);
}
}
}

return $ban_collection;
}
/*
Expand Down

0 comments on commit 11ebd33

Please sign in to comment.