Added initial game logic
This commit is contained in:
@@ -59,10 +59,79 @@ class GameController extends Controller
|
||||
*/
|
||||
public function store(StoreGameRequest $request)
|
||||
{
|
||||
$game = Game::create($request->validated());
|
||||
return response()->json($game, 201);
|
||||
$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.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Requests\StoreGameRequest;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
|
||||
}
|
||||
@@ -11,7 +11,7 @@ class StoreGameRequest extends FormRequest
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -22,7 +22,11 @@ class StoreGameRequest extends FormRequest
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
'variant' => 'required|in:bisca-3,bisca-9',
|
||||
'mode' => 'required|in:single-player,multiplayer',
|
||||
'opponent_id' => $this->mode === 'multiplayer'
|
||||
? 'required|exists:users,id'
|
||||
: 'nullable',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user