Skip to content

Commit

Permalink
Abstract functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lotuuu committed May 13, 2024
1 parent b6a5950 commit 8a31358
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 29 deletions.
37 changes: 11 additions & 26 deletions apps/champions/lib/champions/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -187,28 +187,18 @@ defmodule Champions.Users do
Get a user's available AFK rewards, according to their AFK reward rates and the time since their last claim.
If more than 12 hours have passed since the last claim, the user will have accumulated the maximum amount of rewards.
"""
def get_kaline_afk_rewards(user_id) do
def get_afk_rewards(user_id, type) when type in [:kaline, :dungeon] do
case Users.get_user(user_id) do
{:ok, user} ->
user.kaline_tree_level.afk_reward_rates
|> Enum.map(fn reward_rate ->
currency = Currencies.get_currency(reward_rate.currency_id)
amount = calculate_afk_rewards(user.last_kaline_afk_reward_claim, reward_rate)
%{currency: currency, amount: amount}
end)

{:error, :not_found} ->
{:error, :not_found}
end
end
{level, last_claim} =
case type do
:kaline -> {user.kaline_tree_level, user.last_kaline_afk_reward_claim}
:dungeon -> {user.dungeon_settlement_level, user.last_dungeon_afk_reward_claim}
end

def get_dungeon_afk_rewards(user_id) do
case Users.get_user(user_id) do
{:ok, user} ->
user.dungeon_settlement_level.afk_reward_rates
|> Enum.map(fn reward_rate ->
Enum.map(level.afk_reward_rates, fn reward_rate ->
currency = Currencies.get_currency(reward_rate.currency_id)
amount = calculate_afk_rewards(user.last_dungeon_afk_reward_claim, reward_rate)
amount = calculate_afk_rewards(last_claim, reward_rate)
%{currency: currency, amount: amount}
end)

Expand All @@ -228,14 +218,9 @@ defmodule Champions.Users do
@doc """
Claim a user's AFK rewards, and reset their last AFK reward claim time.
"""
def claim_kaline_afk_rewards(user_id) do
afk_rewards = get_kaline_afk_rewards(user_id)
claim_afk_rewards(user_id, afk_rewards, :kaline)
end

def claim_dungeon_afk_rewards(user_id) do
afk_rewards = get_dungeon_afk_rewards(user_id)
claim_afk_rewards(user_id, afk_rewards, :dungeon)
def claim_afk_rewards(user_id, type) when type in [:kaline, :dungeon] do
afk_rewards = get_afk_rewards(user_id, type)
claim_afk_rewards(user_id, afk_rewards, type)
end

defp claim_afk_rewards(user_id, afk_rewards, type) do
Expand Down
6 changes: 3 additions & 3 deletions apps/gateway/lib/gateway/champions_socket_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ defmodule Gateway.ChampionsSocketHandler do
end

defp handle(%GetKalineAfkRewards{user_id: user_id}),
do: prepare_response(%{afk_rewards: Users.get_kaline_afk_rewards(user_id)}, :afk_rewards)
do: prepare_response(%{afk_rewards: Users.get_afk_rewards(user_id, :kaline)}, :afk_rewards)

defp handle(%ClaimKalineAfkRewards{user_id: user_id}),
do: Users.claim_kaline_afk_rewards(user_id) |> prepare_response(:user)
do: Users.claim_afk_rewards(user_id, :kaline) |> prepare_response(:user)

defp handle(%GetBoxes{}) do
%{boxes: Gacha.get_boxes()} |> prepare_response(:boxes)
Expand Down Expand Up @@ -230,7 +230,7 @@ defmodule Gateway.ChampionsSocketHandler do
end

defp handle(%ClaimDungeonAfkRewards{user_id: user_id}) do
Users.claim_dungeon_afk_rewards(user_id) |> prepare_response(:user)
Users.claim_afk_rewards(user_id, :dungeon) |> prepare_response(:user)
end

defp handle(%LevelUpDungeonSettlement{user_id: user_id}) do
Expand Down

0 comments on commit 8a31358

Please sign in to comment.