Create players

This commit is contained in:
Ryan Pandya 2020-12-28 22:33:14 -08:00
parent 6266b089a5
commit bddf7ff040
13 changed files with 114 additions and 7 deletions

View File

@ -3,14 +3,20 @@ defmodule Bwc do
The core module for the Blank White Cards game.
"""
alias Bwc.{Repo, Card}
alias Bwc.{Repo, Card, Player}
@repo Repo
def get_title do
def new_player, do: Player.changeset(%Player{})
end
def create_player(attrs), do: Player.changeset(%Player{},attrs) |> @repo.insert()
def delete_player(id), do: get_player(id) |> @repo.delete
def list_players, do: @repo.all(Player)
def get_player(id), do: @repo.get!(Player, id)
def list_cards, do: @repo.all(Card)

View File

@ -0,0 +1,18 @@
defmodule Bwc.Player do
use Ecto.Schema
import Ecto.Changeset
schema "players" do
field :username, :string
field :picture, :string
end
def changeset(player, attrs \\ %{}) do
player
|> cast(attrs, [:username, :picture])
|> validate_required(:username)
|> unique_constraint(:username)
|> validate_length(:username, min: 3)
end
end

View File

@ -0,0 +1,10 @@
defmodule Bwc.Repo.Migrations.CreatePlayers do
use Ecto.Migration
def change do
create table "players" do
add :username, :string
add :picture, :string
end
end
end

View File

@ -0,0 +1,7 @@
defmodule Bwc.Repo.Migrations.AddUniqueUsernames do
use Ecto.Migration
def change do
create unique_index(:players, [:username])
end
end

View File

@ -1,11 +1,18 @@
html{
font-family:ProximaNova,-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif
}
.bwc-logo{
width:550px;
}
#top-bar{
border-bottom: 2px solid gainsboro;
padding-bottom:1em;
}
.uk-subnav-pill > * > :first-child{
padding: 0px 50px !important;
}
li.message{
border-bottom:1px solid #eee;
list-style:none;

File diff suppressed because one or more lines are too long

View File

@ -3,7 +3,8 @@ defmodule BwcWeb.PageController do
def index(conn, _params) do
cards = Bwc.list_cards()
players = Bwc.list_players()
status = "The game has not yet started."
render(conn, "game.html", cards: cards, status: status)
render(conn, "game.html", cards: cards, status: status, players: players)
end
end

View File

@ -0,0 +1,18 @@
defmodule BwcWeb.SessionController do
use BwcWeb, :controller
def new(conn, _params) do
player = Bwc.new_player()
render conn, "new.html", player: player
end
def create(conn, params) do
require Logger
Logger.debug(params)
cards = Bwc.list_cards()
case Bwc.create_player(Map.get(params, "player")) do
{:ok, username} -> conn |> put_session(:username, username) |> redirect(to: Routes.card_path(conn, :index, cards))
{:error, player} -> render(conn, "new.html", player: player)
end
end
end

View File

@ -17,6 +17,11 @@ defmodule BwcWeb.Router do
pipe_through :browser
get "/", PageController, :index
get "/join", SessionController, :new
post "/join", SessionController, :create
get "/leave", SessionController, :delete
get "/create", CardController, :new
resources "/cards", CardController, only: [:show, :new, :create, :index]

View File

@ -12,12 +12,17 @@
<body>
<div class="uk-container uk-container-center uk-margin-top uk-margin-large-bottom uk-height-1-1">
<div class="uk-text-center uk-container" id="top-bar">
<div class="uk-text-center uk-container uk-flex uk-flex-around" id="top-bar">
<h1 class="uk-heading-line-">
<a href="/">
<img class="bwc-logo" src="<%= Routes.static_path(@conn, "/images/bwc.png") %>" alt="BWC Logo" />
</a>
</h1>
<div class='auth uk-flex uk-flex-column uk-flex-center'>
<span class='uk-icon' uk-icon="icon:user"></span>
<small><b>You are:&nbsp;</b>
Nobody</small>
</div>
</div>
<p class="alert alert-info" role="alert"><%= get_flash(@conn, :info) %></p>

View File

@ -4,10 +4,18 @@
</div>
<div>
<h2><%= @status %></h2>
<div>
<i>Cards in the pot: <%= length(@cards) %></i>
<emph>
<%= link("(View all)", to: Routes.card_path(@conn, :index)) %>
</emph>
</div>
<div>
<i>Players: <%= @players |> length %></i>
<emph>
<%= link("(Join)", to: Routes.session_path(@conn, :new)) %>
</emph>
</div>
<hr />
<div class="create">
What are you waiting for?

View File

@ -0,0 +1,19 @@
<%= form_for @player, Routes.session_path(@conn, :create), fn(f) -> %>
<div class="new-session uk-panel uk-panel-box uk-panel-box-secondary uk-padding uk-flex uk-flex-column">
<div class="uk-flex-item">
<%= text_input f, :username, "placeholder": "Join the game as..." %>
<%= error_tag f, :username %>
</div>
<div class="controls uk-margin-top">
<div class="uk-flex uk-flex-center">
<ul class="uk-subnav uk-subnav-pill" style="margin-bottom:0px;">
<li>
<%= submit "Join", class: "uk-active uk-subnav-pill", id: "joinGame" %>
</li>
</ul>
</div>
</div>
</div>
<% end %>

View File

@ -0,0 +1,3 @@
defmodule BwcWeb.SessionView do
use BwcWeb, :view
end