silently

silently

Simple game server with channels

Hello everyone,
I am working on a little 2-player game to help me learn elixir/phoenix. Before starting to code (and re-read some documentation), I would be glad to have some feedback about this scenario:

1- Players join their own “user:#user_id” private topic to receive/send private events (see why below). On join/2("user:" <> user_id,...):

  • check the user_id maps the one assigned on the socket during initial connection

2- Then they join a “match:lobby” channel where they can meet. On join/2("match:lobby",...):

  • enable Presence in a handle_info/2 callback

3- Players invite each other to start a match: client 1 sends an invitation event to client 2. On handle_in/3("invite", %{"dest_id" => dest_id}, socket):

  • check dest_id player is still available
  • then send invitation through the “user:#dest_id” topic

4- Let’s say player 2 accepts the challenge. On handle_in/3("accept", %{"match_id" => match_id}, socket):

  • push an event for both players so that they ask back to leave “match:lobby” (till the end of the game when they will join again)
  • create a Game (module agent) that implements the gameplay and is added to a GameSupervisor (through start_child/2?).

5- Depending on user input and game events, the Game agent dispatch events to users until the match ends.

  • here a specific channel with a topic “match:#match_id” could be used, or we could rely on the user channels (“user:#user_id” topics) previously created to send invitations
  • a bit off-topic: maybe I need to persist the Game state to the DB, and retrieve it if the agent crashes and is respawned

Regarding the communication between the Game agent and clients:

  • Game agent->players: the Game agent holds the relevant topics in its state (let it be “user:#user_id” or “match:#match_id”) and can send event to players thanks to the endpoint’s broadcast/3
  • Players->Game agent: on handle_in/3("game_event",...): we need to find the Game agent corresponding to the given user_id or match_id. I guess this is when Registry is useful, but for the moment I feel that implementing my own GameIndex agent, mapping user_id keys to process names would be simpler. Or I could use the socket assigns to store the Game agent process name. This is where I would really appreciate some feedback, I am a bit lost here.

6- When the game ends:

  • the result of the game is persisted to the DB
  • the Game agent is terminated and specific properties are reset (for instance if the Game agent process name was assigned to the socket)
  • players get back to “match:lobby”

In case of a client side reboot (a page refresh for instance) and if the user is currently in a game, we could jump directly from step 1 to 5, using the GameIndex.

Do you see any drawback/oversight in this scenario? Many thanks, Guillaume

Most Liked

amnu3387

amnu3387

Regarding the flow I think your approach is fine.

If you’re going to store things in the DB anyway then sure, use the DB to store intermediate “snapshots” of the game in case of failure - if you’re not going to store (things like results, etc) then a simple ETS table would suffice.

Regarding Game Agents and identification - erlang does provide a global namespace where you can name processes by term - it’s available through elixir at least on genservers and allows you to find processes by assigned ID. I’ve used that in GenServers to find the matching server - the genserver is registered with the ID, after joining the game channel, the ID is assigned to the socket, on msg in, it tries to find the genserver with that ID, if it finds it, calls/casts on the return pid to get the game state, if it doesn’t find an existing genserver with that ID it creates one loading the last saved state from the DB and in case there isn’t one yet, it creates one new game state.

You can use name: {:global, {:game, "string_or_whatever_id"}} to register the process. And then use GenServer.whereis({:global, {:game, "123"}}) which will give you back nil or a pid. MAke sure the terms are the same type, as 123 is diff. than “123” for registration purposes.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics Top

New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

We're in Beta

About us Mission Statement