Draw board #3

Merged
Simponic merged 12 commits from draw_board into main 2023-01-13 19:02:31 -05:00
2 changed files with 103 additions and 83 deletions
Showing only changes of commit b072f8421c - Show all commits

View File

@ -11,14 +11,13 @@ defmodule Chessh.SSH.Client.Board do
client_pid: nil, client_pid: nil,
binbo_pid: nil, binbo_pid: nil,
width: 0, width: 0,
height: 0 height: 0,
flipped: false
# flipped: false
end end
use Chessh.SSH.Client.Screen 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]) :syn.add_node_to_scopes([:games])
:ok = :syn.join(:games, {:game, game_id}, self()) :ok = :syn.join(:games, {:game, game_id}, self())
@ -26,6 +25,8 @@ defmodule Chessh.SSH.Client.Board do
{:ok, binbo_pid} = :binbo.new_server() {:ok, binbo_pid} = :binbo.new_server()
:binbo.new_game(binbo_pid) :binbo.new_game(binbo_pid)
send(client_pid, {:send_to_ssh, Utils.clear_codes()})
{:ok, %State{state | binbo_pid: binbo_pid}} {:ok, %State{state | binbo_pid: binbo_pid}}
end end
@ -49,8 +50,8 @@ defmodule Chessh.SSH.Client.Board do
move_from: move_from, move_from: move_from,
game_id: game_id, game_id: game_id,
cursor: %{x: cursor_x, y: cursor_y} = cursor, cursor: %{x: cursor_x, y: cursor_y} = cursor,
client_pid: client_pid client_pid: client_pid,
# flipped: flipped flipped: flipped
} = state } = state
) do ) do
new_cursor = 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 # TODO: Check move here, then publish new move, subscribers get from DB instead
if move_from && move_to do 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}) :syn.publish(:games, {:game, game_id}, {:new_move, attempted_move})
end end
@ -90,8 +95,8 @@ defmodule Chessh.SSH.Client.Board do
new_move_from => Renderer.from_select_background() new_move_from => Renderer.from_select_background()
}, },
width: width, width: width,
height: height height: height,
# flipped: if(action == "f", do: !flipped, else: flipped) flipped: if(action == "f", do: !flipped, else: flipped)
} }
send(client_pid, {:send_to_ssh, render_state(new_state)}) 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} %State{state | width: width, height: height}
end end
def flip({y, x}),
do: {Renderer.chess_board_height() - 1 - y, Renderer.chess_board_width() - 1 - x}
defp render_state( defp render_state(
%State{ %State{
binbo_pid: binbo_pid binbo_pid: binbo_pid

View File

@ -2,6 +2,7 @@ defmodule Chessh.SSH.Client.Board.Renderer do
alias IO.ANSI alias IO.ANSI
alias Chessh.Utils alias Chessh.Utils
alias Chessh.SSH.Client.Board alias Chessh.SSH.Client.Board
require Logger
@chess_board_height 8 @chess_board_height 8
@chess_board_width 8 @chess_board_width 8
@ -27,13 +28,15 @@ defmodule Chessh.SSH.Client.Board.Renderer do
def render_board_state(fen, %Board.State{ def render_board_state(fen, %Board.State{
width: _width, width: _width,
height: _height, height: _height,
highlighted: highlighted highlighted: highlighted,
flipped: flipped
}) do }) do
board = board =
draw_board( draw_board(
fen, fen,
{@tile_width, @tile_height}, {@tile_width, @tile_height},
highlighted highlighted,
flipped
) )
[ANSI.home()] ++ [ANSI.home()] ++
@ -109,7 +112,8 @@ defmodule Chessh.SSH.Client.Board.Renderer do
defp draw_board( defp draw_board(
fen, fen,
{tile_width, tile_height} = tile_dims, {tile_width, tile_height} = tile_dims,
highlights highlights,
flipped
) do ) do
coordinate_to_piece = make_coordinate_to_piece_art_map(fen) coordinate_to_piece = make_coordinate_to_piece_art_map(fen)
board = make_board(tile_dims) board = make_board(tile_dims)
@ -123,7 +127,10 @@ defmodule Chessh.SSH.Client.Board.Renderer do
%{current_color: ANSI.black(), row_chars: []}, %{current_color: ANSI.black(), row_chars: []},
fn {char, col}, %{current_color: current_color, row_chars: row_chars} = row_state -> fn {char, col}, %{current_color: current_color, row_chars: row_chars} = row_state ->
curr_x = div(col, tile_width) 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 relative_to_tile_col = col - curr_x * tile_width
prefix = prefix =
@ -137,13 +144,12 @@ defmodule Chessh.SSH.Client.Board.Renderer do
end end
end end
{color, row_chars} =
case Map.fetch(coordinate_to_piece, key) do case Map.fetch(coordinate_to_piece, key) do
{:ok, {shade, type}} -> {:ok, {shade, type}} ->
piece = Utils.ascii_chars()["pieces"][shade][type] piece = Utils.ascii_chars()["pieces"][shade][type]
piece_line = Enum.at(piece, row - curr_y * tile_height) piece_line = Enum.at(piece, row - curr_y * tile_height)
pad_left_right = div(tile_width - String.length(piece_line), 2)
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 && if relative_to_tile_col >= pad_left_right &&
relative_to_tile_col < tile_width - pad_left_right do relative_to_tile_col < tile_width - pad_left_right do
@ -153,47 +159,39 @@ defmodule Chessh.SSH.Client.Board.Renderer do
color = color =
if piece_char == " ", if piece_char == " ",
do: ANSI.default_color(), do: ANSI.default_color(),
else: if(shade == "dark", do: @dark_piece_color, else: @light_piece_color) else:
if(shade == "dark", do: @dark_piece_color, else: @light_piece_color)
if color != current_color do if color != current_color do
%{ {color, row_chars ++ [prefix, color, new_char]}
row_state
| current_color: color,
row_chars: row_chars ++ [prefix, color, new_char]
}
else else
%{ {current_color, row_chars ++ [prefix, new_char]}
row_state
| current_color: current_color,
row_chars: row_chars ++ [prefix, new_char]
}
end end
else else
%{ {ANSI.default_color(), row_chars ++ [prefix, ANSI.default_color(), char]}
row_state
| current_color: ANSI.default_color(),
row_chars: row_chars ++ [prefix, ANSI.default_color(), char]
}
end end
_ -> _ ->
if ANSI.white() != current_color do if ANSI.default_color() != current_color do
%{ {ANSI.default_color(), row_chars ++ [prefix, ANSI.default_color(), char]}
row_state
| current_color: ANSI.default_color(),
row_chars: row_chars ++ [prefix, ANSI.default_color(), char]
}
else else
{current_color, row_chars ++ [prefix, char]}
end
end
%{ %{
row_state row_state
| row_chars: row_chars ++ [prefix, char] | current_color: color,
row_chars: row_chars
} }
end end
end
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) curr_num_line_no = rem(row, @tile_height)
Enum.join( Enum.join(
@ -208,10 +206,16 @@ defmodule Chessh.SSH.Client.Board.Renderer do
] ++ row_chars, ] ++ row_chars,
"" ""
) )
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 -> Enum.map(0..(@tile_height - 1), fn row ->
String.duplicate(" ", @tile_width) <> String.duplicate(" ", @tile_width) <>
(Enum.map(0..(@chess_board_width - 1), fn col -> (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 = Utils.ascii_chars()["letters"][List.to_string([?a + col])]
curr_letter_line_no = rem(row, @tile_height) curr_letter_line_no = rem(row, @tile_height)
@ -227,10 +231,18 @@ defmodule Chessh.SSH.Client.Board.Renderer do
String.duplicate(" ", center_prefix_len) <> curr_line, String.duplicate(" ", center_prefix_len) <> curr_line,
@tile_width @tile_width
) )
end) end
)
|> Enum.join("")) |> Enum.join(""))
end)) end)
|> Enum.map(fn row_line -> "#{ANSI.default_background()}#{row_line}" end) end
defp render_flipped(rows, flipped) do
if !flipped do
rows
else
rows
end
end end
defp make_board({tile_width, tile_height}) do defp make_board({tile_width, tile_height}) do