with(['winner']); if ($request->has('type') && in_array($request->type, ['3', '9'])) { $query->where('type', $request->type); } if ($request->has('status') && in_array($request->status, ['Pending', 'Playing', 'Ended', 'Interrupted'])) { $query->where('status', $request->status); } // Sorting $sortField = $request->input('sort_by', 'began_at'); $sortDirection = $request->input('sort_direction', 'desc'); $allowedSortFields = [ 'began_at', 'ended_at', 'total_time', 'type', 'status' ]; if (in_array($sortField, $allowedSortFields)) { $query->orderBy($sortField, $sortDirection === 'asc' ? 'asc' : 'desc'); } // Pagination $perPage = $request->input('per_page', 15); $games = $query->paginate($perPage); return response()->json([ 'data' => $games->items(), 'meta' => [ 'current_page' => $games->currentPage(), 'last_page' => $games->lastPage(), 'per_page' => $games->perPage(), 'total' => $games->total() ] ]); } /** * Store a newly created resource in storage. */ public function store(StoreGameRequest $request) { $validated = $request->validated(); $user = $request->user(); $type = $validated['variant'] === 'bisca-3' ? '3' : '9'; $initialState = $this->initializeGameState($type); $game = Game::create([ 'type' => $type, 'status' => 'in_progress', 'player1_user_id' => $user->id, 'player2_user_id' => $validated['mode'] === 'multiplayer' ? $validated['opponent_id'] : 1, // REPLACE 1 with your actual Bot User ID 'began_at' => now(), 'custom' => json_encode($initialState), 'player1_points' => 0, 'player2_points' => 0, ]); return response()->json([ 'message' => 'Game created successfully', 'game_id' => $game->id, 'game_state' => $initialState, ], 201); } private function initializeGameState($type) { $handSize = $type === 'S' ? 3 : 9; $suits = ['H', 'D', 'C', 'S']; $ranks = ['2', '3', '4', '5', '6', '7', 'J', 'Q', 'K', 'A']; $values = [ '2' => 0, '3' => 0, '4' => 0, '5' => 0, '6' => 0, '7' => 10, 'J' => 3, 'Q' => 2, 'K' => 4, 'A' => 11 ]; $deck = []; foreach ($suits as $suit) { foreach ($ranks as $rank) { $deck[] = ['suit' => $suit, 'rank' => $rank, 'value' => $values[$rank]]; } } // Shuffle and deal shuffle($deck); $player1Hand = array_splice($deck, 0, $handSize); $player2Hand = array_splice($deck, 0, $handSize); // Trump is the suit of the last card in the remaining stock $trumpCard = null; if (count($deck) > 0) { $trumpCard = $deck[count($deck) - 1]; // Bottom card } return [ 'player1_hand' => $player1Hand, 'player2_hand' => $player2Hand, 'stock' => $deck, 'trump' => $trumpCard ? $trumpCard['suit'] : null, 'current_trick' => [], 'current_turn' => 'player1', 'score' => [ 'player1' => 0, 'player2' => 0 ], 'captured' => [ 'player1' => [], 'player2' => [] ] ]; } /** * Display the specified resource. */ public function show(Game $game) { return $game; } /** * Update the specified resource in storage. */ public function update(Request $request, Game $game) { $game->update($request->validated()); return response()->json($game); } /** * Remove the specified resource from storage. */ public function destroy(Game $game) { // } }