1
0
mirror of https://github.com/danbee/chess synced 2025-03-04 08:39:06 +00:00
chess/web/models/game.ex
Dan Barber 7ff1b03f89
Refactor the board data structure
Now we're only storing the pieces keyed by the piece position rather
than storing the entire board as a set of nested Maps. This makes moving
pieces much easier. We're also now storing the position as a pair of
array indices which should make calculating the possible moves easier.
2017-01-18 16:36:38 +00:00

36 lines
646 B
Elixir

defmodule Chess.Game do
use Chess.Web, :model
schema "games" do
field :board, :map
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct) do
struct
|> cast(%{}, [:board])
|> set_default_board
|> validate_required([:board])
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:board])
|> validate_required([:board])
end
def ordered(query) do
query
|> order_by([game], desc: game.inserted_at)
end
def set_default_board(changeset) do
changeset
|> put_change(:board, Chess.Board.default)
end
end