From f5773f606a738e092845597f2af77e9602b06552 Mon Sep 17 00:00:00 2001 From: "Elizabeth (Lizzy) Hunt" Date: Sat, 22 Apr 2023 23:32:55 -0600 Subject: [PATCH] README changes, create thread on join, update package-lock (#21) --- README.md | 5 ++- config/config.exs | 1 + front/package-lock.json | 8 +--- lib/chessh/discord/notifier.ex | 61 ++++++++++++++++++++---------- lib/chessh/ssh/client/game/game.ex | 12 +++++- 5 files changed, 58 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index db3cdbe..a22b7ef 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,10 @@ mix deps.compile # Install frontend stuff cd front npm install -npm start + +# Generate SSH keys +cd ../priv +./make_keys.sh # Run the frontend and server concurrently! cd .. diff --git a/config/config.exs b/config/config.exs index c11b753..11c0a93 100644 --- a/config/config.exs +++ b/config/config.exs @@ -26,6 +26,7 @@ config :chessh, Web, config :chessh, DiscordNotifications, game_move_notif_delay_ms: 0, game_created_notif_delay_ms: 15_000, + game_player_joined_notif_delay_ms: 0, reschedule_delay: 3 * 1000 config :joken, default_signer: "secret" diff --git a/front/package-lock.json b/front/package-lock.json index 51f1b64..fb572c0 100644 --- a/front/package-lock.json +++ b/front/package-lock.json @@ -11,6 +11,7 @@ "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", + "asciinema-player": "^3.0.1", "http-proxy-middleware": "^2.0.6", "react": "^18.2.0", "react-code-blocks": "^0.0.9-0", @@ -19,9 +20,6 @@ "react-router-dom": "^6.6.2", "react-scripts": "5.0.1", "web-vitals": "^2.1.4" - }, - "devDependencies": { - "asciinema-player": "^3.0.1" } }, "node_modules/@adobe/css-tools": { @@ -4747,7 +4745,6 @@ "version": "3.0.1", "resolved": "https://registry.npmjs.org/asciinema-player/-/asciinema-player-3.0.1.tgz", "integrity": "sha512-plm/C/MhOtZWysrfcT/rzxOuu8vxvvDSvF50pqZS6KpJUDmATedAhO54zktbE/g7RiaaYfzgX8xjRhlQdgISwA==", - "dev": true, "dependencies": { "@babel/runtime": "^7.15.4", "solid-js": "^1.3.0" @@ -15017,7 +15014,6 @@ "version": "1.6.9", "resolved": "https://registry.npmjs.org/solid-js/-/solid-js-1.6.9.tgz", "integrity": "sha512-kV3fMmm+1C2J95c8eDOPKGfZHnuAkHUBLG4hX1Xu08bXeAIPqmxuz/QdH3B8SIdTp3EatBVIyA6RCes3hrGzpg==", - "dev": true, "dependencies": { "csstype": "^3.1.0" } @@ -20496,7 +20492,6 @@ "version": "3.0.1", "resolved": "https://registry.npmjs.org/asciinema-player/-/asciinema-player-3.0.1.tgz", "integrity": "sha512-plm/C/MhOtZWysrfcT/rzxOuu8vxvvDSvF50pqZS6KpJUDmATedAhO54zktbE/g7RiaaYfzgX8xjRhlQdgISwA==", - "dev": true, "requires": { "@babel/runtime": "^7.15.4", "solid-js": "^1.3.0" @@ -27928,7 +27923,6 @@ "version": "1.6.9", "resolved": "https://registry.npmjs.org/solid-js/-/solid-js-1.6.9.tgz", "integrity": "sha512-kV3fMmm+1C2J95c8eDOPKGfZHnuAkHUBLG4hX1Xu08bXeAIPqmxuz/QdH3B8SIdTp3EatBVIyA6RCes3hrGzpg==", - "dev": true, "requires": { "csstype": "^3.1.0" } 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)})