feat: add simple (mean) guessing game
This commit is contained in:
parent
8702144f97
commit
673d0656fa
2 changed files with 43 additions and 0 deletions
42
pento/lib/pento_web/live/wrong_live.ex
Normal file
42
pento/lib/pento_web/live/wrong_live.ex
Normal file
|
@ -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
|
||||
|
||||
get "/", PageController, :home
|
||||
live "/guess", WrongLive
|
||||
end
|
||||
|
||||
# Other scopes may use custom stacks.
|
||||
|
|
Loading…
Reference in a new issue