feat: make the first game winable
parent
673d0656fa
commit
f46feded1c
|
@ -1,8 +1,16 @@
|
||||||
defmodule PentoWeb.WrongLive do
|
defmodule PentoWeb.WrongLive do
|
||||||
use PentoWeb, :live_view
|
use PentoWeb, :live_view
|
||||||
|
|
||||||
|
@winning_score 15
|
||||||
|
|
||||||
def mount(_params, _session, socket) do
|
def mount(_params, _session, socket) do
|
||||||
{:ok, assign(socket, score: 0, message: "Make a guess:", time: time())}
|
socket =
|
||||||
|
socket
|
||||||
|
|> assign(score: 0)
|
||||||
|
|> assign(message: "Make a guess:")
|
||||||
|
|> assign(number: random_number())
|
||||||
|
|
||||||
|
{:ok, socket}
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(assigns) do
|
def render(assigns) do
|
||||||
|
@ -10,33 +18,71 @@ defmodule PentoWeb.WrongLive do
|
||||||
<h1 class="mb-4 text-4xl font-extrabold">Your score: <%= @score %></h1>
|
<h1 class="mb-4 text-4xl font-extrabold">Your score: <%= @score %></h1>
|
||||||
<h2>
|
<h2>
|
||||||
<%= @message %>
|
<%= @message %>
|
||||||
It's <%= @time %>
|
|
||||||
</h2>
|
</h2>
|
||||||
<br />
|
<br />
|
||||||
<h2>
|
<h2 class="flex justify-center">
|
||||||
|
<%= if @score >= winning_score() do %>
|
||||||
|
<.link
|
||||||
|
href={~p"/guess"}
|
||||||
|
class="bg-blue-500 hover:bg-blue-700 text-white text-center font-bold py-2 px-4 border border-blue-700 rounded m-1"
|
||||||
|
>
|
||||||
|
Play again!
|
||||||
|
</.link>
|
||||||
|
<% else %>
|
||||||
<%= for n <- 1..10 do %>
|
<%= for n <- 1..10 do %>
|
||||||
<.link
|
<.link
|
||||||
class="bg-blue-500 hover:bg-blue-700
|
class="bg-blue-500 hover:bg-blue-700 text-white text-center font-bold py-2 px-4 border border-blue-700 rounded m-1"
|
||||||
text-white font-bold py-2 px-4 border border-blue-700 rounded m-1"
|
|
||||||
phx-click="guess"
|
phx-click="guess"
|
||||||
phx-value-number={n}
|
phx-value-number={n}
|
||||||
>
|
>
|
||||||
<%= n %>
|
<%= n %>
|
||||||
</.link>
|
</.link>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
<% end %>
|
||||||
</h2>
|
</h2>
|
||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|
||||||
defp time() do
|
defp random_number() do
|
||||||
DateTime.utc_now |> to_string
|
:rand.uniform(10)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp winning_score(), do: @winning_score
|
||||||
|
|
||||||
def handle_event("guess", %{"number" => guess}, socket) do
|
def handle_event("guess", %{"number" => guess}, socket) do
|
||||||
message = "Your guess: #{guess}. Wrong. Guess again."
|
number = socket.assigns.number |> to_string()
|
||||||
|
|
||||||
|
socket =
|
||||||
|
if number == guess do
|
||||||
|
correct_guess(guess, socket)
|
||||||
|
else
|
||||||
|
wrong_guess(guess, socket)
|
||||||
|
end
|
||||||
|
|
||||||
|
{:noreply, socket}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp correct_guess(guess, socket) do
|
||||||
|
score = socket.assigns.score + 8
|
||||||
|
|
||||||
|
message = if score >= @winning_score do
|
||||||
|
"Your guess: #{guess}. Correct. You won!"
|
||||||
|
else
|
||||||
|
"Your guess: #{guess}. Correct. Guess the next number."
|
||||||
|
end
|
||||||
|
|
||||||
|
socket
|
||||||
|
|> assign(message: message)
|
||||||
|
|> assign(score: score)
|
||||||
|
|> assign(number: random_number())
|
||||||
|
end
|
||||||
|
|
||||||
|
defp wrong_guess(guess, socket) do
|
||||||
|
message = "Your guess: #{guess}. Wrong. Guess again."
|
||||||
score = socket.assigns.score - 1
|
score = socket.assigns.score - 1
|
||||||
|
|
||||||
{:noreply, assign(socket, message: message, score: score, time: time())}
|
socket
|
||||||
|
|> assign(message: message)
|
||||||
|
|> assign(score: score)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue