Skip to content

Commit 5f11571

Browse files
committed
Make game collaborative
1 parent 0c77416 commit 5f11571

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
# Tetris
22

3-
To start your new Phoenix application:
3+
To start tetris:
44

55
1. Install dependencies with `mix deps.get`
6-
2. Start Phoenix endpoint with `mix phoenix.server`
6+
2. Start Phoenix endpoint with `mix phoenix.server` or interactively with `iex
7+
-S mix phoenix.server`
78

89
Now you can visit `localhost:4000` from your browser.
10+
11+
To start a new game, from iex:
12+
13+
```elixir
14+
Tetris.restart_game
15+
```

lib/tetris.ex

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule Tetris do
1818
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
1919
# for other strategies and supported options
2020
opts = [strategy: :one_for_one, name: Tetris.Supervisor]
21+
start_game
2122
Supervisor.start_link(children, opts)
2223
end
2324

@@ -27,4 +28,14 @@ defmodule Tetris do
2728
Tetris.Endpoint.config_change(changed, removed)
2829
:ok
2930
end
31+
32+
def restart_game do
33+
Process.unregister(:game)
34+
start_game
35+
end
36+
37+
def start_game do
38+
{:ok, game} = Tetris.Game.start
39+
Process.register(game, :game)
40+
end
3041
end

web/channels/game.ex

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ defmodule Tetris.GameChannel do
88

99
def handle_in("event", %{"event" => event_name}, socket) do
1010
# NOTE: Don't use String.to_atom like this folks
11-
Tetris.Game.handle_input(socket.assigns[:game], String.to_atom(event_name))
11+
Tetris.Game.handle_input(:game, String.to_atom(event_name))
1212
{:noreply, socket}
1313
end
1414

1515
def handle_info(:after_join, socket) do
16-
{:ok, game} = Tetris.Game.start
17-
spawn(fn() -> Tetris.Websocket.run(game, socket) end)
18-
socket = assign(socket, :game, game)
16+
spawn(fn() -> Tetris.Websocket.run(:game, socket) end)
1917
{:noreply, socket}
2018
end
2119
end

0 commit comments

Comments
 (0)