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"> <img class="game-info__opponent-status" src="/images/eye-closed.svg" alt="offline">
</p> </p>
</div> </div>
<div class="move-list"> <div class="move-list">
<table class="table table--condensed"> <table class="table table--condensed">
<thead> <thead>

View File

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