From 873cd0d399097e46ad38a7784b1e03ac546b1cfa Mon Sep 17 00:00:00 2001 From: Simponic Date: Wed, 1 Feb 2023 14:22:01 -0700 Subject: [PATCH] Add discord notifications for games --- .env.example | 4 + config/config.exs | 9 +- config/runtime.exs | 5 + lib/chessh/application.ex | 1 + lib/chessh/discord/notifier.ex | 129 ++++++++++++++++++ lib/chessh/ssh/client/game/game.ex | 14 +- .../20221219082326_create_player.exs | 4 +- 7 files changed, 160 insertions(+), 6 deletions(-) create mode 100644 lib/chessh/discord/notifier.ex diff --git a/.env.example b/.env.example index f27a541..51168fc 100644 --- a/.env.example +++ b/.env.example @@ -26,3 +26,7 @@ REACT_APP_SSH_PORT=42069 REDIS_HOST=localhost REDIS_PORT=6379 + +NEW_GAME_PINGABLE_ROLE_ID=1123232 +NEW_GAME_CHANNEL_WEBHOOK=https://discordapp.com/api/webhooks/ +REMIND_MOVE_CHANNEL_WEBHOOK=https://discordapp.com/api/webhooks/ \ No newline at end of file diff --git a/config/config.exs b/config/config.exs index f74e1e4..e732049 100644 --- a/config/config.exs +++ b/config/config.exs @@ -14,14 +14,19 @@ config :chessh, RateLimits, player_session_message_burst_rate: 8, player_public_keys: 15, create_game_ms: 60 * 1000, - create_game_rate: 3 + create_game_rate: 3, + discord_notification_rate: 3, + discord_notification_rate_ms: 1000 config :chessh, Web, discord_oauth_login_url: "https://discord.com/api/oauth2/token", discord_user_api_url: "https://discord.com/api/users/@me", discord_scope: "identify" -config :chessh, DiscordNotifications, looking_for_games_mention: "<@&1070084105796075550>" +config :chessh, DiscordNotifications, + game_move_notif_delay_ms: 3 * 60 * 1000, + game_created_notif_delay_ms: 30 * 1000, + reschedule_delay: 5 * 1000 config :joken, default_signer: "secret" diff --git a/config/runtime.exs b/config/runtime.exs index 5e03614..684e48e 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -3,6 +3,11 @@ import Config config :chessh, ssh_port: String.to_integer(System.get_env("SSH_PORT", "34355")) +config :chessh, DiscordNotifications, + looking_for_games_role_mention: "<@&#{System.get_env("NEW_GAME_PINGABLE_ROLE_ID")}>", + discord_game_move_notif_webhook: System.get_env("REMIND_MOVE_CHANNEL_WEBHOOK"), + discord_new_game_notif_webhook: System.get_env("NEW_GAME_CHANNEL_WEBHOOK") + config :chessh, Web, discord_client_id: System.get_env("DISCORD_CLIENT_ID"), discord_client_secret: System.get_env("DISCORD_CLIENT_SECRET"), diff --git a/lib/chessh/application.ex b/lib/chessh/application.ex index 59926cc..f92707f 100644 --- a/lib/chessh/application.ex +++ b/lib/chessh/application.ex @@ -18,6 +18,7 @@ defmodule Chessh.Application do children = [ Chessh.Repo, Chessh.SSH.Daemon, + Chessh.DiscordNotifier, Plug.Cowboy.child_spec( scheme: :http, plug: Chessh.Web.Endpoint, diff --git a/lib/chessh/discord/notifier.ex b/lib/chessh/discord/notifier.ex new file mode 100644 index 0000000..09c4ec0 --- /dev/null +++ b/lib/chessh/discord/notifier.ex @@ -0,0 +1,129 @@ +defmodule Chessh.DiscordNotifier do + use GenServer + + @name :discord_notifier + + alias Chessh.{Game, Player, Repo} + + def start_link(state \\ []) do + GenServer.start_link(__MODULE__, state, name: @name) + end + + @impl true + def init(state) do + {:ok, state} + end + + @impl true + def handle_cast(x, state), do: handle_info(x, state) + + @impl true + def handle_info({:attempt_notification, notification} = body, state) do + [discord_notification_rate, discord_notification_rate_ms] = + Application.get_env(:chessh, RateLimits) + |> Keyword.take([:discord_notification_rate, :discord_notification_rate_ms]) + |> Keyword.values() + + reschedule_delay = Application.get_env(:chessh, RateLimits)[:reschedule_delay] + + case Hammer.check_rate_inc( + :redis, + "discord-webhook-message-rate", + discord_notification_rate_ms, + discord_notification_rate, + 1 + ) do + {:allow, _count} -> + send_notification(notification) + + {:deny, _limit} -> + Process.send_after(self(), body, reschedule_delay) + end + + {:noreply, state} + end + + @impl true + def handle_info({:schedule_notification, notification, delay}, state) do + Process.send_after(self(), {:attempt_notification, notification}, delay) + {:noreply, state} + end + + defp send_notification({:move_reminder, game_id}) do + [min_delta_t, discord_game_move_notif_webhook] = + Application.get_env(:chessh, DiscordNotifications) + |> Keyword.take([:game_move_notif_delay_ms, :discord_game_move_notif_webhook]) + |> Keyword.values() + + case Repo.get(Game, game_id) do + nil -> + nil + + game -> + %Game{ + dark_player: %Player{discord_id: dark_player_discord_id}, + light_player: %Player{discord_id: light_player_discord_id}, + turn: turn, + updated_at: last_updated, + moves: move_count + } = Repo.preload(game, [:dark_player, :light_player]) + + delta_t = NaiveDateTime.diff(NaiveDateTime.utc_now(), last_updated, :millisecond) + + if delta_t >= min_delta_t do + post_discord( + 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})." + ) + end + end + end + + defp send_notification({:game_created, game_id}) do + [pingable_mention, discord_game_created_notif_webhook] = + Application.get_env(:chessh, DiscordNotifications) + |> Keyword.take([:looking_for_games_role_mention, :discord_new_game_notif_webhook]) + |> Keyword.values() + + case Repo.get(Game, game_id) do + nil -> + nil + + game -> + %Game{ + dark_player: dark_player, + light_player: light_player + } = Repo.preload(game, [:dark_player, :light_player]) + + message = + case {is_nil(light_player), is_nil(dark_player)} do + {true, false} -> + "#{pingable_mention}, <@#{dark_player.discord_id}> is looking for an opponent to play as light in Game #{game_id}" + + {false, true} -> + "#{pingable_mention}, <@#{light_player.discord_id}> is looking for an opponent to play as dark in Game #{game_id}" + + _ -> + false + end + + if message do + post_discord(discord_game_created_notif_webhook, message) + end + end + end + + defp post_discord(webhook, message) do + :httpc.request( + :post, + { + String.to_charlist(webhook), + [], + 'application/json', + %{content: message} |> Jason.encode!() |> String.to_charlist() + }, + [], + [] + ) + end +end diff --git a/lib/chessh/ssh/client/game/game.ex b/lib/chessh/ssh/client/game/game.ex index 4a79d05..fc48d6f 100644 --- a/lib/chessh/ssh/client/game/game.ex +++ b/lib/chessh/ssh/client/game/game.ex @@ -77,7 +77,7 @@ defmodule Chessh.SSH.Client.Game do ) do {:allow, _count} -> # Starting a new game - {:ok, %Game{} = game} = + {:ok, %Game{id: game_id} = game} = Game.changeset( %Game{}, Map.merge( @@ -92,6 +92,12 @@ defmodule Chessh.SSH.Client.Game do ) |> Repo.insert() + GenServer.cast( + :discord_notifier, + {:schedule_notification, {:game_created, game_id}, + Application.get_env(:chessh, DiscordNotifications)[:game_created_notif_delay_ms]} + ) + init([ %State{ state @@ -403,6 +409,12 @@ defmodule Chessh.SSH.Client.Game do :syn.publish(:games, {:game, game_id}, {:new_move, attempted_move}) + GenServer.cast( + :discord_notifier, + {:schedule_notification, {:move_reminder, game_id}, + Application.get_env(:chessh, DiscordNotifications)[:game_move_notif_delay_ms]} + ) + _ -> nil end diff --git a/priv/repo/migrations/20221219082326_create_player.exs b/priv/repo/migrations/20221219082326_create_player.exs index 0e605c9..2b7d2b6 100644 --- a/priv/repo/migrations/20221219082326_create_player.exs +++ b/priv/repo/migrations/20221219082326_create_player.exs @@ -2,11 +2,9 @@ defmodule Chessh.Repo.Migrations.CreatePlayer do use Ecto.Migration def change do - execute("CREATE EXTENSION IF NOT EXISTS citext", "") - create table(:players) do add(:discord_id, :string, null: false) - add(:username, :citext, null: false) + add(:username, :string, null: false) add(:hashed_password, :string, null: true) timestamps() end