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

Mining Simulator

mining_simulator.livemd

Mining Simulator

Mix.install([
  {:kino, github: "livebook-dev/kino", override: true},
  {:kino_lab, "~> 0.1.0-dev", github: "jonatanklosko/kino_lab"},
  {:vega_lite, "~> 0.1.4"},
  {:kino_vega_lite, "~> 0.1.1"},
  {:benchee, "~> 0.1"},
  {:ecto, "~> 3.7"},
  {:math, "~> 0.7.0"},
  {:faker, "~> 0.17.0"},
  {:utils, path: "#{__DIR__}/../utils"},
  {:tested_cell, git: "https://github.com/BrooklinJazz/tested_cell"}
])

Navigation

Return Home Report An Issue

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.

Mining Simulator

We’re going to create a gold mining simulator game.

This project is designed to learn about supervisors and fault tolerance.

A MineSupervisor will supervise a dynamic number of Mine workers. Each Mine will be a GenServer.

flowchart
  MS[MineSupervisor]
  M1[Mine]
  M2[Mine]
  M3[Mine]

  MS --> M1
  MS --> M2
  MS --> M3

Each Mine will harvest 5 gold every 5 seconds.

Upon harvesting, each Mine will send a message to the ResourceStore GenServer to store 5 more gold.

flowchart
M1[Mine] --send 5 gold--> ResourceStore

Create A New Mix Project

Using the command line, create a new supervised project in the projects folder called mining_simulator.

mix new mining_simulator --sup

Evaluate the cell below to ensure you have correctly created the project.

Utils.feedback(:created_project, "mining_simulator")

Dialyzer, Credo, and ExDoc are optional for this project.

Configure the MineSupervisor

Configure the MineSupervisor to start in application.ex. The MineSupervisor should be able to dynamically supervise its child Mine workers.

Configure the ResourceStore

Configure the ResourceStore to start in application.ex. The ResourceStore should be a GenServer or Agent, which starts with 200 gold in its state.

MiningSimulator.new_mine/0

The MiningSimulator.new_mine/0 function should start a Mine GenServer under the MineSupervisor. It should remove 200 gold from the ResourceStore.

The Mine harvests resources on an interval and sends a message to add gold to the ResourceStore.

If there is less than 200 gold in the ResourceStore, the new_mine/0 function should return an error tuple {:error, :not_enough_funds}

MiningSimulator.explode/1

Don’t worry, these Mine’s are 100% robot operated and fully insured so if a mine explodes we can start a new mine.

The MiningSimulator.explode/1 function should take the pid of a Mine and kill the Mine‘s process. The MineSupervisor should then automatically start up a new Mine.

(Bonus) Finite Resources

Each Mine should start with a finite amount of resources (A random amount between 500 and 1000 gold). Every time the Mine harvests, resources decrease the total gold available by 5.

The’ Mine’ should terminate without being restarted by the MineSupervisor when the’ Mine’ runs out of resources.

Commit Your Progress

Run the following in your command line from the project folder to track and save your progress in a Git commit.

$ git add .
$ git commit -m "finish mining simulator exercise"