summaryrefslogtreecommitdiff
path: root/lib/chessh/ssh/client/client.ex
blob: f160ba5dd551f4e55f9d137737e4d7636774fcf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
defmodule Chessh.SSH.Client do
  alias IO.ANSI
  require Logger

  use GenServer

  @clear_codes [
    ANSI.clear(),
    ANSI.home()
  ]

  @min_terminal_width 64
  @min_terminal_height 38
  @max_terminal_width 220
  @max_terminal_height 100

  defmodule State do
    defstruct tui_pid: nil,
              width: 0,
              height: 0,
              player_session: nil,
              screen_processes: []
  end

  @impl true
  def init([%State{} = state]) do
    send(self(), {:set_screen_process, Chessh.SSH.Client.Menu, %Chessh.SSH.Client.Menu.State{}})
    {:ok, state}
  end

  @impl true
  def handle_info(
        {:set_screen_process, module, screen_state},
        %State{width: width, height: height, screen_processes: screen_processes} = state
      ) do
    {:ok, new_screen_pid} = GenServer.start_link(module, [%{screen_state | client_pid: self()}])
    send(new_screen_pid, {:render, width, height})

    {:noreply, %{state | screen_processes: [new_screen_pid | screen_processes]}}
  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{width: width, height: height, screen_processes: [screen_pid | rest_processes]} =
          state
      ) do
    case keymap(data) do
      :quit ->
        {:stop, :normal, state}

      :previous_screen ->
        Logger.debug(inspect(rest_processes))
        Process.exit(screen_pid, :kill)

        {:noreply, %State{state | screen_processes: rest_processes}}

      action ->
        send(screen_pid, {:input, width, height, action})
        {:noreply, state}
    end
  end

  def handle(
        :refresh,
        %State{} = state
      ) do
    render(state)
    {:noreply, state}
  end

  def handle(
        {:send_to_ssh, data},
        %State{width: width, height: height, tui_pid: tui_pid} = state
      ) do
    case get_terminal_dim_msg(width, height) do
      {true, msg} -> send(tui_pid, {:send_data, msg})
      {false, _} -> send(tui_pid, {:send_data, data})
    end

    {:noreply, state}
  end

  def handle(
        {:resize, {width, height}},
        %State{tui_pid: tui_pid, screen_processes: [screen_pid | _]} = state
      ) do
    case get_terminal_dim_msg(width, height) do
      {true, msg} -> send(tui_pid, {:send_data, msg})
      {false, _} -> send(screen_pid, {:render, width, height})
    end

    {:noreply, %State{state | width: width, height: height}}
  end

  def keymap(key) do
    case key do
      # Exit keys - C-c and C-d
      <<3>> -> :quit
      <<4>> -> :quit
      # C-b
      <<2>> -> :previous_screen
      # Arrow keys
      "\e[A" -> :up
      "\e[B" -> :down
      "\e[D" -> :left
      "\e[C" -> :right
      "\r" -> :return
      x -> x
    end
  end

  defp get_terminal_dim_msg(width, height) do
    case {height > @max_terminal_height, height < @min_terminal_height,
          width > @max_terminal_width, width < @min_terminal_width} do
      {true, _, _, _} -> {true, @clear_codes ++ ["The terminal height is too large."]}
      {_, true, _, _} -> {true, @clear_codes ++ ["The terminal height is too small."]}
      {_, _, true, _} -> {true, @clear_codes ++ ["The terminal width is too large"]}
      {_, _, _, true} -> {true, @clear_codes ++ ["The terminal width is too small."]}
      {false, false, false, false} -> {false, nil}
    end
  end

  defp render(%State{
         tui_pid: tui_pid,
         width: width,
         height: height,
         screen_processes: [screen_pid | _]
       }) do
    {out_of_range, msg} = get_terminal_dim_msg(width, height)

    if out_of_range && msg do
      send(tui_pid, {:send_data, msg})
    else
      send(screen_pid, {:render, width, height})
    end
  end
end