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/game/promotion.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/game/promotion.ex')
-rw-r--r-- | lib/chessh/ssh/client/game/promotion.ex | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/lib/chessh/ssh/client/game/promotion.ex b/lib/chessh/ssh/client/game/promotion.ex new file mode 100644 index 0000000..c4cece6 --- /dev/null +++ b/lib/chessh/ssh/client/game/promotion.ex @@ -0,0 +1,63 @@ +defmodule Chessh.SSH.Client.Game.PromotionScreen do + alias Chessh.Utils + alias Chessh.SSH.Client.Game + alias IO.ANSI + + defmodule State do + defstruct game_pid: nil, + client_pid: nil, + game_state: nil + end + + use Chessh.SSH.Client.Screen + + @promotion_screen Utils.clear_codes() ++ + [ + "Press the key associated to the piece you'd like to promote", + " 'q' - queen", + " 'r' - rook", + " 'n' - knight", + " 'b' - bishop" + ] + + def init([%State{} = state | _]) do + {:ok, state} + end + + def render(_, _, %State{client_pid: client_pid} = state) do + rendered = + Enum.flat_map( + Enum.zip(0..(length(@promotion_screen) - 1), @promotion_screen), + fn {i, promotion} -> + [ + ANSI.cursor(i, 0), + promotion + ] + end + ) ++ [ANSI.home()] + + send( + client_pid, + {:send_to_ssh, rendered} + ) + + state + end + + def input( + _, + _, + action, + %State{client_pid: client_pid, game_pid: game_pid, game_state: %Game.State{} = game_state} = + state + ) do + promotion = if Enum.member?(["q", "b", "n", "r"], action), do: action, else: nil + + if promotion do + send(client_pid, {:go_back_one_screen, game_state}) + send(game_pid, {:promotion, promotion}) + end + + state + end +end |