Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Score Tracker

score_tracker.livemd

Score Tracker

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Home Report An Issue GenServer DrillsStack Server

Score Tracker

You’re going to create a ScoreTracker GenServer which is a generic score tracker.

Requirements

  • The initial score starts as 0.
  • Handle an asynchronous {:add_points, amount} message to increment the score state.
  • Handle a synchronous :current_score message to retrieve the current score.
  • Create the start_link/1, current_score/1, and add_points/2 Client API functions as documented below.
{:ok, pid} = ScoreTracker.start_link([])
:ok = ScoreTracker.add_points(pid, 10)
10 = ScoreTracker.current_score(pid)

Example Solution

defmodule ScoreTracker do
  use GenServer

  def start_link(_opts) do
    GenServer.start_link(__MODULE__, [])
  end

  def add_points(score_tracker_pid, amount) do
    GenServer.cast(score_tracker_pid, {:add_points, amount})
  end

  def get_score(score_tracker_pid) do
    GenServer.call(score_tracker_pid, :current_score)
  end

  @impl true
  def init(_opts) do
    {:ok, 0}
  end

  @impl true
  def handle_cast({:add_points, points}, state) do
    {:noreply, state + points}
  end

  @impl true
  def handle_call(:current_score, _from, state) do
    {:reply, state, state}
  end
end

Implement the ScoreTracker module below.

defmodule ScoreTracker do
  @moduledoc """
  iex> {:ok, pid} = ScoreTracker.start_link([])
  iex> ScoreTracker.add_points(pid, 10)
  :ok
  iex> ScoreTracker.current_score(pid)
  10
  """
  use GenServer

  # Client API

  def start_link(_opts) do
  end

  def add_points(score_tracker_pid, amount) do
  end

  def current_score(score_tracker_pid) do
  end

  # Server API

  def init(_init_arg) do
  end

  def handle_cast({:add_points, amount}, state) do
  end

  def handle_call(:current_score, _from, state) do
  end
end

Bonus: Multiplayer Score Tracker

Create a MultiplayerScoreTracker which can track scores for multiple named players.

Requirements

  • The initial score starts as a map %{}.
  • Handle an asynchronous {:add_points, player_name, amount} message to increment the score state.
  • Handle a synchronous {:current_score, player_name} message to retrieve the current score.
  • Handle a synchronous :all_scores message to retrieve a map of all scores.
  • Create the start_link/1, current_score/2, and add_points/3, and all_scores/1 Client API functions as documented below. For sake of practice, you will be required to define any necessary handlers.
{:ok, pid} = MultiplayerScoreTracker.start_link([])

:ok = MultiplayerScoreTracker.add_points(pid, :player1, 10)
:ok = MultiplayerScoreTracker.add_points(pid, :player1, 20)
:ok = MultiplayerScoreTracker.add_points(pid, :player2, 20)

%{
  player1: 30,
  player2: 20
} = MultiplayerScoreTracker.all_scores(pid)

30 = MultiplayerScoreTracker.current_score(pid, :player1)

Example Solution

defmodule MultiplayerScoreTracker do
  use GenServer

  def start_link(_opts) do
    GenServer.start_link(__MODULE__, [])
  end

  def add_points(multiplayer_score_tracker_pid, player_name, amount) do
    GenServer.cast(multiplayer_score_tracker_pid, {:add_points, player_name, amount})
  end

  def all_scores(multiplayer_score_tracker_pid) do
    GenServer.call(multiplayer_score_tracker_pid, :all_scores)
  end

  def current_score(multiplayer_score_tracker_pid, player_name) do
    GenServer.call(multiplayer_score_tracker_pid, {:current_score, player_name})
  end

  @impl true
  def init(_opts) do
    {:ok, %{}}
  end

  @impl true
  def handle_cast({:add_points, player_name, amount}, state) do
    {:noreply, Map.update(state, player_name, amount, fn current -> current + amount end)}
  end

  @impl true
  def handle_call(:all_scores, _from, state) do
    {:reply, state, state}
  end

  @impl true
  def handle_call({:current_score, player_name}, _from, state) do
    {:reply, state[player_name], state}
  end
end
defmodule MultiplayerScoreTracker do
  @moduledoc """
  iex> {:ok, pid} = MultiplayerScoreTracker.start_link([])
  iex> :ok = MultiplayerScoreTracker.add_points(pid, :player1, 10)
  iex> :ok = MultiplayerScoreTracker.add_points(pid, :player1, 20)
  iex> :ok = MultiplayerScoreTracker.add_points(pid, :player2, 20)
  iex> MultiplayerScoreTracker.all_scores(pid)
  %{
    player1: 30,
    player2: 20
  }
  iex> MultiplayerScoreTracker.current_score(pid, :player1)
  30
  """
  use GenServer

  def start_link(_opts) do
  end

  def add_points(multiplayer_score_tracker_pid, player_name, amount) do
  end

  def all_scores(multiplayer_score_tracker_pid) do
  end

  def current_score(multiplayer_score_tracker_pid, player_name) do
  end
end

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Score Tracker exercise"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation

Home Report An Issue GenServer DrillsStack Server