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
|
||||
|
||||
alias Chess.Store.Game
|
||||
alias Chess.Auth.User
|
||||
|
||||
def index(conn, _params) do
|
||||
changeset = Game.changeset(%Game{})
|
||||
@ -14,6 +15,18 @@ defmodule ChessWeb.GameController do
|
||||
render(conn, "index.html", games: games, changeset: changeset)
|
||||
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
|
||||
changeset = Game.create_changeset(
|
||||
%Game{},
|
||||
|
||||
@ -35,7 +35,7 @@ defmodule ChessWeb.Router do
|
||||
pipe_through [:browser, :auth, :ensure_auth]
|
||||
|
||||
resources "/games", GameController,
|
||||
only: [:index, :create, :show, :delete]
|
||||
only: [:index, :new, :create, :show, :delete]
|
||||
end
|
||||
|
||||
# Other scopes may use custom stacks.
|
||||
|
||||
@ -15,8 +15,6 @@
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<%= form_for @changeset, game_path(@conn, :create), [class: "create-game"], fn _f -> %>
|
||||
<div class="form-group">
|
||||
<%= submit "Create game", class: "btn btn-primary" %>
|
||||
</div>
|
||||
<% end %>
|
||||
<p>
|
||||
<%= link "New game", to: game_path(@conn, :new), class: "button" %>
|
||||
</p>
|
||||
|
||||
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
|
||||
use ChessWeb.FeatureCase
|
||||
|
||||
import Wallaby.Query, only: [
|
||||
css: 1,
|
||||
css: 2,
|
||||
button: 1,
|
||||
text_field: 1,
|
||||
link: 1,
|
||||
]
|
||||
import Wallaby.Query
|
||||
|
||||
import Chess.Factory, only: [create_user: 2, create_game_for: 1]
|
||||
|
||||
test "visit homepage", %{session: session} do
|
||||
@ -17,27 +12,42 @@ defmodule Chess.GamesTest do
|
||||
end
|
||||
|
||||
test "can create a new game", %{session: session} do
|
||||
create_user("zelda", "ganonsucks")
|
||||
|
||||
session
|
||||
|> login()
|
||||
|> create_game()
|
||||
|> visit("/games")
|
||||
|> click(link("New game"))
|
||||
|> select("game[opponent_id]", option: "zelda")
|
||||
|> click(button("Create game"))
|
||||
|> assert_has(css(".board"))
|
||||
end
|
||||
|
||||
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)
|
||||
|
||||
session
|
||||
|> login()
|
||||
|> create_game()
|
||||
|> visit("/games")
|
||||
|> click(link("New game"))
|
||||
|> select("game[opponent_id]", option: "urbosa")
|
||||
|> click(button("Create game"))
|
||||
|> click(link("Back to games"))
|
||||
|> assert_has(css(".table tr", count: 1))
|
||||
end
|
||||
|
||||
test "can move a piece", %{session: session} do
|
||||
create_user("zelda", "ganonsucks")
|
||||
|
||||
session
|
||||
|> login()
|
||||
|> create_game()
|
||||
|> visit("/games")
|
||||
|> click(link("New game"))
|
||||
|> select("game[opponent_id]", option: "zelda")
|
||||
|> click(button("Create game"))
|
||||
|
||||
session
|
||||
|> click(css("#f4-r1"))
|
||||
@ -60,10 +70,12 @@ defmodule Chess.GamesTest do
|
||||
|> click(button("Log in"))
|
||||
end
|
||||
|
||||
defp create_game(session) do
|
||||
def select(session, name, [option: option]) do
|
||||
session
|
||||
|> find(css("[name='#{name}']"))
|
||||
|> click(option(option))
|
||||
|
||||
session
|
||||
|> visit("/games")
|
||||
|> click(button("Create game"))
|
||||
end
|
||||
|
||||
defp square_selected(square) do
|
||||
|
||||
@ -3,7 +3,7 @@ defmodule Chess.Factory do
|
||||
alias Chess.Store.Game
|
||||
alias Chess.Repo
|
||||
|
||||
def create_user(username \\ "zelda", password \\ "password") do
|
||||
def create_user(username \\ "link", password \\ "ilovezelda") do
|
||||
User.changeset(
|
||||
%User{},
|
||||
%{username: username, password: password}
|
||||
|
||||
@ -8,7 +8,6 @@ defmodule ChessWeb.FeatureCase do
|
||||
alias Chess.Repo
|
||||
import Ecto
|
||||
import Ecto.Changeset
|
||||
import Ecto.Query
|
||||
|
||||
import ChessWeb.Router.Helpers
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user