Do not send notifications when game is over & fix promotion screen #15

Merged
Simponic merged 1 commits from fix-promotion into main 2023-02-01 19:36:40 -05:00
2 changed files with 30 additions and 28 deletions

View File

@ -55,28 +55,26 @@ defmodule Chessh.DiscordNotifier do
|> Keyword.take([:game_move_notif_delay_ms, :discord_game_move_notif_webhook]) |> Keyword.take([:game_move_notif_delay_ms, :discord_game_move_notif_webhook])
|> Keyword.values() |> Keyword.values()
case Repo.get(Game, game_id) do case Repo.get(Game, game_id) |> Repo.preload([:dark_player, :light_player]) do
nil -> %Game{
nil dark_player: %Player{discord_id: dark_player_discord_id},
light_player: %Player{discord_id: light_player_discord_id},
game -> turn: turn,
%Game{ updated_at: last_updated,
dark_player: %Player{discord_id: dark_player_discord_id}, moves: move_count,
light_player: %Player{discord_id: light_player_discord_id}, status: :continue
turn: turn, } ->
updated_at: last_updated,
moves: move_count,
status: status
} = Repo.preload(game, [:dark_player, :light_player])
delta_t = NaiveDateTime.diff(NaiveDateTime.utc_now(), last_updated, :millisecond) delta_t = NaiveDateTime.diff(NaiveDateTime.utc_now(), last_updated, :millisecond)
if delta_t >= min_delta_t && status == :continue do if delta_t >= min_delta_t do
post_discord( post_discord(
discord_game_move_notif_webhook, discord_game_move_notif_webhook,
"<@#{if turn == :light, do: light_player_discord_id, else: dark_player_discord_id}> it is your move in Game #{game_id} (move #{move_count})." "<@#{if turn == :light, do: light_player_discord_id, else: dark_player_discord_id}> it is your move in Game #{game_id} (move #{move_count})."
) )
end end
_ ->
nil
end end
end end
@ -99,10 +97,10 @@ defmodule Chessh.DiscordNotifier do
message = message =
case {is_nil(light_player), is_nil(dark_player)} do case {is_nil(light_player), is_nil(dark_player)} do
{true, false} -> {true, false} ->
"#{pingable_mention}, <@#{dark_player.discord_id}> is looking for an opponent to play as light in Game #{game_id}" "#{pingable_mention}, <@#{dark_player.discord_id}> is looking for an opponent to play with light pieces in Game #{game_id}"
{false, true} -> {false, true} ->
"#{pingable_mention}, <@#{light_player.discord_id}> is looking for an opponent to play as dark in Game #{game_id}" "#{pingable_mention}, <@#{light_player.discord_id}> is looking for an opponent to play with dark pieces in Game #{game_id}"
_ -> _ ->
false false

View File

@ -262,22 +262,21 @@ defmodule Chessh.SSH.Client.Game do
do: Renderer.flip({new_cursor.y, new_cursor.x}), do: Renderer.flip({new_cursor.y, new_cursor.x}),
else: {new_cursor.y, new_cursor.x} else: {new_cursor.y, new_cursor.x}
piece_type =
:binbo_position.get_piece(
:binbo_board.notation_to_index(Renderer.to_chess_coord(maybe_flipped_cursor_tup)),
:binbo.game_state(binbo_pid)
)
{new_move_from, move_to} = {new_move_from, move_to} =
if action == :return do if action == :return do
coords = {new_cursor.y, new_cursor.x} coords = {new_cursor.y, new_cursor.x}
case move_from do case move_from do
nil -> nil ->
if piece_type != 0 do if :binbo_position.get_piece(
{coords, nil} :binbo_board.notation_to_index(
else Renderer.to_chess_coord(maybe_flipped_cursor_tup)
),
:binbo.game_state(binbo_pid)
) == 0 do
{move_from, nil} {move_from, nil}
else
{coords, nil}
end end
_ -> _ ->
@ -307,10 +306,15 @@ defmodule Chessh.SSH.Client.Game do
}) })
if move_from && move_to do if move_from && move_to do
maybe_flipped_to = if flipped, do: Renderer.flip(move_to), else: move_to [maybe_flipped_to, maybe_flipped_from] =
[move_to, move_from]
|> Enum.map(fn coord -> if flipped, do: Renderer.flip(coord), else: coord end)
promotion_possible = promotion_possible =
case piece_type do case :binbo_position.get_piece(
:binbo_board.notation_to_index(Renderer.to_chess_coord(maybe_flipped_from)),
:binbo.game_state(binbo_pid)
) do
1 -> 1 ->
# Light pawn # Light pawn
{y, _} = maybe_flipped_to {y, _} = maybe_flipped_to