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

Store move when it's made

This commit is contained in:
Daniel Barber 2018-05-08 12:36:31 +02:00
parent fc5b500d27
commit 743f6d3095
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
6 changed files with 29 additions and 15 deletions

View File

@ -30,13 +30,20 @@ defmodule Chess.Board do
board["#{file},#{rank}"]
end
def move_piece(board, move_params) do
[from_file, from_rank] = move_params["from"]
[to_file, to_rank] = move_params["to"]
def move_piece(board, %{"from" => from, "to" => to}) do
[from_file, from_rank] = from
[to_file, to_rank] = to
{piece, board} = Map.pop(board, "#{from_file},#{from_rank}")
{piece_captured, board} = Map.pop(board, "#{to_file},#{to_rank}")
Map.put(board, "#{to_file},#{to_rank}", piece)
%{
from: %{file: from_file, rank: from_rank},
to: %{file: to_file, rank: to_rank},
board: Map.put(board, "#{to_file},#{to_rank}", piece),
piece: piece,
piece_captured: piece_captured,
}
end
def default do

View File

@ -61,6 +61,7 @@ defmodule Chess.GameState do
|> Enum.all?(fn({to_file, to_rank}) ->
board
|> Board.move_piece(%{"from" => [file, rank], "to" => [to_file, to_rank]})
|> Map.get(:board)
|> king_in_check?(piece["colour"])
end)
end

View File

@ -35,11 +35,7 @@ defmodule Chess.Store.Game do
|> foreign_key_constraint(:opponent_id)
end
def move_changeset(struct, move_params) do
params = %{
board: Board.move_piece(struct.board, move_params),
}
def move_changeset(struct, params) do
struct
|> cast(params, required_attrs())
|> validate_king_in_check(struct, params)

View File

@ -3,6 +3,8 @@ defmodule ChessWeb.GameChannel do
use ChessWeb, :channel
alias Ecto.Multi
alias Chess.Store.Game
alias Chess.Board
alias Chess.Moves
@ -38,14 +40,18 @@ defmodule ChessWeb.GameChannel do
|> Game.for_user_id()
|> Repo.get!(socket.assigns.game_id)
changeset = Game.move_changeset(game, move_params)
params = Board.move_piece(game.board, move_params)
case Repo.update(changeset) do
{:ok, game} ->
Multi.new
|> Multi.update(:game, Game.move_changeset(game, params))
|> Multi.insert(:move, Ecto.build_assoc(game, :moves, params))
|> Repo.transaction
|> case do
{:ok, %{game: game}} ->
send_update(game)
{:noreply, socket}
{:error, changeset} ->
{:error, :game, changeset, _} ->
{message, _} = changeset.errors[:board]
{:reply, {:error, %{message: message}}, socket}

View File

@ -45,7 +45,8 @@ defmodule Chess.BoardTest do
"3,0" => %{"type" => "queen", "colour" => "white"},
}
new_board = Board.move_piece(board, %{"from" => [3, 0], "to" => [5, 2]})
%{board: new_board} =
Board.move_piece(board, %{"from" => [3, 0], "to" => [5, 2]})
assert new_board == %{
"5,2" => %{"type" => "queen", "colour" => "white"},

View File

@ -89,7 +89,10 @@ defmodule Chess.GameTest do
move_params = %{"from" => [4, 1], "to" => [4, 3]}
changeset = Game.move_changeset(game, move_params)
changeset = Game.move_changeset(
game,
Board.move_piece(game.board, move_params)
)
assert {:ok, new_game} = Repo.update(changeset)
assert new_game.turn == "black"