-
Notifications
You must be signed in to change notification settings - Fork 3
/
match_actor.ex
220 lines (174 loc) · 5.98 KB
/
match_actor.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
defmodule Dice.Game.MatchActor do
use SpawnSdk.Actor,
kind: :unamed,
stateful: true,
state_type: Dice.Game.MatchState,
deactivate_timeout: 60 * 60 * 1000
require Logger
import Dice.Game.Match.StateHelper
alias Dice.Game.{
Board,
JoinMatchAction,
MatchEvent,
PlayMatchAction,
Snapshot,
MatchCreateAction
}
alias Dice.Matchmaking.MatchRefInput
@finished_statuses [:finished, :finished_draw]
@start_in_seconds 5
defact create(%MatchCreateAction{matchmaking_ref: matchmaking_ref}, %Context{} = ctx) do
match_id = ctx.self.name
Logger.info(
"Initializing match for match_id: #{inspect(match_id)} (from #{inspect(matchmaking_ref)})",
label: "#{__MODULE__}"
)
state = initialize_state(ctx.state, match_id, matchmaking_ref)
Value.noreply_state!(state)
end
defact stop(%Context{state: state} = ctx) do
match_id = ctx.self.name
event = if state.status == :finished_draw, do: :draw, else: :finish
Logger.info(
"Match is finished. Status: #{inspect(state.status)}, winner: #{inspect(state.winner_ref)}",
label: "#{__MODULE__}"
)
brand_new_match = initialize_state(nil, match_id, state.matchmaking_ref)
Value.of()
|> Value.state(brand_new_match)
|> value_broadcast(event, state)
|> Value.effect(
SideEffect.to(state.matchmaking_ref, "match_finished", %MatchRefInput{ref: match_id})
)
|> Value.noreply!()
end
defact start(%Context{state: state}) do
cond do
state.status != :starting ->
Value.noreply_state!(state)
true ->
starting_player_ref = state.players |> Map.keys() |> Enum.random()
new_state =
state
|> put_state(status: :playing)
|> put_state(scheduled_to: nil)
|> put_state_turn_ref(starting_player_ref)
Value.of()
|> Value.state(new_state)
|> value_broadcast(:start, new_state)
|> Value.noreply!()
end
end
defact prepare_start(%Context{state: state} = ctx) do
match_id = ctx.self.name
cond do
Enum.count(state.players) != 2 ->
Value.noreply_state!(state)
state.status in [:starting, :playing, :finished, :finished_draw] ->
Value.noreply_state!(state)
true ->
scheduled_to = DateTime.add(DateTime.now!("Etc/UTC"), @start_in_seconds, :second)
new_state =
state
|> put_state(status: :starting)
|> put_state(scheduled_to: DateTime.to_unix(scheduled_to, :millisecond))
Value.of()
|> Value.state(new_state)
|> Value.effect(SideEffect.to(match_id, "start", nil, delay: @start_in_seconds * 1_000))
|> value_broadcast(:prepare_start, new_state)
|> Value.noreply!()
end
end
defact join(%JoinMatchAction{player_ref: player_ref}, %Context{state: state}) do
player_found = player_by_ref(state, player_ref)
cond do
not is_nil(player_found) ->
Value.noreply_state!(state)
Enum.count(state.players) == 2 ->
Value.noreply_state!(state)
true ->
players = Map.merge(state.players, new_player(player_ref))
# status = if Enum.count(players) == 2, do: :joined_last, else: :joined
new_state = put_state(state, players: players)
Value.of()
|> Value.state(new_state)
|> value_broadcast(:join, state)
|> Value.noreply!()
end
end
defact play(
%PlayMatchAction{player_ref: player_ref, row_index: row_index},
%Context{state: state} = ctx
) do
match_id = ctx.self.name
player = player_by_ref(state, player_ref)
cond do
state.status in @finished_statuses ->
Value.noreply_state!(state)
state.status != :playing ->
Value.noreply_state!(state)
state.player_turn_ref != player_ref ->
Value.noreply_state!(state)
Board.is_row_full?(player.board, row_index) ->
Value.noreply_state!(state)
true ->
enemy = enemy_by_ref(state, player_ref)
enemy_ref = get_enemy_ref(state, player_ref)
{player_snapshot, enemy_snapshot, winner} =
do_combat(player, enemy, row_index, state.player_turn_dice)
players =
state.players
|> Map.replace(player_ref, player_snapshot)
|> Map.replace(enemy_ref, enemy_snapshot)
state =
state
|> put_state(players: players)
|> put_turn_or_winner_state(player_ref, enemy_ref, winner)
reply =
Value.of()
|> value_broadcast(:play, state)
|> Value.state(state)
if state.status in @finished_statuses do
reply
|> Value.effects([
SideEffect.to(match_id, "stop")
])
else
reply
end
|> Value.noreply!()
end
end
defp do_combat(%Snapshot{} = player, %Snapshot{} = enemy, row_index, dice_num) do
player_snapshot = Board.push(player.board, row_index, dice_num) |> Board.get_snapshot()
enemy_snapshot = Board.pop(enemy.board, row_index, dice_num) |> Board.get_snapshot()
winner_status =
if Board.finished?(player_snapshot.board) do
cond do
player_snapshot.total > enemy_snapshot.total -> :player
player_snapshot.total < enemy_snapshot.total -> :enemy
player_snapshot.total == enemy_snapshot.total -> :draw
end
end
{player_snapshot, enemy_snapshot, winner_status}
end
defp initialize_state(state, match_id, matchmaking_ref) do
datetime = DateTime.now!("Etc/UTC") |> DateTime.to_unix(:millisecond)
put_state(state,
id: match_id,
matchmaking_ref: matchmaking_ref,
created_at: datetime,
status: :waiting_players
)
end
defp value_broadcast(value, event, state) do
Logger.info("Game event (#{inspect(state.id)}) sent #{inspect(event)}", label: "#{__MODULE__}")
Value.broadcast(
value,
Broadcast.to("match:#{state.id}", %MatchEvent{
event: Atom.to_string(event),
state: state
})
)
end
end