From 8026e66d9b618566c225f52f12d87822b9609902 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Mon, 13 May 2019 11:53:09 +0200 Subject: [PATCH 01/19] Add :undecided_group_match and :decided_playoff_match --- spec/factories/matches.rb | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/spec/factories/matches.rb b/spec/factories/matches.rb index 3f0a89c..a069ecf 100644 --- a/spec/factories/matches.rb +++ b/spec/factories/matches.rb @@ -10,7 +10,18 @@ FactoryBot.define do after(:create) do |match, evaluator| match.match_scores = create_list(:match_score, evaluator.match_scores_count) end - state { 3 } + state { :in_progress } + end + + factory :decided_playoff_match do + transient do + match_scores_count { 2 } + end + after(:create) do |match, evaluator| + match.match_scores = create_list(:match_score, evaluator.match_scores_count, points: rand(10)) + match.match_scores.first.points += 1 + end + state { :team1_won } end end @@ -23,7 +34,17 @@ FactoryBot.define do after(:create) do |match, evaluator| match.match_scores = create_list(:match_score, evaluator.match_scores_count) end - state { 3 } + state { :in_progress } + end + + factory :undecided_group_match do + transient do + match_scores_count { 2 } + end + after(:create) do |match, evaluator| + match.match_scores = create_list(:match_score, evaluator.match_scores_count, points: 3) + end + state { :team1_won } end end end From 6d66328dc86336063f57b0a5074c40ada9ed1b22 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Thu, 9 May 2019 16:09:37 +0200 Subject: [PATCH 02/19] Move group_stage tournament into stage tournament --- spec/factories/tournaments.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/factories/tournaments.rb b/spec/factories/tournaments.rb index 6924332..942abad 100644 --- a/spec/factories/tournaments.rb +++ b/spec/factories/tournaments.rb @@ -19,11 +19,11 @@ FactoryBot.define do after(:create) do |tournament, evaluator| tournament.stages = create_list(:stage, evaluator.stage_count) end - end - factory :group_stage_tournament do - after(:create) do |tournament, _evaluator| - tournament.stages = create_list(:group_stage, 1) + factory :group_stage_tournament do + after(:create) do |tournament, _evaluator| + tournament.stages << create(:group_stage) + end end end end From 55f16563282e962c46dcc84a557eacd029e5f631 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Thu, 9 May 2019 16:14:27 +0200 Subject: [PATCH 03/19] Manually create the list of stages to assign stage levels correctly --- spec/factories/tournaments.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spec/factories/tournaments.rb b/spec/factories/tournaments.rb index 942abad..8c40054 100644 --- a/spec/factories/tournaments.rb +++ b/spec/factories/tournaments.rb @@ -17,7 +17,10 @@ FactoryBot.define do stage_count { 1 } end after(:create) do |tournament, evaluator| - tournament.stages = create_list(:stage, evaluator.stage_count) + # this is basically a manual create_list as we need to count up the level of the stage + tournament.stages.concat 1..evaluator.stage_count.map do |level| + create(:playoff_stage, level: level, match_count: -1) + end end factory :group_stage_tournament do From 7afb5ae552f9817bca82af79efda4eeddba01527 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Thu, 9 May 2019 16:21:49 +0200 Subject: [PATCH 04/19] Create realistic number of Matches When match_count is -1 the number of matches generated is automatically 2 ^ stage.level -> This is the amount of stages present in a "real" tournament stage. When supplying a positive number, it generates that many matches like before. This also now adds the position to the match list after creating them --- spec/factories/stages.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spec/factories/stages.rb b/spec/factories/stages.rb index e8dce2d..168acb6 100644 --- a/spec/factories/stages.rb +++ b/spec/factories/stages.rb @@ -19,7 +19,10 @@ FactoryBot.define do match_count { 4 } end after(:create) do |stage, evaluator| - stage.matches = create_list(:match, evaluator.match_count) + # match_count -1 automatically generates 2 ^ stage.level matches + # (as this would be the amount of stages present in the real world) + stage.matches = create_list(:match, evaluator.match_count == -1 ? 2**stage.level : evaluator.match_count) + stage.matches.each_with_index { |match, i| match.position = i } end end end From cb82ca44d8385fc2dd2674ad6df7b855849f3d2a Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Thu, 9 May 2019 20:56:30 +0200 Subject: [PATCH 05/19] Put running playoff matches in a playoff stage (only on first level) --- spec/factories/stages.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spec/factories/stages.rb b/spec/factories/stages.rb index 168acb6..13c2e65 100644 --- a/spec/factories/stages.rb +++ b/spec/factories/stages.rb @@ -16,12 +16,15 @@ FactoryBot.define do factory :playoff_stage do level { rand(10) } transient do + match_state { :not_started } match_count { 4 } end after(:create) do |stage, evaluator| # match_count -1 automatically generates 2 ^ stage.level matches # (as this would be the amount of stages present in the real world) - stage.matches = create_list(:match, evaluator.match_count == -1 ? 2**stage.level : evaluator.match_count) + stage.matches = create_list(:running_playoff_match, + evaluator.match_count == -1 ? 2**stage.level : evaluator.match_count, + state: evaluator.match_state) stage.matches.each_with_index { |match, i| match.position = i } end end From 8a70d5f333a1f10dd13b921cfe405a20e40ad7ad Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Fri, 10 May 2019 11:50:46 +0200 Subject: [PATCH 06/19] For stage tournament, first stage has running matches --- spec/factories/tournaments.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/spec/factories/tournaments.rb b/spec/factories/tournaments.rb index 8c40054..8b62bb5 100644 --- a/spec/factories/tournaments.rb +++ b/spec/factories/tournaments.rb @@ -18,9 +18,14 @@ FactoryBot.define do end after(:create) do |tournament, evaluator| # this is basically a manual create_list as we need to count up the level of the stage - tournament.stages.concat 1..evaluator.stage_count.map do |level| - create(:playoff_stage, level: level, match_count: -1) - end + levels = 1..evaluator.stage_count + tournament.stages.concat levels + .map { |level| + create(:playoff_stage, + level: level, + match_count: -1, + match_state: level == evaluator.stage_count ? :in_progress : :not_started) + } end factory :group_stage_tournament do From 42d9b0957f4798273da49baca5762f3de9e0f001 Mon Sep 17 00:00:00 2001 From: Thor77 Date: Fri, 10 May 2019 12:52:49 +0200 Subject: [PATCH 07/19] Save match position --- spec/factories/stages.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spec/factories/stages.rb b/spec/factories/stages.rb index 13c2e65..a8e55b9 100644 --- a/spec/factories/stages.rb +++ b/spec/factories/stages.rb @@ -25,7 +25,10 @@ FactoryBot.define do stage.matches = create_list(:running_playoff_match, evaluator.match_count == -1 ? 2**stage.level : evaluator.match_count, state: evaluator.match_state) - stage.matches.each_with_index { |match, i| match.position = i } + stage.matches.each_with_index do |match, i| + match.position = i + match.save + end end end end From b9afa956cee034fd2bac2c9703a9d843cdc0e064 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Fri, 10 May 2019 13:04:36 +0200 Subject: [PATCH 08/19] Add empty_prepared_playoff_match --- spec/factories/matches.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/factories/matches.rb b/spec/factories/matches.rb index a069ecf..36cb2d3 100644 --- a/spec/factories/matches.rb +++ b/spec/factories/matches.rb @@ -13,6 +13,10 @@ FactoryBot.define do state { :in_progress } end + factory :empty_prepared_playoff_match do + state { :not_started } + end + factory :decided_playoff_match do transient do match_scores_count { 2 } From 80b14900d0b399ce3f0381b56a7b443ee0f823e8 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Fri, 10 May 2019 13:07:59 +0200 Subject: [PATCH 09/19] Makes match_type configurable instead of only setting its state --- spec/factories/stages.rb | 7 +++---- spec/factories/tournaments.rb | 18 +++++++++++------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/spec/factories/stages.rb b/spec/factories/stages.rb index a8e55b9..8348817 100644 --- a/spec/factories/stages.rb +++ b/spec/factories/stages.rb @@ -16,15 +16,14 @@ FactoryBot.define do factory :playoff_stage do level { rand(10) } transient do - match_state { :not_started } + match_type { :running_playoff_match } match_count { 4 } end after(:create) do |stage, evaluator| # match_count -1 automatically generates 2 ^ stage.level matches # (as this would be the amount of stages present in the real world) - stage.matches = create_list(:running_playoff_match, - evaluator.match_count == -1 ? 2**stage.level : evaluator.match_count, - state: evaluator.match_state) + stage.matches = create_list(evaluator.match_type, + evaluator.match_count == -1 ? 2**stage.level : evaluator.match_count) stage.matches.each_with_index do |match, i| match.position = i match.save diff --git a/spec/factories/tournaments.rb b/spec/factories/tournaments.rb index 8b62bb5..2360ff9 100644 --- a/spec/factories/tournaments.rb +++ b/spec/factories/tournaments.rb @@ -19,13 +19,17 @@ FactoryBot.define do after(:create) do |tournament, evaluator| # this is basically a manual create_list as we need to count up the level of the stage levels = 1..evaluator.stage_count - tournament.stages.concat levels - .map { |level| - create(:playoff_stage, - level: level, - match_count: -1, - match_state: level == evaluator.stage_count ? :in_progress : :not_started) - } + + tournament.stages.concat(levels.map do |level| + create(:playoff_stage, + level: level, + match_count: -1, + match_type: if evaluator.stage_count + :running_playoff_match + else + :empty_prepared_playoff_match + end) + end) end factory :group_stage_tournament do From c55b45bdc157d42790c8389de6639fa4896ec55f Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Fri, 10 May 2019 13:14:44 +0200 Subject: [PATCH 10/19] Add position to all matches --- spec/factories/matches.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/factories/matches.rb b/spec/factories/matches.rb index 36cb2d3..54090c4 100644 --- a/spec/factories/matches.rb +++ b/spec/factories/matches.rb @@ -3,6 +3,7 @@ FactoryBot.define do factory :playoff_match, aliases: [:match], class: Match do stage + position { 0 } factory :running_playoff_match do transient do match_scores_count { 2 } @@ -31,6 +32,7 @@ FactoryBot.define do factory :group_match, class: Match do group + position { 0 } factory :running_group_match do transient do match_scores_count { 2 } From 3f4c6336c23b7c813edd46928633fa2de617b8ad Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Fri, 10 May 2019 18:30:13 +0200 Subject: [PATCH 11/19] Add dummy_stage_tournament --- spec/factories/tournaments.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/spec/factories/tournaments.rb b/spec/factories/tournaments.rb index 2360ff9..1e73fcd 100644 --- a/spec/factories/tournaments.rb +++ b/spec/factories/tournaments.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true FactoryBot.define do - factory :tournament do + factory :tournament, aliases: [:stageless_tournament] do name { Faker::Creature::Dog.name } description { Faker::Lorem.sentence } user @@ -38,5 +38,14 @@ FactoryBot.define do end end end + + factory :dummy_stage_tournament do + transient do + stage_count { 3 } + end + after(:create) do |tournament, evaluator| + tournament.stages.concat create_list(:stage, evaluator.stage_count) + end + end end end From f73820f2f2c4e31fa8419745a18ae1583f88f23f Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Fri, 10 May 2019 18:49:39 +0200 Subject: [PATCH 12/19] Reduce standart amount of teams within a tournament to 8 --- spec/factories/tournaments.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/factories/tournaments.rb b/spec/factories/tournaments.rb index 1e73fcd..eb24912 100644 --- a/spec/factories/tournaments.rb +++ b/spec/factories/tournaments.rb @@ -6,7 +6,7 @@ FactoryBot.define do description { Faker::Lorem.sentence } user transient do - teams_count { 16 } + teams_count { 8 } end after(:create) do |tournament, evaluator| tournament.teams = create_list(:team, evaluator.teams_count, tournament: tournament) From 03d7369a1c2ec06223d2029ca9412a319f7acb4c Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Fri, 10 May 2019 20:44:18 +0200 Subject: [PATCH 13/19] Remove link between team.owner and tournament --- app/models/match_score.rb | 2 +- app/models/team.rb | 5 ++++- spec/controllers/teams_controller_spec.rb | 3 ++- spec/controllers/tournaments_controller_spec.rb | 4 ++-- spec/factories/teams.rb | 5 ----- 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/app/models/match_score.rb b/app/models/match_score.rb index 408952d..b2a3f0c 100644 --- a/app/models/match_score.rb +++ b/app/models/match_score.rb @@ -4,5 +4,5 @@ class MatchScore < ApplicationRecord belongs_to :match belongs_to :team - delegate :owner, to: :team + delegate :owner, to: :match end diff --git a/app/models/team.rb b/app/models/team.rb index f266f9c..7f2580b 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -7,5 +7,8 @@ class Team < ApplicationRecord validates :name, presence: true - delegate :owner, to: :tournament + def owner + match_scores.first.owner + # this will produce errors if we make teams reusable + end end diff --git a/spec/controllers/teams_controller_spec.rb b/spec/controllers/teams_controller_spec.rb index 7bcbdb8..3fc4516 100644 --- a/spec/controllers/teams_controller_spec.rb +++ b/spec/controllers/teams_controller_spec.rb @@ -4,7 +4,8 @@ require 'rails_helper' RSpec.describe TeamsController, type: :controller do before do - @team = create(:team) + match_score = create(:match_score) + @team = match_score.team @owner = @team.owner end diff --git a/spec/controllers/tournaments_controller_spec.rb b/spec/controllers/tournaments_controller_spec.rb index da27ca7..7231e60 100644 --- a/spec/controllers/tournaments_controller_spec.rb +++ b/spec/controllers/tournaments_controller_spec.rb @@ -8,8 +8,8 @@ RSpec.describe TournamentsController, type: :controller do @user = @tournament.owner @another_user = create(:user) @private_tournament = create(:tournament, user: @another_user, public: false) - @teams = create_list(:detached_team, 4) - @teams16 = create_list(:detached_team, 16) + @teams = create_list(:team, 4) + @teams16 = create_list(:team, 16) @groups = create_list(:group, 4) end diff --git a/spec/factories/teams.rb b/spec/factories/teams.rb index b33adc4..234f71c 100644 --- a/spec/factories/teams.rb +++ b/spec/factories/teams.rb @@ -3,10 +3,5 @@ FactoryBot.define do factory :team do name { Faker::Creature::Dog.name } - tournament - end - - factory :detached_team, class: Team do - name { Faker::Creature::Dog.name } end end From dcb19461beb4a3e272974127ad4e73a4531eebce Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Fri, 10 May 2019 23:13:12 +0200 Subject: [PATCH 14/19] Add group_stage_only_tournament factory --- spec/controllers/statistics_controller_spec.rb | 2 +- spec/factories/tournaments.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/spec/controllers/statistics_controller_spec.rb b/spec/controllers/statistics_controller_spec.rb index 36673b2..d516098 100644 --- a/spec/controllers/statistics_controller_spec.rb +++ b/spec/controllers/statistics_controller_spec.rb @@ -13,7 +13,7 @@ RSpec.describe StatisticsController, type: :controller do context 'tournament with a group stage' do before do - @tournament = create(:group_stage_tournament) + @tournament = create(:group_stage_only_tournament) @group_stage = @tournament.stages.find_by(level: -1) @most_dominant_score = create(:group_score, team: @tournament.teams.first, diff --git a/spec/factories/tournaments.rb b/spec/factories/tournaments.rb index eb24912..a24edf8 100644 --- a/spec/factories/tournaments.rb +++ b/spec/factories/tournaments.rb @@ -12,6 +12,15 @@ FactoryBot.define do tournament.teams = create_list(:team, evaluator.teams_count, tournament: tournament) end + factory :group_stage_only_tournament do + transient do + group_count { 2 } + end + after(:create) do |tournament, evaluator| + tournament.stages << create(:group_stage, group_count: evaluator.group_count) + end + end + factory :stage_tournament do transient do stage_count { 1 } From d548eee245efc6cc950a32f2637b998b34134c8d Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Wed, 15 May 2019 09:00:51 +0200 Subject: [PATCH 15/19] Change .map to .each --- spec/factories/tournaments.rb | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/spec/factories/tournaments.rb b/spec/factories/tournaments.rb index a24edf8..6320cde 100644 --- a/spec/factories/tournaments.rb +++ b/spec/factories/tournaments.rb @@ -27,18 +27,16 @@ FactoryBot.define do end after(:create) do |tournament, evaluator| # this is basically a manual create_list as we need to count up the level of the stage - levels = 1..evaluator.stage_count - - tournament.stages.concat(levels.map do |level| - create(:playoff_stage, - level: level, - match_count: -1, - match_type: if evaluator.stage_count - :running_playoff_match - else - :empty_prepared_playoff_match - end) - end) + (1..evaluator.stage_count).each do |level| + tournament.stages << create(:playoff_stage, + level: level, + match_count: -1, + match_type: if evaluator.stage_count + :running_playoff_match + else + :empty_prepared_playoff_match + end) + end end factory :group_stage_tournament do From b5377db1f323c28aaf3d633a257a63bd99f745b1 Mon Sep 17 00:00:00 2001 From: Thor77 Date: Wed, 15 May 2019 09:36:36 +0200 Subject: [PATCH 16/19] Reformat indention --- spec/factories/tournaments.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/spec/factories/tournaments.rb b/spec/factories/tournaments.rb index 6320cde..6d7098e 100644 --- a/spec/factories/tournaments.rb +++ b/spec/factories/tournaments.rb @@ -28,14 +28,12 @@ FactoryBot.define do after(:create) do |tournament, evaluator| # this is basically a manual create_list as we need to count up the level of the stage (1..evaluator.stage_count).each do |level| - tournament.stages << create(:playoff_stage, - level: level, - match_count: -1, - match_type: if evaluator.stage_count - :running_playoff_match - else - :empty_prepared_playoff_match - end) + tournament.stages << create( + :playoff_stage, + level: level, + match_count: -1, + match_type: evaluator.stage_count ? :running_playoff_match : :empty_prepared_playoff_match + ) end end From 27709f0136876b45731594d8f2f7c5ba0c151d1a Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Wed, 15 May 2019 11:23:12 +0200 Subject: [PATCH 17/19] This change will basically solve climate change as it saves all the computing power in the whole world. --- spec/factories/matches.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/factories/matches.rb b/spec/factories/matches.rb index 54090c4..030c87c 100644 --- a/spec/factories/matches.rb +++ b/spec/factories/matches.rb @@ -23,7 +23,8 @@ FactoryBot.define do match_scores_count { 2 } end after(:create) do |match, evaluator| - match.match_scores = create_list(:match_score, evaluator.match_scores_count, points: rand(10)) + match.match_scores = create_list(:match_score, evaluator.match_scores_count, points: 37) + # random number generated by blapplications match.match_scores.first.points += 1 end state { :team1_won } From b36e3c4251c7c9145ebcb9a0013d8fe1f1993931 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Wed, 15 May 2019 08:53:31 +0200 Subject: [PATCH 18/19] Delegate owner of team to tournament again --- app/models/team.rb | 5 +---- spec/controllers/teams_controller_spec.rb | 3 +-- spec/factories/stages.rb | 2 +- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/app/models/team.rb b/app/models/team.rb index 7f2580b..f266f9c 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -7,8 +7,5 @@ class Team < ApplicationRecord validates :name, presence: true - def owner - match_scores.first.owner - # this will produce errors if we make teams reusable - end + delegate :owner, to: :tournament end diff --git a/spec/controllers/teams_controller_spec.rb b/spec/controllers/teams_controller_spec.rb index 3fc4516..8dc9eb1 100644 --- a/spec/controllers/teams_controller_spec.rb +++ b/spec/controllers/teams_controller_spec.rb @@ -4,8 +4,7 @@ require 'rails_helper' RSpec.describe TeamsController, type: :controller do before do - match_score = create(:match_score) - @team = match_score.team + @team = create(:tournament).teams.first @owner = @team.owner end diff --git a/spec/factories/stages.rb b/spec/factories/stages.rb index 8348817..0bbc826 100644 --- a/spec/factories/stages.rb +++ b/spec/factories/stages.rb @@ -26,7 +26,7 @@ FactoryBot.define do evaluator.match_count == -1 ? 2**stage.level : evaluator.match_count) stage.matches.each_with_index do |match, i| match.position = i - match.save + match.save! end end end From 51056d340ca0cc6bc50e0e099a1ee33e5226d74b Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Wed, 22 May 2019 09:19:16 +0200 Subject: [PATCH 19/19] Save the whole stage instead of the single matches --- spec/factories/stages.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/factories/stages.rb b/spec/factories/stages.rb index 0bbc826..b5736cd 100644 --- a/spec/factories/stages.rb +++ b/spec/factories/stages.rb @@ -26,8 +26,8 @@ FactoryBot.define do evaluator.match_count == -1 ? 2**stage.level : evaluator.match_count) stage.matches.each_with_index do |match, i| match.position = i - match.save! end + stage.save! end end end