Video Game Spawner
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.9", 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.
Video Game Spawner
It’s classic in video games for a spawner to spawn enemy creatures. Typically the spawner has a limit for how many enemy creatures it can spawn. If a creature dies, the spawner then re-spawns that creature.
You’re going to create a Spawner
application to simulate a game with enemy spawns.
This project aims to help you learn about supervisors and fault tolerance.
A SpawnerSupervisor
will supervise three Creature
GenServers
s.
flowchart
S[Spawner]
C1[Creature]
C2[Creature]
C3[Creature]
S --> C1
S --> C2
S --> C3
S --> C4
When a Creature
process dies, the Spawner
supervisor will automatically restart it.
Create A New Mix Project
Using the command line, create a new supervised project in the projects
folder called spawner
.
mix new spawner --sup
Create The Creature GenServer
Create a Creature
GenServer. It should start as a minimal GenServer with no additional functionality for now.
Configure The Spawner
Configure the Spawner to start in application.ex
.
(Optional) Use :observer To View Your Spawner And Creature
Start your project
iex -S mix
Then start the observer.
:observer.start()
Use the observer to kill a Creature
process. It should be automatically restarted
by the Spawner
supervisor.
:kill
A Creature
process should be killed when it receives a :kill
message.
Create a Spawner.kill/1
which accepts the pid
of a Creature
process and sends it the :kill
message.
Upon terminating, the Spawner
supervisor should automatically restart the Creature
process.
Mark As Completed
file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")
progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()
default = Map.get(existing_progress, file_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, file_name, completed)))
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 solutions
$ git checkout -b video-game-spawner-exercise
$ git add .
$ git commit -m "finish video game spawner exercise"
$ git push origin video-game-spawner-exercise
Create a pull request from your video-game-spawner-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 instructor by including @BrooklinJazz
in your PR description to get feedback.
You (or your instructor) 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.