1
0
mirror of https://github.com/danbee/chess synced 2025-03-04 08:39:06 +00:00

Correctly choose pieces

This commit is contained in:
Daniel Barber 2021-07-12 12:29:41 -05:00
parent b01403cbe4
commit 7e2c841b59
2 changed files with 14 additions and 4 deletions

View File

@ -12,6 +12,7 @@
<img class="game-info__opponent-status" src="/images/eye-closed.svg" alt="offline">
</p>
</div>
<div class="move-list">
<table class="table table--condensed">
<thead>

View File

@ -6,12 +6,15 @@ defmodule ChessWeb.BoardLive do
alias Chess.Repo
alias Chess.Board
import Chess.Auth, only: [get_user!: 1]
def render(assigns) do
Phoenix.View.render(ChessWeb.GameView, "board.html", assigns)
end
def mount(_params, %{"user_id" => user_id, "game_id" => game_id}, socket) do
user = Repo.get!(User, user_id)
game =
Game.for_user(user)
|> Repo.get!(game_id)
@ -24,19 +27,25 @@ defmodule ChessWeb.BoardLive do
end
defp handle_click(socket, file, rank) do
board = socket.assigns[:game].board
game = socket.assigns[:game]
board = game.board
user = socket.assigns[:user]
colour = ChessWeb.GameView.player_colour(user, game)
assigns =
case socket.assigns do
%{:selected => nil} ->
case Board.piece(board, {file, rank}) do
%{"colour" => "white"} ->
%{"colour" => ^colour} ->
[{:selected, selected(file, rank)}]
_ ->
[]
end
_ ->
[]
[{:selected, nil}]
end
assign(socket, assigns)
@ -45,7 +54,7 @@ defmodule ChessWeb.BoardLive do
defp selected(file, rank) do
{
String.to_integer(file),
String.to_integer(rank),
String.to_integer(rank)
}
end
end