mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Some refactoring
This commit is contained in:
parent
c2c6b81cb8
commit
dddebf06ad
@ -5,7 +5,7 @@ defmodule Chess.MoveList do
|
||||
|
||||
def transform(moves) do
|
||||
moves
|
||||
|> Enum.map(fn(move) -> Move.transform(move) end)
|
||||
|> Enum.map(&(Move.transform(&1)))
|
||||
|> Enum.chunk_every(2)
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,8 +1,11 @@
|
||||
defmodule Chess.Moves do
|
||||
@moduledoc false
|
||||
|
||||
alias Ecto.Multi
|
||||
|
||||
alias Chess.Repo
|
||||
alias Chess.Board
|
||||
alias Chess.Store.Move
|
||||
alias Chess.Store.Game
|
||||
|
||||
alias Chess.Moves.Pieces.Pawn
|
||||
alias Chess.Moves.Pieces.Bishop
|
||||
@ -11,6 +14,15 @@ defmodule Chess.Moves do
|
||||
alias Chess.Moves.Pieces.Queen
|
||||
alias Chess.Moves.Pieces.King
|
||||
|
||||
def make_move(game, move_params) do
|
||||
params = Board.move_piece(game.board, move_params)
|
||||
|
||||
Multi.new
|
||||
|> Multi.update(:game, Game.move_changeset(game, params))
|
||||
|> Multi.insert(:move, Ecto.build_assoc(game, :moves, params))
|
||||
|> Repo.transaction
|
||||
end
|
||||
|
||||
def available(board, {file, rank}) do
|
||||
piece =
|
||||
board
|
||||
|
||||
@ -5,7 +5,6 @@ defmodule Chess.Store.Move do
|
||||
use Timex.Ecto.Timestamps
|
||||
|
||||
import Ecto.Changeset
|
||||
# import Ecto.Query
|
||||
|
||||
alias Chess.Store.Game
|
||||
|
||||
|
||||
@ -3,8 +3,6 @@ defmodule ChessWeb.GameChannel do
|
||||
|
||||
use ChessWeb, :channel
|
||||
|
||||
alias Ecto.Multi
|
||||
|
||||
alias Chess.Store.Game
|
||||
alias Chess.Board
|
||||
alias Chess.Moves
|
||||
@ -43,18 +41,11 @@ defmodule ChessWeb.GameChannel do
|
||||
def handle_in("game:move", params, socket) do
|
||||
move_params = convert_params(params)
|
||||
|
||||
game =
|
||||
socket.assigns.current_user_id
|
||||
|> Game.for_user_id()
|
||||
|> preload(:moves)
|
||||
|> Repo.get!(socket.assigns.game_id)
|
||||
|
||||
params = Board.move_piece(game.board, move_params)
|
||||
|
||||
Multi.new
|
||||
|> Multi.update(:game, Game.move_changeset(game, params))
|
||||
|> Multi.insert(:move, Ecto.build_assoc(game, :moves, params))
|
||||
|> Repo.transaction
|
||||
socket.assigns.current_user_id
|
||||
|> Game.for_user_id()
|
||||
|> preload(:moves)
|
||||
|> Repo.get!(socket.assigns.game_id)
|
||||
|> Moves.make_move(move_params)
|
||||
|> case do
|
||||
{:ok, _} ->
|
||||
send_update(socket)
|
||||
@ -91,8 +82,8 @@ defmodule ChessWeb.GameChannel do
|
||||
|
||||
def convert_params(%{"from" => from, "to" => to}) do
|
||||
%{
|
||||
"from" => Enum.map(from, fn(s) -> String.to_integer(s) end),
|
||||
"to" => Enum.map(to, fn(s) -> String.to_integer(s) end),
|
||||
"from" => Enum.map(from, &(String.to_integer(&1))),
|
||||
"to" => Enum.map(to, &(String.to_integer(&1))),
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user