Background: Writing a card game simulator. Assume an initial state with 5 cards. The play_card function plays(discard) the first card from the list (state). A message is written to the shell and the state updated. A second call would then do the same and update the new state. I am getting an error on the line “{:reply, “#{cardplayedname}”, new_state}”, the return from the handle_call callback. What am I doing wrong? Any help is appreciated.
The error message is "no match of right hand side value: “ping1"”. That’s indicating a pattern matching error. Bit, I am unable to see why.
defmodule DBGame.Ping do
use GenServer
#client api
def start_link(opts \\ []) do
state = ["ping1","ping2","ping3","ping4","ping5"]
GenServer.start_link(__MODULE__, state, opts)
end
def play_card(pid, cardplayedindex) do
GenServer.call(pid, {:card, cardplayedindex})
end
#server callbacks
def init(:ok, state) do
{:ok, state}
end
def handle_call({:card, cardplayedindex}, _from, state) do
case(card_played(cardplayedindex, state)) do
{:ok, cardplayedname} ->
new_state = update_stats(state, cardplayedname)
{:reply, "#{cardplayedname}", new_state}
_ ->
{:reply, :error, state}
end
end
#helper functions
defp card_played(cardplayedindex, state) do
cardplayedname = Enum.at(state, cardplayedindex, "nocard")
IO.puts("#{cardplayedname} is played")
{:ok, cardplayedname}
end
defp update_stats(state, cardplayedname) do
new_state = List.delete(state, cardplayedname)
#new_stat = state
end
end






















