mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Game now has an opponent
This commit is contained in:
parent
5ac47f3ac7
commit
46c3109b7c
@ -2,6 +2,7 @@ defmodule ChessWeb.GameController do
|
|||||||
use ChessWeb, :controller
|
use ChessWeb, :controller
|
||||||
|
|
||||||
alias Chess.Store.Game
|
alias Chess.Store.Game
|
||||||
|
alias Chess.Auth.User
|
||||||
|
|
||||||
def index(conn, _params) do
|
def index(conn, _params) do
|
||||||
changeset = Game.changeset(%Game{})
|
changeset = Game.changeset(%Game{})
|
||||||
@ -14,6 +15,18 @@ defmodule ChessWeb.GameController do
|
|||||||
render(conn, "index.html", games: games, changeset: changeset)
|
render(conn, "index.html", games: games, changeset: changeset)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def new(conn, _params) do
|
||||||
|
changeset = Game.changeset(%Game{})
|
||||||
|
|
||||||
|
query = from user in "users",
|
||||||
|
where: user.id != ^current_user(conn).id,
|
||||||
|
select: {user.username, user.id}
|
||||||
|
|
||||||
|
opponents = query |> Repo.all
|
||||||
|
|
||||||
|
render(conn, "new.html", changeset: changeset, opponents: opponents)
|
||||||
|
end
|
||||||
|
|
||||||
def create(conn, _params) do
|
def create(conn, _params) do
|
||||||
changeset = Game.create_changeset(
|
changeset = Game.create_changeset(
|
||||||
%Game{},
|
%Game{},
|
||||||
|
|||||||
@ -35,7 +35,7 @@ defmodule ChessWeb.Router do
|
|||||||
pipe_through [:browser, :auth, :ensure_auth]
|
pipe_through [:browser, :auth, :ensure_auth]
|
||||||
|
|
||||||
resources "/games", GameController,
|
resources "/games", GameController,
|
||||||
only: [:index, :create, :show, :delete]
|
only: [:index, :new, :create, :show, :delete]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Other scopes may use custom stacks.
|
# Other scopes may use custom stacks.
|
||||||
|
|||||||
@ -15,8 +15,6 @@
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<%= form_for @changeset, game_path(@conn, :create), [class: "create-game"], fn _f -> %>
|
<p>
|
||||||
<div class="form-group">
|
<%= link "New game", to: game_path(@conn, :new), class: "button" %>
|
||||||
<%= submit "Create game", class: "btn btn-primary" %>
|
</p>
|
||||||
</div>
|
|
||||||
<% end %>
|
|
||||||
|
|||||||
14
lib/chess_web/templates/game/new.html.eex
Normal file
14
lib/chess_web/templates/game/new.html.eex
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<h2>New game</h2>
|
||||||
|
|
||||||
|
<%= form_for @changeset, game_path(@conn, :create), [class: "create-game"],
|
||||||
|
fn form -> %>
|
||||||
|
<div class="form-group">
|
||||||
|
<%= label form, :opponent_id %>
|
||||||
|
<%= select form, :opponent_id, @opponents %>
|
||||||
|
<%= error_tag form, :opponent_id %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<%= submit "Create game", class: "button" %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
@ -1,13 +1,8 @@
|
|||||||
defmodule Chess.GamesTest do
|
defmodule Chess.GamesTest do
|
||||||
use ChessWeb.FeatureCase
|
use ChessWeb.FeatureCase
|
||||||
|
|
||||||
import Wallaby.Query, only: [
|
import Wallaby.Query
|
||||||
css: 1,
|
|
||||||
css: 2,
|
|
||||||
button: 1,
|
|
||||||
text_field: 1,
|
|
||||||
link: 1,
|
|
||||||
]
|
|
||||||
import Chess.Factory, only: [create_user: 2, create_game_for: 1]
|
import Chess.Factory, only: [create_user: 2, create_game_for: 1]
|
||||||
|
|
||||||
test "visit homepage", %{session: session} do
|
test "visit homepage", %{session: session} do
|
||||||
@ -17,27 +12,42 @@ defmodule Chess.GamesTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "can create a new game", %{session: session} do
|
test "can create a new game", %{session: session} do
|
||||||
|
create_user("zelda", "ganonsucks")
|
||||||
|
|
||||||
session
|
session
|
||||||
|> login()
|
|> login()
|
||||||
|> create_game()
|
|> visit("/games")
|
||||||
|
|> click(link("New game"))
|
||||||
|
|> select("game[opponent_id]", option: "zelda")
|
||||||
|
|> click(button("Create game"))
|
||||||
|> assert_has(css(".board"))
|
|> assert_has(css(".board"))
|
||||||
end
|
end
|
||||||
|
|
||||||
test "can only see own games", %{session: session} do
|
test "can only see own games", %{session: session} do
|
||||||
user = create_user("zelda@hyrule.kingdom", "ganonsucks")
|
create_user("urbosa", "gerudoqueen")
|
||||||
|
|
||||||
|
user = create_user("zelda", "ganonsucks")
|
||||||
create_game_for(user)
|
create_game_for(user)
|
||||||
|
|
||||||
session
|
session
|
||||||
|> login()
|
|> login()
|
||||||
|> create_game()
|
|> visit("/games")
|
||||||
|
|> click(link("New game"))
|
||||||
|
|> select("game[opponent_id]", option: "urbosa")
|
||||||
|
|> click(button("Create game"))
|
||||||
|> click(link("Back to games"))
|
|> click(link("Back to games"))
|
||||||
|> assert_has(css(".table tr", count: 1))
|
|> assert_has(css(".table tr", count: 1))
|
||||||
end
|
end
|
||||||
|
|
||||||
test "can move a piece", %{session: session} do
|
test "can move a piece", %{session: session} do
|
||||||
|
create_user("zelda", "ganonsucks")
|
||||||
|
|
||||||
session
|
session
|
||||||
|> login()
|
|> login()
|
||||||
|> create_game()
|
|> visit("/games")
|
||||||
|
|> click(link("New game"))
|
||||||
|
|> select("game[opponent_id]", option: "zelda")
|
||||||
|
|> click(button("Create game"))
|
||||||
|
|
||||||
session
|
session
|
||||||
|> click(css("#f4-r1"))
|
|> click(css("#f4-r1"))
|
||||||
@ -60,10 +70,12 @@ defmodule Chess.GamesTest do
|
|||||||
|> click(button("Log in"))
|
|> click(button("Log in"))
|
||||||
end
|
end
|
||||||
|
|
||||||
defp create_game(session) do
|
def select(session, name, [option: option]) do
|
||||||
|
session
|
||||||
|
|> find(css("[name='#{name}']"))
|
||||||
|
|> click(option(option))
|
||||||
|
|
||||||
session
|
session
|
||||||
|> visit("/games")
|
|
||||||
|> click(button("Create game"))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp square_selected(square) do
|
defp square_selected(square) do
|
||||||
|
|||||||
@ -3,7 +3,7 @@ defmodule Chess.Factory do
|
|||||||
alias Chess.Store.Game
|
alias Chess.Store.Game
|
||||||
alias Chess.Repo
|
alias Chess.Repo
|
||||||
|
|
||||||
def create_user(username \\ "zelda", password \\ "password") do
|
def create_user(username \\ "link", password \\ "ilovezelda") do
|
||||||
User.changeset(
|
User.changeset(
|
||||||
%User{},
|
%User{},
|
||||||
%{username: username, password: password}
|
%{username: username, password: password}
|
||||||
|
|||||||
@ -8,7 +8,6 @@ defmodule ChessWeb.FeatureCase do
|
|||||||
alias Chess.Repo
|
alias Chess.Repo
|
||||||
import Ecto
|
import Ecto
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
import Ecto.Query
|
|
||||||
|
|
||||||
import ChessWeb.Router.Helpers
|
import ChessWeb.Router.Helpers
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user