mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Update with fucking moves
This commit is contained in:
parent
33796e88c4
commit
8b6811d13a
@ -8,9 +8,9 @@ defmodule Chess.Api.GameController do
|
||||
render conn, "show.json", game: game
|
||||
end
|
||||
|
||||
def update(conn, %{"id" => id, "game" => game_params}) do
|
||||
def update(conn, %{"id" => id, "move" => move_params}) do
|
||||
game = Repo.get!(Game, id)
|
||||
changeset = Game.changeset(game, game_params)
|
||||
changeset = Game.changeset(game, %{ board: new_board(game, move_params) })
|
||||
|
||||
case Repo.update(changeset) do
|
||||
{:ok, game} ->
|
||||
@ -19,4 +19,26 @@ defmodule Chess.Api.GameController do
|
||||
render(conn, "edit.html", game: game, changeset: changeset)
|
||||
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 move_from(move) do
|
||||
move["from"]
|
||||
end
|
||||
|
||||
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"]
|
||||
end
|
||||
end
|
||||
|
||||
@ -16,13 +16,19 @@ class ChessBoardSquare extends React.Component {
|
||||
}
|
||||
|
||||
selectSquare() {
|
||||
var { store } = this.props;
|
||||
var { piece, store } = this.props;
|
||||
var { gameId, selectedSquare } = store.getState();
|
||||
|
||||
if (store.getState().selectedSquare != null) {
|
||||
store.dispatch(movePiece(store.getState().selectedSquare, this.squareCoords()));
|
||||
$.ajax({ method: "PATCH", url: "/api/games/" + store.getState().gameId, data: { game: { board: store.getState().board } }});
|
||||
if (selectedSquare != null) {
|
||||
store.dispatch(movePiece(selectedSquare, this.squareCoords()));
|
||||
|
||||
$.ajax({
|
||||
method: "PATCH",
|
||||
url: "/api/games/" + gameId,
|
||||
data: { move: { from: selectedSquare, to: this.squareCoords() } }
|
||||
});
|
||||
}
|
||||
else if (this.props.piece != undefined) {
|
||||
else if (piece != undefined) {
|
||||
store.dispatch(selectPiece(this.squareCoords()));
|
||||
}
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user