summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorElizabeth (Lizzy) Hunt <elizabeth.hunt@simponic.xyz>2023-04-22 23:32:55 -0600
committerGitHub <noreply@github.com>2023-04-22 23:32:55 -0600
commitf5773f606a738e092845597f2af77e9602b06552 (patch)
treef5611aa4fef4c5d972f18451e2127762ffd54bd7 /lib
parentfb1825e964ee5ebb8427c56eaa34e9437c56c5ab (diff)
downloadchessh-f5773f606a738e092845597f2af77e9602b06552.tar.gz
chessh-f5773f606a738e092845597f2af77e9602b06552.zip
README changes, create thread on join, update package-lock (#21)
Diffstat (limited to 'lib')
-rw-r--r--lib/chessh/discord/notifier.ex61
-rw-r--r--lib/chessh/ssh/client/game/game.ex12
2 files changed, 52 insertions, 21 deletions
diff --git a/lib/chessh/discord/notifier.ex b/lib/chessh/discord/notifier.ex
index 7751041..93d27f1 100644
--- a/lib/chessh/discord/notifier.ex
+++ b/lib/chessh/discord/notifier.ex
@@ -54,11 +54,30 @@ defmodule Chessh.DiscordNotifier do
{:noreply, state}
end
+ defp send_notification({:player_joined, game_id}) do
+ case Repo.get(Game, game_id) |> Repo.preload([:dark_player, :light_player]) do
+ %Game{
+ status: :continue,
+ dark_player: %Player{discord_id: dark_player_discord_id},
+ light_player: %Player{discord_id: light_player_discord_id}
+ } = game ->
+ game = maybe_put_new_thread_on_game(game)
+
+ post_discord(
+ game.discord_thread_id,
+ %{
+ content:
+ "Everyone (<@#{dark_player_discord_id}> as the dark pieces, <@#{light_player_discord_id}> as light) has joined! Play chess!"
+ }
+ )
+
+ _ ->
+ nil
+ end
+ end
+
defp send_notification({:move_reminder, game_id}) do
- [min_delta_t, remind_move_channel_id] =
- Application.get_env(:chessh, DiscordNotifications)
- |> Keyword.take([:game_move_notif_delay_ms, :remind_move_channel_id])
- |> Keyword.values()
+ min_delta_t = Application.get_env(:chessh, DiscordNotifications)[:game_move_notif_delay_ms]
case Repo.get(Game, game_id) |> Repo.preload([:dark_player, :light_player]) do
%Game{
@@ -68,23 +87,10 @@ defmodule Chessh.DiscordNotifier do
last_move: last_move,
updated_at: last_updated,
moves: move_count,
- status: :continue,
- discord_thread_id: discord_thread_id
+ status: :continue
} = game ->
delta_t = NaiveDateTime.diff(NaiveDateTime.utc_now(), last_updated, :millisecond)
-
- game =
- if is_nil(discord_thread_id) do
- {:ok, game} =
- Game.changeset(game, %{
- discord_thread_id: make_private_discord_thread_id(remind_move_channel_id, game)
- })
- |> Repo.update()
-
- game
- else
- game
- end
+ game = maybe_put_new_thread_on_game(game)
if delta_t >= min_delta_t do
post_discord(
@@ -233,4 +239,21 @@ defmodule Chessh.DiscordNotifier do
bot_token = Application.get_env(:chessh, DiscordNotifications)[:discord_bot_token]
{'Authorization', 'Bot #{bot_token}'}
end
+
+ defp maybe_put_new_thread_on_game(%Game{discord_thread_id: discord_thread_id} = game) do
+ remind_move_channel_id =
+ Application.get_env(:chessh, DiscordNotifications)[:remind_move_channel_id]
+
+ if is_nil(discord_thread_id) do
+ {:ok, game} =
+ Game.changeset(game, %{
+ discord_thread_id: make_private_discord_thread_id(remind_move_channel_id, game)
+ })
+ |> Repo.update()
+
+ game
+ else
+ game
+ end
+ end
end
diff --git a/lib/chessh/ssh/client/game/game.ex b/lib/chessh/ssh/client/game/game.ex
index cd641f0..4ac0f31 100644
--- a/lib/chessh/ssh/client/game/game.ex
+++ b/lib/chessh/ssh/client/game/game.ex
@@ -32,7 +32,10 @@ defmodule Chessh.SSH.Client.Game do
def init([
%State{
color: color,
- game: %Game{dark_player_id: dark_player_id, light_player_id: light_player_id},
+ game: %Game{
+ dark_player_id: dark_player_id,
+ light_player_id: light_player_id
+ },
player_session: %{player_id: player_id}
} = state
| tail
@@ -47,7 +50,6 @@ defmodule Chessh.SSH.Client.Game do
else
case {is_nil(dark_player_id), is_nil(light_player_id)} do
{true, false} -> %State{state | color: :dark}
- {false, true} -> %State{state | color: :light}
{_, _} -> %State{state | color: :light}
end
end
@@ -210,6 +212,12 @@ defmodule Chessh.SSH.Client.Game do
:player_joined,
%State{client_pid: client_pid, game: %Game{id: game_id}} = state
) do
+ GenServer.cast(
+ :discord_notifier,
+ {:schedule_notification, {:player_joined, game_id},
+ Application.get_env(:chessh, DiscordNotifications)[:game_player_joined_notif_delay_ms]}
+ )
+
game = Repo.get(Game, game_id) |> Repo.preload([:light_player, :dark_player])
new_state = %State{state | game: game}
send(client_pid, {:send_to_ssh, Renderer.render_board_state(new_state)})