From 0b073bf45a210330aafa69a942566119a411d090 Mon Sep 17 00:00:00 2001 From: James Date: Fri, 25 Jul 2025 11:23:46 +0200 Subject: [PATCH 1/2] maj backend for delete time & add input energyUsed+Cost --- app/controllers/api/v1/matches_controller.rb | 18 +++++++++--------- app/models/match.rb | 10 +--------- .../data_lab/currency_rates_service.rb | 1 - 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/app/controllers/api/v1/matches_controller.rb b/app/controllers/api/v1/matches_controller.rb index 86ce7b4..3e069ac 100644 --- a/app/controllers/api/v1/matches_controller.rb +++ b/app/controllers/api/v1/matches_controller.rb @@ -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) @@ -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), @@ -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, @@ -232,8 +233,8 @@ 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, @@ -241,7 +242,6 @@ def match_json(match) perksMultiplier: match.perksMultiplier, luckrate: calculations[:luckrate], calculated: { - energyCost: calculations[:energyCost], tokenValue: calculations[:tokenValue], premiumValue: calculations[:premiumValue], profit: calculations[:profit] @@ -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 diff --git a/app/models/match.rb b/app/models/match.rb index 5bb8fc4..f803a67 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -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 @@ -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) diff --git a/app/services/data_lab/currency_rates_service.rb b/app/services/data_lab/currency_rates_service.rb index 061258c..2a3e85f 100644 --- a/app/services/data_lab/currency_rates_service.rb +++ b/app/services/data_lab/currency_rates_service.rb @@ -5,7 +5,6 @@ class CurrencyRatesService flex: 0.00743, bft: 3.0, sm: 0.028, # Taux par défaut pour les Sponsor Marks - energy: 1.49 }.freeze FLEX_PACKS = [ From cc342135ac5af9b733f6ce2edf449e31cbca992a Mon Sep 17 00:00:00 2001 From: James Date: Fri, 25 Jul 2025 14:39:10 +0200 Subject: [PATCH 2/2] delete IG time bdd --- .../data_lab/match_metrics_calculator.rb | 42 +------------------ ...20250725093058_remove_time_from_matches.rb | 5 +++ db/schema.rb | 3 +- 3 files changed, 8 insertions(+), 42 deletions(-) create mode 100644 db/migrate/20250725093058_remove_time_from_matches.rb diff --git a/app/services/data_lab/match_metrics_calculator.rb b/app/services/data_lab/match_metrics_calculator.rb index 25ad428..4336ff2 100644 --- a/app/services/data_lab/match_metrics_calculator.rb +++ b/app/services/data_lab/match_metrics_calculator.rb @@ -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 diff --git a/db/migrate/20250725093058_remove_time_from_matches.rb b/db/migrate/20250725093058_remove_time_from_matches.rb new file mode 100644 index 0000000..9647fb6 --- /dev/null +++ b/db/migrate/20250725093058_remove_time_from_matches.rb @@ -0,0 +1,5 @@ +class RemoveTimeFromMatches < ActiveRecord::Migration[8.0] + def change + remove_column :matches, :time, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 4fc501f..72cfefa 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_04_29_211224) do +ActiveRecord::Schema[8.0].define(version: 2025_07_25_093058) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" @@ -127,7 +127,6 @@ t.float "feeCost" t.integer "slots" t.float "luckrate" - t.integer "time" t.decimal "energyUsed", precision: 10, scale: 3 t.float "energyCost" t.decimal "totalToken", precision: 10, scale: 3