From 25c050350440df93dbdf8302b0530fc4561ab23f Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Wed, 17 Jan 2024 10:58:37 +0000 Subject: [PATCH] more worke on the server --- lib/server.ex | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/server.ex b/lib/server.ex index adc8e58..f65fc4e 100644 --- a/lib/server.ex +++ b/lib/server.ex @@ -117,7 +117,7 @@ defmodule Server do get_game_state(state, game_id, pid_to_inform) {:make_move, game_id, move, pid_to_inform} -> - state = try_to_play_checks(state, game_id, move, pid_to_inform) + try_to_play_checks(state, game_id, move, pid_to_inform) end @@ -155,10 +155,12 @@ defmodule Server do def try_to_play(state, game_id, move, pid_to_inform) do name = state.name - try_propose {:make_move, game_id, name, move} + # TODO create new hand + new_hand = 2 + try_propose {:make_move, game_id, name, move, new_hand} do - {:decision, {:make_move, ^game_id, ^name, ^move}} -> - state = apply_game(state, {:make_move, game_id, name, move}) + {:decision, {:make_move, ^game_id, ^name, ^move, ^new_hand}} -> + state = apply_game(state, {:make_move, game_id, name, move, new_hand}) if is_finished(state, game_id) do {_, score} = state.games[game_id] @@ -170,8 +172,8 @@ defmodule Server do set_modifed(state, game_id) - {:decision, {:make_move, ^game_id, new_name, new_move}} -> - state = apply_game(state, {:make_move, game_id, new_name, new_move}) + {:decision, {:make_move, ^game_id, new_name, new_move, new_new_hand}} -> + state = apply_game(state, {:make_move, game_id, new_name, new_move, new_new_hand}) if is_finished(state, game_id) do {_, score} = state.games[game_id] @@ -258,8 +260,28 @@ defmodule Server do # Apply Game States # - def apply_game(state, {:make_move, game_id, new_name, new_move}) do - raise :todo + def simplify_game_state(game_state) do + # TODO actualy do this + game_state + end + + def apply_game(state, {:make_move, game_id, player_name, pos_move, new_hand}) do + game = state.games[game_id] + case game do + {:finished, _} -> + raise :game_already_finished + :not_playing_in_game -> + %{state | instance: state.instance + 1 } + game -> + game_state = game.game_state + {b, e} = Enum.split(game_state, pos_move) + game_state = b ++ [ game.hand[player_name] ] ++ e + game_state = simplefy_game_state(game_state) + hand = Map.put(game.hand, player_name, new_hand) + game = %{game| hand: hand, game_state: game_state } + # TODO decide if it's ending state + %{state| games: Map.put(state.games, game_id, game)} + end end def apply_game(state, {:start_game, game_id, participants, new_game_state, hand}) do