Show previous move in message body, which notification is now instant, add check highlighting

This commit is contained in:
Elizabeth Hunt 2023-02-08 22:06:58 -07:00
parent 2bd03144da
commit bf9363aaf8
Signed by: simponic
GPG Key ID: 52B3774857EB24B1
4 changed files with 47 additions and 12 deletions

View File

@ -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"

View File

@ -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,
%{
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

View File

@ -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)
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

View File

@ -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