Pokemon Server
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.8.0", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Setup
Ensure you type the ea keyboard shortcut to evaluate all Elixir cells before starting. Alternatively you can evaluate the Elixir cells as you read.
Overview
You’re going to create a pokemon battle game with persistence.
Each Pokemon will be a GenServer process storing the pokemon’s stats in state.
classDiagram
class Pokemon {
health: 20
attack: 5
type: :fire
name: "charmander"
}
Pokemon can attack each other. An attack should update the Pokemon‘s health in the GenServer‘s state.
Create Pokemon Server
Create a Pokemon module which will be a GenServer and a struct definition. It should store the following in values in state. You do not need to validate these values, you can assume each will be the following:
-
:namewill be a string. -
:typewill be an atom of either:grass,:water, or:fire -
:healthwill be an integer with a default value of20 -
:attackwill be an integer with a default value of5
You should be able to create a Pokemon struct.
%Pokemon{name: "Charmander", type: :fire, health: 20, attack: 5}
Pokemon GenServers should be able to attack one another. This should subtract one pokemon’s :attack from the other pokemon’s :health, and update the value in the state of the GenServer. A pokemon’s health should not be able to fall below 0.
attacker = Pokemon.start_link(pokemon: %Pokemon{name: "Bulbasaur1", type: :grass})
defender = Pokemon.start_link(pokemon: %Pokemon{name: "Bulbasaur2", type: :grass})
Pokemon.attack(attacker, defender)
%Pokemon{name: "Bulbasaur1", type: :grass, health: 15, attack: 20}
Type Advantages and Weaknesses
Multiply the damage dealt by an attack by 2 if the attacker has a type advantage. Multiply the damage applied by 0.5 if the attacker has a type weakness.
Type Advantages are:
- fire -> grass
- grass -> water
- water -> fire
Type weaknesses are:
-
fire -> water
-
grass -> fire
-
water -> grass
Example Solution
defmodule Pokemon do
use GenServer
@enforce_keys [:name, :type]
defstruct @enforce_keys ++ [health: 20, attack: 5]
def start_link(opts \\ []) do
{pokemon, opts} = Keyword.pop!(opts, :pokemon)
GenServer.start_link(__MODULE__, pokemon, opts)
end
@impl true
def init(state) do
{:ok, state}
end
def attack(attacker, defender) do
attacker_type = Pokemon.get(attacker, :type)
defender_type = Pokemon.get(defender, :type)
damage_amount = Pokemon.get(attacker, :attack) * multiplier(attacker_type, defender_type)
GenServer.call(defender, {:apply_damage, damage_amount})
end
def multiplier(attacker_type, defender_type) do
case {attacker_type, defender_type} do
{:fire, :grass} -> 2
{:grass, :water} -> 2
{:water, :fire} -> 2
{:fire, :water} -> 0.5
{:grass, :fire} -> 0.5
{:water, :grass} -> 0.5
_ -> 1
end
end
def get(pokemon, key) do
GenServer.call(pokemon, {:get, key})
end
def handle_call({:get, key}, _from, pokemon) do
{:reply, Map.get(pokemon, key), pokemon}
end
@impl true
def handle_call({:apply_damage, amount}, _from, pokemon) do
updated_pokemon = Map.update!(pokemon, :health, fn health -> max(health - amount, 0) end)
{:reply, updated_pokemon, updated_pokemon}
end
end
Implement the Pokemon GenServer and struct definition as documented below.
defmodule Pokemon do
@moduledoc """
Pokemon Server
Defines a Pokemon GenServer and struct.
## Examples
iex> %Pokemon{name: "Charmander", type: :fire}
%Pokemon{name: "Charmander", type: :fire, health: 20, attack: 5}
"""
use GenServer
@doc """
Start the Pokemon GenServer.
## Examples
iex> {:ok, pokemon} = Pokemon.start_link([pokemon: %Pokemon{name: "Charmander", type: :fire}])
"""
def start_link(opts \\ []) do
end
@doc """
Apply the attacker's attack damage to the defender's health.
## Examples
With default values.
iex> {:ok, attacker} = Pokemon.start_link(pokemon: %Pokemon{name: "Bulbasaur1", type: :grass})
iex> {:ok, defender} = Pokemon.start_link(pokemon: %Pokemon{name: "Bulbasaur2", type: :grass})
iex> Pokemon.attack(attacker, defender)
%Pokemon{name: "Bulbasaur2", type: :grass, health: 15, attack: 5}
iex> Pokemon.attack(attacker, defender)
%Pokemon{name: "Bulbasaur2", type: :grass, health: 10, attack: 5}
iex> Pokemon.attack(attacker, defender)
%Pokemon{name: "Bulbasaur2", type: :grass, health: 5, attack: 5}
iex> Pokemon.attack(attacker, defender)
%Pokemon{name: "Bulbasaur2", type: :grass, health: 0, attack: 5}
iex> Pokemon.attack(attacker, defender)
%Pokemon{name: "Bulbasaur2", type: :grass, health: 0, attack: 5}
With type advantage
iex> {:ok, attacker} = Pokemon.start_link(pokemon: %Pokemon{name: "Charmander", type: :fire})
iex> {:ok, defender} = Pokemon.start_link(pokemon: %Pokemon{name: "Bulbasaur", type: :grass})
iex> Pokemon.attack(attacker, defender)
%Pokemon{name: "Bulbasaur", type: :grass, health: 10, attack: 5}
With type weakness
iex> {:ok, attacker} = Pokemon.start_link(pokemon: %Pokemon{name: "Bulbasaur", type: :grass})
iex> {:ok, defender} = Pokemon.start_link(pokemon: %Pokemon{name: "Charmander", type: :fire})
iex> Pokemon.attack(attacker, defender)
%Pokemon{name: "Charmander", type: :fire, health: 17.5, attack: 5}
Ensure you use the attacking pokemon's attack number to apply damage to the defending pokemon's health.
iex> {:ok, attacker} = Pokemon.start_link(pokemon: %Pokemon{name: "Bulbasaur1", type: :grass, attack: 20})
iex> {:ok, defender} = Pokemon.start_link(pokemon: %Pokemon{name: "Bulbasaur2", type: :grass, health: 45})
iex> Pokemon.attack(attacker, defender)
%Pokemon{name: "Bulbasaur2", type: :grass, health: 25, attack: 5}
"""
def attack(attacker, defender) do
end
end
Mark As Completed
file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")
save_name =
case Path.basename(__DIR__) do
"reading" -> "pokemon_server_reading"
"exercises" -> "pokemon_server_exercise"
end
progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()
default = Map.get(existing_progress, save_name, false)
form =
Kino.Control.form(
[
completed: input = Kino.Input.checkbox("Mark As Completed", default: default)
],
report_changes: true
)
Task.async(fn ->
for %{data: %{completed: completed}} <- Kino.Control.stream(form) do
File.write!(
progress_path,
Jason.encode!(Map.put(existing_progress, save_name, completed), pretty: true)
)
end
end)
form
Commit Your Progress
Run the following in your command line from the curriculum folder to track and save your progress in a Git commit.
Ensure that you do not already have undesired or unrelated changes by running git status or by checking the source control tab in Visual Studio Code.
$ git checkout -b pokemon-server-exercise
$ git add .
$ git commit -m "finish pokemon server exercise"
$ git push origin pokemon-server-exercise
Create a pull request from your pokemon-server-exercise branch to your solutions branch.
Please do not create a pull request to the DockYard Academy repository as this will spam our PR tracker.
DockYard Academy Students Only:
Notify your teacher by including @BrooklinJazz in your PR description to get feedback.
You (or your teacher) may merge your PR into your solutions branch after review.
If you are interested in joining the next academy cohort, sign up here to receive more news when it is available.
Up Next
| Previous | Next |
|---|---|
| Traffic Light Server | Asynchronous Messages |