Consumable Protocol
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.8.0", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Overview
You’re creating a (RPG) Role Playing Game where there are Character structs which store information about a character.
Define a Character struct with the following keys and default values.
classDiagram
class Character {
strength: 10
dexterity: 10
constitution: 10
wisdom: 10
intelligence: 10
charisma: 10
}
Example Solution
defmodule Character do
defstruct [strength: 10, dexterity: 10, constitution: 10, wisdom: 10, intelligence: 10, charisma: 10]
end
Enter your solution below.
Our game has Consumable items when modify a character’s base stats. For example, we’ll have
a StrengthPotion struct which will increase a Character‘s strength based on the :level key.
Define item structs with the following keys and default values.
classDiagram
class StrengthPotion {
level: 1
}
class DexterityPotion {
level: 1
}
class ConstitutionPotion {
level: 1
}
class WisdomPotion {
level: 1
}
class IntelligencePotion {
level: 1
}
class CharismaPotion {
level: 1
}
Example Solution
defmodule StrengthPotion do
defstruct [level: 1]
end
defmodule DexterityPotion do
defstruct [level: 1]
end
defmodule ConstitutionPotion do
defstruct [level: 1]
end
defmodule WisdomPotion do
defstruct [level: 1]
end
defmodule IntelligencePotion do
defstruct [level: 1]
end
defmodule CharismaPotion do
defstruct [level: 1]
end
Enter your solution below.
Enum.map("abc", fn char -> char end)
To make our system extendable, we’ll have a Consumable protocol, and separate implementations for structs such as the StrengthPotion.
Create a Consumable protocol with a consume/2 function which accepts an item and a Character struct. We’ll apply the items affects and return a newly updated character.
Also create Consumable implementations for the StrengthPotion, DexterityPotion, ConstitutionPotion, WisdomPotion, IntelligencePotion, and CharismaPotion structs.
When consumed, the item should increase the associated Character struct key by the :level of the Consumable item.
Consumable.consume(%StrengthPotion{level: 1}, %Character{})
%Character{strength: 11, dexterity: 10, consititution: 10, wisdom: 10, intelligence: 10, charisma: 10}
Consumable.consume(%DexterityPotion{level: 2}, %Character{})
%Character{strength: 10, dexterity: 12, consititution: 10, wisdom: 10, intelligence: 10, charisma: 10}
Example Solution
defprotocol Consumable do
def consume(item, character)
end
defimpl Consumable, for: StrengthPotion do
def consume(potion, character) do
%{character | strength: character.strength + potion.level}
end
end
defimpl Consumable, for: DexterityPotion do
def consume(potion, character) do
%{character | dexterity: character.dexterity + potion.level}
end
end
defimpl Consumable, for: ConstitutionPotion do
def consume(potion, character) do
%{character | constitution: character.constitution + potion.level}
end
end
defimpl Consumable, for: WisdomPotion do
def consume(potion, character) do
%{character | wisdom: character.wisdom + potion.level}
end
end
defimpl Consumable, for: IntelligencePotion do
def consume(potion, character) do
%{character | intelligence: character.intelligence + potion.level}
end
end
defimpl Consumable, for: CharismaPotion do
def consume(potion, character) do
%{character | charisma: character.charisma + potion.level}
end
end
Mark As Completed
file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")
save_name =
case Path.basename(__DIR__) do
"reading" -> "consumable_protocol_reading"
"exercises" -> "consumable_protocol_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 consumable-protocol-exercise
$ git add .
$ git commit -m "finish consumable protocol exercise"
$ git push origin consumable-protocol-exercise
Create a pull request from your consumable-protocol-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 |
|---|---|
| Math With Protocols | Battle Map |