summaryrefslogtreecommitdiff
path: root/lib/chessh/ssh/client/game/game.ex
blob: 9dbde7fd24378283076774905894b3a0093b69be (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
356
357
358
359
360
361
362
363
defmodule Chessh.SSH.Client.Game do
  require Logger
  alias Chessh.{Game, Utils, Repo}
  alias Chessh.SSH.Client.Game.Renderer

  @default_fen "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"

  defmodule State do
    defstruct cursor: %{x: 7, y: 7},
              highlighted: %{},
              move_from: nil,
              game: nil,
              client_pid: nil,
              binbo_pid: nil,
              width: 0,
              height: 0,
              flipped: false,
              color: nil,
              player_session: nil
  end

  use Chessh.SSH.Client.Screen

  defp initialize_game(game_id, fen) do
    :syn.add_node_to_scopes([:games])
    :ok = :syn.join(:games, {:game, game_id}, self())

    :binbo.start()
    {:ok, binbo_pid} = :binbo.new_server()
    :binbo.new_game(binbo_pid, fen)

    binbo_pid
  end

  def init([
        %State{
          color: color,
          game: %Game{dark_player_id: dark_player_id, light_player_id: light_player_id},
          player_session: %{player_id: player_id}
        } = state
        | tail
      ])
      when is_nil(color) do
    {is_dark, is_light} = {player_id == dark_player_id, player_id == light_player_id}

    new_state =
      if is_dark || is_light do
        %State{state | color: if(is_light, do: :light, else: :dark)}
      else
        case {is_nil(dark_player_id), is_nil(light_player_id)} do
          {true, false} -> %State{state | color: :dark}
          {false, true} -> %State{state | color: :light}
          {_, _} -> %State{state | color: :light}
        end
      end

    init([new_state | tail])
  end

  def init([
        %State{
          player_session: player_session,
          color: color,
          client_pid: client_pid,
          game:
            %Game{
              id: game_id,
              fen: fen,
              dark_player_id: dark_player_id,
              light_player_id: light_player_id
            } = game
        } = state
        | _
      ]) do
    maybe_changeset =
      case color do
        :light ->
          if !light_player_id,
            do: Game.changeset(game, %{light_player_id: player_session.player_id})

        :dark ->
          if !dark_player_id,
            do: Game.changeset(game, %{dark_player_id: player_session.player_id})
      end

    {status, maybe_joined_game} =
      if maybe_changeset do
        maybe_changeset
        |> Repo.update()
      else
        {:undefined, nil}
      end

    if status == :ok && maybe_joined_game do
      :syn.publish(:games, {:game, game_id}, :player_joined)
    end

    binbo_pid = initialize_game(game_id, fen)

    new_game = Repo.get(Game, game_id) |> Repo.preload([:light_player, :dark_player])

    player_color =
      if(new_game.light_player_id == player_session.player_id, do: :light, else: :dark)

    send(client_pid, {:send_to_ssh, Utils.clear_codes()})

    {:ok,
     %State{
       state
       | binbo_pid: binbo_pid,
         color: player_color,
         game: new_game,
         flipped: player_color == :dark
     }}
  end

  def init([
        %State{player_session: player_session, color: color, client_pid: client_pid, game: nil} =
          state
        | _
      ]) do
    {:ok, %Game{id: game_id, fen: fen}} =
      Game.changeset(
        %Game{},
        Map.merge(
          if(color == :light,
            do: %{light_player_id: player_session.player_id},
            else: %{dark_player_id: player_session.player_id}
          ),
          %{
            fen: @default_fen
          }
        )
      )
      |> Repo.insert()

    binbo_pid = initialize_game(game_id, fen)
    send(client_pid, {:send_to_ssh, Utils.clear_codes()})

    {:ok,
     %State{
       state
       | game: Repo.get(Game, game_id) |> Repo.preload([:light_player, :dark_player]),
         binbo_pid: binbo_pid
     }}
  end

  def handle_info(
        {:new_move, move},
        %State{game: %Game{id: game_id}, client_pid: client_pid, binbo_pid: binbo_pid} = state
      ) do
    :binbo.move(binbo_pid, move)

    new_state = %State{
      state
      | game: Repo.get(Game, game_id) |> Repo.preload([:light_player, :dark_player])
    }

    send(client_pid, {:send_to_ssh, render_state(new_state)})

    {:noreply, new_state}
  end

  def handle_info(
        :player_joined,
        %State{client_pid: client_pid, game: %Game{id: game_id}} = state
      ) do
    game = Repo.get(Game, game_id) |> Repo.preload([:light_player, :dark_player])
    new_state = %State{state | game: game}
    send(client_pid, {:send_to_ssh, render_state(new_state)})
    {:noreply, new_state}
  end

  def input(
        width,
        height,
        action,
        %State{
          move_from: move_from,
          cursor: %{x: cursor_x, y: cursor_y} = cursor,
          client_pid: client_pid,
          flipped: flipped,
          binbo_pid: binbo_pid
        } = state
      ) do
    new_cursor =
      case action do
        :left -> %{y: cursor_y, x: Utils.wrap_around(cursor_x, -1, Renderer.chess_board_width())}
        :right -> %{y: cursor_y, x: Utils.wrap_around(cursor_x, 1, Renderer.chess_board_width())}
        :down -> %{y: Utils.wrap_around(cursor_y, 1, Renderer.chess_board_height()), x: cursor_x}
        :up -> %{y: Utils.wrap_around(cursor_y, -1, Renderer.chess_board_height()), x: cursor_x}
        _ -> cursor
      end

    {new_move_from, move_to} =
      if action == :return do
        coords = {new_cursor.y, new_cursor.x}

        case move_from do
          nil -> {coords, nil}
          _ -> {nil, coords}
        end
      else
        {move_from, nil}
      end

    new_state = %State{
      state
      | cursor: new_cursor,
        move_from: new_move_from,
        highlighted: %{
          {new_cursor.y, new_cursor.x} => Renderer.to_select_background(),
          new_move_from => Renderer.from_select_background()
        },
        width: width,
        height: height,
        flipped: if(action == "f", do: !flipped, else: flipped)
    }

    if move_from && move_to do
      maybe_flipped_from = if flipped, do: flip(move_from), else: move_from
      maybe_flipped_to = if flipped, do: flip(move_to), else: move_to

      piece_type =
        :binbo_position.get_piece(
          :binbo_board.notation_to_index(Renderer.to_chess_coord(maybe_flipped_from)),
          :binbo.game_state(binbo_pid)
        )

      promotion_possible =
        case piece_type do
          1 ->
            # Light pawn
            {y, _} = maybe_flipped_to
            y == 0

          17 ->
            # Dark pawn
            {y, _} = maybe_flipped_to
            y == Renderer.chess_board_height() - 1

          _ ->
            false
        end

      if promotion_possible do
        send(
          client_pid,
          {:set_screen_process, Chessh.SSH.Client.Game.PromotionScreen,
           %Chessh.SSH.Client.Game.PromotionScreen.State{
             client_pid: client_pid,
             game_pid: self(),
             game_state: new_state
           }}
        )

        receive do
          {:promotion, promotion} ->
            attempt_move(move_from, move_to, state, promotion)
        end
      else
        attempt_move(move_from, move_to, state)
      end
    end

    send(client_pid, {:send_to_ssh, render_state(new_state)})
    new_state
  end

  def render(width, height, %State{client_pid: client_pid} = state) do
    new_state = %State{state | width: width, height: height}
    send(client_pid, {:send_to_ssh, render_state(new_state)})
    new_state
  end

  defp attempt_move(
         from,
         to,
         %State{} = state
       ),
       do: attempt_move(from, to, state, nil)

  defp attempt_move(
         from,
         to,
         %State{
           game: %Game{id: game_id, turn: turn},
           binbo_pid: binbo_pid,
           flipped: flipped,
           color: turn
         },
         promotion
       ) do
    game = Repo.get(Game, game_id)

    attempted_move =
      if(flipped,
        do: "#{Renderer.to_chess_coord(flip(from))}#{Renderer.to_chess_coord(flip(to))}",
        else: "#{Renderer.to_chess_coord(from)}#{Renderer.to_chess_coord(to)}"
      ) <>
        if(promotion, do: promotion, else: "")

    case :binbo.move(
           binbo_pid,
           attempted_move
         ) do
      {:ok, status} ->
        {:ok, fen} = :binbo.get_fen(binbo_pid)

        {:ok, _new_game} =
          game
          |> Game.changeset(
            Map.merge(
              %{
                fen: fen,
                moves: game.moves + 1,
                turn: if(game.turn == :dark, do: :light, else: :dark)
              },
              changeset_from_status(status)
            )
          )
          |> Repo.update()

        :syn.publish(:games, {:game, game_id}, {:new_move, attempted_move})

      x ->
        Logger.debug(inspect(x))
        nil
    end
  end

  defp attempt_move(_, _, _, _) do
    Logger.debug("No matching clause for move attempt - must be illegal?")
    nil
  end

  defp flip({y, x}),
    do: {Renderer.chess_board_height() - 1 - y, Renderer.chess_board_width() - 1 - x}

  defp render_state(
         %State{
           game: %Game{fen: fen}
         } = state
       ) do
    Renderer.render_board_state(fen, state)
  end

  defp changeset_from_status(game_status) do
    case game_status do
      :continue ->
        %{}

      {:draw, _} ->
        %{status: :draw}

      {:checkmate, :white_wins} ->
        %{status: :winner, winner: :light}

      {:checkmate, :black_wins} ->
        %{status: :winner, winner: :dark}
    end
  end
end