Skip to content
This repository was archived by the owner on May 26, 2023. It is now read-only.

Commit bd674d5

Browse files
authored
Merge pull request #196 from ExpDev07/dev
New features
2 parents cc0224a + 95711c5 commit bd674d5

File tree

8 files changed

+66
-29
lines changed

8 files changed

+66
-29
lines changed

app/Http/Controllers/HomeController.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ public function render(Request $request): Response
4040
->orderByDesc('timestamp')
4141
->limit(8)->get())->toArray($request);
4242

43-
$playerList = Player::getAllOnlinePlayers(true);
43+
$playerList = Player::getAllOnlinePlayers(true) ?? [];
4444
$players = array_keys($playerList);
4545
usort($players, function ($a, $b) use ($playerList) {
4646
return $playerList[$a]['id'] <=> $playerList[$b]['id'];
4747
});
4848
$staff = Player::query()->where('is_staff', '=', true)->whereIn('steam_identifier', $players)->get();
4949

5050
return Inertia::render('Home', [
51-
'quote' => $quote,
52-
'bans' => $bans,
53-
'playerMap' => Player::fetchSteamPlayerNameMap($bans, 'identifier'),
54-
'staff' => PlayerIndexResource::collection($staff),
51+
'quote' => $quote,
52+
'bans' => $bans,
53+
'playerMap' => Player::fetchSteamPlayerNameMap($bans, 'identifier'),
54+
'staff' => PlayerIndexResource::collection($staff),
5555
]);
5656
}
5757

@@ -71,15 +71,19 @@ public function playerCountApi(): \Illuminate\Http\Response
7171
/**
7272
* Returns player count info
7373
*
74-
* @return array
74+
* @return array|null
7575
*/
76-
private function playerCount(): array
76+
private function playerCount(): ?array
7777
{
7878
$totalPlayers = 0;
7979
$joinedPlayers = 0;
8080
$queuePlayers = 0;
8181

8282
$data = Server::collectAllApiData();
83+
if (!$data) {
84+
return null;
85+
}
86+
8387
foreach ($data as $server) {
8488
$totalPlayers += $server['total'];
8589
$joinedPlayers += $server['joined'];

app/Http/Controllers/PlayerController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function index(Request $request): Response
2929
{
3030
$start = round(microtime(true) * 1000);
3131

32-
$playerList = Player::getAllOnlinePlayers(true);
32+
$playerList = Player::getAllOnlinePlayers(true) ?? [];
3333
$players = array_keys($playerList);
3434
usort($players, function ($a, $b) use ($playerList) {
3535
return $playerList[$a]['id'] <=> $playerList[$b]['id'];

app/Player.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@ public function bans(): Builder
311311
* Returns a map of steamIdentifier->serverId,server for each online player
312312
*
313313
* @param bool $useCache
314-
* @return array
314+
* @return array|null
315315
*/
316-
public static function getAllOnlinePlayers(bool $useCache): array
316+
public static function getAllOnlinePlayers(bool $useCache): ?array
317317
{
318318
$serverIps = explode(',', env('OP_FW_SERVERS', ''));
319319

@@ -326,6 +326,10 @@ public static function getAllOnlinePlayers(bool $useCache): array
326326
if ($serverIp) {
327327
$steamIdentifiers = Server::fetchSteamIdentifiers($serverIp, $useCache);
328328

329+
if ($steamIdentifiers === null) {
330+
return null;
331+
}
332+
329333
foreach ($steamIdentifiers as $key => $val) {
330334
if (!isset($result[$key])) {
331335
$result[$key] = [
@@ -356,6 +360,11 @@ public static function getOnlineStatus(string $steamIdentifier, bool $useCache):
356360
}
357361

358362
$players = self::getAllOnlinePlayers($useCache);
363+
364+
if ($players === null) {
365+
return new PlayerStatus(PlayerStatus::STATUS_UNAVAILABLE, '', 0);
366+
}
367+
359368
if (isset($players[$steamIdentifier])) {
360369
$player = $players[$steamIdentifier];
361370
return new PlayerStatus(PlayerStatus::STATUS_ONLINE, $player['server'], $player['id']);

app/Server.php

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static function getServerName(string $serverIp): string
8282

8383
$names = explode(',', env('SERVER_NAMES', ''));
8484
if (!empty($names)) {
85-
foreach($names as $def) {
85+
foreach ($names as $def) {
8686
$def = explode('=', $def);
8787
if (sizeof($def) === 2 && $def[0] === $host) {
8888
return $def[1];
@@ -98,9 +98,9 @@ public static function getServerName(string $serverIp): string
9898
*
9999
* @param string $serverIp
100100
* @param bool $useCache
101-
* @return array
101+
* @return array|null
102102
*/
103-
public static function fetchSteamIdentifiers(string $serverIp, bool $useCache): array
103+
public static function fetchSteamIdentifiers(string $serverIp, bool $useCache): ?array
104104
{
105105
if (!$serverIp) {
106106
return [];
@@ -119,6 +119,10 @@ public static function fetchSteamIdentifiers(string $serverIp, bool $useCache):
119119
$json = null;
120120
try {
121121
$json = GeneralHelper::get($serverIp . 'connections.json') ?? null;
122+
123+
if (!$json) {
124+
return null;
125+
}
122126
} catch (Throwable $t) {
123127
return [];
124128
}
@@ -151,9 +155,9 @@ public static function fetchSteamIdentifiers(string $serverIp, bool $useCache):
151155
/**
152156
* Collects all the /api.json data from all servers
153157
*
154-
* @return array
158+
* @return array|null
155159
*/
156-
public static function collectAllApiData(): array
160+
public static function collectAllApiData(): ?array
157161
{
158162
$serverIps = explode(',', env('OP_FW_SERVERS', ''));
159163

@@ -173,16 +177,21 @@ public static function collectAllApiData(): array
173177
try {
174178
$json = GeneralHelper::get($serverIp . 'api.json') ?? [];
175179

176-
$response = OPFWHelper::parseResponse($json);
177-
178-
if ($response->status && $response->data) {
179-
$json = $response->data;
180-
181-
$result[] = [
182-
'joined' => isset($json['joinedAmount']) ? intval($json['joinedAmount']) : 0,
183-
'total' => isset($json['maxClients']) ? intval($json['maxClients']) : 0,
184-
'queue' => isset($json['queueAmount']) ? intval($json['queueAmount']) : 0,
185-
];
180+
if (!$json) {
181+
$result = null;
182+
break;
183+
} else {
184+
$response = OPFWHelper::parseResponse($json);
185+
186+
if ($response->status && $response->data) {
187+
$json = $response->data;
188+
189+
$result[] = [
190+
'joined' => isset($json['joinedAmount']) ? intval($json['joinedAmount']) : 0,
191+
'total' => isset($json['maxClients']) ? intval($json['maxClients']) : 0,
192+
'queue' => isset($json['queueAmount']) ? intval($json['queueAmount']) : 0,
193+
];
194+
}
186195
}
187196
} catch (Throwable $t) {
188197
}

resources/js/Pages/Home.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</div>
2222

2323
<div class="ml-8 mobile:w-full mobile:ml-0">
24-
<div class="p-4 bg-gray-100 shadow-lg dark:bg-gray-700 dark:text-gray-100 flex justify-between">
24+
<div class="p-4 bg-gray-100 shadow-lg dark:bg-gray-700 dark:text-gray-100 flex justify-between" v-if="playerCount">
2525
<vue-circle ref="serverCount"
2626
:progress="playerCountPercentage()"
2727
:size="70"
@@ -37,6 +37,9 @@
3737
</vue-circle>
3838
<p class="ml-3 pt-5" v-html="playerCount">{{ playerCount }}</p>
3939
</div>
40+
<div class="p-4 bg-gray-100 shadow-lg dark:bg-gray-700 dark:text-gray-100 flex justify-between" v-else>
41+
<p class="py-5 px-3">{{ t('home.no_player_count') }}</p>
42+
</div>
4043
</div>
4144
</div>
4245

@@ -182,6 +185,8 @@ export default {
182185
this.playerCount = this.localizePlayerCount()
183186
184187
this.$refs.serverCount.updateProgress(this.playerCountPercentage());
188+
} else {
189+
this.playerCount = null;
185190
}
186191
} catch(e) {}
187192
},

resources/js/Pages/Players/Index.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@
102102
<span class="font-semibold" v-if="player.status.status === 'online'">
103103
{{ player.status.serverId }} <sup>{{ player.status.serverName }}</sup>
104104
</span>
105+
<span class="font-semibold" v-else-if="player.status.status === 'unavailable'" :title="t('global.status.unavailable_info')">
106+
{{ t('global.status.unavailable') }}
107+
</span>
105108
<span class="font-semibold" v-else>
106109
{{ t('global.status.' + player.status.status) }}
107110
</span>

resources/js/Pages/Players/Show.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
player.status.serverId
2525
}}]</sup></span>
2626
</badge>
27+
<badge class="border-red-200 bg-warning-pale dark:bg-dark-warning-pale"
28+
v-else-if="player.status.status === 'unavailable'"
29+
:title="t('global.status.unavailable_info')">
30+
<span class="font-semibold">{{ t('global.status.unavailable') }}</span>
31+
</badge>
2732
<badge class="border-red-200 bg-danger-pale dark:bg-dark-danger-pale" v-else>
2833
<span class="font-semibold">{{ t('global.status.' + player.status.status) }}</span>
2934
</badge>

resources/js/locales/en-us.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"status": {
1616
"online": "Online",
1717
"offline": "Offline",
18-
"unavailable": "Unavailable"
18+
"unavailable": "Unavailable",
19+
"unavailable_info": "Player status or id is unavailable at this moment"
1920
},
2021
"all": "All",
2122
"back": "Back",
@@ -56,7 +57,8 @@
5657
"locations": "/tp_coords locations",
5758
"location_description": "Hidden locations around the city to avoid no-clipping long distances.",
5859
"staff_locations": "Staff locations",
59-
"staff_description": "Roofs for staff to chill on while doing staff stuff."
60+
"staff_description": "Roofs for staff to chill on while doing staff stuff.",
61+
"no_player_count": "Player count is not available at this time"
6062
},
6163
"statistics": {
6264
"title": "Statistics",
@@ -257,7 +259,7 @@
257259
"update_ban": "Update Ban",
258260
"banned_for": "Banned for about {0}",
259261
"banned_by": "<i>{0}</i> was banned by <i>{1}</i>",
260-
"forever": "<i>{0}</i> permanently banned this player",
262+
"forever": "<i>{0}</i> indefinitely banned this player",
261263
"forever_edit": "forever",
262264
"temp-select": "Toggle date/time input",
263265
"temp-type": "Length Unit",

0 commit comments

Comments
 (0)