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,
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class CoinTransaction extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'coin_transactions';
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'transaction_datetime',
|
||||
'user_id',
|
||||
'match_id',
|
||||
'game_id',
|
||||
'coin_transaction_type_id',
|
||||
'coins',
|
||||
'custom'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'transaction_datetime' => 'datetime',
|
||||
'custom' => 'array',
|
||||
];
|
||||
|
||||
public function type()
|
||||
{
|
||||
return $this->belongsTo(CoinTransactionType::class, 'coin_transaction_type_id');
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class CoinTransactionType extends Model
|
||||
{
|
||||
use HasFactory, SoftDeletes;
|
||||
|
||||
protected $table = 'coin_transaction_types';
|
||||
|
||||
protected $fillable = ['name', 'type', 'custom'];
|
||||
|
||||
protected $casts = [
|
||||
'custom' => 'array',
|
||||
];
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Http\Requests\StoreGameRequest;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user