Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
CREATING A
STEALING POINTS
GAME SERVER
WITH ELIXIR
Kleber N. Gueriero
Web Developer
Ruby Developer
There is this alchemists
language ...
Created by José Valim
(Rails Core Team)
Influenced by nice
Languages(such as
Ruby and Erlang)
Functional
Runs on Erlang VM
and has a lot of
Erlang inheritance
Which kind of
inheritances?
Ready to Fault-tolerance
Handles High
Concurrency
The Game
Structs
defmodule ExStealerGame.Player do
defstruct [:id, :name, :score, :client_pid]
end
The Structs
defmodule ExStealerGame.Matc...
defmodule ExStealerGame do
use Application
def start(_type, _args) do
IO.puts "STARTING THE APPLICATION"
ExStealerGame.Sup...
Supervisors
New server
process was
started by
Supervisor
defmodule ExStealerGame.Supervisor do
use Supervisor
def start_link() do
Supervisor.start_link(__MODULE__, [])
End
...
end...
defmodule ExStealerGame.Supervisor do
use Supervisor
…
def init(_) do
children = [
worker(Task, [Server, :start_server, []...
Keeping State
- Elixir is Immutable
- Processes are used to keep state
- Processes sends and receives messages to
each oth...
Agents
defmodule ExStealerGame.Match do
...
def start_link do
Agent.start_link(fn -> %Match{last_id: 0} end, name: __MODULE__)
en...
defmodule ExStealerGame.Match do
...
def register_player(player_name, client_pid) do
new_player = %Player{id: next_id(), n...
defmodule ExStealerGame.Match do
...
def get_players do
Agent.get(__MODULE__, fn(current_match) -> current_match.players e...
defmodule ExStealerGame.Server do
def start_server() do
{:ok, socket} = :gen_tcp.listen(@port, [some_options])
accept_conn...
Tasks
defmodule ExStealerGame.Server do
…
defp accept_connection(socket) do
{:ok, client} = :gen_tcp.accept(socket)
handle_playe...
defmodule ExStealerGame.Server do
…
defp match_loop(client) do
client
|> read_client() #wait for client’s message and rece...
Pattern Matching
defmodule ExStealerGame.Server do
defp handle_match_message("quit", client) do
quit_player(client)
end
defp handle_match_m...
defmodule ExStealerGame.Server do
defp quit_player(client) do
IO.puts "PLAYER QUITTING"
Match.get_player(client)
|> Match....
defmodule ExStealerGame.Server do
defp handle_match_message(message, client) do
case Integer.parse(message) do
{target_id,...
defmodule ExStealerGame.Server do
defp handle_match_state([]) do
send_score_to_clients()
end
defp handle_match_state(winne...
defmodule ExStealerGame.Server do
defp handle_match_state(winners) do
Match.get_players
|> Enum.each(fn(player) ->
"<End o...
- Structs
- Supervisors
- Agents
- Tasks
- Pattern Matching
Thank’s!
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had a SRE team at - Creating a stealing points ga...
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had a SRE team at - Creating a stealing points ga...
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had a SRE team at - Creating a stealing points ga...
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had a SRE team at - Creating a stealing points ga...
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had a SRE team at - Creating a stealing points ga...
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had a SRE team at - Creating a stealing points ga...
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had a SRE team at - Creating a stealing points ga...
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had a SRE team at - Creating a stealing points ga...
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had a SRE team at - Creating a stealing points ga...
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had a SRE team at - Creating a stealing points ga...
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had a SRE team at - Creating a stealing points ga...
Upcoming SlideShare
Loading in …5
×

TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had a SRE team at - Creating a stealing points game server with elixir

55 views

Published on

Trilha Programação Funcional How we figured out we had a SRE team at - Creating a stealing points game server with elixir

Published in: Education
  • Be the first to comment

TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had a SRE team at - Creating a stealing points game server with elixir

  1. 1. CREATING A STEALING POINTS GAME SERVER WITH ELIXIR
  2. 2. Kleber N. Gueriero Web Developer Ruby Developer
  3. 3. There is this alchemists language ...
  4. 4. Created by José Valim (Rails Core Team)
  5. 5. Influenced by nice Languages(such as Ruby and Erlang)
  6. 6. Functional
  7. 7. Runs on Erlang VM and has a lot of Erlang inheritance
  8. 8. Which kind of inheritances?
  9. 9. Ready to Fault-tolerance
  10. 10. Handles High Concurrency
  11. 11. The Game
  12. 12. Structs
  13. 13. defmodule ExStealerGame.Player do defstruct [:id, :name, :score, :client_pid] end The Structs defmodule ExStealerGame.Match do defstruct [:last_id, {:players, []}] end
  14. 14. defmodule ExStealerGame do use Application def start(_type, _args) do IO.puts "STARTING THE APPLICATION" ExStealerGame.Supervisor.start_link end end Starting Point
  15. 15. Supervisors
  16. 16. New server process was started by Supervisor
  17. 17. defmodule ExStealerGame.Supervisor do use Supervisor def start_link() do Supervisor.start_link(__MODULE__, []) End ... end The Supervisor
  18. 18. defmodule ExStealerGame.Supervisor do use Supervisor … def init(_) do children = [ worker(Task, [Server, :start_server, []]), worker(Match, [])] supervise children, strategy: :one_for_one end end The Supervisor
  19. 19. Keeping State - Elixir is Immutable - Processes are used to keep state - Processes sends and receives messages to each other to request to receive/set this state
  20. 20. Agents
  21. 21. defmodule ExStealerGame.Match do ... def start_link do Agent.start_link(fn -> %Match{last_id: 0} end, name: __MODULE__) end end The Match’s State
  22. 22. defmodule ExStealerGame.Match do ... def register_player(player_name, client_pid) do new_player = %Player{id: next_id(), name: player_name, score: @starting_score, client_pid: client_pid} Agent.update(__MODULE__, fn(current) -> %{current | players: [new_player | current.players]} end) new_player end end The Match’s State
  23. 23. defmodule ExStealerGame.Match do ... def get_players do Agent.get(__MODULE__, fn(current_match) -> current_match.players end) end end The Match’s State
  24. 24. defmodule ExStealerGame.Server do def start_server() do {:ok, socket} = :gen_tcp.listen(@port, [some_options]) accept_connection(socket) end end Starting the Server
  25. 25. Tasks
  26. 26. defmodule ExStealerGame.Server do … defp accept_connection(socket) do {:ok, client} = :gen_tcp.accept(socket) handle_player_join(client) # keep player with Agent and send msg to client {:ok, pid} = Task.start(fn -> match_loop(client) end) :gen_tcp.controlling_process(client, pid) accept_connection(socket) # recursively keeps open to new connections end end Connecting to Clients
  27. 27. defmodule ExStealerGame.Server do … defp match_loop(client) do client |> read_client() #wait for client’s message and receive it |> handle_match_message(client) match_loop(client) #again, recursively, keeps the game’s match running end end The Game Loop
  28. 28. Pattern Matching
  29. 29. defmodule ExStealerGame.Server do defp handle_match_message("quit", client) do quit_player(client) end defp handle_match_message(message, client) do ... end end Quitting the Game
  30. 30. defmodule ExStealerGame.Server do defp quit_player(client) do IO.puts "PLAYER QUITTING" Match.get_player(client) |> Match.remove_player send_score_to_clients() # send message to clients with players list and score Process.exit(self(), :kill) # kills current process, along with the conection end end Quitting the Game
  31. 31. defmodule ExStealerGame.Server do defp handle_match_message(message, client) do case Integer.parse(message) do {target_id, _} -> Match.get_player(client).id |> Match.execute_steal(target_id) Match.get_winners |> handle_match_state :error -> send_score_to_clients() end end end Stealing Points
  32. 32. defmodule ExStealerGame.Server do defp handle_match_state([]) do send_score_to_clients() end defp handle_match_state(winners) do ... end end Checking if there is a winner
  33. 33. defmodule ExStealerGame.Server do defp handle_match_state(winners) do Match.get_players |> Enum.each(fn(player) -> "<End of Match Message>" |> send_to_client(player.client_pid) end) :timer.sleep 2000 Match.restart_match send_score_to_clients end end Checking if there is a winner
  34. 34. - Structs - Supervisors - Agents - Tasks - Pattern Matching
  35. 35. Thank’s!

×