-
Notifications
You must be signed in to change notification settings - Fork 35
/
notifier.ex
64 lines (51 loc) · 2.02 KB
/
notifier.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
defmodule Blackjack.PlayerNotifier do
use GenServer
alias Blackjack.{Round, RoundServer}
@callback deal_card(RoundServer.callback_arg, Round.player_id, Blackjack.Deck.card) :: any
@callback move(RoundServer.callback_arg, Round.player_id) :: any
@callback busted(RoundServer.callback_arg, Round.player_id) :: any
@callback winners(RoundServer.callback_arg, Round.player_id, [Round.player_id]) :: any
@callback unauthorized_move(RoundServer.callback_arg, Round.player_id) :: any
@spec child_spec(RoundServer.id, [RoundServer.player]) :: Supervisor.Spec.spec
def child_spec(round_id, players) do
import Supervisor.Spec
supervisor(Supervisor,
[
Enum.map(players, &worker(__MODULE__, [round_id, &1], [id: {__MODULE__, &1.id}])),
[strategy: :one_for_one]
]
)
end
@spec publish(RoundServer.id, Round.player_id, Round.player_instruction) :: :ok
def publish(round_id, player_id, player_instruction), do:
GenServer.cast(service_name(round_id, player_id), {:notify, player_instruction})
@doc false
def start_link(round_id, player), do:
GenServer.start_link(
__MODULE__,
{round_id, player},
name: service_name(round_id, player.id)
)
@doc false
def init({round_id, player}), do:
{:ok, %{round_id: round_id, player: player}}
@doc false
def handle_cast({:notify, player_instruction}, state) do
{fun, args} = decode_instruction(player_instruction)
all_args = [state.player.callback_arg, state.player.id | args]
apply(state.player.callback_mod, fun, all_args)
{:noreply, state}
end
defp service_name(round_id, player_id), do:
Blackjack.service_name({__MODULE__, round_id, player_id})
defp decode_instruction({:deal_card, card}), do:
{:deal_card, [card]}
defp decode_instruction(:move), do:
{:move, []}
defp decode_instruction(:busted), do:
{:busted, []}
defp decode_instruction(:unauthorized_move), do:
{:unauthorized_move, []}
defp decode_instruction({:winners, player_ids}), do:
{:winners, [player_ids]}
end