feat: 1.0 stable api

This commit is contained in:
2025-12-02 13:05:35 -05:00
Unverified
parent 187bda91ef
commit f524284cb2
4 changed files with 127 additions and 73 deletions

View File

@@ -423,9 +423,15 @@ async fn handle_connection(
broadcast_message_all_observers(&observers, &message).await;
} else {
// Create next matches
// TODO: Make this a function
for (i, id) in tourney.top_half.iter().enumerate() {
let player1_addr = tourney.players.get(id).unwrap();
let player2_addr = tourney.players.get(tourney.bottom_half.get(i).unwrap()).unwrap();
let player2_addr = tourney.players.get(tourney.bottom_half.get(i).unwrap());
if player2_addr.is_none() { continue; }
let player2_addr = player2_addr.unwrap();
// TODO: gen without collisions
let match_id: u32 = rand::rng().random_range(100000..=999999);
let new_match = Arc::new(RwLock::new(Match::new(
match_id,
@@ -475,11 +481,11 @@ async fn handle_connection(
if !demo_mode {
let connection = opponent.connection.clone();
let column = column_parse.clone()?;
let waiting = *waiting_timeout.read().await * 1000 + (rand::rng().random_range(0..=500) - 250);
let waiting = *waiting_timeout.read().await as i64 * 1000 + (rand::rng().random_range(0..=500) - 250);
let matches_move = matches.clone();
let match_id_move = current_match.id;
current_match.wait_thread = Some(tokio::spawn(async move {
tokio::time::sleep(tokio::time::Duration::from_millis(waiting)).await;
tokio::time::sleep(tokio::time::Duration::from_millis(waiting as u64)).await;
let mut matches_guard = matches_move.write().await;
let mut current_match = matches_guard.get_mut(&match_id_move).unwrap().write().await;
@@ -708,6 +714,15 @@ async fn handle_connection(
matches.write().await.insert(match_id, new_match.clone());
}
}
else if text.starts_with("TOURNAMENT:WAIT:") {
if admin.read().await.is_none() || admin.read().await.unwrap() != addr {
let _ = send(&tx, "ERROR:INVALID:AUTH");
continue;
}
let new_timeout = text.split(":").collect::<Vec<&str>>()[2].parse::<f64>()?;
*waiting_timeout.write().await = (new_timeout * 1000.0) as u64;
}
else {
let _ = send(&tx, "ERROR:UNKNOWN");

View File

@@ -74,13 +74,23 @@ impl Tournament {
}
pub fn next(&mut self) {
let first = *self.bottom_half.last().unwrap();
let last = *self.top_half.last().unwrap();
if self.is_completed {
return;
}
self.top_half[0] = first;
self.bottom_half[0] = last;
if self.top_half.len() <= 1 || self.bottom_half.is_empty() {
self.is_completed = true;
return;
}
if self.top_half[0] == 0 {
let last_from_top = self.top_half.pop().unwrap();
let first_from_bottom = self.bottom_half.remove(0);
self.top_half.insert(1, first_from_bottom);
self.bottom_half.push(last_from_top);
let expected_bottom_start = self.top_half.len() as u32;
if self.top_half[1] == 1 && self.bottom_half[0] == expected_bottom_start {
self.is_completed = true;
}
}