diff --git a/mix.lock b/mix.lock index 401c51c..982fe42 100644 --- a/mix.lock +++ b/mix.lock @@ -1,33 +1,33 @@ %{"bunt": {:hex, :bunt, "0.1.6", "5d95a6882f73f3b9969fdfd1953798046664e6f77ec4e486e6fafc7caad97c6f", [:mix], []}, "certifi": {:hex, :certifi, "0.7.0", "861a57f3808f7eb0c2d1802afeaae0fa5de813b0df0979153cbafcd853ababaf", [:rebar3], []}, - "combine": {:hex, :combine, "0.9.3", "192e609b48b3f2210494e26f85db1712657be1a8f15795656710317ea43fc449", [:mix], []}, - "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], []}, - "cowboy": {:hex, :cowboy, "1.0.4", "a324a8df9f2316c833a470d918aaf73ae894278b8aa6226ce7a9bf699388f878", [:rebar, :make], [{:cowlib, "~> 1.0.0", [hex: :cowlib, optional: false]}, {:ranch, "~> 1.0", [hex: :ranch, optional: false]}]}, - "cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], []}, + "combine": {:hex, :combine, "0.9.6", "8d1034a127d4cbf6924c8a5010d3534d958085575fa4d9b878f200d79ac78335", [:mix], []}, + "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [], []}, + "cowboy": {:hex, :cowboy, "1.0.4", "a324a8df9f2316c833a470d918aaf73ae894278b8aa6226ce7a9bf699388f878", [], [{:cowlib, "~> 1.0.0", [hex: :cowlib, optional: false]}, {:ranch, "~> 1.0", [hex: :ranch, optional: false]}]}, + "cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [], []}, "credo": {:hex, :credo, "0.5.3", "0c405b36e7651245a8ed63c09e2d52c2e2b89b6d02b1570c4d611e0fcbecf4a2", [:mix], [{:bunt, "~> 0.1.6", [hex: :bunt, optional: false]}]}, - "db_connection": {:hex, :db_connection, "1.1.0", "b2b88db6d7d12f99997b584d09fad98e560b817a20dab6a526830e339f54cdb3", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, optional: true]}]}, - "decimal": {:hex, :decimal, "1.3.1", "157b3cedb2bfcb5359372a7766dd7a41091ad34578296e951f58a946fcab49c6", [:mix], []}, - "ecto": {:hex, :ecto, "2.0.6", "9dcbf819c2a77f67a66b83739b7fcc00b71aaf6c100016db4f798930fa4cfd47", [:mix], [{:db_connection, "~> 1.0", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.1.2 or ~> 1.2", [hex: :decimal, optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, optional: true]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}, {:postgrex, "~> 0.12.0", [hex: :postgrex, optional: true]}, {:sbroker, "~> 1.0-beta", [hex: :sbroker, optional: true]}]}, - "fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], []}, - "gettext": {:hex, :gettext, "0.12.2", "4f71a0df7b0bf5693419da593b4ddde558cc28e8f56a991fbfc2b51be1d5c848", [:mix], []}, - "hackney": {:hex, :hackney, "1.6.3", "d489d7ca2d4323e307bedc4bfe684323a7bf773ecfd77938f3ee8074e488e140", [:mix, :rebar3], [{:certifi, "0.7.0", [hex: :certifi, optional: false]}, {:idna, "1.2.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]}, + "db_connection": {:hex, :db_connection, "1.1.0", "b2b88db6d7d12f99997b584d09fad98e560b817a20dab6a526830e339f54cdb3", [], [{:connection, "~> 1.0.2", [hex: :connection, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, optional: true]}]}, + "decimal": {:hex, :decimal, "1.3.1", "157b3cedb2bfcb5359372a7766dd7a41091ad34578296e951f58a946fcab49c6", [], []}, + "ecto": {:hex, :ecto, "2.1.3", "ffb24e150b519a4c0e4c84f9eabc8587199389bc499195d5d1a93cd3b2d9a045", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, optional: true]}]}, + "fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [], []}, + "gettext": {:hex, :gettext, "0.13.0", "daafbddc5cda12738bb93b01d84105fe75b916a302f1c50ab9fb066b95ec9db4", [:mix], []}, + "hackney": {:hex, :hackney, "1.6.5", "8c025ee397ac94a184b0743c73b33b96465e85f90a02e210e86df6cbafaa5065", [:rebar3], [{:certifi, "0.7.0", [hex: :certifi, optional: false]}, {:idna, "1.2.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]}, "hound": {:hex, :hound, "1.0.2", "b6dd20142d00c28009fad503a23fa4a76bc11899e0d198f36a9c1448427788b2", [:mix], [{:httpoison, "~> 0.8", [hex: :httpoison, optional: false]}, {:poison, ">= 1.4.0", [hex: :poison, optional: false]}]}, - "httpoison": {:hex, :httpoison, "0.10.0", "4727b3a5e57e9a4ff168a3c2883e20f1208103a41bccc4754f15a9366f49b676", [:mix], [{:hackney, "~> 1.6.3", [hex: :hackney, optional: false]}]}, + "httpoison": {:hex, :httpoison, "0.11.0", "b9240a9c44fc46fcd8618d17898859ba09a3c1b47210b74316c0ffef10735e76", [:mix], [{:hackney, "~> 1.6.3", [hex: :hackney, optional: false]}]}, "idna": {:hex, :idna, "1.2.0", "ac62ee99da068f43c50dc69acf700e03a62a348360126260e87f2b54eced86b2", [:rebar3], []}, "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []}, - "mime": {:hex, :mime, "1.0.1", "05c393850524767d13a53627df71beeebb016205eb43bfbd92d14d24ec7a1b51", [:mix], []}, + "mime": {:hex, :mime, "1.0.1", "05c393850524767d13a53627df71beeebb016205eb43bfbd92d14d24ec7a1b51", [], []}, "mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []}, - "phoenix": {:hex, :phoenix, "1.2.1", "6dc592249ab73c67575769765b66ad164ad25d83defa3492dc6ae269bd2a68ab", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, optional: false]}, {:plug, "~> 1.1", [hex: :plug, optional: false]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}]}, - "phoenix_ecto": {:hex, :phoenix_ecto, "3.0.1", "42eb486ef732cf209d0a353e791806721f33ff40beab0a86f02070a5649ed00a", [:mix], [{:ecto, "~> 2.0", [hex: :ecto, optional: false]}, {:phoenix_html, "~> 2.6", [hex: :phoenix_html, optional: true]}, {:plug, "~> 1.0", [hex: :plug, optional: false]}]}, - "phoenix_html": {:hex, :phoenix_html, "2.8.0", "777598a4b6609ad6ab8b180f7b25c9af2904644e488922bb9b9b03ce988d20b1", [:mix], [{:plug, "~> 1.0", [hex: :plug, optional: false]}]}, - "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.0.6", "4490d588c4f60248b1c5f1f0dc0a7271e1aed4bddbd8b1542630f7bf6bc7b012", [:mix], [{:fs, "~> 0.9.1", [hex: :fs, optional: false]}, {:phoenix, "~> 1.0 or ~> 1.2-rc", [hex: :phoenix, optional: false]}]}, - "phoenix_pubsub": {:hex, :phoenix_pubsub, "1.0.1", "c10ddf6237007c804bf2b8f3c4d5b99009b42eca3a0dfac04ea2d8001186056a", [:mix], []}, - "plug": {:hex, :plug, "1.3.0", "6e2b01afc5db3fd011ca4a16efd9cb424528c157c30a44a0186bcc92c7b2e8f3", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1", [hex: :cowboy, optional: true]}, {:mime, "~> 1.0", [hex: :mime, optional: false]}]}, - "poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [:mix], []}, - "poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []}, - "postgrex": {:hex, :postgrex, "0.12.1", "2f8b46cb3a44dcd42f42938abedbfffe7e103ba4ce810ccbeee8dcf27ca0fb06", [:mix], [{:connection, "~> 1.0", [hex: :connection, optional: false]}, {:db_connection, "~> 1.0-rc.4", [hex: :db_connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]}, - "ranch": {:hex, :ranch, "1.2.1", "a6fb992c10f2187b46ffd17ce398ddf8a54f691b81768f9ef5f461ea7e28c762", [:make], []}, + "phoenix": {:hex, :phoenix, "1.2.1", "6dc592249ab73c67575769765b66ad164ad25d83defa3492dc6ae269bd2a68ab", [], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, optional: false]}, {:plug, "~> 1.1", [hex: :plug, optional: false]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}]}, + "phoenix_ecto": {:hex, :phoenix_ecto, "3.2.1", "6cf11d590c61977f50fcb98ad8a10ee90ba8670a82cbf5eaf49cfaee2e95e8b7", [:mix], [{:ecto, "~> 2.1", [hex: :ecto, optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, optional: false]}, {:plug, "~> 1.0", [hex: :plug, optional: false]}]}, + "phoenix_html": {:hex, :phoenix_html, "2.9.2", "371160b30cf4e10443b015efce6f03e1f19aae98ff6487620477b13a5b2ef660", [:mix], [{:plug, "~> 1.0", [hex: :plug, optional: false]}]}, + "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.0.6", "4490d588c4f60248b1c5f1f0dc0a7271e1aed4bddbd8b1542630f7bf6bc7b012", [], [{:fs, "~> 0.9.1", [hex: :fs, optional: false]}, {:phoenix, "~> 1.0 or ~> 1.2-rc", [hex: :phoenix, optional: false]}]}, + "phoenix_pubsub": {:hex, :phoenix_pubsub, "1.0.1", "c10ddf6237007c804bf2b8f3c4d5b99009b42eca3a0dfac04ea2d8001186056a", [], []}, + "plug": {:hex, :plug, "1.3.0", "6e2b01afc5db3fd011ca4a16efd9cb424528c157c30a44a0186bcc92c7b2e8f3", [], [{:cowboy, "~> 1.0.1 or ~> 1.1", [hex: :cowboy, optional: true]}, {:mime, "~> 1.0", [hex: :mime, optional: false]}]}, + "poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [], []}, + "poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [], []}, + "postgrex": {:hex, :postgrex, "0.13.0", "e101ab47d0725955c5c8830ae8812412992e02e4bd9db09e17abb0a5d82d09c7", [:mix], [{:connection, "~> 1.0", [hex: :connection, optional: false]}, {:db_connection, "~> 1.1", [hex: :db_connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]}, + "ranch": {:hex, :ranch, "1.2.1", "a6fb992c10f2187b46ffd17ce398ddf8a54f691b81768f9ef5f461ea7e28c762", [], []}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []}, - "timex": {:hex, :timex, "3.1.5", "413d6d8d6f0162a5d47080cb8ca520d790184ac43e097c95191c7563bf25b428", [], [{:combine, "~> 0.7", [hex: :combine, optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5", [hex: :tzdata, optional: false]}]}, - "timex_ecto": {:hex, :timex_ecto, "3.0.5", "3ec6c25e10d2c0020958e5df64d2b5e690e441faa2c2259da8bc6bd3d7f39256", [:mix], [{:ecto, "~> 2.0", [hex: :ecto, optional: false]}, {:timex, "~> 3.0", [hex: :timex, optional: false]}]}, - "tzdata": {:hex, :tzdata, "0.5.9", "575be217b039057a47e133b72838cbe104fb5329b19906ea4e66857001c37edb", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, optional: false]}]}} + "timex": {:hex, :timex, "3.1.7", "71f9c32e13ff4860e86a314303757cc02b3ead5db6e977579a2935225ce9a666", [:mix], [{:combine, "~> 0.7", [hex: :combine, optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5", [hex: :tzdata, optional: false]}]}, + "timex_ecto": {:hex, :timex_ecto, "3.1.1", "37d54f6879d96a6789bb497296531cfb853631de78e152969d95cff03c1368dd", [:mix], [{:ecto, "~> 2.1.0", [hex: :ecto, optional: false]}, {:timex, "~> 3.0", [hex: :timex, optional: false]}]}, + "tzdata": {:hex, :tzdata, "0.5.10", "087e8dfe8c0283473115ad8ca6974b898ecb55ca5c725427a142a79593391e90", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, optional: false]}]}} diff --git a/test/features/games_test.exs b/test/features/games_test.exs index baa0cdb..3c61d47 100644 --- a/test/features/games_test.exs +++ b/test/features/games_test.exs @@ -19,15 +19,15 @@ defmodule Chess.GamesTest do navigate_to "/" create_game - click({:css, "#e2"}) + click({:css, "#f4-r1"}) - assert has_class?({:css, "#e2"}, "selected") - assert square_has_piece("e2", "white", "pawn") + assert has_class?({:css, "#f4-r1"}, "selected") + assert square_has_piece("f4-r1", "white", "pawn") - click({:css, "#e4"}) + click({:css, "#f4-r3"}) - assert !square_has_piece("e2", "white", "pawn") - assert square_has_piece("e4", "white", "pawn") + assert !square_has_piece("f4-r1", "white", "pawn") + assert square_has_piece("f4-r3", "white", "pawn") end defp create_game do diff --git a/web/controllers/api/game_controller.ex b/web/controllers/api/game_controller.ex index 306f765..be6d593 100644 --- a/web/controllers/api/game_controller.ex +++ b/web/controllers/api/game_controller.ex @@ -1,5 +1,6 @@ defmodule Chess.Api.GameController do use Chess.Web, :controller + require Logger alias Chess.Game @@ -10,7 +11,7 @@ defmodule Chess.Api.GameController do def update(conn, %{"id" => id, "move" => move_params}) do game = Repo.get!(Game, id) - changeset = Game.changeset(game, %{board: new_board(game, move_params)}) + changeset = Game.changeset(game, %{board: new_board(game.board, move_params)}) case Repo.update(changeset) do {:ok, game} -> @@ -18,25 +19,12 @@ defmodule Chess.Api.GameController do end end - defp new_board(game, move_params) do - game.board - |> put_in(move(move_from(move_params)), nil) - |> put_in(move(move_to(move_params)), piece(game, move_params)) - end + defp new_board(board, move_params) do + [from_file, from_rank] = move_params["from"] + [to_file, to_rank] = move_params["to"] - defp move_from(move) do - move["from"] - end + {piece, board} = Map.pop(board, "#{from_file},#{from_rank}") - defp move(square) do - [square["rank"], square["file"]] - end - - defp piece(game, move) do - get_in(game.board, move(move_from(move))) - end - - defp move_to(move) do - move["to"] + Map.put(board, "#{to_file},#{to_rank}", piece) end end diff --git a/web/models/board.ex b/web/models/board.ex new file mode 100644 index 0000000..341a453 --- /dev/null +++ b/web/models/board.ex @@ -0,0 +1,49 @@ +defmodule Chess.Board do + def output(board) do + Enum.map(0..7, fn (rank) -> + Enum.map(0..7, fn (file) -> + board["#{file},#{rank}"] + end) + end) + end + + def default do + %{ + "0,7" => %{type: :rook, colour: :black}, + "1,7" => %{type: :knight, colour: :black}, + "2,7" => %{type: :bishop, colour: :black}, + "3,7" => %{type: :queen, colour: :black}, + "4,7" => %{type: :king, colour: :black}, + "5,7" => %{type: :bishop, colour: :black}, + "6,7" => %{type: :knight, colour: :black}, + "7,7" => %{type: :rook, colour: :black}, + + "0,6" => %{type: :pawn, colour: :black}, + "1,6" => %{type: :pawn, colour: :black}, + "2,6" => %{type: :pawn, colour: :black}, + "3,6" => %{type: :pawn, colour: :black}, + "4,6" => %{type: :pawn, colour: :black}, + "5,6" => %{type: :pawn, colour: :black}, + "6,6" => %{type: :pawn, colour: :black}, + "7,6" => %{type: :pawn, colour: :black}, + + "0,1" => %{type: :pawn, colour: :white}, + "1,1" => %{type: :pawn, colour: :white}, + "2,1" => %{type: :pawn, colour: :white}, + "3,1" => %{type: :pawn, colour: :white}, + "4,1" => %{type: :pawn, colour: :white}, + "5,1" => %{type: :pawn, colour: :white}, + "6,1" => %{type: :pawn, colour: :white}, + "7,1" => %{type: :pawn, colour: :white}, + + "0,0" => %{type: :rook, colour: :white}, + "1,0" => %{type: :knight, colour: :white}, + "2,0" => %{type: :bishop, colour: :white}, + "3,0" => %{type: :queen, colour: :white}, + "4,0" => %{type: :king, colour: :white}, + "5,0" => %{type: :bishop, colour: :white}, + "6,0" => %{type: :knight, colour: :white}, + "7,0" => %{type: :rook, colour: :white} + } + end +end diff --git a/web/models/game.ex b/web/models/game.ex index 26031ae..7f2b659 100644 --- a/web/models/game.ex +++ b/web/models/game.ex @@ -4,7 +4,7 @@ defmodule Chess.Game do schema "games" do field :board, :map - timestamps + timestamps() end @doc """ @@ -30,55 +30,6 @@ defmodule Chess.Game do def set_default_board(changeset) do changeset - |> put_change(:board, default_board) - end - - def default_board do - %{ - "8" => %{ - a: %{type: "rook", colour: "black"}, - b: %{type: "knight", colour: "black"}, - c: %{type: "bishop", colour: "black"}, - d: %{type: "queen", colour: "black"}, - e: %{type: "king", colour: "black"}, - f: %{type: "bishop", colour: "black"}, - g: %{type: "knight", colour: "black"}, - h: %{type: "rook", colour: "black"} - }, - "7" => %{ - a: %{type: "pawn", colour: "black"}, - b: %{type: "pawn", colour: "black"}, - c: %{type: "pawn", colour: "black"}, - d: %{type: "pawn", colour: "black"}, - e: %{type: "pawn", colour: "black"}, - f: %{type: "pawn", colour: "black"}, - g: %{type: "pawn", colour: "black"}, - h: %{type: "pawn", colour: "black"} - }, - "6" => %{a: nil, b: nil, c: nil, d: nil, e: nil, f: nil, g: nil, h: nil}, - "5" => %{a: nil, b: nil, c: nil, d: nil, e: nil, f: nil, g: nil, h: nil}, - "4" => %{a: nil, b: nil, c: nil, d: nil, e: nil, f: nil, g: nil, h: nil}, - "3" => %{a: nil, b: nil, c: nil, d: nil, e: nil, f: nil, g: nil, h: nil}, - "2" => %{ - a: %{type: "pawn", colour: "white"}, - b: %{type: "pawn", colour: "white"}, - c: %{type: "pawn", colour: "white"}, - d: %{type: "pawn", colour: "white"}, - e: %{type: "pawn", colour: "white"}, - f: %{type: "pawn", colour: "white"}, - g: %{type: "pawn", colour: "white"}, - h: %{type: "pawn", colour: "white"} - }, - "1" => %{ - a: %{type: "rook", colour: "white"}, - b: %{type: "knight", colour: "white"}, - c: %{type: "bishop", colour: "white"}, - d: %{type: "queen", colour: "white"}, - e: %{type: "king", colour: "white"}, - f: %{type: "bishop", colour: "white"}, - g: %{type: "knight", colour: "white"}, - h: %{type: "rook", colour: "white"} - } - } + |> put_change(:board, Chess.Board.default) end end diff --git a/web/static/js/components/chess-board-square.js b/web/static/js/components/chess-board-square.js index 530bcd3..0c6b3f0 100644 --- a/web/static/js/components/chess-board-square.js +++ b/web/static/js/components/chess-board-square.js @@ -1,4 +1,5 @@ import React from "react"; +import _ from "lodash"; import $ from "jquery"; import classNames from "classnames"; @@ -7,12 +8,10 @@ import { selectPiece, setBoard } from "../store/actions"; class ChessBoardSquare extends React.Component { constructor(props) { super(props); - - this.selectSquare = this.selectSquare.bind(this); } squareCoords() { - return { rank: this.props.rank, file: this.props.file }; + return [this.props.file, this.props.rank]; } selectSquare() { @@ -38,13 +37,12 @@ class ChessBoardSquare extends React.Component { return false; } else { - return this.squareCoords().rank == store.getState().selectedSquare.rank - && this.squareCoords().file == store.getState().selectedSquare.file; + return _.isEqual(this.squareCoords(), store.getState().selectedSquare); } } squareId() { - return this.props.file + this.props.rank; + return `f${this.props.file}-r${this.props.rank}`; } squareClass() { @@ -65,7 +63,7 @@ class ChessBoardSquare extends React.Component { return
} } diff --git a/web/static/js/components/chess-board.js b/web/static/js/components/chess-board.js index 50a4220..0c285a3 100644 --- a/web/static/js/components/chess-board.js +++ b/web/static/js/components/chess-board.js @@ -12,7 +12,7 @@ class ChessBoard extends React.Component { store.dispatch(setGameId(gameId)); - $.ajax({ method: "GET", url: "/api/games/" + gameId }) + $.ajax({ method: "GET", url: `/api/games/${gameId}` }) .then((data) => store.dispatch(setBoard(data))); } diff --git a/web/views/api/game_view.ex b/web/views/api/game_view.ex index 6a25272..dfa27a7 100644 --- a/web/views/api/game_view.ex +++ b/web/views/api/game_view.ex @@ -1,7 +1,9 @@ defmodule Chess.Api.GameView do use Chess.Web, :view + alias Chess.Board + def render("show.json", %{game: game}) do - game.board + Board.output(game.board) end end