2023-11-01 21:12:18 +01:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
sync::Arc,
|
|
|
|
task::{ready, Poll},
|
|
|
|
};
|
|
|
|
|
|
|
|
use axum::response::sse::Event;
|
|
|
|
use futures_util::Stream;
|
|
|
|
use tokio::{
|
|
|
|
select,
|
|
|
|
sync::{
|
|
|
|
broadcast::{error::RecvError, Receiver},
|
|
|
|
RwLock,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
use tokio_util::sync::ReusableBoxFuture;
|
|
|
|
|
2023-11-06 18:38:00 +01:00
|
|
|
use crate::{
|
|
|
|
error::Error,
|
|
|
|
game::{Game, GameId, PlayerId},
|
|
|
|
};
|
2023-11-01 21:12:18 +01:00
|
|
|
|
|
|
|
pub struct PlayerBroadcastStream {
|
2023-11-06 18:38:00 +01:00
|
|
|
id: GameId,
|
|
|
|
player_id: PlayerId,
|
|
|
|
games: Arc<RwLock<HashMap<GameId, Game>>>,
|
2023-11-01 21:12:18 +01:00
|
|
|
inner: ReusableBoxFuture<'static, (Result<Result<Event, Error>, RecvError>, Receiver<()>)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PlayerBroadcastStream {
|
|
|
|
async fn make_future(
|
|
|
|
mut rx: Receiver<()>,
|
2023-11-06 18:38:00 +01:00
|
|
|
games: Arc<RwLock<HashMap<GameId, Game>>>,
|
|
|
|
id: GameId,
|
|
|
|
player_id: PlayerId,
|
2023-11-01 21:12:18 +01:00
|
|
|
) -> (Result<Result<Event, Error>, RecvError>, Receiver<()>) {
|
|
|
|
let result = match rx.recv().await {
|
2023-11-06 18:38:00 +01:00
|
|
|
Ok(_) => Ok(Self::build_template(games, &id, &player_id).await),
|
2023-11-01 21:12:18 +01:00
|
|
|
Err(e) => Err(e),
|
|
|
|
};
|
|
|
|
(result, rx)
|
|
|
|
}
|
|
|
|
async fn build_template(
|
2023-11-06 18:38:00 +01:00
|
|
|
games: Arc<RwLock<HashMap<GameId, Game>>>,
|
|
|
|
id: &GameId,
|
|
|
|
player_id: &PlayerId,
|
2023-11-01 21:12:18 +01:00
|
|
|
) -> Result<Event, Error> {
|
|
|
|
let games = games.read().await;
|
2023-11-06 18:38:00 +01:00
|
|
|
let game = games.get(id).ok_or(Error::NotFound)?;
|
2023-11-01 21:12:18 +01:00
|
|
|
|
2023-11-06 18:38:00 +01:00
|
|
|
Ok(Event::default().data(game.player_view(player_id, true).await?))
|
2023-11-01 21:12:18 +01:00
|
|
|
}
|
|
|
|
pub fn new(
|
|
|
|
recv: Receiver<()>,
|
2023-11-06 18:38:00 +01:00
|
|
|
games: Arc<RwLock<HashMap<GameId, Game>>>,
|
|
|
|
id: GameId,
|
|
|
|
player_id: PlayerId,
|
2023-11-01 21:12:18 +01:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
inner: ReusableBoxFuture::new(Self::make_future(
|
|
|
|
recv,
|
|
|
|
games.clone(),
|
|
|
|
id.clone(),
|
|
|
|
player_id.clone(),
|
|
|
|
)),
|
|
|
|
games,
|
|
|
|
id,
|
|
|
|
player_id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stream for PlayerBroadcastStream {
|
|
|
|
type Item = Result<Event, Error>;
|
|
|
|
|
|
|
|
fn poll_next(
|
|
|
|
mut self: std::pin::Pin<&mut Self>,
|
|
|
|
cx: &mut std::task::Context<'_>,
|
|
|
|
) -> std::task::Poll<Option<Self::Item>> {
|
|
|
|
let (result, rx) = ready!(self.inner.poll(cx));
|
|
|
|
let future = Self::make_future(
|
|
|
|
rx,
|
|
|
|
self.games.clone(),
|
|
|
|
self.id.clone(),
|
|
|
|
self.player_id.clone(),
|
|
|
|
);
|
|
|
|
self.inner.set(future);
|
|
|
|
match result {
|
|
|
|
Ok(item) => Poll::Ready(Some(item)),
|
|
|
|
Err(RecvError::Closed) => Poll::Ready(None),
|
|
|
|
Err(RecvError::Lagged(n)) => Poll::Ready(Some(Err(Error::StreamLagged(n)))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ViewerBroadcastStream {
|
2023-11-06 18:38:00 +01:00
|
|
|
id: GameId,
|
|
|
|
games: Arc<RwLock<HashMap<GameId, Game>>>,
|
2023-11-01 21:12:18 +01:00
|
|
|
inner: ReusableBoxFuture<
|
|
|
|
'static,
|
|
|
|
(
|
|
|
|
Result<Result<Event, Error>, RecvError>,
|
|
|
|
Receiver<()>,
|
|
|
|
Receiver<()>,
|
|
|
|
),
|
|
|
|
>,
|
|
|
|
|
2023-11-06 18:38:00 +01:00
|
|
|
base_url: &'static str,
|
2023-11-01 21:12:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ViewerBroadcastStream {
|
|
|
|
async fn make_future(
|
|
|
|
mut rx1: Receiver<()>,
|
|
|
|
mut rx2: Receiver<()>,
|
2023-11-06 18:38:00 +01:00
|
|
|
games: Arc<RwLock<HashMap<GameId, Game>>>,
|
|
|
|
id: GameId,
|
|
|
|
base_url: &'static str,
|
2023-11-01 21:12:18 +01:00
|
|
|
) -> (
|
|
|
|
Result<Result<Event, Error>, RecvError>,
|
|
|
|
Receiver<()>,
|
|
|
|
Receiver<()>,
|
|
|
|
) {
|
|
|
|
let result = match select! {
|
|
|
|
a = rx1.recv() => a,
|
|
|
|
b = rx2.recv() => b
|
|
|
|
} {
|
2023-11-06 18:38:00 +01:00
|
|
|
Ok(_) => Ok(Self::build_template(games, &id, base_url).await),
|
2023-11-01 21:12:18 +01:00
|
|
|
Err(e) => Err(e),
|
|
|
|
};
|
|
|
|
(result, rx1, rx2)
|
|
|
|
}
|
|
|
|
async fn build_template(
|
2023-11-06 18:38:00 +01:00
|
|
|
games: Arc<RwLock<HashMap<GameId, Game>>>,
|
|
|
|
id: &GameId,
|
|
|
|
base_url: &'static str,
|
2023-11-01 21:12:18 +01:00
|
|
|
) -> Result<Event, Error> {
|
|
|
|
let games = games.read().await;
|
2023-11-06 18:38:00 +01:00
|
|
|
let game = games.get(id).ok_or(Error::NotFound)?;
|
2023-11-01 21:12:18 +01:00
|
|
|
|
2023-11-06 18:38:00 +01:00
|
|
|
Ok(Event::default().data(game.viewer_view(true, base_url).await?))
|
2023-11-01 21:12:18 +01:00
|
|
|
}
|
|
|
|
pub fn new(
|
|
|
|
rx1: Receiver<()>,
|
|
|
|
rx2: Receiver<()>,
|
2023-11-06 18:38:00 +01:00
|
|
|
games: Arc<RwLock<HashMap<GameId, Game>>>,
|
|
|
|
id: GameId,
|
|
|
|
base_url: &'static str,
|
2023-11-01 21:12:18 +01:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
inner: ReusableBoxFuture::new(Self::make_future(
|
|
|
|
rx1,
|
|
|
|
rx2,
|
|
|
|
games.clone(),
|
|
|
|
id.clone(),
|
2023-11-06 18:38:00 +01:00
|
|
|
base_url,
|
2023-11-01 21:12:18 +01:00
|
|
|
)),
|
|
|
|
games,
|
|
|
|
id,
|
|
|
|
base_url,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stream for ViewerBroadcastStream {
|
|
|
|
type Item = Result<Event, Error>;
|
|
|
|
|
|
|
|
fn poll_next(
|
|
|
|
mut self: std::pin::Pin<&mut Self>,
|
|
|
|
cx: &mut std::task::Context<'_>,
|
|
|
|
) -> std::task::Poll<Option<Self::Item>> {
|
|
|
|
let (result, rx1, rx2) = ready!(self.inner.poll(cx));
|
2023-11-06 18:38:00 +01:00
|
|
|
let future =
|
|
|
|
Self::make_future(rx1, rx2, self.games.clone(), self.id.clone(), self.base_url);
|
2023-11-01 21:12:18 +01:00
|
|
|
self.inner.set(future);
|
|
|
|
match result {
|
|
|
|
Ok(item) => Poll::Ready(Some(item)),
|
|
|
|
Err(RecvError::Closed) => Poll::Ready(None),
|
|
|
|
Err(RecvError::Lagged(n)) => Poll::Ready(Some(Err(Error::StreamLagged(n)))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|