simplify game start
This commit is contained in:
parent
cef5898c44
commit
80fcc059f3
|
@ -249,3 +249,9 @@ impl File {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for GameId {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
std::fmt::Display::fmt(&self.inner, f)
|
||||
}
|
||||
}
|
||||
|
|
24
src/main.rs
24
src/main.rs
|
@ -38,13 +38,16 @@ impl GameState {
|
|||
fn data_for_id(&self, game_id: &GameId) -> Option<GameData> {
|
||||
self.inner.lock().unwrap().get(game_id).cloned()
|
||||
}
|
||||
|
||||
fn save_data(&self, game_id: GameId, data: GameData) {
|
||||
self.inner.lock().unwrap().insert(game_id, data);
|
||||
}
|
||||
}
|
||||
|
||||
struct GameDropper(GameState, GameId);
|
||||
|
||||
impl Drop for GameDropper {
|
||||
fn drop(&mut self) {
|
||||
tracing::info!("Dropping");
|
||||
self.0.inner.lock().unwrap().remove(&self.1);
|
||||
}
|
||||
}
|
||||
|
@ -52,28 +55,21 @@ impl Drop for GameDropper {
|
|||
async fn start(Json(start): Json<Start>, game_state: Data<GameState>) -> HttpResponse {
|
||||
let game_id = GameId::new();
|
||||
|
||||
let (tx, mut rx) = tokio::sync::mpsc::channel(1);
|
||||
|
||||
let mut board_state = BoardState::starting_positions();
|
||||
|
||||
let serializable = board_state.to_serializable();
|
||||
|
||||
let game_dropper = GameDropper((**game_state).clone(), game_id.clone());
|
||||
|
||||
let game_data = GameData { sender: tx };
|
||||
let (sender, mut rx) = tokio::sync::mpsc::channel(1);
|
||||
|
||||
game_state
|
||||
.inner
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(game_id.clone(), game_data);
|
||||
game_state.save_data(game_id.clone(), GameData { sender });
|
||||
|
||||
let game_span = tracing::info_span!(
|
||||
parent: None,
|
||||
"Game Session",
|
||||
game.id = tracing::field::debug(&game_id)
|
||||
game.id = %game_id,
|
||||
);
|
||||
|
||||
let mut board_state = BoardState::starting_positions();
|
||||
let serializable = board_state.to_serializable();
|
||||
|
||||
actix_web::rt::spawn(
|
||||
async move {
|
||||
while let Ok(Some(msg)) =
|
||||
|
|
Loading…
Reference in a new issue