GameStory for client on game end
This commit is contained in:
parent
3602d18039
commit
e57d4a3cbf
|
|
@ -7,7 +7,11 @@ use core::num::NonZeroU8;
|
||||||
pub use ident::*;
|
pub use ident::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{character::CharacterId, game::GameOver, role::RoleTitle};
|
use crate::{
|
||||||
|
character::CharacterId,
|
||||||
|
game::{GameOver, story::GameStory},
|
||||||
|
role::RoleTitle,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||||
pub enum ClientMessage {
|
pub enum ClientMessage {
|
||||||
|
|
@ -46,6 +50,7 @@ pub enum ServerMessage {
|
||||||
InvalidMessageForGameState,
|
InvalidMessageForGameState,
|
||||||
NoSuchTarget,
|
NoSuchTarget,
|
||||||
GameOver(GameOver),
|
GameOver(GameOver),
|
||||||
|
Story(GameStory),
|
||||||
Update(PlayerUpdate),
|
Update(PlayerUpdate),
|
||||||
Sleep,
|
Sleep,
|
||||||
Reset,
|
Reset,
|
||||||
|
|
|
||||||
|
|
@ -77,6 +77,20 @@ impl JoinedPlayers {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn send_to(&self, player_ids: &[PlayerId], message: ServerMessage) {
|
||||||
|
let players: tokio::sync::MutexGuard<'_, HashMap<PlayerId, JoinedPlayer>> =
|
||||||
|
self.players.lock().await;
|
||||||
|
let senders = players
|
||||||
|
.iter()
|
||||||
|
.filter(|(pid, _)| player_ids.contains(*pid))
|
||||||
|
.map(|(pid, p)| (*pid, p.sender.clone()))
|
||||||
|
.collect::<Box<[_]>>();
|
||||||
|
core::mem::drop(players);
|
||||||
|
for (_, send) in senders {
|
||||||
|
send.send(message.clone()).log_debug();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn send_all_lobby(&self, in_lobby: Box<[PublicIdentity]>, in_lobby_ids: &[PlayerId]) {
|
pub async fn send_all_lobby(&self, in_lobby: Box<[PublicIdentity]>, in_lobby_ids: &[PlayerId]) {
|
||||||
let players: tokio::sync::MutexGuard<'_, HashMap<PlayerId, JoinedPlayer>> =
|
let players: tokio::sync::MutexGuard<'_, HashMap<PlayerId, JoinedPlayer>> =
|
||||||
self.players.lock().await;
|
self.players.lock().await;
|
||||||
|
|
|
||||||
|
|
@ -277,11 +277,17 @@ impl GameRunner {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum ProcessOutcome {
|
||||||
|
Lobby(Lobby),
|
||||||
|
SendPlayer(PlayerId, ServerMessage),
|
||||||
|
}
|
||||||
|
|
||||||
pub struct GameEnd {
|
pub struct GameEnd {
|
||||||
game: Option<GameRunner>,
|
game: Option<GameRunner>,
|
||||||
page: Option<usize>,
|
page: Option<usize>,
|
||||||
result: GameOver,
|
result: GameOver,
|
||||||
last_error_log: Instant,
|
last_error_log: Instant,
|
||||||
|
notified_connected: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GameEnd {
|
impl GameEnd {
|
||||||
|
|
@ -290,6 +296,7 @@ impl GameEnd {
|
||||||
result,
|
result,
|
||||||
page: None,
|
page: None,
|
||||||
game: Some(game),
|
game: Some(game),
|
||||||
|
notified_connected: false,
|
||||||
last_error_log: Instant::now() - core::time::Duration::from_secs(60),
|
last_error_log: Instant::now() - core::time::Duration::from_secs(60),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -302,6 +309,20 @@ impl GameEnd {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn next(&mut self) -> Option<Lobby> {
|
pub async fn next(&mut self) -> Option<Lobby> {
|
||||||
|
if !self.notified_connected {
|
||||||
|
self.notified_connected = true;
|
||||||
|
let game = self.game().ok()?;
|
||||||
|
let story = game.game.story();
|
||||||
|
let player_ids = game
|
||||||
|
.player_sender
|
||||||
|
.iter()
|
||||||
|
.map(|(i, _)| i.player_id)
|
||||||
|
.collect::<Box<[_]>>();
|
||||||
|
|
||||||
|
game.joined_players
|
||||||
|
.send_to(&player_ids, ServerMessage::Story(story))
|
||||||
|
.await;
|
||||||
|
}
|
||||||
let msg = match self.game().unwrap().comms.message().await {
|
let msg = match self.game().unwrap().comms.message().await {
|
||||||
Ok(msg) => msg,
|
Ok(msg) => msg,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
|
@ -313,10 +334,22 @@ impl GameEnd {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
self.process(msg)
|
match self.process(msg)? {
|
||||||
|
ProcessOutcome::Lobby(lobby) => Some(lobby),
|
||||||
|
ProcessOutcome::SendPlayer(player_id, msg) => {
|
||||||
|
self.game()
|
||||||
|
.ok()?
|
||||||
|
.joined_players
|
||||||
|
.get_sender(player_id)
|
||||||
|
.await?
|
||||||
|
.send(msg)
|
||||||
|
.log_debug();
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process(&mut self, message: Message) -> Option<Lobby> {
|
fn process(&mut self, message: Message) -> Option<ProcessOutcome> {
|
||||||
match message {
|
match message {
|
||||||
Message::Host(HostMessage::Echo(msg)) => {
|
Message::Host(HostMessage::Echo(msg)) => {
|
||||||
self.game().unwrap().comms.host().send(msg).log_debug();
|
self.game().unwrap().comms.host().send(msg).log_debug();
|
||||||
|
|
@ -366,7 +399,7 @@ impl GameEnd {
|
||||||
.send(ServerToHostMessage::Lobby(Box::new([])))
|
.send(ServerToHostMessage::Lobby(Box::new([])))
|
||||||
.log_debug();
|
.log_debug();
|
||||||
let lobby = self.game.take()?.into_lobby();
|
let lobby = self.game.take()?.into_lobby();
|
||||||
return Some(lobby);
|
return Some(ProcessOutcome::Lobby(lobby));
|
||||||
}
|
}
|
||||||
|
|
||||||
Message::Host(_) => self
|
Message::Host(_) => self
|
||||||
|
|
@ -378,16 +411,12 @@ impl GameEnd {
|
||||||
GameError::InvalidMessageForGameState,
|
GameError::InvalidMessageForGameState,
|
||||||
))
|
))
|
||||||
.log_debug(),
|
.log_debug(),
|
||||||
Message::Client(IdentifiedClientMessage {
|
Message::Client(IdentifiedClientMessage { identity, .. }) => {
|
||||||
identity,
|
let story = self.game().ok()?.game.story();
|
||||||
message: _,
|
return Some(ProcessOutcome::SendPlayer(
|
||||||
}) => {
|
identity.player_id,
|
||||||
let result = self.result;
|
ServerMessage::Story(story),
|
||||||
self.game()
|
));
|
||||||
.ok()?
|
|
||||||
.player_sender
|
|
||||||
.send_if_present(identity.player_id, ServerMessage::GameOver(result))
|
|
||||||
.log_debug();
|
|
||||||
}
|
}
|
||||||
Message::ConnectedList(_) => {}
|
Message::ConnectedList(_) => {}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ use std::rc::Rc;
|
||||||
|
|
||||||
use gloo::storage::errors::StorageError;
|
use gloo::storage::errors::StorageError;
|
||||||
use werewolves_proto::{
|
use werewolves_proto::{
|
||||||
|
game::story::GameStory,
|
||||||
message::{ClientMessage, Identification, PublicIdentity},
|
message::{ClientMessage, Identification, PublicIdentity},
|
||||||
player::PlayerId,
|
player::PlayerId,
|
||||||
role::RoleTitle,
|
role::RoleTitle,
|
||||||
|
|
@ -11,7 +12,7 @@ use yew::prelude::*;
|
||||||
use crate::{
|
use crate::{
|
||||||
clients::client::connection::{Connection2, ConnectionError},
|
clients::client::connection::{Connection2, ConnectionError},
|
||||||
components::{
|
components::{
|
||||||
Button, CoverOfDarkness, Identity,
|
Button, CoverOfDarkness, Identity, Story,
|
||||||
client::{ClientNav, Signin},
|
client::{ClientNav, Signin},
|
||||||
},
|
},
|
||||||
storage::StorageKey,
|
storage::StorageKey,
|
||||||
|
|
@ -29,6 +30,7 @@ pub enum ClientEvent2 {
|
||||||
joined: bool,
|
joined: bool,
|
||||||
players: Rc<[PublicIdentity]>,
|
players: Rc<[PublicIdentity]>,
|
||||||
},
|
},
|
||||||
|
Story(GameStory),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Clone, PartialEq)]
|
#[derive(Default, Clone, PartialEq)]
|
||||||
|
|
@ -79,6 +81,11 @@ pub fn Client2(ClientProps { auto_join }: &ClientProps) -> Html {
|
||||||
let connection = use_mut_ref(|| Connection2::new(client_state.setter(), ident.clone(), recv));
|
let connection = use_mut_ref(|| Connection2::new(client_state.setter(), ident.clone(), recv));
|
||||||
|
|
||||||
let content = match &*client_state {
|
let content = match &*client_state {
|
||||||
|
ClientEvent2::Story(story) => html! {
|
||||||
|
<div class="post-game">
|
||||||
|
<Story story={story.clone()} />
|
||||||
|
</div>
|
||||||
|
},
|
||||||
ClientEvent2::Sleep => html! {
|
ClientEvent2::Sleep => html! {
|
||||||
<CoverOfDarkness />
|
<CoverOfDarkness />
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -218,6 +218,7 @@ impl Connection2 {
|
||||||
fn message_to_client_state(&self, msg: ServerMessage) -> Option<ClientEvent2> {
|
fn message_to_client_state(&self, msg: ServerMessage) -> Option<ClientEvent2> {
|
||||||
log::debug!("received message: {msg:?}");
|
log::debug!("received message: {msg:?}");
|
||||||
Some(match msg {
|
Some(match msg {
|
||||||
|
ServerMessage::Story(story) => ClientEvent2::Story(story),
|
||||||
ServerMessage::Sleep => ClientEvent2::Sleep,
|
ServerMessage::Sleep => ClientEvent2::Sleep,
|
||||||
ServerMessage::Disconnect => ClientEvent2::Disconnected,
|
ServerMessage::Disconnect => ClientEvent2::Disconnected,
|
||||||
ServerMessage::LobbyInfo {
|
ServerMessage::LobbyInfo {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue