diff options
author | Logan Hunt <loganhunt@simponic.xyz> | 2023-01-17 14:00:18 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-17 14:00:18 -0700 |
commit | bdf99b4ee989df1813745e1dfd2983689b09ca85 (patch) | |
tree | f2c1c52cc2d51019b742800c24d5761f32495b95 /lib/chessh/ssh/client/client.ex | |
parent | 53be77e2c57bac6b861d7c3d1d2d6355816a823b (diff) | |
download | chessh-bdf99b4ee989df1813745e1dfd2983689b09ca85.tar.gz chessh-bdf99b4ee989df1813745e1dfd2983689b09ca85.zip |
Persistent game (#5)
* Remove unnecessary server in board
* Initial persistent games
* Remove done chessh README stuff, warning issue
* Show current players and move
* Add promotion
* Merge default changeset on all status
Diffstat (limited to 'lib/chessh/ssh/client/client.ex')
-rw-r--r-- | lib/chessh/ssh/client/client.ex | 43 |
1 files changed, 35 insertions, 8 deletions
diff --git a/lib/chessh/ssh/client/client.ex b/lib/chessh/ssh/client/client.ex index da9779b..72bbb66 100644 --- a/lib/chessh/ssh/client/client.ex +++ b/lib/chessh/ssh/client/client.ex @@ -24,8 +24,13 @@ defmodule Chessh.SSH.Client do end @impl true - def init([%State{} = state]) do - send(self(), {:set_screen_process, Chessh.SSH.Client.Menu, %Chessh.SSH.Client.Menu.State{}}) + def init([%State{player_session: player_session} = state]) do + send( + self(), + {:set_screen_process, Chessh.SSH.Client.Menu, + %Chessh.SSH.Client.Menu.State{player_session: player_session}} + ) + {:ok, state} end @@ -57,6 +62,11 @@ defmodule Chessh.SSH.Client do end @impl true + def handle_info({:go_back_one_screen, previous_state}, %State{} = state) do + {:noreply, go_back_one_screen(state, previous_state)} + end + + @impl true def handle_info(:quit, %State{} = state) do {:stop, :normal, state} end @@ -87,8 +97,7 @@ defmodule Chessh.SSH.Client do %State{ width: width, height: height, - screen_pid: screen_pid, - screen_state_initials: [_ | rest_initial] + screen_pid: screen_pid } = state ) do case keymap(data) do @@ -96,10 +105,7 @@ defmodule Chessh.SSH.Client do {:stop, :normal, state} :previous_screen -> - [{prev_module, prev_state_initial} | _] = rest_initial - send(self(), {:set_screen_process, prev_module, prev_state_initial}) - - {:noreply, %State{state | screen_state_initials: rest_initial}} + {:noreply, go_back_one_screen(state)} action -> send(screen_pid, {:input, width, height, action}) @@ -181,4 +187,25 @@ defmodule Chessh.SSH.Client do send(screen_pid, {:render, width, height}) end end + + defp go_back_one_screen( + %State{ + screen_state_initials: [_ | rest_initial] + } = state, + previous_state + ) do + [{prev_module, prev_state_initial} | _] = rest_initial + + send( + self(), + {:set_screen_process, prev_module, + if(is_nil(previous_state), do: prev_state_initial, else: previous_state)} + ) + + %State{state | screen_state_initials: rest_initial} + end + + defp go_back_one_screen(%State{} = state) do + go_back_one_screen(state, nil) + end end |