diff --git a/assets/css/components/_board.scss b/assets/css/components/_board.scss index c9fac3c..5d0ef5f 100644 --- a/assets/css/components/_board.scss +++ b/assets/css/components/_board.scss @@ -8,16 +8,13 @@ width: 1.5%; } -.board__container { - grid-area: board; -} - .board { background: $background-color; border-collapse: unset; border-radius: 2.8%; border-spacing: 1px; color: $foreground-color; + grid-area: board; height: var(--board-size); padding: calc(var(--board-size) / 20); position: relative; @@ -61,14 +58,6 @@ display: flex; flex-direction: row; } - - .board__body { - flex-direction: column-reverse; - } - - .board__row { - flex-direction: row; - } } .board--player-is-black { @@ -81,45 +70,20 @@ display: flex; flex-direction: row-reverse; } - - .board__body { - flex-direction: column; - } - - .board__row { - flex-direction: row-reverse; - } } .board__body { background: $background-color; border: 0.25rem solid $foreground-color; border-radius: calc(var(--board-size) / 100); - display: flex; - flex-direction: column; + display: grid; + grid-template-columns: repeat(8, 1fr); + grid-template-rows: repeat(8, 1fr); height: 100%; padding: 1%; width: 100%; } -.board__row { - display: flex; - flex-grow: 1; - - @include odd-between(1, 8) { - .square { - @include odd-between(1, 8) { @extend %square--black; } - @include even-between(1, 8) { @extend %square--white; } - } - } - @include even-between(1, 8) { - .square { - @include odd-between(1, 8) { @extend %square--white; } - @include even-between(1, 8) { @extend %square--black; } - } - } -} - .board__rank-labels { display: none; height: 90%; diff --git a/lib/chess/board.ex b/lib/chess/board.ex index e533e99..63f18fd 100644 --- a/lib/chess/board.ex +++ b/lib/chess/board.ex @@ -1,6 +1,8 @@ defmodule Chess.Board do @moduledoc false + require Logger + def search(board, %{"type" => type, "colour" => colour}) do board |> Enum.filter(fn {_index, piece} -> @@ -17,9 +19,13 @@ defmodule Chess.Board do |> indexes_to_tuples end + def piece(board, {file, rank}) do + board["#{file},#{rank}"] + end + def move_piece(board, %{ - "from" => [from_file, from_rank], - "to" => [to_file, to_rank] + from: {from_file, from_rank}, + to: {to_file, to_rank} }) do {piece, board} = Map.pop(board, to_index({from_file, from_rank})) {piece_captured, board} = Map.pop(board, to_index({to_file, to_rank})) @@ -54,15 +60,15 @@ defmodule Chess.Board do def castling_move(board, %{from: {4, rank}, to: {2, _rank}}) do move_piece(board, %{ - "from" => [0, rank], - "to" => [3, rank] + from: {0, rank}, + to: {3, rank} }) end def castling_move(board, %{"from" => [4, rank], "to" => [6, _rank]}) do move_piece(board, %{ - "from" => [7, rank], - "to" => [5, rank] + from: {7, rank}, + to: {5, rank} }) end diff --git a/lib/chess/moves.ex b/lib/chess/moves.ex index 3fc1c00..73f729a 100644 --- a/lib/chess/moves.ex +++ b/lib/chess/moves.ex @@ -14,6 +14,8 @@ defmodule Chess.Moves do alias Chess.Moves.Pieces.Queen alias Chess.Moves.Pieces.King + require Logger + def make_move(game, move_params) do params = game.board diff --git a/lib/chess_web.ex b/lib/chess_web.ex index aa29f97..db3c672 100644 --- a/lib/chess_web.ex +++ b/lib/chess_web.ex @@ -39,7 +39,8 @@ defmodule ChessWeb do # Import convenience functions from controllers import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1] - import Phoenix.LiveView.Helpers + + import Phoenix.Component # Use all HTML functionality (forms, tags, etc) use Phoenix.HTML @@ -52,11 +53,10 @@ defmodule ChessWeb do end end - def live_view do quote do use Phoenix.LiveView, - layout: {ChessWeb.LayoutView, "live.html"} + layout: {ChessWeb.LayoutView, :live} unquote(view_helpers()) end @@ -90,6 +90,7 @@ defmodule ChessWeb do def router do quote do use Phoenix.Router + import Phoenix.LiveView.Router end end diff --git a/lib/chess_web/channels/game_channel.ex b/lib/chess_web/channels/game_channel.ex index e3707c6..9f85bd9 100644 --- a/lib/chess_web/channels/game_channel.ex +++ b/lib/chess_web/channels/game_channel.ex @@ -5,7 +5,6 @@ defmodule ChessWeb.GameChannel do import ChessWeb.GameView, only: [player: 2, opponent: 2] - alias Chess.Board alias Chess.Emails alias Chess.Mailer alias Chess.MoveList @@ -29,7 +28,7 @@ defmodule ChessWeb.GameChannel do opponent_id: opponent(game, socket.assigns.user_id).id, player: player(game, socket.assigns.user_id), opponent: opponent(game, socket.assigns.user_id).name, - board: Board.transform(game.board), + board: game.board, turn: game.turn, state: game.state, moves: MoveList.transform(game.moves) @@ -135,7 +134,7 @@ defmodule ChessWeb.GameChannel do |> Queries.game_with_moves(socket.assigns.game_id) payload = %{ - board: Board.transform(game.board), + board: game.board, turn: game.turn, state: game.state, moves: MoveList.transform(game.moves) diff --git a/lib/chess_web/endpoint.ex b/lib/chess_web/endpoint.ex index 9a5b77a..fd404cd 100644 --- a/lib/chess_web/endpoint.ex +++ b/lib/chess_web/endpoint.ex @@ -4,15 +4,14 @@ defmodule ChessWeb.Endpoint do @session_options [ store: :cookie, key: "_chess_key", - signing_salt: "9LqUhZTU" + signing_salt: "9LqUhZTU", + same_site: "Lax" ] if sandbox = Application.compile_env(:chess, :sandbox) do plug(Phoenix.Ecto.SQL.Sandbox, sandbox: sandbox) end - socket("/socket", ChessWeb.UserSocket) - socket("/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]) # Serve at "/" the static files from "priv/static" directory. diff --git a/lib/chess_web/router.ex b/lib/chess_web/router.ex index d640d43..bb56960 100644 --- a/lib/chess_web/router.ex +++ b/lib/chess_web/router.ex @@ -42,13 +42,6 @@ defmodule ChessWeb.Router do resources("/password", PasswordController, only: [:edit, :update], singleton: true) end - # Other scopes may use custom stacks. - scope "/api", as: :api do - pipe_through([:api, :auth, :ensure_auth]) - - resources("/opponents", ChessWeb.Api.OpponentsController, only: [:index]) - end - if Mix.env() == :dev do forward("/sent_emails", Bamboo.SentEmailViewerPlug) end diff --git a/lib/chess_web/templates/game/board.html.leex b/lib/chess_web/templates/game/board.html.leex index d11be6e..f243169 100644 --- a/lib/chess_web/templates/game/board.html.leex +++ b/lib/chess_web/templates/game/board.html.leex @@ -21,9 +21,12 @@