Hi,
I am new to GenServer and I am having a hard time understanding it. I am trying to create a war card game. I am using GenServer to set up a server, client’s API’s and server callbacks. Functions I want to create are:
- starting a game
- adding a deck
- shuffling a deck
- placing a bet
- drawing players card
- drawing computers card
Up until now I have:
defmodule WarGame.Boundary.GameManager do
use GenServer
alias WarGame.Core.Game
alias WarGame.Core.Deck
# Client API
def start_link(options \\ []) do
GenServer.start_link(__MODULE__, %{}, options)
end
def start_game(manager \\ __MODULE__, game_fields) do
GenServer.call(manager, {:start_game, game_fields})
end
def add_deck(manager \\__MODULE__) do
GenServer.call(manager, {:add_deck})
end
# Server Callbackszz
def init(war_game) do
{:ok, war_game}
end
def handle_call({:add_deck}, _from. war_game) do
deck = Deck.new_deck()
game = %Game{}
new_war_game = Map.put(war_game, game.deck, deck)
{:reply, :ok, new_war_game}
end
def handle_call({:start_game, game_fields}, _from, war_game) do
game = Game.new(game_fields)
new_war_game = Map.put(war_game, game.user_cards, game)
{:reply, :ok, new_war_game}
end
end
I managed to start a server and start a game. However, I don’t know how to add fields etc, so my add_deck
call doesn’t work.
This is my WarGame.Core.Deck
defmodule WarGame.Core.Deck do
defmodule Card do
defstruct value: [],
suit: []
end
def new_deck do
for suit <- suits(), value <- values() do
%Card{value: value, suit: suit}
end
end
def shuffle(new_deck) do
new_deck
|> Enum.shuffle()
end
def deal_a_card(cards) do
[card | new_deck] = shuffle(cards)
[{to_tuple(card), new_deck}]
end
defp to_tuple(
%WarGame.Core.Deck.Card{value: value, suit: suit}
), do: {value, suit}
defp values(), do: Enum.to_list(2..14)
defp suits(), do: [:hearts, :diamonds, :spades, :clubs]
end
My WarGame.Core.Game
defmodule WarGame.Core.Game do
defstruct user_cards: [],
croupier_cards: [],
user_bets: [],
deck: %{},
used: []
def new(fields) do
struct!(__MODULE__, fields)
end
end
I will be thankful for explanation how to update, change and show fields using GenServer in simple words. I did read documentations but as you can see it didn’t help. I did try Map.update but I am not getting it
Best wishes to all!