Skip to content

Commit

Permalink
Websocket mock client and more
Browse files Browse the repository at this point in the history
  • Loading branch information
AminArria committed Jul 17, 2024
1 parent 358da0b commit 322550c
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 5 deletions.
2 changes: 1 addition & 1 deletion apps/arena/lib/arena/game_bounties_fetcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Arena.GameBountiesFetcher do
@moduledoc false
use GenServer

@update_interval_ms 30_000
@update_interval_ms 300_000

# API
def start_link(_) do
Expand Down
44 changes: 44 additions & 0 deletions apps/game_client/lib/game_client/game_test_handler.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
defmodule GameTestHandler do
@moduledoc """
GameClient socket handler.
It handles the communication with the server.
"""

use WebSockex, restart: :transient
require Logger
alias Arena.Serialization

def start_link(gateway_jwt, player_id, game_id) do
ws_url = ws_url(gateway_jwt, player_id, game_id)

WebSockex.start_link(ws_url, __MODULE__, %{})
end

def handle_connect(_, state) do
Logger.info("Game connected")
{:ok, state}
end

def handle_frame({:binary, event}, state) do
decoded = Serialization.GameEvent.decode(event)
case decoded.event do
{:update, game_update} ->
Logger.info("Received update", %{server_timestamp: game_update.server_timestamp, status: game_update.status})
{:ok, state}
_ ->
{:ok, state}
end
end

def terminate(close_reason, _state) do
Logger.info("Game socket closed: #{inspect(close_reason)}")
:ok
end

defp ws_url(gateway_jwt, player_id, game_id) do
# FIX ME Remove hardcoded host
# host = "localhost:4000"
host = "arena-brazil-testing.curseofmirra.com"
"wss://#{host}/play/#{game_id}/#{player_id}?gateway_jwt=#{gateway_jwt}"
end
end
44 changes: 44 additions & 0 deletions apps/game_client/lib/game_client/test_handler.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
defmodule TestHandler do
@moduledoc """
GameClient socket handler.
It handles the communication with the server.
"""

use WebSockex, restart: :transient
require Logger
alias Arena.Serialization

@gateway_jwt "eyJhbGciOiJFZDI1NTE5IiwidHlwIjoiSldUIn0.eyJkZXYiOiJhaUJWOTN1NkhBdmpJV3h2R2plQWlrTXVuM0xURVBwcmhMelFMb1VOR0ljPSIsImV4cCI6MTcyMTIxNDA0OSwiaWF0IjoxNzIxMjA2ODQ5LCJqdGkiOiIydmhmOTBsc3FoNmxhNzN1YXMwMDFnN2giLCJuYmYiOjE3MjEyMDY4NDksInN1YiI6IjU1MWJhNzMxLWU2YmUtNDgzNi1hNjk2LTQ5NzE1MDQyMDViMyJ9.dvm3I5PlEiEVIQCtn0C9q1WZVKWpugkuhoPexoNMKeaiGizrAGFP38pVJ-EzgMcLc7HihDUxJVZkYSUGT2xlAw"
@player_id "551ba731-e6be-4836-a696-4971504205b3"

def start_link() do
ws_url = ws_url(@gateway_jwt, @player_id)

WebSockex.start_link(ws_url, __MODULE__, %{})
end

def handle_frame({:binary, event}, state) do
decoded = Serialization.LobbyEvent.decode(event)
case decoded.event do
{:joined, _} ->
Logger.info("Joined lobby")
{:ok, state}
{:game, %{game_id: game_id}} ->
Logger.info("Moving to game")
GameTestHandler.start_link(@gateway_jwt, @player_id, game_id)
{:ok, state}
end
end

def terminate(close_reason, _state) do
Logger.info("Closing matchmaking socket: #{inspect(close_reason)}")
:ok
end

defp ws_url(gateway_jwt, player_id) do
# FIX ME Remove hardcoded host
# host = "localhost:4000"
host = "arena-brazil-testing.curseofmirra.com"
"wss://#{host}/join/#{player_id}/muflus/amin?gateway_jwt=#{gateway_jwt}"
end
end
3 changes: 2 additions & 1 deletion apps/game_client/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ defmodule GameClient.MixProject do
{:credo, "~> 1.7", only: [:dev, :test], runtime: false},
{:websockex, "~> 0.4.3"},
{:exbase58, "~> 1.0.2"},
{:ueberauth_google, "~> 0.10"}
{:ueberauth_google, "~> 0.10"},
{:arena, in_umbrella: true}
]
end

Expand Down
4 changes: 2 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import Config

# Configures Elixir's Logger
config :logger, :console,
format: "$time $metadata[$level] $message\n",
metadata: [:request_id]
format: "$time [$level] $message $metadata\n",
metadata: [:status, :server_timestamp, :request_id]

# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason
Expand Down
5 changes: 4 additions & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import Config
##########################

# Do not include metadata nor timestamps in development logs
config :logger, :console, format: "[$level] $message\n"
# config :logger, :console, format: "[$level] $message\n"
config :logger, :console,
format: "$time [$level] $message $metadata\n",
metadata: [:status, :server_timestamp]

# Set a higher stacktrace during development. Avoid configuring such
# in production as building large stacktraces may be expensive.
Expand Down

0 comments on commit 322550c

Please sign in to comment.