diff options
Diffstat (limited to 'lib/seeding')
-rw-r--r-- | lib/seeding/.keep | 0 | ||||
-rw-r--r-- | lib/seeding/README.md | 10 | ||||
-rw-r--r-- | lib/seeding/early_bird_seeding.rb | 20 | ||||
-rw-r--r-- | lib/seeding/fair_ranked_seeding.rb | 43 | ||||
-rw-r--r-- | lib/seeding/random_seeding.rb | 20 |
5 files changed, 93 insertions, 0 deletions
diff --git a/lib/seeding/.keep b/lib/seeding/.keep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lib/seeding/.keep diff --git a/lib/seeding/README.md b/lib/seeding/README.md new file mode 100644 index 0000000..d323b6d --- /dev/null +++ b/lib/seeding/README.md @@ -0,0 +1,10 @@ +Seeding interface +================= + +Files in this directory should be _modules_ implement the following +interface: + + - `seed(TournamentStage)` + + Take a tournament stage, assign players to teams and teams to + matches (matches must exist). diff --git a/lib/seeding/early_bird_seeding.rb b/lib/seeding/early_bird_seeding.rb new file mode 100644 index 0000000..bf7b3c2 --- /dev/null +++ b/lib/seeding/early_bird_seeding.rb @@ -0,0 +1,20 @@ +module Seeding + module EarlyBirdSeeding + def self.seed(tournament_stage) + matches = tournament_stage.matches + match = matches.first + match_num = 0 + teams = 0 + tournament_stage.tournament.players.each_slice(tournament_stage.tournament.min_players_per_team) do |slice| + if teams < tournament_stage.tournament.min_teams_per_match + match.teams.push Team.create(players: slice) + teams += 1 + else + match_num += 1 + match = matches[match_num] + teams = 0 + end + end + end + end +end diff --git a/lib/seeding/fair_ranked_seeding.rb b/lib/seeding/fair_ranked_seeding.rb new file mode 100644 index 0000000..870ebdd --- /dev/null +++ b/lib/seeding/fair_ranked_seeding.rb @@ -0,0 +1,43 @@ +module Seeding + module FairRankedSeeding + def self.seed(tournament_stage) + matches = tournament.current_stage.matches + match = matches.first + match_num = 0 + players_used = 0 + (tournament.players.count/tournament.min_players_per_team).floor.times do + match.teams.push Team.create() + end + best_first(tournament).each_slice(tournament.min_teams_per_match) do |slice| + (0..tournament.min_teams_per_match-1).each do |index| + match.teams[index].players += slice[index] + end + players_used += 1 + if players_used == tournament.min_players_per_team + match_num += 1 + match = matches[match_num] + players_used = 0 + end + end + end + + private + def self.best_first(tournament) + tournament.players.sort {|a, b| better(a, b, tournament) } + end + + def self.better(player1, player2, tournament) + ps1 = previous_score(player1, tournament) + ps2 = previous_score(player2, tournament) + ps1 <=> ps2 + end + + def self.previous_score(player, tournament) + score = tournament.statistics.where(match: player.matches.last, user: player, name: :score) + if score.nil? + return 0 + end + score + end + end +end diff --git a/lib/seeding/random_seeding.rb b/lib/seeding/random_seeding.rb new file mode 100644 index 0000000..ccdba11 --- /dev/null +++ b/lib/seeding/random_seeding.rb @@ -0,0 +1,20 @@ +module Seeding + module RandomSeeding + def self.seed(tournament_stage) + matches = tournament_stage.matches + match = matches.first + match_num = 0 + teams = 0 + tournament_stage.tournament.players.shuffle.each_slice(tournament_stage.tournament.min_players_per_team) do |slice| + if teams < tournament_stage.tournament.min_teams_per_match + match.teams.push Team.create(players: slice) + teams += 1 + else + match_num += 1 + match = matches[match_num] + teams = 0 + end + end + end + end +end |