From bf9363aaf8d5d2ef2360caad85f2c518a7ccbf5c Mon Sep 17 00:00:00 2001 From: Simponic Date: Wed, 8 Feb 2023 22:06:58 -0700 Subject: [PATCH] Show previous move in message body, which notification is now instant, add check highlighting --- config/config.exs | 4 +-- lib/chessh/discord/notifier.ex | 19 ++++++++++---- lib/chessh/ssh/client/game/game.ex | 34 ++++++++++++++++++++++---- lib/chessh/ssh/client/game/renderer.ex | 2 ++ 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/config/config.exs b/config/config.exs index bac2e14..c11b753 100644 --- a/config/config.exs +++ b/config/config.exs @@ -24,8 +24,8 @@ config :chessh, Web, discord_scope: "identify" config :chessh, DiscordNotifications, - game_move_notif_delay_ms: 20 * 1000, - game_created_notif_delay_ms: 5 * 1000, + game_move_notif_delay_ms: 0, + game_created_notif_delay_ms: 15_000, reschedule_delay: 3 * 1000 config :joken, default_signer: "secret" diff --git a/lib/chessh/discord/notifier.ex b/lib/chessh/discord/notifier.ex index 41c79a3..c15febe 100644 --- a/lib/chessh/discord/notifier.ex +++ b/lib/chessh/discord/notifier.ex @@ -5,6 +5,8 @@ defmodule Chessh.DiscordNotifier do alias Chessh.{Game, Player, Repo} + require Logger + def start_link(state \\ []) do GenServer.start_link(__MODULE__, state, name: @name) end @@ -60,6 +62,7 @@ defmodule Chessh.DiscordNotifier do dark_player: %Player{discord_id: dark_player_discord_id}, light_player: %Player{discord_id: light_player_discord_id}, turn: turn, + last_move: last_move, updated_at: last_updated, moves: move_count, status: :continue, @@ -83,7 +86,10 @@ defmodule Chessh.DiscordNotifier do if delta_t >= min_delta_t do post_discord( game.discord_thread_id, - "<@#{if turn == :light, do: light_player_discord_id, else: dark_player_discord_id}> it is your move in Game #{game_id} (move #{move_count})." + %{ + content: + "<@#{if turn == :light, do: light_player_discord_id, else: dark_player_discord_id}> it is your move in Game #{game_id} (move #{move_count}): your opponent played #{last_move}." + } ) end @@ -138,7 +144,7 @@ defmodule Chessh.DiscordNotifier do end if message do - post_discord(new_game_channel_id, message) + post_discord(new_game_channel_id, %{content: message}) end end end @@ -167,7 +173,10 @@ defmodule Chessh.DiscordNotifier do post_discord( thread_id, - "This private thread is used to communicate move notifications. It will be destroyed on game end." + %{ + content: + "This private thread is used to communicate move notifications. It will be destroyed on game end." + } ) thread_id @@ -177,8 +186,8 @@ defmodule Chessh.DiscordNotifier do end end - defp post_discord(channel_id, message) do - make_discord_api_call(:post, "channels/#{channel_id}/messages", %{content: message}) + defp post_discord(channel_id, body) do + make_discord_api_call(:post, "channels/#{channel_id}/messages", body) end defp destroy_channel(channel_id) do diff --git a/lib/chessh/ssh/client/game/game.ex b/lib/chessh/ssh/client/game/game.ex index 6b2ef60..da2bd99 100644 --- a/lib/chessh/ssh/client/game/game.ex +++ b/lib/chessh/ssh/client/game/game.ex @@ -455,7 +455,8 @@ defmodule Chessh.SSH.Client.Game do defp make_highlight_map( %State{ - game: %Game{last_move: last_move}, + game: %Game{last_move: last_move, turn: turn}, + binbo_pid: binbo_pid, flipped: flipped }, extra_highlights \\ %{} @@ -465,10 +466,33 @@ defmodule Chessh.SSH.Client.Game do [String.slice(last_move, 0..1), String.slice(last_move, 2..4)] |> Enum.map(fn coord -> Renderer.from_chess_coord(coord, flipped) end) - %{ - prev_move_from => Renderer.previous_move_background(), - prev_move_to => Renderer.previous_move_background() - } + binbo_bin_color = if(turn == :light, do: 0, else: 16) + binbo_atom_color = if(turn == :light, do: :white, else: :black) + + check_highlight = + if :binbo_position.is_in_check(binbo_bin_color, :binbo.game_state(binbo_pid)) do + {:ok, pieces_list} = :binbo.get_pieces_list(binbo_pid, :notation) + + {king_square, _, _} = + Enum.find(pieces_list, fn piece -> + case piece do + {_, ^binbo_atom_color, :king} -> true + _ -> false + end + end) + + %{Renderer.from_chess_coord(king_square, flipped) => Renderer.in_check_color()} + else + %{} + end + + Map.merge( + %{ + prev_move_from => Renderer.previous_move_background(), + prev_move_to => Renderer.previous_move_background() + }, + check_highlight + ) else %{} end diff --git a/lib/chessh/ssh/client/game/renderer.ex b/lib/chessh/ssh/client/game/renderer.ex index 6d7d7a7..c7d3a96 100644 --- a/lib/chessh/ssh/client/game/renderer.ex +++ b/lib/chessh/ssh/client/game/renderer.ex @@ -12,6 +12,7 @@ defmodule Chessh.SSH.Client.Game.Renderer do @previous_move_background ANSI.light_magenta_background() @from_select_background ANSI.light_green_background() @to_select_background ANSI.light_yellow_background() + @in_check_color ANSI.yellow_background() @dark_piece_color ANSI.red() @light_piece_color ANSI.light_cyan() @@ -21,6 +22,7 @@ defmodule Chessh.SSH.Client.Game.Renderer do def to_select_background(), do: @to_select_background def from_select_background(), do: @from_select_background def previous_move_background(), do: @previous_move_background + def in_check_color(), do: @in_check_color def to_chess_coord({y, x}) when x >= 0 and x < @chess_board_width and y >= 0 and y < @chess_board_height do