129 lines
4.1 KiB
PHP
129 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Game;
|
|
use App\Models\User;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Models\CoinTransaction;
|
|
|
|
class StatisticsController extends Controller
|
|
{
|
|
public function getPublicStats()
|
|
{
|
|
$totalPlayers = User::where('type', 'P')->count();
|
|
|
|
$totalGames = Game::where('status', 'Ended')->count();
|
|
$activeGames = Game::where('status', 'Playing')->count();
|
|
|
|
return response()->json([
|
|
'total_players' => $totalPlayers,
|
|
'total_games' => $totalGames,
|
|
'active_games' => $activeGames,
|
|
]);
|
|
}
|
|
|
|
public function getLeaderboard(Request $request)
|
|
{
|
|
$type = $request->query('type');
|
|
|
|
$query = User::where('type', 'P')
|
|
->withCount(['wonGames' => function ($query) use ($type) {
|
|
if ($type) {
|
|
$query->where('type', $type);
|
|
}
|
|
}])
|
|
->orderBy('won_games_count', 'desc')
|
|
->take(10);
|
|
|
|
$leaderboard = $query->get(['id', 'nickname', 'photo_avatar_filename']);
|
|
|
|
return response()->json($leaderboard);
|
|
}
|
|
|
|
public function getPersonalStats(Request $request)
|
|
{
|
|
$user = $request->user();
|
|
|
|
$totalGames = Game::where('status', 'Ended')
|
|
->where(function ($q) use ($user) {
|
|
$q->where('player1_user_id', $user->id)
|
|
->orWhere('player2_user_id', $user->id);
|
|
})
|
|
->count();
|
|
|
|
$totalWins = Game::where('status', 'Ended')
|
|
->where('winner_user_id', $user->id)
|
|
->count();
|
|
|
|
$totalLosses = $totalGames - $totalWins; // Simplest math
|
|
|
|
$winRate = 0;
|
|
if ($totalGames > 0) {
|
|
$winRate = round(($totalWins / $totalGames) * 100, 1);
|
|
}
|
|
|
|
return response()->json([
|
|
'nickname' => $user->nickname,
|
|
'total_games' => $totalGames,
|
|
'total_wins' => $totalWins,
|
|
'total_losses' => $totalLosses,
|
|
'win_rate' => $winRate . '%',
|
|
'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();
|
|
|
|
$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();
|
|
|
|
|
|
$totalCoinsPurchased = CoinTransaction::whereHas('type', function($q) {
|
|
$q->where('name', 'Coin purchase');
|
|
})
|
|
->sum('coins');
|
|
|
|
$purchasesPerMonth = 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,
|
|
]
|
|
]);
|
|
}
|
|
} |