Skip to content

Commit

Permalink
Only request the same information once
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanDebrunner committed Dec 6, 2023
1 parent e4651b4 commit 69eeb37
Showing 1 changed file with 96 additions and 52 deletions.
148 changes: 96 additions & 52 deletions overlay/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ impl TeamInfoRaw {
)
.unwrap();

info!(
"Got team information for team {}, requesting member details",
team_id
);

let (members, flag) = tokio::join!(
futures::future::join_all(
data["roster"]
Expand Down Expand Up @@ -123,6 +128,8 @@ impl TeamInfoRaw {
);
let members = members.into_iter().collect();

info!("Got member details for team {}", team_id);

Self {
team_name: data["name"].as_str().map_or(
match team_color {
Expand Down Expand Up @@ -174,10 +181,7 @@ async fn fetch_game_data(
.send()
.await
{
info!(
"Got game data for tid:{}, gid:{} from UWH API",
tournament_id, game_id
);
info!("Got game data for tid:{tournament_id}, gid:{game_id} from UWH API");
let text = data
.text()
.await
Expand Down Expand Up @@ -237,6 +241,7 @@ async fn fetch_game_data(
TeamInfoRaw::new(uwhportal_url, tournament_id, team_id_black, Color::Black,),
TeamInfoRaw::new(uwhportal_url, tournament_id, team_id_white, Color::White,)
);
info!("Got all data for tid:{tournament_id}, gid:{game_id}. Sending to network thread");
tr.send((
GameData {
pool,
Expand All @@ -254,7 +259,7 @@ async fn fetch_game_data(
.unwrap();
return;
}
warn!("Game data request failed. Trying again in 5 seconds.");
warn!("Game data request for tid:{tournament_id}, gid:{game_id} failed. Trying again in 5 seconds.");
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
}
}
Expand Down Expand Up @@ -282,6 +287,15 @@ pub async fn networking_thread(
};
info!("Connected to refbox!");

#[derive(PartialEq, Eq)]
struct RequestedInfo {
tournament_id: u32,
game_id: u32,
is_current_game: bool,
}

let mut requested_infos = Vec::new();

let (tr, rc) = crossbeam_channel::bounded::<(GameData, bool)>(3);
let mut buff = vec![0u8; 1024];
let mut read_bytes;
Expand Down Expand Up @@ -322,21 +336,29 @@ pub async fn networking_thread(
|| next_game_data.as_ref().unwrap().game_id != next_gid
|| next_game_data.as_ref().unwrap().tournament_id != tournament_id_new
{
info!(
"Fetching game data to cache for tid: {}, gid: {}",
tournament_id_new, next_gid,
);
tokio::spawn(async move {
fetch_game_data(
tr_,
&uwhscores_url,
&uwhportal_url,
tournament_id_new,
next_gid,
false,
)
.await;
});
let requested_info = RequestedInfo {
tournament_id: tournament_id_new,
game_id: next_gid,
is_current_game: false,
};
if !requested_infos.contains(&requested_info) {
info!(
"Fetching game data to cache for tid: {}, gid: {}",
tournament_id_new, next_gid,
);
requested_infos.push(requested_info);
tokio::spawn(async move {
fetch_game_data(
tr_,
&uwhscores_url,
&uwhportal_url,
tournament_id_new,
next_gid,
false,
)
.await;
});
}
}

// initial case when no data is initialised
Expand All @@ -346,21 +368,29 @@ pub async fn networking_thread(
let uwhportal_url = config.uwhportal_url.clone();
game_id = Some(game_id_new);
tournament_id = Some(tournament_id_new);
info!(
"Fetching intial game data for tid: {}, gid: {}",
tournament_id_new, game_id_new
);
tokio::spawn(async move {
fetch_game_data(
tr_,
&uwhscores_url,
&uwhportal_url,
tournament_id_new,
game_id_new,
true,
)
.await;
});
let requested_info = RequestedInfo {
tournament_id: tournament_id_new,
game_id: game_id_new,
is_current_game: true,
};
if !requested_infos.contains(&requested_info) {
info!(
"Fetching intial game data for tid: {}, gid: {}",
tournament_id_new, game_id_new
);
requested_infos.push(requested_info);
tokio::spawn(async move {
fetch_game_data(
tr_,
&uwhscores_url,
&uwhportal_url,
tournament_id_new,
game_id_new,
true,
)
.await;
});
}
}

if let (Some(game_id_old), Some(tournament_id_old)) =
Expand Down Expand Up @@ -389,28 +419,42 @@ pub async fn networking_thread(
})
.unwrap_or_else(|e| error!("Frontend could not recieve snapshot!: {e}"));
} else {
info!(
"Fetching game data for tid: {}, gid: {}. Cache is empty or invalid!",
tournament_id_new, game_id_new,
);
let (uwhscores_url_, uwhportal_url_, tr__) =
(uwhscores_url.clone(), uwhportal_url.clone(), tr_.clone());
tokio::spawn(async move {
fetch_game_data(
tr__,
&uwhscores_url_,
&uwhportal_url_,
tournament_id_new,
game_id_new,
true,
)
.await;
});
let requested_info = RequestedInfo {
tournament_id: tournament_id_new,
game_id: game_id_new,
is_current_game: true,
};
if !requested_infos.contains(&requested_info) {
info!(
"Fetching game data for tid: {tournament_id_new}, gid: {game_id_new}. Cache is empty or invalid!"

);
requested_infos.push(requested_info);
let (uwhscores_url_, uwhportal_url_, tr__) =
(uwhscores_url.clone(), uwhportal_url.clone(), tr_.clone());
tokio::spawn(async move {
fetch_game_data(
tr__,
&uwhscores_url_,
&uwhportal_url_,
tournament_id_new,
game_id_new,
true,
)
.await;
});
}
}
continue;
}
}
if let Ok((game_data, is_current_game)) = rc.try_recv() {
let requested_info = RequestedInfo {
tournament_id: game_data.tournament_id,
game_id: game_data.game_id,
is_current_game,
};
requested_infos.retain(|info| *info != requested_info);
if is_current_game {
info!("Got game state update from network!");
tx.send(StatePacket {
Expand Down

0 comments on commit 69eeb37

Please sign in to comment.