Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds working pubsub #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion game/lib/game.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Game do
%{game |
steps: tail,
sentence: new_sentence,
score: score(user_guess, game.sentence)}
score: game.score + score(user_guess, game.sentence)}
end

def show(game) do
Expand Down
36 changes: 25 additions & 11 deletions game/lib/game_web/live/game_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ defmodule GameWeb.GameLive do
<%= @game.score %>
</div>

<div id="scoreboard">
<%= inspect @scoreboard, pretty: true %>
</div>

<.simple_form
for={@form}
id="guess-form"
Expand All @@ -29,8 +33,11 @@ defmodule GameWeb.GameLive do

socket = assign(socket,
game: Game.new(reading.text, reading.steps),
scoreboard: Highscore.show()
)

Phoenix.PubSub.subscribe(Game.PubSub, Highscore.topic())

{:ok, socket |> clear_form() }
end

Expand All @@ -41,17 +48,13 @@ defmodule GameWeb.GameLive do
end

def handle_params(_params, _url, socket) do
{:noreply, socket}
end

def handle_event("erase", _params, socket) do
%{game: game} = socket.assigns
email = socket.assigns.current_user.email
score = socket.assigns.game.score

socket = case game do
%{steps: []} -> push_patch(socket, to: ~p"/done")
game ->
game = Game.erase(game)
assign(socket, :game, game)
socket = cond do
socket.assigns.live_action == :done ->
assign(socket, :scoreboard, Highscore.add(email, score))
true -> socket
end

{:noreply, socket}
Expand All @@ -64,7 +67,18 @@ defmodule GameWeb.GameLive do

def handle_event("guess", params, socket) do
changes = params["game"] |> changeset()
{:noreply, guess(socket, changes) |> clear_form() }

socket = case socket.assigns.game do
%{steps: []} -> push_patch(socket, to: ~p"/done")
game -> guess(socket, changes)
end |> clear_form()

{:noreply, socket}
end

def handle_info({:new_scores, scores}, socket) do

{:noreply, assign(socket, :scoreboard, scores)}
end

defp guess(socket, changeset) do
Expand Down
21 changes: 17 additions & 4 deletions game/lib/highscore.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,25 @@ defmodule Highscore do

def show(pid \\ :hs) do
GenServer.call(pid, :show)
|> convert()
end

def add(pid \\ :hs, email, score) do
scores = GenServer.call(pid, {:add, email, score}) |> convert()

Phoenix.PubSub.broadcast(Game.PubSub, topic(), {:new_scores, scores})

scores
end

def convert(scores) do
scores
|> Enum.sort_by(fn {_key, val} -> -val end)
|> Enum.take(5)
end

def add(pid \\ :hs, email, score) do
GenServer.cast(pid, {:add, email, score})
def topic do
"highscores"
end

# Callbacks
Expand All @@ -29,9 +42,9 @@ defmodule Highscore do
end

@impl true
def handle_cast({:add, email, score}, scores) do
def handle_call({:add, email, score}, _from, scores) do
new_state = Map.put(scores, email, score)
{:noreply, new_state}
{:reply, new_state, new_state}
end

end