diff options
| author | Logan Hunt <loganhunt@simponic.xyz> | 2023-01-13 17:02:31 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-13 17:02:31 -0700 |
| commit | b1b62f154ab5f74a9217dbf5a6422640ac929df3 (patch) | |
| tree | 6fa3b6e8d7e9219decda28948bf3f02c3cf2dab2 /lib/chessh/ssh/client.ex | |
| parent | fdc22875284b78f9fb98993cbe44ce893c0de413 (diff) | |
| parent | 3a6c603b0b12964d8a04ae4f78fb61bc094adf12 (diff) | |
| download | chessh-b1b62f154ab5f74a9217dbf5a6422640ac929df3.tar.gz chessh-b1b62f154ab5f74a9217dbf5a6422640ac929df3.zip | |
Merge pull request #3 from Simponic/draw_board
Draw board
Diffstat (limited to 'lib/chessh/ssh/client.ex')
| -rw-r--r-- | lib/chessh/ssh/client.ex | 123 |
1 files changed, 0 insertions, 123 deletions
diff --git a/lib/chessh/ssh/client.ex b/lib/chessh/ssh/client.ex deleted file mode 100644 index 4ed2fd1..0000000 --- a/lib/chessh/ssh/client.ex +++ /dev/null @@ -1,123 +0,0 @@ -defmodule Chessh.SSH.Client do - alias IO.ANSI - alias Chessh.SSH.Client.Menu - require Logger - - use GenServer - - @clear_codes [ - ANSI.clear(), - ANSI.reset(), - ANSI.home() - ] - - @min_terminal_width 64 - @min_terminal_height 31 - @max_terminal_width 255 - @max_terminal_height 127 - - @terminal_bad_dim_msg [ - @clear_codes | "The dimensions of your terminal are not within in the valid range" - ] - - defmodule State do - defstruct tui_pid: nil, - width: 0, - height: 0, - player_session: nil, - buffer: [], - state_stack: [{Menu, %Menu.State{}}] - end - - @impl true - def init([%State{tui_pid: tui_pid} = state]) do - send(tui_pid, {:send_data, render(state)}) - {:ok, state} - end - - @impl true - def handle_info(:quit, %State{} = state) do - {:stop, :normal, state} - end - - @impl true - def handle_info(msg, state) do - [burst_ms, burst_rate] = - Application.get_env(:chessh, RateLimits) - |> Keyword.take([:player_session_message_burst_ms, :player_session_message_burst_rate]) - |> Keyword.values() - - case Hammer.check_rate_inc( - "player-session-#{state.player_session.id}-burst-message-rate", - burst_ms, - burst_rate, - 1 - ) do - {:allow, _count} -> - handle(msg, state) - - {:deny, _limit} -> - {:noreply, state} - end - end - - def handle( - {:data, data}, - %State{tui_pid: tui_pid, state_stack: [{module, _screen_state} | _tail]} = state - ) do - action = keymap(data) - - if action == :quit do - {:stop, :normal, state} - else - new_state = module.handle_input(action, state) - - send(tui_pid, {:send_data, render(new_state)}) - - {:noreply, new_state} - end - end - - def handle({:resize, {width, height}}, %State{tui_pid: tui_pid} = state) do - new_state = %State{state | width: width, height: height} - - if height <= @max_terminal_height || width <= @max_terminal_width do - send(tui_pid, {:send_data, render(new_state)}) - end - - {:noreply, new_state} - end - - def keymap(key) do - case key do - # Exit keys - C-c and C-d - <<3>> -> :quit - <<4>> -> :quit - # Arrow keys - "\e[A" -> :up - "\e[B" -> :down - "\e[D" -> :left - "\e[C" -> :right - "\r" -> :return - x -> x - end - end - - def terminal_size_allowed(width, height) do - Enum.member?(@min_terminal_width..@max_terminal_width, width) && - Enum.member?(@min_terminal_height..@max_terminal_height, height) - end - - def render( - %State{width: width, height: height, state_stack: [{module, _screen_state} | _]} = state - ) do - if terminal_size_allowed(width, height) do - [ - @clear_codes ++ - module.render(state) - ] - else - @terminal_bad_dim_msg - end - end -end |
