-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ebd2ce
commit e89f353
Showing
13 changed files
with
177 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
defmodule TwentyFourtyEight.Game.Game do | ||
Check warning on line 1 in lib/twenty_fourty_eight/game/game.ex GitHub Actions / test
|
||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
alias TwentyFourtyEight.Repo | ||
|
||
@slug_length 8 | ||
@slug_alphabet String.graphemes( | ||
"_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
) | ||
|
||
schema "game" do | ||
field :num_rows, :integer, default: 6 | ||
field :num_cols, :integer, default: 6 | ||
field :starting_number, :integer, default: 2 | ||
field :turn_start_number, :integer, default: 1 | ||
field :winning_number, :integer, default: 2048 | ||
field :slug, :string | ||
timestamps() | ||
end | ||
|
||
def changeset(attrs) do | ||
%__MODULE__{} | ||
|> cast(attrs, [:num_rows, :num_cols, :starting_number, :turn_start_number, :winning_number]) | ||
|> validate_inclusion(:num_rows, 1..6) | ||
|> validate_inclusion(:num_cols, 1..6) | ||
|> validate_inclusion(:starting_number, [1, 2, 4]) | ||
|> validate_inclusion(:turn_start_number, [1, 2, 4]) | ||
|> validate_inclusion(:winning_number, [1024, 2048]) | ||
end | ||
|
||
def insert(changeset) do | ||
changeset | ||
|> cast(%{slug: generate_slug()}, [:slug]) | ||
|> unique_constraint(:slug) | ||
|> Repo.insert() | ||
end | ||
|
||
def get_by_slug(slug) do | ||
Repo.get_by(__MODULE__, slug: slug) | ||
end | ||
|
||
defp generate_slug() do | ||
1..@slug_length | ||
|> Enum.map_join("", fn _ -> Enum.random(@slug_alphabet) end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 11 additions & 3 deletions
14
lib/twenty_fourty_eight_web/controllers/game_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
defmodule TwentyFourtyEightWeb.GameController do | ||
use TwentyFourtyEightWeb, :controller | ||
|
||
alias TwentyFourtyEight.Game.Game | ||
|
||
def new(conn, _params) do | ||
render(conn, :new) | ||
changeset = Game.changeset(%{}) | ||
render(conn, :new, changeset: changeset) | ||
end | ||
|
||
def create(conn, _params) do | ||
redirect(conn, to: ~p"/#{id}") | ||
def create(conn, params) do | ||
changeset = Game.changeset(params["game"]) | ||
|
||
case Game.insert(changeset) do | ||
{:ok, game} -> redirect(conn, to: ~p"/#{game.slug}") | ||
{:error, changeset} -> render(conn, :new, changeset: changeset) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,30 @@ | ||
defmodule TwentyFourtyEightWeb.GameHTML do | ||
use TwentyFourtyEightWeb, :html | ||
|
||
embed_templates "game_html/*" | ||
def new(assigns) do | ||
~H""" | ||
<div class="cozy"> | ||
<.simple_form :let={f} for={@changeset} action={~p"/"}> | ||
<.input field={f[:num_rows]} type="select" options={1..6} label="Number of rows" /> | ||
<.input field={f[:num_cols]} type="select" options={1..6} label="Number of columns" /> | ||
<.input field={f[:starting_number]} type="select" options={[1, 2, 4]} label="First number" /> | ||
<.input | ||
field={f[:turn_start_number]} | ||
type="select" | ||
options={[1, 2, 4]} | ||
label="Number added at each turn" | ||
/> | ||
<.input | ||
field={f[:winning_number]} | ||
type="select" | ||
options={[1024, 2048]} | ||
label="Number required to win" | ||
/> | ||
<:actions> | ||
<.button>Start game</.button> | ||
</:actions> | ||
</.simple_form> | ||
</div> | ||
""" | ||
end | ||
end |
1 change: 0 additions & 1 deletion
1
lib/twenty_fourty_eight_web/controllers/game_html/new.html.heex
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule TwentyFourtyEight.Repo.Migrations.CreateGameTable do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:game) do | ||
add :num_rows, :integer, default: 6, null: false | ||
add :num_cols, :integer, default: 6, null: false | ||
add :starting_number, :integer, default: 2, null: false | ||
add :turn_start_number, :integer, default: 1, null: false | ||
add :winning_number, :integer, default: 2048, null: false | ||
add :score, :integer, default: 0, null: false | ||
add :turns, :integer, default: 0, null: false | ||
add :state, :string, default: "new", null: false | ||
add :slug, :string, null: false | ||
add :board, :json | ||
|
||
timestamps() | ||
end | ||
|
||
create constraint(:game, "started_game_must_have_board", | ||
check: "state = 'new' OR board IS NOT NULL", | ||
comment: "Board must be non-null out of the new state." | ||
) | ||
|
||
create unique_index(:game, [:slug]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters