diff --git a/app/controllers/api/v1/matches_controller.rb b/app/controllers/api/v1/matches_controller.rb index a1670bd..86ce7b4 100644 --- a/app/controllers/api/v1/matches_controller.rb +++ b/app/controllers/api/v1/matches_controller.rb @@ -125,10 +125,10 @@ 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(2), + total_energy: calculations.sum { |c| c[:energyUsed] }.round(3), total_energy_cost: calculations.sum { |c| c[:energyCost] }.round(2), total_bft: { - amount: day_matches.sum(&:totalToken).to_f.round(2), + amount: day_matches.sum(&:totalToken).to_f.round(3), value: calculations.sum { |c| c[:tokenValue] }.round(2) }, total_flex: { @@ -174,11 +174,11 @@ def monthly_summary summary: { matchesCount: @matches.count, energyUsed: { - amount: calculations.sum { |c| c[:energyUsed] }.round(2), + amount: calculations.sum { |c| c[:energyUsed] }.round(3), cost: calculations.sum { |c| c[:energyCost] }.round(2) }, totalBft: { - amount: @matches.sum(&:totalToken).to_f.round(2), + amount: @matches.sum(&:totalToken).to_f.round(3), value: calculations.sum { |c| c[:tokenValue] }.round(2) }, totalFlex: { diff --git a/app/controllers/api/v1/summaries_controller.rb b/app/controllers/api/v1/summaries_controller.rb index 3071412..946f90a 100644 --- a/app/controllers/api/v1/summaries_controller.rb +++ b/app/controllers/api/v1/summaries_controller.rb @@ -10,11 +10,11 @@ def daily render json: { matchesCount: matches.count, energyUsed: { - amount: total_calculations.sum { |c| c[:energyUsed] }.round(2), + amount: total_calculations.sum { |c| c[:energyUsed] }.round(3), cost: total_calculations.sum { |c| c[:energyCost] }.round(2) }, totalBft: { - amount: matches.sum(&:totalToken).to_f.round(2), + amount: matches.sum(&:totalToken).to_f.round(3), value: total_calculations.sum { |c| c[:tokenValue] }.round(2) }, totalFlex: { @@ -47,10 +47,10 @@ def monthly render json: { total_matches: matches.count, - total_energy: total_calculations.sum { |c| c[:energyUsed] }.round(2), + total_energy: total_calculations.sum { |c| c[:energyUsed] }.round(3), total_energy_cost: total_calculations.sum { |c| c[:energyCost] }.round(2), total_bft: { - amount: matches.sum(&:totalToken).to_f.round(2), + amount: matches.sum(&:totalToken).to_f.round(3), value: total_calculations.sum { |c| c[:tokenValue] }.round(2) }, total_flex: { diff --git a/app/models/match.rb b/app/models/match.rb index c37a966..5bb8fc4 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -17,7 +17,7 @@ class Match < ApplicationRecord 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: { only_integer: true, greater_than_or_equal_to: 0 }, allow_nil: true + validates :totalToken, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true validates :totalPremiumCurrency, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, allow_nil: true private @@ -29,13 +29,13 @@ def normalize_map 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 = (time.to_f / 10.0).round(2) + self.energyUsed = energyUsed.to_f end end def calculate_values # Valeurs par défaut - self.energyCost = (energyUsed * 1.49).round(2) + 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/db/migrate/20240711000000_change_total_token_to_decimal.rb b/db/migrate/20240711000000_change_total_token_to_decimal.rb new file mode 100644 index 0000000..8966a87 --- /dev/null +++ b/db/migrate/20240711000000_change_total_token_to_decimal.rb @@ -0,0 +1,9 @@ +class ChangeTotalTokenToDecimal < ActiveRecord::Migration[7.1] + def up + change_column :matches, :totalToken, :decimal, precision: 10, scale: 3 + end + + def down + change_column :matches, :totalToken, :integer + end +end diff --git a/db/migrate/20240711000001_change_energy_used_to_decimal.rb b/db/migrate/20240711000001_change_energy_used_to_decimal.rb new file mode 100644 index 0000000..f1ec493 --- /dev/null +++ b/db/migrate/20240711000001_change_energy_used_to_decimal.rb @@ -0,0 +1,9 @@ +class ChangeEnergyUsedToDecimal < ActiveRecord::Migration[7.1] + def up + change_column :matches, :energyUsed, :decimal, precision: 10, scale: 3 + end + + def down + change_column :matches, :energyUsed, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 17d7865..4fc501f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -128,9 +128,9 @@ t.integer "slots" t.float "luckrate" t.integer "time" - t.integer "energyUsed" + t.decimal "energyUsed", precision: 10, scale: 3 t.float "energyCost" - t.integer "totalToken" + t.decimal "totalToken", precision: 10, scale: 3 t.float "tokenValue" t.integer "totalPremiumCurrency" t.float "premiumCurrencyValue" @@ -188,9 +188,6 @@ t.boolean "active", default: true, null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.string "target_tweet_id" - t.string "target_tweet_url" - t.string "validation_type", default: "retweet" t.string "zealy_quest_id" t.index ["quest_id"], name: "index_quests_on_quest_id", unique: true end @@ -242,18 +239,6 @@ t.index ["game_id"], name: "index_slots_on_game_id" end - create_table "social_quest_submissions", force: :cascade do |t| - t.bigint "user_id", null: false - t.string "quest_id", null: false - t.string "submitted_tweet_url", null: false - t.boolean "valid", default: false - t.datetime "validated_at" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["user_id", "quest_id"], name: "index_social_quest_submissions_on_user_id_and_quest_id", unique: true - t.index ["user_id"], name: "index_social_quest_submissions_on_user_id" - end - create_table "team_members", force: :cascade do |t| t.bigint "team_id", null: false t.bigint "user_id", null: false @@ -433,7 +418,6 @@ t.index ["session_token"], name: "index_users_on_session_token" t.index ["stripe_customer_id"], name: "index_users_on_stripe_customer_id" t.index ["username"], name: "index_users_on_username", unique: true - t.index ["zealy_user_id"], name: "index_users_on_zealy_user_id", unique: true end add_foreign_key "badge_useds", "matches" @@ -454,8 +438,6 @@ add_foreign_key "rounds", "users", column: "boss_b_id" add_foreign_key "slots", "currencies" add_foreign_key "slots", "games" - add_foreign_key "social_quest_submissions", "quests", primary_key: "quest_id" - add_foreign_key "social_quest_submissions", "users" add_foreign_key "team_members", "teams" add_foreign_key "team_members", "users" add_foreign_key "teams", "tournaments"