Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions app/controllers/api/v1/admin/forge_settings_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
module Api
module V1
module Admin
class ForgeSettingsController < BaseController
before_action :set_forge_setting, only: [:show, :update, :destroy]

Check failure on line 5 in app/controllers/api/v1/admin/forge_settings_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 5 in app/controllers/api/v1/admin/forge_settings_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

def index
settings = ForgeSetting.includes(:rarity).references(:rarity).order('rarities.id ASC')

Check failure on line 8 in app/controllers/api/v1/admin/forge_settings_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
render json: settings.map { |s| json_for(s) }
end

def show
render json: json_for(@forge_setting)
end

def create
setting = ForgeSetting.new(forge_setting_params)
if setting.save
invalidate_caches
render json: json_for(setting), status: :created
else
render json: { errors: setting.errors.full_messages }, status: :unprocessable_entity
end
end

def update
if @forge_setting.update(forge_setting_params)
invalidate_caches
render json: json_for(@forge_setting)
else
render json: { errors: @forge_setting.errors.full_messages }, status: :unprocessable_entity
end
end

def destroy
@forge_setting.destroy
invalidate_caches
head :no_content
end

private

def set_forge_setting
@forge_setting = ForgeSetting.find(params[:id])
end

def forge_setting_params
params.require(:forge_setting).permit(
:rarity_id,
:operation_type,
:supply,
:nb_previous_required,
:nb_digital_required,
:cash,
:fusion_core,
:bft_tokens,
:sponsor_marks_reward
)
end

def json_for(setting)
{
id: setting.id,
rarity: setting.rarity.name,
rarity_id: setting.rarity_id,
operation_type: setting.operation_type,
supply: setting.supply,
nb_previous_required: setting.nb_previous_required,
nb_digital_required: setting.nb_digital_required,
cash: setting.cash,
fusion_core: setting.fusion_core,
bft_tokens: setting.bft_tokens,
sponsor_marks_reward: setting.sponsor_marks_reward,
created_at: setting.created_at,
updated_at: setting.updated_at
}
end

def invalidate_caches
Rails.cache.delete_matched("data_lab/forge/*")
end
end
end
end
end

Check failure on line 86 in app/controllers/api/v1/admin/forge_settings_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/TrailingEmptyLines: 2 trailing blank lines detected.

64 changes: 64 additions & 0 deletions app/controllers/api/v1/admin/perks_lock_settings_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module Api
module V1
module Admin
class PerksLockSettingsController < BaseController
before_action :set_setting, only: [:show, :update, :destroy]

Check failure on line 5 in app/controllers/api/v1/admin/perks_lock_settings_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 5 in app/controllers/api/v1/admin/perks_lock_settings_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

def index
settings = PerksLockSetting.includes(:rarity).order('rarities.id ASC')

Check failure on line 8 in app/controllers/api/v1/admin/perks_lock_settings_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
render json: settings.map { |s| json_for(s) }
end

def show
render json: json_for(@setting)
end

def create
setting = PerksLockSetting.new(setting_params)
if setting.save
render json: json_for(setting), status: :created
else
render json: { errors: setting.errors.full_messages }, status: :unprocessable_entity
end
end

def update
if @setting.update(setting_params)
render json: json_for(@setting)
else
render json: { errors: @setting.errors.full_messages }, status: :unprocessable_entity
end
end

def destroy
@setting.destroy
head :no_content
end

private

def set_setting
@setting = PerksLockSetting.find(params[:id])
end

def setting_params
params.require(:perks_lock_setting).permit(:rarity_id, :star_0, :star_1, :star_2, :star_3)
end

def json_for(s)
{
id: s.id,
rarity_id: s.rarity_id,
rarity: s.rarity.name,
star_0: s.star_0,
star_1: s.star_1,
star_2: s.star_2,
star_3: s.star_3
}
end
end
end
end
end

Check failure on line 63 in app/controllers/api/v1/admin/perks_lock_settings_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/TrailingEmptyLines: 2 trailing blank lines detected.

28 changes: 27 additions & 1 deletion app/controllers/api/v1/data_lab_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
before_action :authenticate_user!

def slots_metrics
cache_key = "data_lab/slots/#{current_user.id}/#{params[:badge_rarity]}"
game = Game.find_by(name: "Boss Fighters")
slots_version = Slot.where(game: game).maximum(:updated_at)&.to_i || 0
cache_key = "data_lab/slots/#{current_user.id}/#{params[:badge_rarity]}/#{slots_version}"
response = Rails.cache.fetch(cache_key, expires_in: 1.hour) do
DataLab::SlotsMetricsCalculator.new(current_user, params[:badge_rarity]).calculate
end
Expand Down Expand Up @@ -42,6 +44,30 @@
render json: response
end

def forge_metrics
type = params[:type].presence || "merge"
item = params[:item].presence || "digital"

version = [ForgeSetting.maximum(:updated_at), Rarity.maximum(:updated_at)].compact.max&.to_i || 0

Check failure on line 51 in app/controllers/api/v1/data_lab_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 51 in app/controllers/api/v1/data_lab_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
cache_key = "data_lab/forge/#{current_user.id}/#{type}/#{item}/#{version}"
response = Rails.cache.fetch(cache_key, expires_in: 1.hour) do
DataLab::ForgeMetricsCalculator.new(current_user).calculate(type: type, item: item)
end

render json: response
end

def perks_lock
settings = PerksLockSetting.includes(:rarity).order('rarities.id ASC')
render json: settings.map { |s| {
"RARITY": s.rarity.name,
"NO STAR": s.star_0 || 0,
"1 STAR": s.star_1 || 0,
"2 STARS": s.star_2 || 0,
"3 STARS": s.star_3 || 0
}}
end

def currency_metrics
cache_key = "data_lab/currency/#{current_user.id}"
response = Rails.cache.fetch(cache_key, expires_in: 1.hour) do
Expand Down
10 changes: 10 additions & 0 deletions app/models/forge_setting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class ForgeSetting < ApplicationRecord
belongs_to :rarity

OPERATION_TYPES = %w[merge_digital merge_nft craft_nft].freeze

validates :operation_type, presence: true, inclusion: { in: OPERATION_TYPES }
validates :rarity_id, presence: true
end


6 changes: 6 additions & 0 deletions app/models/perks_lock_setting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class PerksLockSetting < ApplicationRecord
belongs_to :rarity
validates :rarity_id, presence: true
end


12 changes: 6 additions & 6 deletions app/services/data_lab/craft_metrics_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def initialize(user)
end

def calculate
@badges = load_badges
@badges.map do |badge|
rarity = badge.rarity.name
craft_data = badge.item_crafting
@badges = load_items
@badges.map do |item|
rarity = item.rarity.name
craft_data = item.item_crafting
next unless craft_data

flex_cost = calculate_flex_cost(craft_data.craft_tokens)
Expand All @@ -24,7 +24,7 @@ def calculate

{
"1. rarity": rarity,
"2. supply": badge.supply,
"2. supply": item.supply,
"3. nb_previous_rarity_item": nb_previous_rarity,
"4. flex_craft": craft_data.craft_tokens,
"5. flex_craft_cost": format_currency(flex_cost),
Expand All @@ -40,7 +40,7 @@ def calculate_nb_previous_rarity(rarity)
rarity.downcase == "common" ? 0 : 2
end

def load_badges
def load_items
Item.includes(:type, :rarity, :item_crafting)
.joins(:rarity)
.where(types: { name: "Badge" })
Expand Down
85 changes: 85 additions & 0 deletions app/services/data_lab/forge_metrics_calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
module DataLab
class ForgeMetricsCalculator
include Constants::Utils

def initialize(user)
@user = user
@currency_rates = {
"bft" => Constants::CurrencyConstants.currency_rates[:bft],
"sm" => Constants::CurrencyConstants.currency_rates[:sm]
}
end

# params: type => "merge"|"craft", item => "digital"|"nft"
def calculate(type: "merge", item: "digital")
if type == "merge" && item == "digital"
merge_digital_table
elsif type == "merge" && item == "nft"
merge_nft_table
elsif type == "craft" && item == "nft"
craft_nft_table
else
[]
end
end

private

def merge_digital_table
# Inclure toutes les raretés disponibles, ordonnées
rarities = Rarity.order(:id).to_a
rarities.map do |rarity|
fs = ForgeSetting.find_by(rarity: rarity, operation_type: "merge_digital")
{
"RARITY": rarity.name,
"NB PREVIOUS RARITY ITEM": fs&.nb_previous_required,
"CASH": fs&.cash
}
end
end

def merge_nft_table
rarities = Rarity.order(:id).to_a
rarities.map do |rarity|
fs = ForgeSetting.find_by(rarity: rarity, operation_type: "merge_nft")
bft_tokens = fs&.bft_tokens
sp_reward = fs&.sponsor_marks_reward
bft_cost = bft_tokens ? (bft_tokens * @currency_rates["bft"]).round(2) : nil
sp_value = sp_reward ? (sp_reward * @currency_rates["sm"]).round(2) : nil
{
"RARITY": rarity.name,
"SUPPLY": fs&.supply,
"NB PREVIOUS RARITY ITEM": fs&.nb_previous_required,
"CASH": fs&.cash,
"FUSION CORE": fs&.fusion_core,
"$BFT": fs&.bft_tokens,
"$BFT COST": bft_cost ? format_currency(bft_cost) : nil,
"SP. MARKS REWARD": fs&.sponsor_marks_reward,
"SP. MARKS VALUE": sp_value ? format_currency(sp_value) : nil
}
end
end

def craft_nft_table
rarities = Rarity.order(:id).to_a
rarities.map do |rarity|
fs = ForgeSetting.find_by(rarity: rarity, operation_type: "craft_nft")
bft_tokens = fs&.bft_tokens
sp_reward = fs&.sponsor_marks_reward
bft_cost = bft_tokens ? (bft_tokens * @currency_rates["bft"]).round(2) : nil
sp_value = sp_reward ? (sp_reward * @currency_rates["sm"]).round(2) : nil
{
"RARITY": rarity.name,
"SUPPLY": fs&.supply,
"NB DIGITAL": fs&.nb_digital_required,
"$BFT": fs&.bft_tokens,
"$BFT COST": bft_cost ? format_currency(bft_cost) : nil,
"SP. MARKS REWARD": fs&.sponsor_marks_reward,
"SP. MARKS VALUE": sp_value ? format_currency(sp_value) : nil
}
end
end
end
end


Loading
Loading