Created
August 14, 2020 22:31
-
-
Save gustavonmartins/7d5973d6cb282938885751b378a5904c to your computer and use it in GitHub Desktop.
GameChannel
This file contains hidden or 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 CamelupWeb.GameChannel do | |
use CamelupWeb, :channel | |
alias CamelUp.{GameSaloon, GameTable, GameTablePrivateView} | |
def join("games:" <> game_id, %{"name" => uname}, socket) do | |
game_id = game_id |> String.to_integer() | |
socket = assign(socket, :game_user, %{:uuid => socket.assigns.user_id, :char => uname}) | |
send(self(), :after_join) | |
{:ok, assign(socket, :game_id, game_id)} | |
end | |
def handle_info(:after_join, socket) do | |
gs_base = | |
case :ets.lookup(:game_cache, :main) do | |
[{:main, %GameSaloon{} = gs}] -> gs | |
[] -> %GameSaloon{} | |
end | |
gs = | |
gs_base | |
|> GameSaloon.be_joined_by_user(socket.assigns.game_user, socket.assigns.game_id) | |
{:ok, %GameTable{} = it} = | |
gs | |
|> GameSaloon.get_table_by_id(socket.assigns.game_id) | |
:ets.insert(:game_cache, {:main, gs}) | |
it2 = it |> GameTablePrivateView.to_view(socket.assigns.game_user.char) | |
# push(socket, "broadcast_game_table", it2) | |
broadcast!(socket, "broadcast_game_table", it2) | |
socket |> IO.inspect(label: "socket is: ") | |
{:noreply, socket} | |
end | |
def terminate(_, socket) do | |
gs_base = | |
case :ets.lookup(:game_cache, :main) do | |
[{:main, %GameSaloon{} = gs}] -> gs | |
end | |
gs = | |
gs_base | |
|> GameSaloon.be_left_by_user(socket.assigns.game_user) | |
:ets.insert(:game_cache, {:main, gs}) | |
:ok | |
end | |
def handle_in("decision", %{"first" => fst, "second" => snd, "third" => trd}, socket) do | |
action = | |
case fst do | |
"warmup" -> :warmup | |
"start" -> :start | |
"shake" -> :shake | |
"get_leg_money" -> :get_leg_money | |
"bet_on_leg" -> :bet_on_leg | |
"get_final_winner_money" -> :get_final_winner_money | |
"get_final_looser_money" -> :get_final_looser_money | |
end | |
[{:main, %GameSaloon{} = gs}] = :ets.lookup(:game_cache, :main) | |
gs = gs |> GameSaloon.user_action(socket.assigns.game_user, action) | |
table_id = gs |> GameSaloon.get_user_table_id(socket.assigns.game_user.uuid) | |
{:ok, gt} = gs |> GameSaloon.get_table_by_id(table_id) | |
gt2 = gt |> GameTablePrivateView.to_view(socket.assigns.game_user.char) | |
broadcast!(socket, "broadcast_game_table", gt2) | |
:ets.insert(:game_cache, {:main, gs}) | |
{:reply, {:ok, %{}}, socket} | |
end | |
end |
This file contains hidden or 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 CamelupWeb.RoomChannelTest do | |
use CamelupWeb.ChannelCase, async: true | |
alias CamelupWeb.UserSocket | |
alias CamelupWeb.GameChannel | |
setup do | |
rand_int = :rand.uniform(1_000_000) | |
{:ok, _, socket} = | |
UserSocket | |
|> socket("user_id", %{user_id: 2323}) | |
|> subscribe_and_join(GameChannel, "games:" <> Integer.to_string(rand_int), %{ | |
name: "Gustavo" | |
}) | |
%{socket: socket} | |
end | |
test "Sending decisions", %{socket: socket} do | |
ref = | |
socket | |
|> push("decision", %{first: "warmup", second: nil, third: nil}) | |
assert_reply ref, :ok | |
assert_broadcast("broadcast_game_table", %CamelUp.GameTablePrivateView{state: :q1}) | |
end | |
end |
This file contains hidden or 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
1) test Sending decisions (CamelupWeb.RoomChannelTest) | |
test/camelup_web/channels/room_channel_test.exs:19 | |
Assertion failed, no matching message after 100ms | |
Showing 4 of 4 messages in the mailbox | |
code: assert_receive %Phoenix.Socket.Broadcast{event: "broadcast_game_table", payload: %CamelUp.GameTablePrivateView{state: :q1}} | |
mailbox: | |
pattern: %Phoenix.Socket.Broadcast{event: "broadcast_game_table", payload: %CamelUp.GameTablePrivateView{state: :q1}} | |
value: %Phoenix.Socket.Broadcast{ | |
event: "broadcast_game_table", | |
payload: %CamelUp.GameTablePrivateView{ | |
state: :q0, | |
avaiableLegBets: [%{"color" => :black, "value" => 5}, %{"color" => :blue, "value" => 5}, %{"color" => :green, "value" => 5}, %{"color" => :orange, "value" => 5}, %{"color" => :red, "value" => 5}], | |
circuit: [%{"items" => "black, blue, green, orange, red", "position" => "0"}], | |
personalItems: %{finalLegBets: [:black, :blue, :green, :orange, :red], tiles: ["Oasis", "Mirage"]}, | |
playerStatuses: [%{:name => "Gustavo", "bets" => [], "money" => 0}], | |
previousDices: [] | |
}, | |
topic: "games:213763" | |
} | |
pattern: %Phoenix.Socket.Broadcast{event: "broadcast_game_table", payload: %CamelUp.GameTablePrivateView{state: :q1}} | |
value: %Phoenix.Socket.Message{ | |
event: "broadcast_game_table", | |
payload: %CamelUp.GameTablePrivateView{ | |
state: :q0, | |
avaiableLegBets: [%{"color" => :black, "value" => 5}, %{"color" => :blue, "value" => 5}, %{"color" => :green, "value" => 5}, %{"color" => :orange, "value" => 5}, %{"color" => :red, "value" => 5}], | |
circuit: [%{"items" => "black, blue, green, orange, red", "position" => "0"}], | |
personalItems: %{finalLegBets: [:black, :blue, :green, :orange, :red], tiles: ["Oasis", "Mirage"]}, | |
playerStatuses: [%{:name => "Gustavo", "bets" => [], "money" => 0}], | |
previousDices: [] | |
}, | |
join_ref: nil, | |
ref: nil, | |
topic: "games:213763" | |
} | |
pattern: %Phoenix.Socket.Broadcast{event: "broadcast_game_table", payload: %CamelUp.GameTablePrivateView{state: :q1}} | |
value: %Phoenix.Socket.Broadcast{ | |
event: "broadcast_game_table", | |
payload: %CamelUp.GameTablePrivateView{ | |
state: :q0, | |
avaiableLegBets: [%{"color" => :black, "value" => 5}, %{"color" => :blue, "value" => 5}, %{"color" => :green, "value" => 5}, %{"color" => :orange, "value" => 5}, %{"color" => :red, "value" => 5}], | |
circuit: [%{"items" => "black", "position" => "1"}, %{"items" => "blue, green, orange, red", "position" => "0"}], | |
personalItems: %{finalLegBets: [:black, :blue, :green, :orange, :red], tiles: ["Oasis", "Mirage"]}, | |
playerStatuses: [%{:name => "Gustavo", "bets" => [], "money" => 0}], | |
previousDices: [:black] | |
}, | |
topic: "games:213763" | |
} | |
pattern: %Phoenix.Socket.Broadcast{event: "broadcast_game_table", payload: %CamelUp.GameTablePrivateView{state: :q1}} | |
value: %Phoenix.Socket.Message{ | |
event: "broadcast_game_table", | |
payload: %CamelUp.GameTablePrivateView{ | |
state: :q0, | |
avaiableLegBets: [%{"color" => :black, "value" => 5}, %{"color" => :blue, "value" => 5}, %{"color" => :green, "value" => 5}, %{"color" => :orange, "value" => 5}, %{"color" => :red, "value" => 5}], | |
circuit: [%{"items" => "black", "position" => "1"}, %{"items" => "blue, green, orange, red", "position" => "0"}], | |
personalItems: %{finalLegBets: [:black, :blue, :green, :orange, :red], tiles: ["Oasis", "Mirage"]}, | |
playerStatuses: [%{:name => "Gustavo", "bets" => [], "money" => 0}], | |
previousDices: [:black] | |
}, | |
join_ref: nil, | |
ref: nil, | |
topic: "games:213763" | |
} | |
stacktrace: | |
test/camelup_web/channels/room_channel_test.exs:25: (test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment