Skip to content

Commit

Permalink
Merge pull request #19 from Vrabbers/game-refactor
Browse files Browse the repository at this point in the history
Game refactor
  • Loading branch information
Vrabbers authored Mar 29, 2024
2 parents 63d5c7b + d746130 commit 6c6021e
Show file tree
Hide file tree
Showing 22 changed files with 1,139 additions and 851 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM elixir:1.14-alpine AS build
FROM elixir:1.16-alpine AS build

RUN mkdir /app
WORKDIR /app
Expand All @@ -14,7 +14,7 @@ RUN mix local.rebar --force
RUN mix deps.get
RUN mix release

FROM alpine:3.17 AS final
FROM alpine:3.19 AS final

RUN mkdir /app
WORKDIR /app
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ without IEx, use ``mix --no-halt``.

## Bot usage

To start a game type <kbd>ffc:join</kbd> in a guild channel. Other users can then join in by also typing in
<kbd>ffc:join</kbd>. Once you've got all your friends in, type in <kbd>ffc:close</kbd> to start the game!
To start a game type `/create` in a guild channel. Other users can then join in by pressing the join button. Once you've got all your friends in, press <kbd>Close and start</kbd> to start the game!
4 changes: 4 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ config :nostrum,
:direct_messages,
:message_content
]

config :hammer,
backend:
{Hammer.Backend.ETS, [expiry_ms: :timer.minutes(10), cleanup_interval_ms: :timer.minutes(2)]}
4 changes: 2 additions & 2 deletions lib/ffc_ex/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ defmodule FfcEx.Application do
def start(_type, _args) do
children = [
{Task.Supervisor, name: FfcEx.TaskSupervisor},
FfcEx.Game.MessageQueue,
FfcEx.PlayerRouter,
FfcEx.GameRegistrySupervisor,
FfcEx.GameLobbies,
FfcEx.DmCache,
FfcEx.Interactions,
FfcEx.BaseConsumer,
FfcEx.ThumbnailCache
]

opts = [strategy: :one_for_one, name: FfcEx.Supervisor]
Expand All @@ -30,7 +30,7 @@ defmodule FfcEx.GameRegistrySupervisor do
def init([]) do
children = [
FfcEx.GameSupervisor,
FfcEx.GameRegistry
{Registry, keys: :unique, name: FfcEx.GameRegistry}
]

# The registry and the supervisor are interdependent
Expand Down
31 changes: 19 additions & 12 deletions lib/ffc_ex/base_consumer.ex
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
defmodule FfcEx.BaseConsumer do
use Nostrum.Consumer

alias FfcEx.Broadcaster
alias FfcEx.GameResponder
alias FfcEx.Interactions
alias FfcEx.{Game, GameCmdParser, GameRegistry, PlayerRouter}
alias FfcEx.{GameCmdParser, GameRegistry, PlayerRouter}
alias Nostrum.{Api, Struct.Event, Struct.User, Struct.Message}

require Logger

def start_link() do
Consumer.start_link(__MODULE__, name: __MODULE__)
end

@impl true
def handle_event({:READY, %Event.Ready{v: v}, _ws_state}) do
Logger.info("#{__MODULE__} ready on gateway v#{v}.")
Expand All @@ -31,21 +29,30 @@ defmodule FfcEx.BaseConsumer do
case int do
nil ->
id = PlayerRouter.lookup(msg.author.id)
GameRegistry.get_game(id)
GameRegistry.get_game_responder(id)

x ->
GameRegistry.get_game(x)
GameRegistry.get_game_responder(x)
end

if game != nil and Game.part_of?(game, msg.author.id) do
res = Game.do_cmd(game, msg.author.id, cmd)

if game != nil and GameResponder.part_of?(game, msg.author.id) do
if int != nil do
PlayerRouter.set_for(msg.author.id, int)
end

if res and match?({:chat, _}, cmd) do
Api.create_reaction!(msg.channel_id, msg.id, "✅")
case GameResponder.command(game, msg.author.id, cmd) do
{:green_check, term} ->
Broadcaster.send_messages(term, msg.author.id)
Api.create_reaction!(msg.channel_id, msg.id, "✅")

:chat_too_long ->
Api.create_reaction!(msg.channel_id, msg.id, "❗")

:ratelimited ->
Api.create_reaction!(msg.channel_id, msg.id, "⏰")

term ->
Broadcaster.send_messages(term, msg.author.id)
end
end
end
Expand Down
53 changes: 53 additions & 0 deletions lib/ffc_ex/broadcaster.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
defmodule FfcEx.Broadcaster do
alias FfcEx.ThumbnailCache
alias FfcEx.DmCache

def tell(user_id, message) do
send_messages({:tell, user_id, message}, :no_author)
end

def broadcast_to(users, message) do
send_messages({:broadcast_to, users, message}, :no_author)
end

def send_messages(messages, author_id) when is_list(messages) do
Enum.each(messages, &send_messages(&1, author_id))
end

def send_messages({:tell, uid, msg}, author_id) do
Task.await(do_tell(uid, msg, author_id))
end

def send_messages({:broadcast_to, uids, msg}, _author_id) do
Task.await_many(
for user_id <- uids do
do_tell(user_id, msg)
end
)
end

defp do_tell(user_id, message, author_id) do
id =
if user_id == :author do
author_id
else
user_id
end

Task.Supervisor.async(FfcEx.TaskSupervisor, fn -> do_send_to(id, message) end)
end

defp do_tell(user_id, message) do
Task.Supervisor.async(FfcEx.TaskSupervisor, fn -> do_send_to(user_id, message) end)
end

defp do_send_to(user_id, message) do
{:ok, channel} = DmCache.create(user_id)

if Keyword.keyword?(message) do
ThumbnailCache.send_with_thumbnail_caching!(channel, message)
else
Nostrum.Api.create_message!(channel, message)
end
end
end
7 changes: 7 additions & 0 deletions lib/ffc_ex/dm_cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ defmodule FfcEx.DmCache do
case Api.create_dm(user) do
{:ok, channel} ->
:ets.insert(table, {user, channel.id})
Process.send_after(self(), {:invalidate, user}, :timer.hours(1))
{:reply, {:ok, channel.id}, table}

{:error, _} ->
{:reply, :error, table}
end
end

@impl true
def handle_info({:invalidate, user}, table) do
:ets.delete(table, user)
{:noreply, table}
end
end
13 changes: 13 additions & 0 deletions lib/ffc_ex/format.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule FfcEx.Format do
def uname(uid) when is_integer(uid) do
{:ok, user} = Nostrum.Api.get_user(uid)
uname(user)
end

def uname(user) do
case user.discriminator do
"0" -> user.username
x -> "#{user.username}\##{x}"
end
end
end
Loading

0 comments on commit 6c6021e

Please sign in to comment.