summaryrefslogtreecommitdiff
path: root/lib/chessh/discord/notifier.ex
blob: 09c4ec0451cffce9c2dfa0135989e05114b27d51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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