Added routes, admin statistics to the controller and added models for coin transaction and coin transaction type
This commit is contained in:
@@ -7,6 +7,8 @@ use App\Models\Game;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Models\CoinTransaction;
|
||||
use App\Models\CoinTransactionType;
|
||||
|
||||
class StatisticsController extends Controller
|
||||
{
|
||||
@@ -73,4 +75,63 @@ class StatisticsController extends Controller
|
||||
'current_balance' => $user->coins_balance, // Extra info
|
||||
]);
|
||||
}
|
||||
|
||||
public function getAdminStats()
|
||||
{
|
||||
$totalPlayers = User::where('type', 'P')->count();
|
||||
$totalGames = Game::count();
|
||||
$totalBlockedUsers = User::where('blocked', true)->count();
|
||||
|
||||
$gamesByType = Game::select('type', DB::raw('count(*) as total'))
|
||||
->groupBy('type')
|
||||
->get();
|
||||
|
||||
// 3. Games Per Month (Line/Bar Chart)
|
||||
$gamesPerMonth = Game::select(
|
||||
DB::raw('DATE_FORMAT(began_at, "%Y-%m") as month'),
|
||||
DB::raw('count(*) as total')
|
||||
)
|
||||
->whereNotNull('began_at')
|
||||
->groupBy('month')
|
||||
->orderBy('month', 'desc')
|
||||
->limit(12)
|
||||
->get();
|
||||
|
||||
// 4. Financial/Coin Stats (Using the new Tables)
|
||||
// We calculate "Revenue" based on the volume of "Coin purchase" transactions
|
||||
|
||||
// Calculate Total Coins Purchased (All time)
|
||||
// We join with types table to filter by name "Coin purchase"
|
||||
$totalCoinsPurchased = CoinTransaction::whereHas('type', function($q) {
|
||||
$q->where('name', 'Coin purchase');
|
||||
})
|
||||
->sum('coins');
|
||||
|
||||
// Calculate Purchases Per Month (Time Series)
|
||||
// Groups by 'transaction_datetime' (YYYY-MM)
|
||||
$purchasesPerMonth = \App\Models\CoinTransaction::join('coin_transaction_types', 'coin_transactions.coin_transaction_type_id', '=', 'coin_transaction_types.id')
|
||||
->where('coin_transaction_types.name', 'Coin purchase')
|
||||
->select(
|
||||
DB::raw('DATE_FORMAT(coin_transactions.transaction_datetime, "%Y-%m") as month'),
|
||||
DB::raw('SUM(coin_transactions.coins) as total_coins')
|
||||
)
|
||||
->groupBy('month')
|
||||
->orderBy('month', 'desc')
|
||||
->limit(12)
|
||||
->get();
|
||||
|
||||
return response()->json([
|
||||
'global_counters' => [
|
||||
'total_players' => $totalPlayers,
|
||||
'total_games' => $totalGames,
|
||||
'blocked_users' => $totalBlockedUsers,
|
||||
'total_coins_purchased' => (int) $totalCoinsPurchased, // Cast to int
|
||||
],
|
||||
'charts' => [
|
||||
'games_by_type' => $gamesByType,
|
||||
'games_per_month' => $gamesPerMonth,
|
||||
'purchases_per_month' => $purchasesPerMonth,
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user