mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Fix bug with state check
This commit is contained in:
parent
812607ae36
commit
6513cdbee0
@ -114,6 +114,7 @@
|
||||
&.check,
|
||||
&.checkmate,
|
||||
&.stalemate {
|
||||
display: block; // So PhantomJS will display it.
|
||||
display: flex;
|
||||
}
|
||||
|
||||
|
||||
@ -35,8 +35,8 @@ defmodule Chess.Store.Game do
|
||||
struct
|
||||
|> cast(params, required_attrs())
|
||||
|> validate_king_in_check(struct, params)
|
||||
|> check_game_state(struct, params)
|
||||
|> change_turn(struct.turn)
|
||||
|> check_game_state
|
||||
end
|
||||
|
||||
def change_turn(changeset, turn) do
|
||||
@ -57,9 +57,11 @@ defmodule Chess.Store.Game do
|
||||
or_where: game.opponent_id == ^user_id
|
||||
end
|
||||
|
||||
def check_game_state(changeset, struct, params) do
|
||||
def check_game_state(changeset) do
|
||||
changeset
|
||||
|> put_change(:state, GameState.state(params.board, struct.turn))
|
||||
|> put_change(
|
||||
:state, GameState.state(changeset.changes.board, changeset.changes.turn)
|
||||
)
|
||||
end
|
||||
|
||||
def validate_king_in_check(changeset, %Game{turn: turn}, %{board: board}) do
|
||||
|
||||
@ -184,6 +184,41 @@ defmodule Chess.MovesTest do
|
||||
|> assert_has(square_containing("f4-r1", "white.rook"))
|
||||
end
|
||||
|
||||
test "user is informed when the game is in check", %{session: session} do
|
||||
user = insert(:user, %{
|
||||
name: "Link",
|
||||
email: "link@hyrule.com",
|
||||
password: "ilovezelda"
|
||||
})
|
||||
opponent = insert(:user, %{
|
||||
name: "Zelda",
|
||||
email: "zelda@hyrule.com",
|
||||
password: "ganonsucks"
|
||||
})
|
||||
insert(:game, %{
|
||||
board: %{
|
||||
"4,0" => %{"type" => "king", "colour" => "white"},
|
||||
"3,7" => %{"type" => "queen", "colour" => "black"},
|
||||
"7,7" => %{"type" => "king", "colour" => "black"},
|
||||
},
|
||||
user_id: user.id,
|
||||
opponent_id: opponent.id,
|
||||
turn: "black",
|
||||
})
|
||||
|
||||
session
|
||||
|> login("zelda@hyrule.com", "ganonsucks")
|
||||
|> visit("/games")
|
||||
|> click(link("Game with Link"))
|
||||
|
||||
session
|
||||
|> click(css("#f3-r7"))
|
||||
|> click(css("#f4-r7"))
|
||||
|> assert_has(square_containing("f4-r7", "black.queen"))
|
||||
|
||||
assert session |> has_text?("Check")
|
||||
end
|
||||
|
||||
defp square_selected(square) do
|
||||
css("##{square}.selected")
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user