Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/app/GameStat.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function getStatsByType($type)
return GameStat::where("type", $type)->orderBy("order", "ASC")->get();
}

public static function createOrUpdateStat($abbrev, $playersOnline, $type, $order, $steamInGameCount = 0)
public static function createOrUpdateStat($abbrev, $playersOnline, $type, $order, $steamInGameCount = 0, $generalsOnlineCount = 0)
{
$gameStat = GameStat::where("abbrev", $abbrev)->first();
if ($gameStat == null)
Expand All @@ -42,6 +42,7 @@ public static function createOrUpdateStat($abbrev, $playersOnline, $type, $order
$gameStat->type = $type;
$gameStat->order = $order;
$gameStat->steam_players_online = $steamInGameCount;
$gameStat->generals_online_players = $generalsOnlineCount;
$gameStat->save();

GameStatGraph::createStat(
Expand Down
13 changes: 12 additions & 1 deletion src/app/Http/Services/CNCOnlineCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Http\Services\CnCNet\CnCNetAPI;
use App\Http\Services\CnCNet\CnCNetSoleAPI;
use App\Http\Services\CnCOnline\CnCOnlineAPI;
use App\Http\Services\GeneralsOnline\GeneralsOnlineAPI;
use App\Http\Services\OpenRA\OpenRAAPI;
use App\Http\Services\RA3BattleNet\RA3BattleNetAPI;
use App\Http\Services\RenegadeX\RenegadeXAPI;
Expand All @@ -24,6 +25,7 @@ class CNCOnlineCount
private $steamHelper;
private $cncnetSoleAPI;
private $ra3BattleNetAPI;
private $generalsOnlineAPI;

public function __construct()
{
Expand All @@ -35,6 +37,7 @@ public function __construct()
$this->steamHelper = new SteamHelper();
$this->cncnetSoleAPI = new CnCNetSoleAPI();
$this->ra3BattleNetAPI = new RA3BattleNetAPI();
$this->generalsOnlineAPI = new GeneralsOnlineAPI();
}

public function runCountTasks()
Expand Down Expand Up @@ -150,6 +153,7 @@ private function groupAndSaveIntoGameTypes($results)
$newResults["games"][$game] = $count;
$order = $gamesFilter[$game];
$steamInGameCount = 0;
$generalsOnlineCount = 0;

// Hack - Fetch individual steam players online
if (in_array($game, $steamGamesFilter))
Expand All @@ -161,12 +165,19 @@ private function groupAndSaveIntoGameTypes($results)
}
}

// Fetch Generals Online count for Generals Zero Hour
if ($game === 'generalszh')
{
$generalsOnlineCount = $this->generalsOnlineAPI->getOnlineCount();
}

GameStat::createOrUpdateStat(
$game,
$count,
GameStat::TYPE_GAME,
$order,
$steamInGameCount
$steamInGameCount,
$generalsOnlineCount
);
}

Expand Down
60 changes: 60 additions & 0 deletions src/app/Http/Services/GeneralsOnline/GeneralsOnlineAPI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Http\Services\GeneralsOnline;

use App\Constants;
use Exception;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class GeneralsOnlineAPI
{
private $_apiUrl;
private $_apiKey;

public function __construct()
{
$this->_apiUrl = config('app.generals_online_api_url');
$this->_apiKey = config('app.generals_online_api_key');
}

public function getOnlineCount()
{
return Cache::remember('GeneralsOnlineAPI.getOnlineCount', 450, function ()
{
try
{
$response = Http::withHeaders([
'X-API-Key' => $this->_apiKey,
'Accept' => 'application/json',
])->get($this->_apiUrl);

if ($response->successful())
{
return $this->getPlayerCountFromResponse($response->json());
}

Log::error('GeneralsOnlineAPI failed: ' . $response->status());
return 0;
}
catch(Exception $exception)
{
Log::error('GeneralsOnlineAPI exception: ' . $exception->getMessage());
return 0;
}
});
}

private function getPlayerCountFromResponse($data)
{
// The API returns an object with an 'active_users' array
// We need to count the active_users array
if (isset($data['active_users']) && is_array($data['active_users']))
{
return count($data['active_users']);
}

return 0;
}
}
4 changes: 4 additions & 0 deletions src/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

'steam_api_key' => env('STEAM_API_KEY', null),

'generals_online_api_url' => env('GENERALS_ONLINE_API_URL', null),

'generals_online_api_key' => env('GENERALS_ONLINE_API_KEY', null),

/*
|--------------------------------------------------------------------------
| Application Environment
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('game_stats', function (Blueprint $table) {
$table->integer("generals_online_players")->default(0)->after('steam_players_online');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('game_stats', function (Blueprint $table) {
$table->dropColumn('generals_online_players');
});
}
};
5 changes: 5 additions & 0 deletions src/resources/views/components/online-box.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class="stat-game-box stat-game--image-{{ $gameAbrev }}">
<strong>{{ $onlineCount }} online {{ $onlineService ?? '' }}</strong>
@endif
</div>
<div style="padding: 0 0.5rem 0.5rem 0.5rem;">
@if (isset($generalsOnlineCount) && $generalsOnlineCount > 0)
<strong>{{ $generalsOnlineCount }} online GeneralsOnline</strong>
@endif
</div>
<div style="color:#e5e5e5">
@if (isset($steamInGameCount) && $steamInGameCount > 0)
<strong style="font-size:0.9rem;color:#a3a3a3;">{{ $steamInGameCount }} Players On Steam</strong>
Expand Down
1 change: 1 addition & 0 deletions src/resources/views/pages/stats.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'onlineCount' => $game->getOnlineCount(),
'steamInGameCount' => $game->steam_players_online,
'onlineService' => $gameByAbbreviation['online_service'],
'generalsOnlineCount' => $game->generals_online_players ?? 0,
])
@endforeach
</div>
Expand Down