# Dashboard Game Controller - Data Specification

## File: `Modules/Dashboard/app/Http/Controllers/GameController.php`

### Method: `index()`

Controller harus mengirimkan data berikut ke view `dashboard::game`:

```php
public function index(Request $request)
{
    $user = auth()->user();
    
    // Get stats data
    $total_coins = \Modules\Point\Models\Coin::where('user_id', $user->id)
        ->where('source', 'game_reward')
        ->sum('amount');
    
    $today_coins = \Modules\Point\Models\Coin::where('user_id', $user->id)
        ->where('source', 'game_reward')
        ->whereDate('created_at', today())
        ->sum('amount');
    
    // Get game rewards history (paginated)
    $game_rewards = \Modules\Game\Models\GameSession::with(['game', 'user.wallets.platform'])
        ->where('user_id', $user->id)
        ->where('status', 'completed')
        ->orderBy('ended_at', 'desc')
        ->paginate($request->get('per_page', 10));
    
    // Get active games
    $games = \Modules\Game\Models\Game::where('is_active', true)
        ->orderBy('name')
        ->get();
    
    return view('dashboard::game', compact(
        'total_coins',
        'today_coins',
        'game_rewards',
        'games'
    ));
}
```

---

## Data Variables Required

### 1. **$total_coins** (Integer)
- **Source:** Table `coins`
- **Calculation:** Sum of all `amount` from coins table where:
  - `user_id` = current logged in user
  - `source` = 'game_reward'
- **Display:** Stats card "Total Poin yang Didapat"

### 2. **$today_coins** (Integer)
- **Source:** Table `coins`
- **Calculation:** Sum of all `amount` from coins table where:
  - `user_id` = current logged in user
  - `source` = 'game_reward'
  - `created_at` = today's date
- **Display:** Stats card "Total Poin Hari ini"

### 3. **$game_rewards** (Paginated Collection)
- **Source:** Table `game_sessions`
- **Relations:** 
  - `game` (BelongsTo)
  - `user.wallets.platform` (HasMany through User)
- **Filters:**
  - `user_id` = current logged in user
  - `status` = 'completed'
- **Order:** `ended_at` DESC
- **Pagination:** 10, 25, 50, or 100 per page (from request)
- **Fields Used:**
  - `game_sessions.points_earned` → displayed as "+{points_earned}"
  - `game_sessions.ended_at` → formatted as "d/m/Y"
  - `games.name` → game name
  - `wallet_platforms.name` → wallet platform name
- **Display:** Data table "Data Tabel Game Reward"

### 4. **$games** (Collection)
- **Source:** Table `games`
- **Filters:**
  - `is_active` = true
- **Order:** `name` ASC
- **Fields Used:**
  - `games.id` → for routing
  - `games.slug` → for URL (route to play game)
  - `games.name` → game title
  - `games.description` → game description
  - `games.thumbnail` → game image (path in storage)
  - `games.point_reward` → coins earned info
- **Display:** Games grid section "SEMUA GAME"

---

## Database Tables Reference

### Related Tables:
1. **coins** - Coin transactions
   ```
   - id (BIGSERIAL)
   - user_id (BIGINT)
   - type (VARCHAR) - 'earned', 'spent', 'expired', 'adjusted'
   - amount (INTEGER)
   - source (VARCHAR) - 'game_reward'
   - created_at (TIMESTAMP)
   ```

2. **game_sessions** - Game play history
   ```
   - id (BIGSERIAL)
   - user_id (BIGINT)
   - game_id (BIGINT)
   - score (INTEGER)
   - points_earned (INTEGER)
   - started_at (TIMESTAMP)
   - ended_at (TIMESTAMP)
   - status (VARCHAR) - 'playing', 'completed', 'abandoned'
   ```

3. **games** - Available games
   ```
   - id (BIGSERIAL)
   - name (VARCHAR)
   - slug (VARCHAR)
   - description (TEXT)
   - thumbnail (VARCHAR)
   - point_reward (INTEGER)
   - daily_limit (INTEGER)
   - is_active (BOOLEAN)
   ```

4. **wallets** - User wallets
   ```
   - id (BIGSERIAL)
   - user_id (BIGINT)
   - platform_id (BIGINT)
   ```

5. **wallet_platforms** - Wallet platforms
   ```
   - id (BIGSERIAL)
   - name (VARCHAR) - 'Indodax', 'MetaMask', etc.
   ```

---

## Route Definition

Add to `Modules/Dashboard/routes/web.php`:

```php
Route::middleware(['auth'])->prefix('dashboard')->name('dashboard.')->group(function () {
    Route::get('/game', [GameController::class, 'index'])->name('game');
    Route::get('/game/{slug}/play', [GameController::class, 'play'])->name('game.play');
});
```

---

## Sample Controller Implementation

```php
<?php

namespace Modules\Dashboard\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Modules\Game\Models\Game;
use Modules\Game\Models\GameSession;
use Modules\Point\Models\Coin;

class GameController extends Controller
{
    public function index(Request $request)
    {
        $user = auth()->user();
        
        // Total coins from game rewards
        $total_coins = Coin::where('user_id', $user->id)
            ->where('source', 'game_reward')
            ->sum('amount');
        
        // Today's coins from game rewards
        $today_coins = Coin::where('user_id', $user->id)
            ->where('source', 'game_reward')
            ->whereDate('created_at', today())
            ->sum('amount');
        
        // Game rewards history with pagination
        $game_rewards = GameSession::with(['game', 'user.wallets.platform'])
            ->where('user_id', $user->id)
            ->where('status', 'completed')
            ->orderBy('ended_at', 'desc')
            ->paginate($request->get('per_page', 10));
        
        // Active games list
        $games = Game::where('is_active', true)
            ->orderBy('name')
            ->get();
        
        return view('dashboard::game', [
            'title' => 'Game',
            'meta_description' => 'Mainkan game dan dapatkan rewards',
            'total_coins' => $total_coins,
            'today_coins' => $today_coins,
            'game_rewards' => $game_rewards,
            'games' => $games,
        ]);
    }
    
    public function play($slug)
    {
        $game = Game::where('slug', $slug)
            ->where('is_active', true)
            ->firstOrFail();
        
        // Game play logic here
        return view('dashboard::game-play', compact('game'));
    }
}
```

---

## Notes for Backend Developer

1. **Pagination:** The view supports pagination with customizable rows per page (10, 25, 50, 100)
2. **Search Feature:** Search input is prepared but needs backend implementation
3. **Eager Loading:** Make sure to use `with()` for relationships to avoid N+1 queries
4. **Default Values:** All variables have default values/fallbacks, so the page won't break if data is empty
5. **Date Format:** Use Carbon for date formatting: `Carbon::parse($date)->format('d/m/Y')`
6. **Image Storage:** Game thumbnails should be stored in `storage/` directory
7. **Route Naming:** Follow the convention `dashboard.game.*` for consistency

---

**Last Updated:** January 8, 2026
