From 07eaad9b8ded7a719183eec84faaf5b168744f01 Mon Sep 17 00:00:00 2001 From: Logan Hunt Date: Fri, 13 Jan 2023 12:20:01 -0700 Subject: Move renderer to its own module --- lib/chessh/ssh/client/board/renderer.ex | 248 ++++++++++++++++++++++++++++++++ 1 file changed, 248 insertions(+) create mode 100644 lib/chessh/ssh/client/board/renderer.ex (limited to 'lib/chessh/ssh/client/board/renderer.ex') diff --git a/lib/chessh/ssh/client/board/renderer.ex b/lib/chessh/ssh/client/board/renderer.ex new file mode 100644 index 0000000..16987c7 --- /dev/null +++ b/lib/chessh/ssh/client/board/renderer.ex @@ -0,0 +1,248 @@ +defmodule Chessh.SSH.Client.Board.Renderer do + alias IO.ANSI + alias Chessh.Utils + alias Chessh.SSH.Client.Board + + @chess_board_height 8 + @chess_board_width 8 + + @tile_width 7 + @tile_height 4 + + @from_select_background ANSI.green_background() + @to_select_background ANSI.blue_background() + @dark_piece_color ANSI.red() + @light_piece_color ANSI.magenta() + + def chess_board_height(), do: @chess_board_height + def chess_board_width(), do: @chess_board_width + def to_select_background(), do: @to_select_background + def from_select_background(), do: @from_select_background + + def to_chess_coord({y, x}) + when x >= 0 and x < @chess_board_width and y >= 0 and y < @chess_board_height do + "#{List.to_string([?a + x])}#{@chess_board_height - y}" + end + + def render_board_state(fen, %Board.State{ + width: _width, + height: _height, + highlighted: highlighted + }) do + board = + draw_board( + fen, + {@tile_width, @tile_height}, + highlighted + ) + + [ANSI.home()] ++ + Enum.map( + Enum.zip(1..length(board), board), + fn {i, line} -> + [ANSI.cursor(i, 0), line] + end + ) + end + + defp tileIsLight(row, col) do + rem(row, 2) == rem(col, 2) + end + + defp piece_type(char) do + case String.capitalize(char) do + "P" -> "pawn" + "N" -> "knight" + "R" -> "rook" + "B" -> "bishop" + "K" -> "king" + "Q" -> "queen" + _ -> nil + end + end + + defp skip_cols_or_place_piece_reduce(char, {curr_column, data}, rowI) do + case Integer.parse(char) do + {skip, ""} -> + {curr_column + skip, data} + + _ -> + case piece_type(char) do + nil -> + {curr_column, data} + + type -> + shade = if(char == String.capitalize(char), do: "light", else: "dark") + + {curr_column + 1, + Map.put( + data, + "#{rowI}, #{curr_column}", + {shade, type} + )} + end + end + end + + defp make_coordinate_to_piece_art_map(fen) do + rows = + String.split(fen, " ") + |> List.first() + |> String.split("/") + + Enum.zip(rows, 0..(length(rows) - 1)) + |> Enum.map(fn {row, rowI} -> + {@chess_board_height, pieces_per_row} = + Enum.reduce( + String.split(row, ""), + {0, %{}}, + &skip_cols_or_place_piece_reduce(&1, &2, rowI) + ) + + pieces_per_row + end) + |> Enum.reduce(%{}, fn pieces_map_for_this_row, acc -> + Map.merge(acc, pieces_map_for_this_row) + end) + end + + defp draw_board( + fen, + {tile_width, tile_height} = tile_dims, + highlights + ) do + coordinate_to_piece = make_coordinate_to_piece_art_map(fen) + board = make_board(tile_dims) + + (Enum.zip_with([board, 0..(length(board) - 1)], fn [row_str, row] -> + curr_y = div(row, tile_height) + + %{row_chars: row_chars} = + Enum.reduce( + Enum.zip(String.graphemes(row_str), 0..(String.length(row_str) - 1)), + %{current_color: ANSI.black(), row_chars: []}, + fn {char, col}, %{current_color: current_color, row_chars: row_chars} = row_state -> + curr_x = div(col, tile_width) + key = "#{curr_y}, #{curr_x}" + relative_to_tile_col = col - curr_x * tile_width + + prefix = + if relative_to_tile_col == 0 do + case Map.fetch(highlights, {curr_y, curr_x}) do + {:ok, color} -> + color + + _ -> + ANSI.default_background() + end + end + + case Map.fetch(coordinate_to_piece, key) do + {:ok, {shade, type}} -> + piece = Utils.ascii_chars()["pieces"][shade][type] + piece_line = Enum.at(piece, row - curr_y * tile_height) + + piece_line_len = String.length(piece_line) + pad_left_right = div(tile_width - piece_line_len, 2) + + if relative_to_tile_col >= pad_left_right && + relative_to_tile_col < tile_width - pad_left_right do + piece_char = String.at(piece_line, relative_to_tile_col - pad_left_right) + new_char = if piece_char == " ", do: char, else: piece_char + + color = + if piece_char == " ", + do: ANSI.default_color(), + else: if(shade == "dark", do: @dark_piece_color, else: @light_piece_color) + + if color != current_color do + %{ + row_state + | current_color: color, + row_chars: row_chars ++ [prefix, color, new_char] + } + else + %{ + row_state + | current_color: current_color, + row_chars: row_chars ++ [prefix, new_char] + } + end + else + %{ + row_state + | current_color: ANSI.default_color(), + row_chars: row_chars ++ [prefix, ANSI.default_color(), char] + } + end + + _ -> + if ANSI.white() != current_color do + %{ + row_state + | current_color: ANSI.default_color(), + row_chars: row_chars ++ [prefix, ANSI.default_color(), char] + } + else + %{ + row_state + | row_chars: row_chars ++ [prefix, char] + } + end + end + end + ) + + curr_num = Utils.ascii_chars()["numbers"][Integer.to_string(curr_y)] + curr_num_line_no = rem(row, @tile_height) + + Enum.join( + [ + String.pad_trailing( + if(curr_num_line_no < length(curr_num), + do: Enum.at(curr_num, curr_num_line_no), + else: "" + ), + @tile_width + ) + ] ++ row_chars, + "" + ) + end) ++ + Enum.map(0..(@tile_height - 1), fn row -> + String.duplicate(" ", @tile_width) <> + (Enum.map(0..(@chess_board_width - 1), fn col -> + curr_letter = Utils.ascii_chars()["letters"][List.to_string([?a + col])] + curr_letter_line_no = rem(row, @tile_height) + + curr_line = + if(curr_letter_line_no < length(curr_letter), + do: Enum.at(curr_letter, curr_letter_line_no), + else: "" + ) + + center_prefix_len = div(@tile_width - String.length(curr_line), 2) + + String.pad_trailing( + String.duplicate(" ", center_prefix_len) <> curr_line, + @tile_width + ) + end) + |> Enum.join("")) + end)) + |> Enum.map(fn row_line -> "#{ANSI.default_background()}#{row_line}" end) + end + + defp make_board({tile_width, tile_height}) do + rows = + Enum.map(0..(@chess_board_height - 1), fn row -> + Enum.map(0..(@chess_board_width - 1), fn col -> + if(tileIsLight(row, col), do: '▊', else: ' ') + |> List.duplicate(tile_width) + end) + |> Enum.join("") + end) + + Enum.flat_map(rows, fn row -> Enum.map(1..tile_height, fn _ -> row end) end) + end +end -- cgit v1.2.3-70-g09d2 From b072f8421ce3caf4e00b3d6881259f595f57c045 Mon Sep 17 00:00:00 2001 From: Simponic Date: Fri, 13 Jan 2023 13:49:24 -0700 Subject: Add flipped --- lib/chessh/ssh/client/board/board.ex | 26 +++-- lib/chessh/ssh/client/board/renderer.ex | 166 +++++++++++++++++--------------- 2 files changed, 106 insertions(+), 86 deletions(-) (limited to 'lib/chessh/ssh/client/board/renderer.ex') diff --git a/lib/chessh/ssh/client/board/board.ex b/lib/chessh/ssh/client/board/board.ex index 4c57e07..cbbade5 100644 --- a/lib/chessh/ssh/client/board/board.ex +++ b/lib/chessh/ssh/client/board/board.ex @@ -11,14 +11,13 @@ defmodule Chessh.SSH.Client.Board do client_pid: nil, binbo_pid: nil, width: 0, - height: 0 - - # flipped: false + height: 0, + flipped: false end use Chessh.SSH.Client.Screen - def init([%State{game_id: game_id} = state | _]) do + def init([%State{client_pid: client_pid, game_id: game_id} = state | _]) do :syn.add_node_to_scopes([:games]) :ok = :syn.join(:games, {:game, game_id}, self()) @@ -26,6 +25,8 @@ defmodule Chessh.SSH.Client.Board do {:ok, binbo_pid} = :binbo.new_server() :binbo.new_game(binbo_pid) + send(client_pid, {:send_to_ssh, Utils.clear_codes()}) + {:ok, %State{state | binbo_pid: binbo_pid}} end @@ -49,8 +50,8 @@ defmodule Chessh.SSH.Client.Board do move_from: move_from, game_id: game_id, cursor: %{x: cursor_x, y: cursor_y} = cursor, - client_pid: client_pid - # flipped: flipped + client_pid: client_pid, + flipped: flipped } = state ) do new_cursor = @@ -76,7 +77,11 @@ defmodule Chessh.SSH.Client.Board do # TODO: Check move here, then publish new move, subscribers get from DB instead if move_from && move_to do - attempted_move = "#{Renderer.to_chess_coord(move_from)}#{Renderer.to_chess_coord(move_to)}" + attempted_move = + if flipped, + do: + "#{Renderer.to_chess_coord(flip(move_from))}#{Renderer.to_chess_coord(flip(move_to))}", + else: "#{Renderer.to_chess_coord(move_from)}#{Renderer.to_chess_coord(move_to)}" :syn.publish(:games, {:game, game_id}, {:new_move, attempted_move}) end @@ -90,8 +95,8 @@ defmodule Chessh.SSH.Client.Board do new_move_from => Renderer.from_select_background() }, width: width, - height: height - # flipped: if(action == "f", do: !flipped, else: flipped) + height: height, + flipped: if(action == "f", do: !flipped, else: flipped) } send(client_pid, {:send_to_ssh, render_state(new_state)}) @@ -103,6 +108,9 @@ defmodule Chessh.SSH.Client.Board do %State{state | width: width, height: height} end + def flip({y, x}), + do: {Renderer.chess_board_height() - 1 - y, Renderer.chess_board_width() - 1 - x} + defp render_state( %State{ binbo_pid: binbo_pid diff --git a/lib/chessh/ssh/client/board/renderer.ex b/lib/chessh/ssh/client/board/renderer.ex index 16987c7..8c0267c 100644 --- a/lib/chessh/ssh/client/board/renderer.ex +++ b/lib/chessh/ssh/client/board/renderer.ex @@ -2,6 +2,7 @@ defmodule Chessh.SSH.Client.Board.Renderer do alias IO.ANSI alias Chessh.Utils alias Chessh.SSH.Client.Board + require Logger @chess_board_height 8 @chess_board_width 8 @@ -27,13 +28,15 @@ defmodule Chessh.SSH.Client.Board.Renderer do def render_board_state(fen, %Board.State{ width: _width, height: _height, - highlighted: highlighted + highlighted: highlighted, + flipped: flipped }) do board = draw_board( fen, {@tile_width, @tile_height}, - highlighted + highlighted, + flipped ) [ANSI.home()] ++ @@ -109,7 +112,8 @@ defmodule Chessh.SSH.Client.Board.Renderer do defp draw_board( fen, {tile_width, tile_height} = tile_dims, - highlights + highlights, + flipped ) do coordinate_to_piece = make_coordinate_to_piece_art_map(fen) board = make_board(tile_dims) @@ -123,7 +127,10 @@ defmodule Chessh.SSH.Client.Board.Renderer do %{current_color: ANSI.black(), row_chars: []}, fn {char, col}, %{current_color: current_color, row_chars: row_chars} = row_state -> curr_x = div(col, tile_width) - key = "#{curr_y}, #{curr_x}" + + key = + "#{if !flipped, do: curr_y, else: @chess_board_height - curr_y - 1}, #{if !flipped, do: curr_x, else: @chess_board_width - curr_x - 1}" + relative_to_tile_col = col - curr_x * tile_width prefix = @@ -137,63 +144,54 @@ defmodule Chessh.SSH.Client.Board.Renderer do end end - case Map.fetch(coordinate_to_piece, key) do - {:ok, {shade, type}} -> - piece = Utils.ascii_chars()["pieces"][shade][type] - piece_line = Enum.at(piece, row - curr_y * tile_height) - - piece_line_len = String.length(piece_line) - pad_left_right = div(tile_width - piece_line_len, 2) - - if relative_to_tile_col >= pad_left_right && - relative_to_tile_col < tile_width - pad_left_right do - piece_char = String.at(piece_line, relative_to_tile_col - pad_left_right) - new_char = if piece_char == " ", do: char, else: piece_char - - color = - if piece_char == " ", - do: ANSI.default_color(), - else: if(shade == "dark", do: @dark_piece_color, else: @light_piece_color) - - if color != current_color do - %{ - row_state - | current_color: color, - row_chars: row_chars ++ [prefix, color, new_char] - } + {color, row_chars} = + case Map.fetch(coordinate_to_piece, key) do + {:ok, {shade, type}} -> + piece = Utils.ascii_chars()["pieces"][shade][type] + piece_line = Enum.at(piece, row - curr_y * tile_height) + pad_left_right = div(tile_width - String.length(piece_line), 2) + + if relative_to_tile_col >= pad_left_right && + relative_to_tile_col < tile_width - pad_left_right do + piece_char = String.at(piece_line, relative_to_tile_col - pad_left_right) + new_char = if piece_char == " ", do: char, else: piece_char + + color = + if piece_char == " ", + do: ANSI.default_color(), + else: + if(shade == "dark", do: @dark_piece_color, else: @light_piece_color) + + if color != current_color do + {color, row_chars ++ [prefix, color, new_char]} + else + {current_color, row_chars ++ [prefix, new_char]} + end else - %{ - row_state - | current_color: current_color, - row_chars: row_chars ++ [prefix, new_char] - } + {ANSI.default_color(), row_chars ++ [prefix, ANSI.default_color(), char]} end - else - %{ - row_state - | current_color: ANSI.default_color(), - row_chars: row_chars ++ [prefix, ANSI.default_color(), char] - } - end - _ -> - if ANSI.white() != current_color do - %{ - row_state - | current_color: ANSI.default_color(), - row_chars: row_chars ++ [prefix, ANSI.default_color(), char] - } - else - %{ - row_state - | row_chars: row_chars ++ [prefix, char] - } - end - end + _ -> + if ANSI.default_color() != current_color do + {ANSI.default_color(), row_chars ++ [prefix, ANSI.default_color(), char]} + else + {current_color, row_chars ++ [prefix, char]} + end + end + + %{ + row_state + | current_color: color, + row_chars: row_chars + } end ) - curr_num = Utils.ascii_chars()["numbers"][Integer.to_string(curr_y)] + curr_num = + Utils.ascii_chars()["numbers"][ + Integer.to_string(if flipped, do: curr_y + 1, else: @chess_board_height - curr_y) + ] + curr_num_line_no = rem(row, @tile_height) Enum.join( @@ -208,31 +206,45 @@ defmodule Chessh.SSH.Client.Board.Renderer do ] ++ row_chars, "" ) - end) ++ - Enum.map(0..(@tile_height - 1), fn row -> - String.duplicate(" ", @tile_width) <> - (Enum.map(0..(@chess_board_width - 1), fn col -> - curr_letter = Utils.ascii_chars()["letters"][List.to_string([?a + col])] - curr_letter_line_no = rem(row, @tile_height) - - curr_line = - if(curr_letter_line_no < length(curr_letter), - do: Enum.at(curr_letter, curr_letter_line_no), - else: "" - ) - - center_prefix_len = div(@tile_width - String.length(curr_line), 2) - - String.pad_trailing( - String.duplicate(" ", center_prefix_len) <> curr_line, - @tile_width - ) - end) - |> Enum.join("")) - end)) + end) ++ column_coords(flipped)) |> Enum.map(fn row_line -> "#{ANSI.default_background()}#{row_line}" end) end + defp column_coords(flipped) do + Enum.map(0..(@tile_height - 1), fn row -> + String.duplicate(" ", @tile_width) <> + (Enum.map( + if(!flipped, do: 0..(@chess_board_width - 1), else: (@chess_board_width - 1)..0), + fn col -> + curr_letter = Utils.ascii_chars()["letters"][List.to_string([?a + col])] + curr_letter_line_no = rem(row, @tile_height) + + curr_line = + if(curr_letter_line_no < length(curr_letter), + do: Enum.at(curr_letter, curr_letter_line_no), + else: "" + ) + + center_prefix_len = div(@tile_width - String.length(curr_line), 2) + + String.pad_trailing( + String.duplicate(" ", center_prefix_len) <> curr_line, + @tile_width + ) + end + ) + |> Enum.join("")) + end) + end + + defp render_flipped(rows, flipped) do + if !flipped do + rows + else + rows + end + end + defp make_board({tile_width, tile_height}) do rows = Enum.map(0..(@chess_board_height - 1), fn row -> -- cgit v1.2.3-70-g09d2 From 80843947e05cc464f54df7ad6838f5a16d9e7133 Mon Sep 17 00:00:00 2001 From: Logan Hunt Date: Fri, 13 Jan 2023 16:44:45 -0700 Subject: Remove warnings --- lib/chessh/ssh/client/board/renderer.ex | 8 -------- lib/chessh/ssh/tui.ex | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) (limited to 'lib/chessh/ssh/client/board/renderer.ex') diff --git a/lib/chessh/ssh/client/board/renderer.ex b/lib/chessh/ssh/client/board/renderer.ex index 8c0267c..845e538 100644 --- a/lib/chessh/ssh/client/board/renderer.ex +++ b/lib/chessh/ssh/client/board/renderer.ex @@ -237,14 +237,6 @@ defmodule Chessh.SSH.Client.Board.Renderer do end) end - defp render_flipped(rows, flipped) do - if !flipped do - rows - else - rows - end - end - defp make_board({tile_width, tile_height}) do rows = Enum.map(0..(@chess_board_height - 1), fn row -> diff --git a/lib/chessh/ssh/tui.ex b/lib/chessh/ssh/tui.ex index c1c763b..1838fb8 100644 --- a/lib/chessh/ssh/tui.ex +++ b/lib/chessh/ssh/tui.ex @@ -103,7 +103,7 @@ defmodule Chessh.SSH.Tui do def handle_ssh_msg( {:ssh_cm, connection_handler, {:pty, channel_id, want_reply?, {_term, width, height, _pixwidth, _pixheight, _opts}}}, - %State{client_pid: client_pid} = state + %State{} = state ) do Logger.debug("#{inspect(state.player_session)} has requested a PTY") :ssh_connection.reply_request(connection_handler, want_reply?, :success, channel_id) -- cgit v1.2.3-70-g09d2 From 3a6c603b0b12964d8a04ae4f78fb61bc094adf12 Mon Sep 17 00:00:00 2001 From: Logan Hunt Date: Fri, 13 Jan 2023 17:02:11 -0700 Subject: Change some colors around --- lib/chessh/ssh/client/board/renderer.ex | 6 +++--- priv/ascii_chars.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/chessh/ssh/client/board/renderer.ex') diff --git a/lib/chessh/ssh/client/board/renderer.ex b/lib/chessh/ssh/client/board/renderer.ex index 845e538..fa8648f 100644 --- a/lib/chessh/ssh/client/board/renderer.ex +++ b/lib/chessh/ssh/client/board/renderer.ex @@ -10,10 +10,10 @@ defmodule Chessh.SSH.Client.Board.Renderer do @tile_width 7 @tile_height 4 - @from_select_background ANSI.green_background() + @from_select_background ANSI.light_blue_background() @to_select_background ANSI.blue_background() - @dark_piece_color ANSI.red() - @light_piece_color ANSI.magenta() + @dark_piece_color ANSI.light_red() + @light_piece_color ANSI.light_magenta() def chess_board_height(), do: @chess_board_height def chess_board_width(), do: @chess_board_width diff --git a/priv/ascii_chars.json b/priv/ascii_chars.json index 1be31f8..fe7d12e 100644 --- a/priv/ascii_chars.json +++ b/priv/ascii_chars.json @@ -7,7 +7,7 @@ "e": [" __ ", "|_ ", "|__ "], "f": [" __ ", "|_ ", "| "], "g": [" __ ", "/ _ ", "\\__\\"], - "h": ["| |", "|__|", "| |"] + "h": [". .", "|__|", "| |"] }, "numbers": { "0": [" _ ", "| |", "|_|"], -- cgit v1.2.3-70-g09d2