Skip to content

Commit

Permalink
fix: remove global write state
Browse files Browse the repository at this point in the history
  • Loading branch information
sargon64 committed Sep 16, 2023
1 parent bd61722 commit 15b5e42
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
1 change: 1 addition & 0 deletions TournamentAssistantProtos
17 changes: 4 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ lazy_static::lazy_static! {
pub static ref TA_STATE : RwLock<packets::TAState> = {
RwLock::new(packets::TAState::new())
};
pub static ref TA_CON: RwLock<Option<connection::TAConnection>> = {
RwLock::new(None)
};

pub static ref TA_UPDATE_SINK: Sink<TAUpdates> = {
Sink::new()
Expand All @@ -73,14 +70,7 @@ async fn main() -> anyhow::Result<()> {
.unwrap()
.block_on(async {
log::info!("Connecting to Server...");
*TA_CON.write().await = Some(
connection::TAConnection::connect(
std::env::var("TA_WS_URI").unwrap(),
"TA-Relay-TX",
)
.await
.unwrap(),
);

let mut ta_con = connection::TAConnection::connect(
std::env::var("TA_WS_URI").unwrap(),
"TA-Relay-RX",
Expand All @@ -96,8 +86,9 @@ async fn main() -> anyhow::Result<()> {
continue;
}
};
tokio::spawn(async {
packets::route_packet(&mut *TA_STATE.write().await, msg)

tokio::spawn(async move {
packets::route_packet(&mut *TA_STATE.write().await,msg)
.await
.unwrap();

Expand Down
25 changes: 17 additions & 8 deletions src/packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::{
proto::{
models,
packet::{self, event},
},
TA_CON,
}, connection::TAConnection,
// TA_CON,
};

#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -105,15 +105,22 @@ impl TAState {
//add the overlay to the match's associated users.
r#match
.associated_users
.extend(self.server_users.iter().map(|u| u.guid.clone()));
.extend(self.server_users.iter().filter(|f| !f.name.contains("TX")).map(|u| u.guid.clone()));

self.matches.push(r#match.clone());

TA_CON
.write()
.await
.as_mut()
.unwrap()
let mut con = TAConnection::connect(
std::env::var("TA_WS_URI").unwrap(),
"TA-Relay-TX",
)
.await
.unwrap();

con
// .write()
// .await
// .as_mut()
// .unwrap()
.send(packet::Packet {
id: Uuid::new_v4().to_string(),
from: "".to_string(),
Expand All @@ -130,6 +137,8 @@ impl TAState {
)),
})
.await?;

con.close().await;
}
None => {
log::warn!("Received MatchCreatedEvent with no match");
Expand Down

0 comments on commit 15b5e42

Please sign in to comment.