Created
March 9, 2018 15:23
-
-
Save marinho10/ef350e46ba8bbe3a8973a88cd3229ae5 to your computer and use it in GitHub Desktop.
GameState Agent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule SuperSlimeGame.GameState do | |
@moduledoc """ | |
This module holds the game current state. It also contains the game logic. | |
Allows to add new players to the board, move them and detect collisions. | |
""" | |
@doc """ | |
Used by the supervisor to start the Agent that will keep the game state persistent. | |
The initial value passed to the Agent is an empty map. | |
""" | |
def start_link do | |
Agent.start_link(fn -> %{} end, name: __MODULE__) | |
end | |
@doc """ | |
create a game | |
""" | |
def create_game(code) do | |
game = generate_new_game(code) | |
Agent.update(__MODULE__, &Map.put_new(&1, game.id, game)) | |
game | |
end | |
@doc """ | |
update game to a game | |
""" | |
def update_game(game) do | |
Agent.update(__MODULE__, &Map.put(&1, game.id, game)) | |
game | |
end | |
@doc """ | |
Retrieve a game from the map | |
""" | |
def get_game(code) do | |
Agent.get(__MODULE__, &Map.get(&1, code)) | |
end | |
@doc """ | |
Get all the games in the map | |
""" | |
def games do | |
Agent.get(__MODULE__, & &1) | |
end | |
# Game logic | |
defp generate_new_game(code) do | |
%{ | |
:id => code, | |
:player1 => %{:email => "", :score => 0}, | |
:player2 => %{:email => "", :score => 0} | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment