summaryrefslogtreecommitdiff
path: root/lib/chessh/discord/notifier.ex
blob: 6a20e89d21db5d95fde77be755afcb3a079ce324 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
defmodule Chessh.DiscordNotifier do
  use GenServer

  @name :discord_notifier

  alias Chessh.{Game, Player, Repo}

  require Logger

  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, DiscordNotifications)[:reschedule_delay]

    case Hammer.check_rate_inc(
           :redis,
           "discord-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({:player_joined, game_id}) do
    Logger.info("Player joined in #{game_id}")

    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},
        discord_thread_id: nil
      } = 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 = Application.get_env(:chessh, DiscordNotifications)[:game_move_notif_delay_ms]

    case Repo.get(Game, game_id) |> Repo.preload([:dark_player, :light_player]) do
      %Game{
        dark_player: %Player{discord_id: dark_player_discord_id},
        light_player: %Player{discord_id: light_player_discord_id},
        turn: turn,
        last_move: last_move,
        updated_at: last_updated,
        moves: move_count,
        status: :continue
      } = game ->
        delta_t = NaiveDateTime.diff(NaiveDateTime.utc_now(), last_updated, :millisecond)
        game = maybe_put_new_thread_on_game(game)

        if delta_t >= min_delta_t do
          post_discord(
            game.discord_thread_id,
            %{
              content:
                "<@#{if turn == :light, do: light_player_discord_id, else: dark_player_discord_id}> it is your move in Game #{game_id} (move #{move_count}): your opponent played #{last_move}."
            }
          )
        end

      _ ->
        nil
    end
  end

  defp send_notification({:cleanup_thread, game_id}) do
    case Repo.get(Game, game_id) |> Repo.preload([:dark_player, :light_player]) do
      %Game{
        discord_thread_id: discord_thread_id,
        status: status
      } = game ->
        if !is_nil(discord_thread_id) && status != :continue do
          destroy_channel(discord_thread_id)

          Game.changeset(game, %{
            discord_thread_id: nil
          })
          |> Repo.update()
        end

      _ ->
        nil
    end
  end

  defp send_notification({:game_created, game_id}) do
    [pingable_mention, new_game_channel_id] =
      Application.get_env(:chessh, DiscordNotifications)
      |> Keyword.take([:looking_for_games_role_mention, :new_game_channel_id])
      |> Keyword.values()

    case Repo.get(Game, game_id) do
      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 with light pieces in Game #{game_id}"

            {false, true} ->
              "#{pingable_mention}, <@#{light_player.discord_id}> is looking for an opponent to play with dark pieces in Game #{game_id}"

            _ ->
              false
          end

        if message do
          post_discord(new_game_channel_id, %{content: message})
        end
    end
  end

  defp make_private_discord_thread_id(channel_id, %Game{
         id: game_id,
         dark_player: %Player{discord_id: dark_player_discord_id, username: dark_username},
         light_player: %Player{discord_id: light_player_discord_id, username: light_username}
       }) do
    case make_discord_api_call(
           :post,
           "channels/#{channel_id}/threads",
           %{
             # Private thread
             type: 12,
             name: "Game #{game_id} - #{light_username} V #{dark_username}"
           }
         ) do
      {:ok, {_, _, body}} ->
        %{"id" => thread_id} = Jason.decode!(body)

        [light_player_discord_id, dark_player_discord_id]
        |> Enum.map(fn id ->
          make_discord_api_call(:put, 'channels/#{thread_id}/thread-members/#{id}')
        end)

        post_discord(
          thread_id,
          %{
            content:
              "This private thread is used to communicate move notifications. It will be destroyed on game end."
          }
        )

        thread_id

      _ ->
        nil
    end
  end

  defp post_discord(channel_id, body) do
    make_discord_api_call(:post, "channels/#{channel_id}/messages", body)
  end

  defp destroy_channel(channel_id) do
    make_discord_api_call(:delete, "channels/#{channel_id}")
  end

  defp make_discord_api_call(method, route),
    do:
      :httpc.request(
        method,
        {
          'https://discord.com/api/#{route}',
          [
            make_authorization_header()
          ]
        },
        [],
        []
      )

  defp make_discord_api_call(method, route, body),
    do:
      :httpc.request(
        method,
        {
          'https://discord.com/api/#{route}',
          [
            make_authorization_header()
          ],
          'application/json',
          body
          |> Jason.encode!()
          |> String.to_charlist()
        },
        [],
        []
      )

  defp make_authorization_header() 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