From 79b903fc5632242a6ab1ee0f7732dec168331703 Mon Sep 17 00:00:00 2001 From: AndrewMurrell Date: Sun, 27 Apr 2014 01:50:33 -0400 Subject: Added 3 new seeding algorithms. --- app/models/tournament.rb | 1 + lib/sampling/double_bind.rb | 7 ------ lib/sampling/double_blind.rb | 7 ++++++ lib/seeding/README.md | 2 +- lib/seeding/early_bird_seeding.rb | 20 +++++++++++++++++ lib/seeding/fair_ranked_seeding.rb | 46 ++++++++++++++++++++++++++++++++++++++ lib/seeding/random_seeding.rb | 20 +++++++++++++++++ 7 files changed, 95 insertions(+), 8 deletions(-) delete mode 100644 lib/sampling/double_bind.rb create mode 100644 lib/sampling/double_blind.rb create mode 100644 lib/seeding/early_bird_seeding.rb create mode 100644 lib/seeding/fair_ranked_seeding.rb create mode 100644 lib/seeding/random_seeding.rb diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 2d4d6b6..154cc22 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -11,6 +11,7 @@ class Tournament < ActiveRecord::Base def settings @settings ||= Settings.new(self) end + def settings=(setting) setting.each do |key, value| value = false if value == "0" diff --git a/lib/sampling/double_bind.rb b/lib/sampling/double_bind.rb deleted file mode 100644 index 4a5201c..0000000 --- a/lib/sampling/double_bind.rb +++ /dev/null @@ -1,7 +0,0 @@ -module Sampling - module DoubleBlind - def works_with?(game) - return true - end - end -end diff --git a/lib/sampling/double_blind.rb b/lib/sampling/double_blind.rb new file mode 100644 index 0000000..4a5201c --- /dev/null +++ b/lib/sampling/double_blind.rb @@ -0,0 +1,7 @@ +module Sampling + module DoubleBlind + def works_with?(game) + return true + end + end +end diff --git a/lib/seeding/README.md b/lib/seeding/README.md index 0afbb94..67fc19b 100644 --- a/lib/seeding/README.md +++ b/lib/seeding/README.md @@ -1,4 +1,4 @@ Files in this directory should implement the following interface: - seed_matches(tournament) - take the matches of a tournament and the players in a tournament, assign players to teams, and teams to matches + take the matches of a tournament and the players in a tournament, assign players to teams, and teams to matches (all must exist) \ No newline at end of file diff --git a/lib/seeding/early_bird_seeding.rb b/lib/seeding/early_bird_seeding.rb new file mode 100644 index 0000000..30e15fc --- /dev/null +++ b/lib/seeding/early_bird_seeding.rb @@ -0,0 +1,20 @@ +module Seeding + class EarlyBirdSeeding + def seed_matches(tournament) + matches = tournament.current_stage.matches + match = matches.first + match_num = 0 + teams = 0 + tournament.players.each_slice(tournament.min_players_per_team) do |slice| + if teams < tournament.min_teams_per_match + match.teams[teams].players += slice + teams += 1 + else + match_num += 1 + match = matches[match_num] + teams = 0 + end + end + end + end +end \ No newline at end of file diff --git a/lib/seeding/fair_ranked_seeding.rb b/lib/seeding/fair_ranked_seeding.rb new file mode 100644 index 0000000..2d0eeb4 --- /dev/null +++ b/lib/seeding/fair_ranked_seeding.rb @@ -0,0 +1,46 @@ +module Seeding + class FairRankedSeeding + def seed_matches(tournament) + matches = tournament.current_stage.matches + match = matches.first + match_num = 0 + players_used = 0 + 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 best_first(tournament) + tournament.players.sort {|a, b| better(a, b, tournament) } + end + + def better(player1, player2, tournament) + ps1 = previousScore(player1, tournament) + ps2 = previousScore(player2, tournament) + if ps1 > ps2 + return 1 + elsif ps2 > ps1 + return -1 + else + return 0 + end + end + + def previousScore(player, tournament) + score = tournament.statistics.getStatistic(player.matches.last, player, :score) + if score.nil? + return 0 + end + score + end + end +end \ No newline at end of file diff --git a/lib/seeding/random_seeding.rb b/lib/seeding/random_seeding.rb new file mode 100644 index 0000000..ec39e61 --- /dev/null +++ b/lib/seeding/random_seeding.rb @@ -0,0 +1,20 @@ +module Seeding + class RandomSeeding + def seed_matches(tournament) + matches = tournament.current_stage.matches + match = matches.first + match_num = 0 + teams = 0 + tournament.players.shuffle.each_slice(tournament.min_players_per_team) do |slice| + if teams < tournament.min_teams_per_match + match.teams[teams].players += slice + teams += 1 + else + match_num += 1 + match = matches[match_num] + teams = 0 + end + end + end + end +end \ No newline at end of file -- cgit v1.1-4-g5e80