I'm creating a card game in Phoenix with 2 players using Phoenix channels and GenServer. This is what my Game Struct looks like:
schema "games" do
field :winner, :integer
field :player_1, :id, default: nil
field :player_2, :id, default: nil
field :status, :string
## VIRTUAL FIELDS ##
field :player_1_hand, :map, virtual: :true
field :player_2_hand, :map, virtual: :true
timestamps()
end
So, as you can see I'll handle the players hands with virtual fields and then persist the players and the winner to the database.
I have a Lobby channel set up now where players can chat. I have a game channel with no functionality. How do I allow players to invite each other to play games, accept or decline, and then put both players into a game together?
This is what I have so far in my lobby channel (regarding games):
def handle_in("game_invite", %{"username" => username}, socket) do
data = %{"username" => username, "sender" => socket.assigns.current_player.username }
broadcast! socket, "game_invite", data
{:noreply, socket}
end
intercept ["game_invite"]
def handle_out("game_invite", %{"username" => username, "sender" => sender}, socket) do
if socket.assigns.current_player.username == username do
push socket, "game_invite", %{ username: sender}
end
{:noreply, socket}
end
I don't know if I'm even asking the right questions. I'm trying to generate a new game ID (from postgres) put two player ID's in the game, and then let the GameServer use GenServer to manage the player hands.