feat: add simple (mean) guessing game
parent
8702144f97
commit
673d0656fa
|
@ -0,0 +1,42 @@
|
||||||
|
defmodule PentoWeb.WrongLive do
|
||||||
|
use PentoWeb, :live_view
|
||||||
|
|
||||||
|
def mount(_params, _session, socket) do
|
||||||
|
{:ok, assign(socket, score: 0, message: "Make a guess:", time: time())}
|
||||||
|
end
|
||||||
|
|
||||||
|
def render(assigns) do
|
||||||
|
~H"""
|
||||||
|
<h1 class="mb-4 text-4xl font-extrabold">Your score: <%= @score %></h1>
|
||||||
|
<h2>
|
||||||
|
<%= @message %>
|
||||||
|
It's <%= @time %>
|
||||||
|
</h2>
|
||||||
|
<br />
|
||||||
|
<h2>
|
||||||
|
<%= for n <- 1..10 do %>
|
||||||
|
<.link
|
||||||
|
class="bg-blue-500 hover:bg-blue-700
|
||||||
|
text-white font-bold py-2 px-4 border border-blue-700 rounded m-1"
|
||||||
|
phx-click="guess"
|
||||||
|
phx-value-number={n}
|
||||||
|
>
|
||||||
|
<%= n %>
|
||||||
|
</.link>
|
||||||
|
<% end %>
|
||||||
|
</h2>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
defp time() do
|
||||||
|
DateTime.utc_now |> to_string
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("guess", %{"number" => guess}, socket) do
|
||||||
|
message = "Your guess: #{guess}. Wrong. Guess again."
|
||||||
|
|
||||||
|
score = socket.assigns.score - 1
|
||||||
|
|
||||||
|
{:noreply, assign(socket, message: message, score: score, time: time())}
|
||||||
|
end
|
||||||
|
end
|
|
@ -18,6 +18,7 @@ defmodule PentoWeb.Router do
|
||||||
pipe_through :browser
|
pipe_through :browser
|
||||||
|
|
||||||
get "/", PageController, :home
|
get "/", PageController, :home
|
||||||
|
live "/guess", WrongLive
|
||||||
end
|
end
|
||||||
|
|
||||||
# Other scopes may use custom stacks.
|
# Other scopes may use custom stacks.
|
||||||
|
|
Loading…
Reference in New Issue