Skip to content

Commit

Permalink
[feat-player-inv] Validate & Update available pieces
Browse files Browse the repository at this point in the history
  • Loading branch information
dalebezolli committed Dec 27, 2024
1 parent b1bc949 commit a7a6061
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/Http/Controllers/GameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function move(Request $request): \Inertia\Response | \Inertia\ResponseFactory |
$player_color = $player->getCurrentSessionColor()->value;
$current_session = $player->getCurrentSession();
$board = json_decode($current_session['board_state']);
$player_available_pieces = json_decode($current_session['player_'.$player_color.'_inventory']);

if($current_session['session_state'] !== GameSessionState::Playing->value) {
if($request->expectsJson()) {
Expand Down Expand Up @@ -70,7 +71,17 @@ function move(Request $request): \Inertia\Response | \Inertia\ResponseFactory |
'flip' => 'required|boolean',
]);

// TODO: Validate that the piece is available
if(!$player_available_pieces[(int)$data['code']]) {
if($request->expectsJson()) {
return response([
'valid' => false,
'board' => $board,
'message' => 'The piece is not available to you'
])->setStatusCode(400);
} else {
return inertia('Game')->with('flash', 'The piece is not available to you');
}
}

$piece = $this->getPieceMatrix((string)$data['code']);
if($data['flip']) $this->flipPiece($piece);
Expand All @@ -89,6 +100,8 @@ function move(Request $request): \Inertia\Response | \Inertia\ResponseFactory |
);
if($is_valid) {
$this->update_board($board, $piece, $origin_offset, $player_color[0]);
$player_available_pieces[(int)$data['code']] = false;
$current_session['player_'.$player_color.'_inventory'] = $player_available_pieces;
$current_session['board_state'] = json_encode($board);
$current_session['current_round'] = $current_session['current_round'] + 1;
$current_session['current_playing'] = $current_session->getNextPlayerColor();
Expand Down

0 comments on commit a7a6061

Please sign in to comment.