Skip to content

Commit

Permalink
Merge pull request #39 from leoamigood/top_guess
Browse files Browse the repository at this point in the history
Adding top guess sorting
  • Loading branch information
leoamigood authored Feb 13, 2023
2 parents 25b1302 + 5578fb2 commit 9a36400
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/alternis/engines/dictionary_engine_impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule Alternis.Engines.DictionaryEngine.Impl do
where: ^condition,
where: d.language == ^language,
select_merge: %{language: d.language},
order_by: w.frequency,
order_by: fragment("RANDOM()"),
limit: 1
)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/alternis/guess.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ defmodule Alternis.Guess do
|> validate_required([:word, :bulls, :cows, :exact?])
end

def priority(guess) do
length(guess.bulls) * 10 + length(guess.cows)
end

def validate_word(attrs \\ %{}) do
%Guess{}
|> cast(attrs, [:word])
Expand Down
3 changes: 2 additions & 1 deletion lib/alternis_web/live/game_live/game_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ defmodule AlternisWeb.GameLive.GameComponent do
use Phoenix.Component

attr :guesses, :list, required: true
attr :update, :string, default: "append"
slot :header
slot :footer

def timeline(assigns) do
~H"""
<div>
<%= render_slot(@header) %>
<ul id="game-guesses" phx-update="append">
<ul id="game-guesses" phx-update={@update}>
<%= for guess <- @guesses do %>
<li id={guess.id} style={if guess.exact?, do: "color:green"}>
<%= guess.user.username %>: <%= guess.word %> - bulls: <%= length(guess.bulls) %>, cows: <%= length(
Expand Down
2 changes: 1 addition & 1 deletion lib/alternis_web/live/game_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
</:header>
</.board>

<span><%= live_redirect "New Game", to: ~p"/games/new" %></span>
<span><.link navigate={~p"/games/new"}>New Game</.link></span>
23 changes: 22 additions & 1 deletion lib/alternis_web/live/game_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule AlternisWeb.GameLive.Show do

alias Alternis.Landing
alias Alternis.Game.GameState.{Expired, Finished}
alias Alternis.Guess

@game_ended_event "game_ended"
@guess_placed_event "guess_placed"
Expand All @@ -25,6 +26,7 @@ defmodule AlternisWeb.GameLive.Show do
{:ok,
socket
|> assign(:game, game)
|> assign(:update, "append")
|> assign(:current_user, user)
|> assign(:players, online_players(game.id))
|> assign(:guesses, game.guesses), temporary_assigns: [guesses: []]}
Expand All @@ -33,7 +35,10 @@ defmodule AlternisWeb.GameLive.Show do

@impl true
def handle_info(%{event: @guess_placed_event, payload: %{guess: guess}}, socket) do
{:noreply, assign(socket, :guesses, [guess])}
{:noreply,
socket
|> assign(:update, "append")
|> assign(:guesses, [guess])}
end

def handle_info(%{event: @game_ended_event}, socket) do
Expand All @@ -44,6 +49,22 @@ defmodule AlternisWeb.GameLive.Show do
{:noreply, socket |> assign(:players, online_players(game_id))}
end

@impl true
def handle_event("order_top", %{"game_id" => game_id}, socket) do
case Landing.get_game!(game_id) do
nil ->
raise AlternisWeb.GameLive.GameNotFoundError

game ->
sorted = Enum.sort(game.guesses, &(Guess.priority(&1) > Guess.priority(&2)))

{:noreply,
socket
|> assign(:update, "replace")
|> assign(:guesses, sorted)}
end
end

@impl true
def handle_params(_params, _session, socket = %{assigns: %{game: game}}) do
case game.state do
Expand Down
4 changes: 3 additions & 1 deletion lib/alternis_web/live/game_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
<% end %>

<ul>
<.timeline guesses={@guesses}>
<.timeline guesses={@guesses} update={@update}>
<:header>
<strong>Language:</strong>
<%= Recase.to_pascal(@game.language.value) %>
<br/>
<strong>Secret:</strong>
<%= String.pad_leading("", String.length(@game.secret), "*") %>
<br/>
<button phx-click="order_top" phx-value-game_id={@game.id}>Top</button>
</:header>
<:footer>
<%= if @game.in_progress? do %>
Expand Down

0 comments on commit 9a36400

Please sign in to comment.