Skip to content

Commit

Permalink
Add grand final and rebuttal matches for double elim tournaments
Browse files Browse the repository at this point in the history
Need to create tests for double elim tourneys
  • Loading branch information
ismellike committed Jun 3, 2024
1 parent a2a43cd commit 87d737a
Showing 1 changed file with 70 additions and 8 deletions.
78 changes: 70 additions & 8 deletions contracts/arena-tournament-module/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ fn generate_double_elimination_bracket(
{
// Get the bracket structure
let n = teams.len().next_power_of_two();
let sorted_indexes = NestedArray::Single((0..n).collect::<Vec<_>>()).nest();
let sorted_indexes = NestedArray::Single((0..n).collect_vec()).nest();

generate_matches(&sorted_indexes, teams, &mut matches, &mut layer_map);
}
Expand All @@ -263,9 +263,10 @@ fn generate_double_elimination_bracket(
let layers = layer_map.keys().rev().cloned().collect_vec();
for layer in layers {
// Add any new matches to the layer
layer_map
.get_mut(&layer)
.and_then(|x| Some(x.append(&mut next_layer_matches)));
layer_map.get_mut(&layer).map(|x| {
x.append(&mut next_layer_matches);
Some(())
});
let layer_matches = layer_map[&layer].len();
let n = layer_matches.next_power_of_two();
let mut adjusted_matches = NestedArray::Single(
Expand All @@ -279,7 +280,6 @@ fn generate_double_elimination_bracket(
// Create the base matches
let mut bye_match = None;
let mut prev_match = None;
dbg!(adjusted_matches.clone());
while !adjusted_matches.is_empty() {
let mut first = adjusted_matches.pop_front().unwrap_or(0);
let mut second = adjusted_matches.pop_front().unwrap_or(0);
Expand Down Expand Up @@ -333,7 +333,7 @@ fn generate_double_elimination_bracket(
}
}

// Update previous matches for next_match_loser
// Update previous matches for next match links
if first > 0 {
let i = first as usize - 1;
if matches[i].is_losers_bracket.is_none() {
Expand All @@ -353,7 +353,14 @@ fn generate_double_elimination_bracket(
}
}

// dbg!(matches.clone());
// Create the grand finale
let final_match_number = create_match(None, None, None, &mut matches, None);

let losers_final_i = matches.len() - 2;
matches[losers_final_i].next_match_winner = Some(final_match_number);
matches[0].next_match_winner = Some(final_match_number);

// The rebuttal match will be added dynamically on final processing

save_matches(&mut matches, tournament_id, deps.storage)
}
Expand All @@ -377,7 +384,7 @@ pub fn process_matches(

// Process each match result
for result in match_results {
MATCHES.update(
let mut match_ = MATCHES.update(
deps.storage,
(tournament_id.u128(), result.match_number.u128()),
|match_info| -> StdResult<_> {
Expand Down Expand Up @@ -426,6 +433,61 @@ pub fn process_matches(
Ok(match_info)
},
)?;

// If we're processing the last match of a double elim tournament, then we should add a rebuttal match if the loser's bracket won
if match_.match_number == tournament.extension.total_matches
&& matches!(
tournament.extension.elimination_type,
EliminationType::DoubleElimination
)
&& match_.is_losers_bracket.is_none()
{
let loser_final = MATCHES.load(
deps.storage,
(tournament_id.u128(), match_.match_number.u128() - 1),
)?;

let grand_final_winner = match match_.result.as_ref().unwrap() {
MatchResult::Team1 => match_.team_1.clone(),
MatchResult::Team2 => match_.team_2.clone(),
}
.unwrap();

let loser_final_winner = match loser_final.result.unwrap() {
MatchResult::Team1 => loser_final.team_1,
MatchResult::Team2 => loser_final.team_2,
}
.unwrap();

if grand_final_winner == loser_final_winner {
tournament.extension.total_matches += Uint128::one();
MATCHES.save(
deps.storage,
(
tournament_id.u128(),
tournament.extension.total_matches.u128(),
),
&Match {
match_number: tournament.extension.total_matches,
team_1: match_.team_1.clone(),
team_2: match_.team_2.clone(),
result: None,
next_match_winner: None,
next_match_loser: None,
is_losers_bracket: Some(true),
},
)?;

match_.next_match_loser = Some(tournament.extension.total_matches);
match_.next_match_winner = Some(tournament.extension.total_matches);

MATCHES.save(
deps.storage,
(tournament_id.u128(), match_.match_number.u128()),
&match_,
)?;
}
}
}

// Apply updates to the next matches
Expand Down

0 comments on commit 87d737a

Please sign in to comment.