summaryrefslogtreecommitdiff
path: root/presentation/tic_tac_toe.exs
blob: e05bed46189d7714680c3becfd424d57add92fde (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
defmodule Generator do
  def gen_reference() do
    min = String.to_integer("100000", 36)
    max = String.to_integer("ZZZZZZ", 36)

    max
    |> Kernel.-(min)
    |> :rand.uniform()
    |> Kernel.+(min)
    |> Integer.to_string(36)
  end
end

defmodule TicTacToe.GameManager do
  use GenServer

  defmodule State do
    defstruct games: %{},
              joinable_games: [],
              player_games: %{}
  end

  def start_link(_) do
    GenServer.start_link(__MODULE__, %{
      pid: nil
    })
  end

  def init(_) do
    {:ok, %State{}}
  end

  defp create_board(), do: Enum.map(0..2, fn _ -> Enum.map(0..2, fn _ -> :empty end) end)

  defp create_game(game_id, player) do
    %{
      x: player,
      o: nil,
      board: create_board()
    }
  end

  def handle_info(
        {:join, %{client_pid: client_pid, username: username, player_id: connection_id} = player},
        %State{player_games: player_games, games: games, joinable_games: joinable_games} = state
      ) do
    if length(joinable_games) == 0 do
      game_id = Generator.gen_reference()
      send(client_pid, {:join_game, game_id})

      {:ok,
       %State{
         state
         | games: Map.put(games, game_id, create_game(game_id, player)),
           joinable_games: joinable_games ++ [game_id],
           player_games: Map.put(player_games, player_id, game_id)
       }}
    else
      [joining_game_id | rest] = joinable_games
      game = Map.get(games, joining_game_id)
      send(game.x.client_pid, :player_joined)
      send(client_pid, {:join_game, game_id})

      {:ok,
       %State{
         state
         | games: Map.put(games, game_id, %{game | o: player}),
           joinable_games: rest,
           connection_games: Map.put(player_games, connection_id, game_id)
       }}
    end
  end
end

defmodule TicTacToe.SSHDaemon do
  @port 4000
  @key_dir "/tmp/keys"
  use GenServer
  require Logger

  def start_link(_) do
    GenServer.start_link(__MODULE__, %{
      pid: nil
    })
  end

  def init(state) do
    send(self(), :start)

    {:ok, state}
  end

  def handle_info(:start, state) do
    game_manager_pid =
      case GenServer.start_link(TicTacToe.GameManager, [%{}]) do
        {:ok, game_manager_pid} ->
          game_manager_pid

        _ ->
          nil
      end

    case :ssh.daemon(
           @port,
           system_dir: @key_dir,
           ssh_cli:
             {TicTacToe.SSHListener,
              [
                %TicTacToe.SSHListener.State{
                  game_manager_pid: game_manager_pid
                }
              ]},
           disconnectfun: &on_disconnect/1,
           id_string: :random,
           parallel_login: true,
           max_sessions: 1_000,
           subsystems: [],
           no_auth_needed: true
         ) do
      {:ok, pid} ->
        Logger.info("SSH server started on port #{port}, on #{inspect(pid)}")

        Process.link(pid)

        {:noreply, %{state | pid: pid, game_manager_pid: game_manager_pid}, :hibernate}

      {:error, err} ->
        raise inspect(err)
    end

    {:noreply, state}
  end

  def handle_info(_, state), do: {:noreply, state}

  defp on_disconnect(_reason) do
    Logger.info("#{inspect(self())} disconnected")
  end
end

defmodule TicTacToe.SSHListener do
  alias Chessh.SSH.Client

  alias IO.ANSI

  require Logger

  @behaviour :ssh_server_channel
  @session_closed_message [
    ANSI.clear(),
    ["This session has been closed"]
  ]

  defmodule State do
    defstruct channel_id: nil,
              client_pid: nil,
              game_manager_pid: nil,
              connection_ref: nil
  end

  def init([%State{} = init_state]) do
    {:ok, init_state}
  end

  def handle_msg({:ssh_channel_up, channel_id, connection_ref}, %State{} = state) do
    Logger.debug("SSH channel up #{inspect(:ssh.connection_info(connection_ref))}")

    username =
      :ssh.connection_info(connection_ref)
      |> Keyword.fetch!(:user)
      |> String.Chars.to_string()

    {:ok,
     %State{
       state
       | channel_id: channel_id,
         connection_ref: connection_ref,
         player: %{
           id: Generator.gen_reference(),
           username: username
         }
     }}
  end

  def handle_msg(
        {:EXIT, client_pid, _reason},
        %State{client_pid: client_pid, channel_id: channel_id} = state
      ) do
    send(client_pid, :quit)
    {:stop, channel_id, state}
  end

  def handle_msg(
        {:send_data, data},
        %State{connection_ref: connection_ref, channel_id: channel_id} = state
      ) do
    :ssh_connection.send(connection_ref, channel_id, data)
    {:ok, state}
  end

  def handle_msg(
        :session_closed,
        %State{connection_ref: connection_ref, channel_id: channel_id} = state
      ) do
    :ssh_connection.send(connection_ref, channel_id, @session_closed_message)
    {:stop, channel_id, state}
  end

  def handle_msg(msg, term) do
    Logger.debug("Unknown msg #{inspect(msg)}, #{inspect(term)}")
  end

  def handle_ssh_msg(
        {:ssh_cm, _connection_handler, {:data, _channel_id, _type, data}},
        %State{client_pid: client_pid} = state
      ) do
    send(client_pid, {:data, data})
    {:ok, state}
  end

  def handle_ssh_msg(
        {:ssh_cm, connection_handler,
         {:pty, channel_id, want_reply?, {_term, _width, _height, _pixwidth, _pixheight, _opts}}},
        %State{} = state
      ) do
    Logger.debug("#{inspect(state.player_session)} has requested a PTY")
    :ssh_connection.reply_request(connection_handler, want_reply?, :success, channel_id)
    {:ok, state}
  end

  def handle_ssh_msg(
        {:ssh_cm, connection_handler, {:env, channel_id, want_reply?, var, value}},
        state
      ) do
    :ssh_connection.reply_request(connection_handler, want_reply?, :failure, channel_id)

    {:ok, state}
  end

  def handle_ssh_msg(
        {:ssh_cm, _connection_handler,
         {:window_change, _channel_id, _width, _height, _pixwidth, _pixheight}},
        %State{client_pid: client_pid} = state
      ) do
    {:ok, state}
  end

  def handle_ssh_msg(
        {:ssh_cm, connection_handler, {:shell, channel_id, want_reply?}},
        %State{player: player} = state
      ) do
    :ssh_connection.reply_request(connection_handler, want_reply?, :success, channel_id)

    {:ok, client_pid} =
      GenServer.start_link(Client, [
        %Client.State{
          tui_pid: self(),
          player: player
        }
      ])

    send(client_pid, :refresh)
    {:ok, %State{state | client_pid: client_pid}}
  end

  def handle_ssh_msg(
        msg,
        %State{channel_id: channel_id} = state
      ) do
    Logger.debug("UNKOWN MESSAGE #{inspect(msg)}")
    # {:stop, channel_id, state}
    {:ok, state}
  end

  def terminate(_reason, _state) do
    :ok
  end
end

defmodule TicTacToe.Client do
  alias IO.ANSI
  use GenServer

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

  defmodule State do
    defstruct tui_pid: nil,
              game_manager_pid: nil,
              player: %{},
              game_id: nil
  end

  @impl true
  def init([%State{game_manager_pid: game_manager_pid, player: player} = state]) do
    player = %{
      player
      | client_pid: self()
    }

    send(game_manager_pid, {:join, player})

    {:ok,
     %State{
       player: player
     }}
  end

  @impl true
  def handle_info(:quit, %State{} = state) do
    {:stop, :normal, state}
  end

  @impl true
  def handle_info({:join_game, game_id}, %State{} = state) do
    state = %State{state | game_id: game_id}
    render(state)
    {:stop, :normal, state}
  end

  def handle(
        {:data, data},
        %State{} = state
      ) do
    case keymap(data) do
      :quit ->
        {:stop, :normal, state}
    end
  end

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

  defp render(%State{
         tui_pid: tui_pid
       }) do
    send(tui_pid, {:send_data, ["Testing"]})
  end

  def keymap(key) do
    case key do
      # Exit keys - C-c and C-d
      <<3>> -> :quit
      <<4>> -> :quit
      x -> x
    end
  end
end