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
18 changes: 9 additions & 9 deletions app/controllers/api/v1/matches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ def monthly
daily_metrics[day.strftime("%Y-%m-%d")] = {
matches: matches_json(day_matches),
total_matches: day_matches.count,
total_energy: calculations.sum { |c| c[:energyUsed] }.round(3),
total_energy_cost: calculations.sum { |c| c[:energyCost] }.round(2),
total_energy: day_matches.sum { |c| c[:energyUsed] }.round(3),
total_energy_cost: day_matches.sum { |c| c[:energyCost] }.round(2),
total_bft: {
amount: day_matches.sum(&:totalToken).to_f.round(3),
value: calculations.sum { |c| c[:tokenValue] }.round(2)
Expand Down Expand Up @@ -174,8 +174,8 @@ def monthly_summary
summary: {
matchesCount: @matches.count,
energyUsed: {
amount: calculations.sum { |c| c[:energyUsed] }.round(3),
cost: calculations.sum { |c| c[:energyCost] }.round(2)
amount: @matches.sum { |c| c[:energyUsed] }.round(3),
cost: @matches.sum { |c| c[:energyCost] }.round(2)
},
totalBft: {
amount: @matches.sum(&:totalToken).to_f.round(3),
Expand Down Expand Up @@ -209,7 +209,8 @@ def format_match_response(match)
date: match.date,
build: match.build,
map: match.map,
time: match.time,
energyUsed: match.energyUsed,
energyCost: match.energyCost,
result: match.result,
totalToken: match.totalToken,
totalPremiumCurrency: match.totalPremiumCurrency,
Expand All @@ -232,16 +233,15 @@ def match_json(match)
date: match.date,
build: match.build,
map: match.map,
time: match.time,
energyUsed: calculations[:energyUsed],
energyUsed: match.energyUsed,
energyCost: match.energyCost,
result: match.result,
totalToken: match.totalToken,
totalPremiumCurrency: match.totalPremiumCurrency,
bonusMultiplier: match.bonusMultiplier,
perksMultiplier: match.perksMultiplier,
luckrate: calculations[:luckrate],
calculated: {
energyCost: calculations[:energyCost],
tokenValue: calculations[:tokenValue],
premiumValue: calculations[:premiumValue],
profit: calculations[:profit]
Expand Down Expand Up @@ -272,13 +272,13 @@ def match_params
:date,
:build,
:map,
:time,
:result,
:totalToken,
:totalPremiumCurrency,
:bonusMultiplier,
:perksMultiplier,
:energyUsed,
:energyCost,
badge_used_attributes: [ :id, :slot, :rarity, :nftId, :_destroy ]
)
end
Expand Down
10 changes: 1 addition & 9 deletions app/models/match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ class Match < ApplicationRecord

# Callbacks
before_validation :normalize_map
before_validation :calculate_energy_used
before_save :calculate_values
before_update :reset_luckrate

# Validations essentielles
validates :build, presence: true
validates :map, presence: true, inclusion: { in: %w[toxic_river award radiation_rift] }
validates :time, presence: true, numericality: { only_integer: true, greater_than: 0 }
validates :energyUsed, presence: true, numericality: { greater_than: 0 }
validates :result, inclusion: { in: %w[win loss draw] }, allow_nil: true
validates :totalToken, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
Expand All @@ -26,16 +24,10 @@ def normalize_map
self.map = map.gsub(" ", "_") if map.present?
end

def calculate_energy_used
# Recalculer energyUsed si le temps a changé ou si energyUsed est nil
if time.present? && (energyUsed.nil? || time_changed?)
self.energyUsed = energyUsed.to_f
end
end


def calculate_values
# Valeurs par défaut
self.energyCost = (energyUsed.to_f * 1.49).round(2)
self.tokenValue = ((totalToken || 0) * 0.01).round(2)
self.premiumCurrencyValue = ((totalPremiumCurrency || 0) * 0.00744).round(2)
self.profit = (tokenValue + premiumCurrencyValue - energyCost).round(2)
Expand Down
1 change: 0 additions & 1 deletion app/services/data_lab/currency_rates_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
DEFAULT_RATES = {
flex: 0.00743,
bft: 3.0,
sm: 0.028, # Taux par défaut pour les Sponsor Marks

Check failure on line 7 in app/services/data_lab/currency_rates_service.rb

View workflow job for this annotation

GitHub Actions / lint

Style/TrailingCommaInHashLiteral: Avoid comma after the last item of a hash.
energy: 1.49
}.freeze

FLEX_PACKS = [
Expand Down
42 changes: 2 additions & 40 deletions app/services/data_lab/match_metrics_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,49 +22,11 @@ def calculate
private

def calculate_energy_used
return 0 unless @match.time
badge_count = @match.badge_used.count
((@match.time / Constants::MatchConstants::ENERGY_CONSUMPTION[:ONE_ENERGY_MINUTES]) * badge_count).round(3)
@match.energyUsed.to_f.round(3)
end

def calculate_energy_cost
badge_count = @match.badge_used.count
return 0 if badge_count.zero?

energy_cost = 0
badges_calculator = BadgesMetricsCalculator.new(@user)

# Charger et mettre en cache les badges et leurs coûts
badges = badges_calculator.load_badges
badges_calculator.cache_badges(badges)
badges_calculator.cache_recharge_costs(badges)

@match.badge_used.each do |badge_used|
# Récupérer la rareté du badge
rarity = badge_used.rarity&.capitalize
next unless rarity

# Obtenir le coût de recharge pour cette rareté
recharge_cost = badges_calculator.calculate_recharge_cost(rarity)
next unless recharge_cost[:total_usd].positive?

# Calculer le coût pour ce badge
energy_per_badge = calculate_energy_used / badge_count

# Trouver l'item correspondant à la rareté du badge pour obtenir max_energy
rarity_record = Rarity.find_by(name: rarity.capitalize)
next unless rarity_record

item = Item.joins(:type)
.where(types: { name: "Badge" }, rarity_id: rarity_record.id)
.first
next unless item&.max_energy&.positive?

energy_cost += (energy_per_badge * (recharge_cost[:total_usd] / item.max_energy))
end

energy_cost.round(2)
# (calculate_energy_used * Constants::CurrencyConstants.currency_rates[:energy]).round(2)
@match.energyCost.to_f.round(3)
end

def calculate_token_value
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20250725093058_remove_time_from_matches.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveTimeFromMatches < ActiveRecord::Migration[8.0]
def change
remove_column :matches, :time, :integer
end
end
3 changes: 1 addition & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading