diff --git a/lib/chess/game_state.ex b/lib/chess/game_state.ex index 5c9a8df..6211b4a 100644 --- a/lib/chess/game_state.ex +++ b/lib/chess/game_state.ex @@ -38,6 +38,10 @@ defmodule Chess.GameState do |> Board.search(%{"type" => "king", "colour" => colour}) |> List.first + if king == nil do + raise "There is no #{colour} king!" + end + board |> Piece.attacked?(king) end diff --git a/lib/chess_web/controllers/page_controller.ex b/lib/chess_web/controllers/page_controller.ex index bb7036d..74e6bcd 100644 --- a/lib/chess_web/controllers/page_controller.ex +++ b/lib/chess_web/controllers/page_controller.ex @@ -3,7 +3,13 @@ defmodule ChessWeb.PageController do use ChessWeb, :controller + import Chess.Auth, only: [current_user: 1] + def index(conn, _params) do - render(conn, "index.html") + if current_user(conn) != nil do + conn |> redirect(to: game_path(conn, :index)) |> halt() + else + render(conn, "index.html") + end end end diff --git a/test/features/games_test.exs b/test/features/games_test.exs index 19e4886..2774456 100644 --- a/test/features/games_test.exs +++ b/test/features/games_test.exs @@ -29,7 +29,7 @@ defmodule Chess.Features.GamesTest do |> click(button("Create game")) session - |> assert_has(css("h2", text: "Game with Zelda")) + |> assert_has(css("p", text: "Playing Zelda")) |> assert_has(css(".board")) end @@ -65,7 +65,7 @@ defmodule Chess.Features.GamesTest do |> click(link("New game")) |> select("game[opponent_id]", option: "Urbosa") |> click(button("Create game")) - |> click(link("Back to games")) + |> click(link("Chess")) session |> assert_has(css(".table tr", count: 1)) @@ -96,6 +96,6 @@ defmodule Chess.Features.GamesTest do |> click(link("Game with Zelda")) session - |> assert_has(css("h2", text: "Game with Zelda")) + |> assert_has(css("p", text: "Playing Zelda")) end end diff --git a/test/features/moves_test.exs b/test/features/moves_test.exs index c25df33..f64a7dd 100644 --- a/test/features/moves_test.exs +++ b/test/features/moves_test.exs @@ -130,6 +130,7 @@ defmodule Chess.Features.MovesTest do 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, @@ -165,6 +166,7 @@ defmodule Chess.Features.MovesTest do "4,0" => %{"type" => "king", "colour" => "white"}, "4,1" => %{"type" => "rook", "colour" => "white"}, "4,7" => %{"type" => "queen", "colour" => "black"}, + "7,7" => %{"type" => "king", "colour" => "black"}, }, user_id: user.id, opponent_id: opponent.id,