Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign some players to fixed tables #341

Merged
merged 22 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/rounds_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def pairings_data
{
id: pairing.id,
table_number: pairing.table_number,
table_label: pairing.table_label,
policy: {
view_decks:
},
Expand Down
10 changes: 5 additions & 5 deletions app/frontend/pairings/Pairing.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
export let stage: Stage;
export let round: Round;
export let pairing: Pairing;
let left_player = pairing.player1
let right_player = pairing.player2
let left_player = pairing.player1;
let right_player = pairing.player2;
if (stage.format == 'single_sided_swiss' && pairing.player2.side == 'corp') {
left_player = pairing.player2
right_player = pairing.player1
left_player = pairing.player2;
right_player = pairing.player1;
}
</script>

<div class="row m-1 round_pairing align-items-center table_{pairing.table_number}">
<div class="col-sm-2">
Table {pairing.table_number}
{pairing.table_label}
</div>
{#if pairing.policy.view_decks}
<a href="{round.id}/pairings/{pairing.id}/view_decks">
Expand Down
1 change: 1 addition & 0 deletions app/frontend/pairings/PairingsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type Round = {
export type Pairing = {
id: number;
table_number: number;
table_label: string;
policy: PairingPolicies;
player1: Player;
player2: Player;
Expand Down
8 changes: 8 additions & 0 deletions app/models/nil_player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ def runner_identity_object
def include_in_stream?
true
end

def fixed_table_number
nil
end

def fixed_table_number?
false
end
end
9 changes: 9 additions & 0 deletions app/models/pairing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ def side_for(player)
player1 == player ? player1_side : player2_side
end

def table_label
stage.double_elim? ? "Game #{table_number}" : "Table #{table_number}"
end

def fixed_table_number
players.each { |player| return player.fixed_table_number if player.fixed_table_number? }
nil
end

private

def normalise_scores_before_save
Expand Down
24 changes: 19 additions & 5 deletions app/services/pairing_strategies/swiss.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,27 @@ def auto_score(pairing, player_index)
end

def apply_numbers!(sorter)
sorter.sort(round.pairings.non_bye).each_with_index do |pairing, i|
pairing.update(table_number: i + 1)
next_table = 1
sorter.sort(round.pairings.non_bye).each do |pairing|
fixed_table = pairing.fixed_table_number
if fixed_table
table_number = fixed_table
else
table_number = next_table
next_table += 1
end
pairing.update(table_number:)
end

non_bye_tables = round.pairings.non_bye.count
round.pairings.bye.each_with_index do |pairing, i|
pairing.update(table_number: i + non_bye_tables + 1)
round.pairings.bye.each do |pairing|
fixed_table = pairing.fixed_table_number
if fixed_table
table_number = fixed_table
else
table_number = next_table
next_table += 1
end
pairing.update(table_number:)
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/rounds/_pairings.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- right_player_number = 1
.row.m-1.round_pairing.align-items-center class="table_#{pairing.table_number} #{'reported' if pairing.reported? && current_user == @tournament.user}"
.col-sm-2
| Table #{pairing.table_number}
| #{pairing.table_label}
- if @tournament.allow_streaming_opt_out? && policy(round.tournament).edit?
- if pairing.player_ids.all? { |player_id| @players[player_id].include_in_stream? }
= fa_icon 'video-camera',
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240909235836_add_fixed_table_to_players.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddFixedTableToPlayers < ActiveRecord::Migration[7.1]
def change
add_column :players, :fixed_table_number, :integer
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_07_26_082848) do
ActiveRecord::Schema[7.1].define(version: 2024_09_09_235836) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -137,6 +137,7 @@
t.boolean "include_in_stream", default: true
t.bigint "corp_identity_ref_id"
t.bigint "runner_identity_ref_id"
t.integer "fixed_table_number"
t.index ["corp_identity_ref_id"], name: "index_players_on_corp_identity_ref_id"
t.index ["runner_identity_ref_id"], name: "index_players_on_runner_identity_ref_id"
t.index ["tournament_id"], name: "index_players_on_tournament_id"
Expand Down
39 changes: 37 additions & 2 deletions spec/services/pairing_strategies/swiss_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

RSpec.describe PairingStrategies::Swiss do
let(:pairer) { described_class.new(round) }
let(:pairer) { described_class.new(round, Random.new(1000)) }
let(:round) { create(:round, number: 1, stage:) }
let(:stage) { tournament.current_stage }
let(:tournament) { create(:tournament) }
let(:nil_player) { instance_double(NilPlayer, id: nil, points: 0) }
let(:nil_player) { NilPlayer.new }

before do
allow(NilPlayer).to receive(:new).and_return(nil_player)
Expand Down Expand Up @@ -106,6 +106,31 @@
end
end
end

context 'with a player with a fixed table number' do
before do
jack.update fixed_table_number: 42
end

it 'creates pairings' do
pairer.pair!

round.reload

expect(pairings_table_by_player(round.pairings))
.to eq jack.name => 42, jill.name => 42, hansel.name => 1, gretel.name => 1
end

it 'creates first round bye' do
jack.update first_round_bye: true
pairer.pair!

round.reload

expect(pairings_table_by_player(round.pairings))
.to eq hansel.name => 1, gretel.name => 1, jill.name => 2, jack.name => 42
end
end
end

context 'with three players' do
Expand Down Expand Up @@ -223,4 +248,14 @@
end
end
end

def pairings_table_by_player(pairings)
index = {}
pairings.each do |pairing|
pairing.players.each do |player|
index[player.name] = pairing.table_number if player.id
end
end
index
end
end
Loading