From dffe7089337196ce7346da65ad97b6e03881837a Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Sun, 9 Jun 2019 11:46:29 +0200 Subject: [PATCH] Add two new parameters to tournament instant_finalists_amount is the amount of teams that instantly are in the finals after the group stage ends. intermediate_round_participants_amount is the amount of teams that have a chance to advance after group stage but must play a relegation game to actually do so. Both of these values need to match, so that instant_finalists_amount + (intermediate_round_participants_amount / 2) is a power of 2. Or to be more precise, the power of two that is saved in playoff_teams_amount of the tournament. --- app/serializers/tournament_serializer.rb | 3 ++- app/services/tournament_service.rb | 14 ++++++++++++++ db/migrate/0000_create_schema.rb | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 app/services/tournament_service.rb diff --git a/app/serializers/tournament_serializer.rb b/app/serializers/tournament_serializer.rb index 9805797..25582e1 100644 --- a/app/serializers/tournament_serializer.rb +++ b/app/serializers/tournament_serializer.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true class TournamentSerializer < SimpleTournamentSerializer - attributes :description + attributes :description, :playoff_teams_amount, + :instant_finalists_amount, :intermediate_round_participants_amount has_many :stages has_many :teams diff --git a/app/services/tournament_service.rb b/app/services/tournament_service.rb new file mode 100644 index 0000000..239d6fb --- /dev/null +++ b/app/services/tournament_service.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class TournamentService + class << self + def calculate_amount_of_teams_advancing(playoff_teams_amount, amount_of_groups) + # the amount of whole places that advance in a group (e. g. all 1rst places of every group instantly go through) + instant_finalists_amount = (playoff_teams_amount.floor / amount_of_groups.floor) * amount_of_groups.floor + # the amount of teams that still need to play an intermediate round before advancing to playoffs + intermediate_round_participants_amount = (playoff_teams_amount - instant_finalists_amount) * 2 + + [instant_finalists_amount, intermediate_round_participants_amount] + end + end +end diff --git a/db/migrate/0000_create_schema.rb b/db/migrate/0000_create_schema.rb index d0021a2..0d62ca2 100644 --- a/db/migrate/0000_create_schema.rb +++ b/db/migrate/0000_create_schema.rb @@ -52,6 +52,8 @@ class CreateSchema < ActiveRecord::Migration[5.2] t.string :description t.boolean :public, default: true t.integer :playoff_teams_amount + t.integer :instant_finalists_amount, default: 0 + t.integer :intermediate_round_participants_amount, default: 0 # relation to owner t.belongs_to :user, index: true, null: false, foreign_key: { on_delete: :cascade }