Compare commits
2 Commits
67d345646d
...
241420757e
| Author | SHA1 | Date |
|---|---|---|
|
|
241420757e | |
|
|
f126cc8d09 |
|
|
@ -191,10 +191,20 @@ dependencies = [
|
|||
"iana-time-zone",
|
||||
"js-sys",
|
||||
"num-traits",
|
||||
"serde",
|
||||
"wasm-bindgen",
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "chrono-humanize"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "799627e6b4d27827a814e837b9d8a504832086081806d45b1afa34dc982b023b"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ciborium"
|
||||
version = "0.2.2"
|
||||
|
|
@ -1960,6 +1970,7 @@ name = "werewolves"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"chrono-humanize",
|
||||
"ciborium",
|
||||
"convert_case 0.10.0",
|
||||
"futures",
|
||||
|
|
@ -1998,6 +2009,7 @@ dependencies = [
|
|||
name = "werewolves-proto"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"colored",
|
||||
"log",
|
||||
"pretty_assertions",
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ serde = { version = "1.0", features = ["derive"] }
|
|||
uuid = { version = "1.17", features = ["v4", "serde"] }
|
||||
rand = { version = "0.9", features = ["std_rng"] }
|
||||
werewolves-macros = { path = "../werewolves-macros" }
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = { version = "1" }
|
||||
|
|
|
|||
|
|
@ -107,4 +107,6 @@ pub enum GameError {
|
|||
MustSelectTarget,
|
||||
#[error("no current prompt in aura handling")]
|
||||
NoCurrentPromptForAura,
|
||||
#[error("you're not dead")]
|
||||
NotDead,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,8 +25,10 @@ use core::{
|
|||
ops::{Deref, Range, RangeBounds},
|
||||
};
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use rand::{Rng, seq::SliceRandom};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
character::CharacterId,
|
||||
|
|
@ -36,10 +38,12 @@ use crate::{
|
|||
story::{DayDetail, GameActions, GameStory, NightDetails},
|
||||
},
|
||||
message::{
|
||||
CharacterState, Identification,
|
||||
CharacterState, ClientDeadChat, Identification, ServerToClientMessage,
|
||||
dead::{DeadChatContent, DeadChatMessage},
|
||||
host::{HostDayMessage, HostGameMessage, HostNightMessage, ServerToHostMessage},
|
||||
night::ActionResponse,
|
||||
},
|
||||
player::PlayerId,
|
||||
};
|
||||
|
||||
pub use {
|
||||
|
|
@ -51,6 +55,7 @@ type Result<T> = core::result::Result<T, GameError>;
|
|||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Game {
|
||||
started: DateTime<Utc>,
|
||||
history: GameStory,
|
||||
state: GameState,
|
||||
}
|
||||
|
|
@ -59,6 +64,7 @@ impl Game {
|
|||
pub fn new(players: &[Identification], settings: GameSettings) -> Result<Self> {
|
||||
let village = Village::new(players, settings)?;
|
||||
Ok(Self {
|
||||
started: Utc::now(),
|
||||
history: GameStory::new(village.clone()),
|
||||
state: GameState::Night {
|
||||
night: Night::new(village)?,
|
||||
|
|
@ -66,6 +72,10 @@ impl Game {
|
|||
})
|
||||
}
|
||||
|
||||
pub const fn started(&self) -> DateTime<Utc> {
|
||||
self.started
|
||||
}
|
||||
|
||||
pub const fn village(&self) -> &Village {
|
||||
match &self.state {
|
||||
GameState::Day { village, marked: _ } => village,
|
||||
|
|
@ -73,6 +83,50 @@ impl Game {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn process_dead_chat_request(
|
||||
&mut self,
|
||||
player_id: PlayerId,
|
||||
message: ClientDeadChat,
|
||||
) -> Result<ServerToClientMessage> {
|
||||
let char = self
|
||||
.village()
|
||||
.character_by_player_id(player_id)
|
||||
.ok_or(GameError::NoMatchingCharacterFound)?;
|
||||
match message {
|
||||
ClientDeadChat::Send(message) => {
|
||||
let msg = DeadChatMessage {
|
||||
id: Uuid::new_v4(),
|
||||
timestamp: Utc::now(),
|
||||
message: DeadChatContent::PlayerMessage {
|
||||
message,
|
||||
from: char.identity(),
|
||||
},
|
||||
};
|
||||
match &mut self.state {
|
||||
GameState::Day { village, .. } => {
|
||||
village.send_dead_chat_message(msg.clone())?
|
||||
}
|
||||
GameState::Night { night } => night.send_dead_chat_message(msg.clone())?,
|
||||
}
|
||||
Ok(ServerToClientMessage::DeadChatMessage(msg))
|
||||
}
|
||||
ClientDeadChat::GetHistory => {
|
||||
let messages = self
|
||||
.village()
|
||||
.dead_chat()
|
||||
.get_since(self.started, char.character_id());
|
||||
Ok(ServerToClientMessage::DeadChat(messages))
|
||||
}
|
||||
ClientDeadChat::GetSince(since) => {
|
||||
let messages = self
|
||||
.village()
|
||||
.dead_chat()
|
||||
.get_since(since, char.character_id());
|
||||
Ok(ServerToClientMessage::DeadChat(messages))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[doc(hidden)]
|
||||
pub const fn village_mut(&mut self) -> &mut Village {
|
||||
|
|
@ -145,6 +199,7 @@ impl Game {
|
|||
.collect(),
|
||||
),
|
||||
)?;
|
||||
|
||||
self.state = GameState::Night { night };
|
||||
self.process(HostGameMessage::GetState)
|
||||
}
|
||||
|
|
@ -403,7 +458,7 @@ impl Ord for GameTime {
|
|||
}
|
||||
}
|
||||
(GameTime::Night { number: l }, GameTime::Day { number: r }) => {
|
||||
if *l > r.get() {
|
||||
if *l >= r.get() {
|
||||
Ordering::Greater
|
||||
} else {
|
||||
Ordering::Less
|
||||
|
|
|
|||
|
|
@ -32,8 +32,11 @@ use crate::{
|
|||
kill::{self, KillOutcome},
|
||||
night::changes::{ChangesLookup, NightChange},
|
||||
},
|
||||
message::night::{
|
||||
ActionPrompt, ActionPromptTitle, ActionResponse, ActionResult, ActionType, Visits,
|
||||
message::{
|
||||
dead::DeadChatMessage,
|
||||
night::{
|
||||
ActionPrompt, ActionPromptTitle, ActionResponse, ActionResult, ActionType, Visits,
|
||||
},
|
||||
},
|
||||
role::RoleTitle,
|
||||
};
|
||||
|
|
@ -1221,6 +1224,10 @@ impl Night {
|
|||
&self.village
|
||||
}
|
||||
|
||||
pub fn send_dead_chat_message(&mut self, msg: DeadChatMessage) -> Result<()> {
|
||||
self.village.send_dead_chat_message(msg)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[doc(hidden)]
|
||||
pub const fn village_mut(&mut self) -> &mut Village {
|
||||
|
|
|
|||
|
|
@ -24,7 +24,11 @@ use crate::{
|
|||
diedto::DiedTo,
|
||||
error::GameError,
|
||||
game::{GameOver, GameSettings, GameTime},
|
||||
message::{CharacterIdentity, Identification, night::ActionPrompt},
|
||||
message::{
|
||||
CharacterIdentity, Identification,
|
||||
dead::{DeadChat, DeadChatMessage},
|
||||
night::ActionPrompt,
|
||||
},
|
||||
player::PlayerId,
|
||||
role::{Role, RoleTitle},
|
||||
};
|
||||
|
|
@ -33,6 +37,7 @@ use crate::{
|
|||
pub struct Village {
|
||||
characters: Box<[Character]>,
|
||||
time: GameTime,
|
||||
dead_chat: DeadChat,
|
||||
settings: GameSettings,
|
||||
}
|
||||
|
||||
|
|
@ -52,9 +57,18 @@ impl Village {
|
|||
settings,
|
||||
characters,
|
||||
time: GameTime::Night { number: 0 },
|
||||
dead_chat: DeadChat::new(),
|
||||
})
|
||||
}
|
||||
|
||||
pub const fn dead_chat(&self) -> &DeadChat {
|
||||
&self.dead_chat
|
||||
}
|
||||
|
||||
pub fn send_dead_chat_message(&mut self, msg: DeadChatMessage) -> Result<()> {
|
||||
self.dead_chat.add(self.time, msg)
|
||||
}
|
||||
|
||||
pub fn settings(&self) -> GameSettings {
|
||||
self.settings.clone()
|
||||
}
|
||||
|
|
@ -170,14 +184,25 @@ impl Village {
|
|||
return Ok(Some(game_over));
|
||||
}
|
||||
self.time = self.time.next();
|
||||
self.dead_chat_time_transition();
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn dead_chat_time_transition(&mut self) {
|
||||
self.dead_chat.set_dead(
|
||||
self.characters
|
||||
.iter()
|
||||
.filter_map(|c| c.died_to().map(|died_to| (died_to.date_time(), c.clone()))),
|
||||
self.time,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn to_day(&mut self) -> Result<GameTime> {
|
||||
if self.time.is_day() {
|
||||
return Err(GameError::AlreadyDaytime);
|
||||
}
|
||||
self.time = self.time.next();
|
||||
self.dead_chat_time_transition();
|
||||
Ok(self.time)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ mod previous;
|
|||
mod revert;
|
||||
mod role;
|
||||
mod skip;
|
||||
mod time;
|
||||
|
||||
use crate::{
|
||||
character::{Character, CharacterId},
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
// Copyright (C) 2026 Emilis Bliūdžius
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as
|
||||
// published by the Free Software Foundation, either version 3 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
use core::{cmp::Ordering, num::NonZeroU8};
|
||||
#[allow(unused)]
|
||||
use pretty_assertions::{assert_eq, assert_ne, assert_str_eq};
|
||||
|
||||
use crate::game::GameTime;
|
||||
|
||||
#[test]
|
||||
pub fn game_time_test() {
|
||||
for (l, r, exp) in &[
|
||||
(
|
||||
GameTime::Day {
|
||||
number: NonZeroU8::new(1).unwrap(),
|
||||
},
|
||||
GameTime::Night { number: 0 },
|
||||
Ordering::Greater,
|
||||
),
|
||||
(
|
||||
GameTime::Night { number: 1 },
|
||||
GameTime::Day {
|
||||
number: NonZeroU8::new(1).unwrap(),
|
||||
},
|
||||
Ordering::Greater,
|
||||
),
|
||||
(
|
||||
GameTime::Night { number: 0 },
|
||||
GameTime::Day {
|
||||
number: NonZeroU8::new(1).unwrap(),
|
||||
},
|
||||
Ordering::Less,
|
||||
),
|
||||
] {
|
||||
assert_eq!(l.cmp(r), *exp);
|
||||
}
|
||||
}
|
||||
|
|
@ -12,18 +12,22 @@
|
|||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
pub mod dead;
|
||||
pub mod host;
|
||||
mod ident;
|
||||
pub mod night;
|
||||
|
||||
use core::num::NonZeroU8;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
pub use ident::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
character::CharacterId,
|
||||
error::GameError,
|
||||
game::{GameOver, story::GameStory},
|
||||
message::dead::DeadChatMessage,
|
||||
role::RoleTitle,
|
||||
};
|
||||
|
||||
|
|
@ -34,6 +38,14 @@ pub enum ClientMessage {
|
|||
GetState,
|
||||
RoleAck,
|
||||
UpdateSelf(UpdateSelf),
|
||||
DeadChat(ClientDeadChat),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub enum ClientDeadChat {
|
||||
Send(String),
|
||||
GetHistory,
|
||||
GetSince(DateTime<Utc>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
|
|
@ -51,7 +63,7 @@ pub struct DayCharacter {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum ServerMessage {
|
||||
pub enum ServerToClientMessage {
|
||||
Disconnect,
|
||||
LobbyInfo {
|
||||
joined: bool,
|
||||
|
|
@ -66,8 +78,11 @@ pub enum ServerMessage {
|
|||
GameOver(GameOver),
|
||||
Story(GameStory),
|
||||
Update(PlayerUpdate),
|
||||
DeadChat(Box<[DeadChatMessage]>),
|
||||
DeadChatMessage(DeadChatMessage),
|
||||
Sleep,
|
||||
Reset,
|
||||
Error(GameError),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
|
|
|
|||
|
|
@ -0,0 +1,193 @@
|
|||
// Copyright (C) 2025-2026 Emilis Bliūdžius
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as
|
||||
// published by the Free Software Foundation, either version 3 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
character::{Character, CharacterId},
|
||||
diedto::DiedTo,
|
||||
error::GameError,
|
||||
game::GameTime,
|
||||
message::CharacterIdentity,
|
||||
};
|
||||
|
||||
type Result<T> = core::result::Result<T, GameError>;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct DeadChat {
|
||||
deaths: Vec<(GameTime, CharacterId)>,
|
||||
messages: HashMap<GameTime, Vec<DeadChatMessage>>,
|
||||
}
|
||||
|
||||
impl DeadChat {
|
||||
pub fn new() -> Self {
|
||||
let mut messages = HashMap::new();
|
||||
messages.insert(
|
||||
GameTime::Night { number: 0 },
|
||||
vec![DeadChatMessage {
|
||||
id: Uuid::new_v4(),
|
||||
timestamp: Utc::now(),
|
||||
message: DeadChatContent::TimeChange(GameTime::Night { number: 0 }),
|
||||
}],
|
||||
);
|
||||
Self {
|
||||
messages,
|
||||
deaths: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn all_character_ids(&self) -> Box<[CharacterId]> {
|
||||
self.deaths.iter().map(|c| c.1).collect()
|
||||
}
|
||||
|
||||
pub fn sort(&mut self) {
|
||||
self.messages
|
||||
.iter_mut()
|
||||
.for_each(|(_, v)| v.sort_by_key(|s| s.timestamp));
|
||||
}
|
||||
|
||||
pub fn add(&mut self, time: GameTime, msg: DeadChatMessage) -> Result<()> {
|
||||
if !self
|
||||
.deaths
|
||||
.iter()
|
||||
.any(|(t, ch)| time >= *t && msg.message.is_from_character(*ch))
|
||||
{
|
||||
return Err(GameError::NotDead);
|
||||
}
|
||||
if let Some(msgs) = self.messages.get_mut(&time) {
|
||||
msgs.push(msg);
|
||||
} else {
|
||||
self.messages.insert(time, vec![msg]);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_dead(
|
||||
&mut self,
|
||||
dead: impl Iterator<Item = (GameTime, Character)> + Clone,
|
||||
time: GameTime,
|
||||
) {
|
||||
let newly_dead = dead
|
||||
.clone()
|
||||
.filter_map(|(t, c)| (t.next() == time).then_some(c))
|
||||
.collect::<Box<_>>();
|
||||
self.deaths = dead.clone().map(|(t, c)| (t, c.character_id())).collect();
|
||||
let Some(prev) = time.previous() else {
|
||||
return;
|
||||
};
|
||||
let messages = if let Some(messages) = self.messages.get_mut(&prev) {
|
||||
messages
|
||||
} else {
|
||||
self.messages.insert(
|
||||
prev,
|
||||
vec![DeadChatMessage {
|
||||
id: Uuid::new_v4(),
|
||||
timestamp: Utc::now(),
|
||||
message: DeadChatContent::TimeChange(prev),
|
||||
}],
|
||||
);
|
||||
self.messages.get_mut(&prev).unwrap()
|
||||
};
|
||||
for dead in newly_dead {
|
||||
let Some(died_to) = dead.died_to() else {
|
||||
continue;
|
||||
};
|
||||
messages.push(DeadChatMessage {
|
||||
id: Uuid::new_v4(),
|
||||
timestamp: Utc::now(),
|
||||
message: DeadChatContent::Death {
|
||||
character: dead.identity(),
|
||||
cause: died_to.clone(),
|
||||
},
|
||||
});
|
||||
}
|
||||
messages.sort_by_key(|c| c.timestamp);
|
||||
let id = Uuid::new_v4();
|
||||
if let Some(existing) = self.messages.insert(
|
||||
time,
|
||||
vec![DeadChatMessage {
|
||||
id,
|
||||
timestamp: Utc::now(),
|
||||
message: DeadChatContent::TimeChange(time),
|
||||
}],
|
||||
) {
|
||||
log::warn!("replaced: {existing:?}");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_since(&self, t: DateTime<Utc>, character: CharacterId) -> Box<[DeadChatMessage]> {
|
||||
let times_applicable = self
|
||||
.deaths
|
||||
.iter()
|
||||
.filter_map(|(dt, c)| (*c == character).then_some(*dt))
|
||||
.collect::<Box<[_]>>();
|
||||
let mut messages = self
|
||||
.messages
|
||||
.iter()
|
||||
.filter_map(|(gt, c)| {
|
||||
times_applicable
|
||||
.iter()
|
||||
.any(|t| t <= gt)
|
||||
.then_some(c.iter().filter(|c| c.timestamp >= t))
|
||||
})
|
||||
.flatten()
|
||||
.cloned()
|
||||
.collect::<Box<_>>();
|
||||
messages.sort_by_key(|m| m.timestamp);
|
||||
|
||||
messages
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct DeadChatMessage {
|
||||
pub id: Uuid,
|
||||
pub timestamp: DateTime<Utc>,
|
||||
pub message: DeadChatContent,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum DeadChatContent {
|
||||
PlayerMessage {
|
||||
from: CharacterIdentity,
|
||||
message: String,
|
||||
},
|
||||
Death {
|
||||
character: CharacterIdentity,
|
||||
cause: DiedTo,
|
||||
},
|
||||
TimeChange(GameTime),
|
||||
}
|
||||
|
||||
impl DeadChatContent {
|
||||
pub fn is_from_character(&self, character_id: CharacterId) -> bool {
|
||||
match self {
|
||||
DeadChatContent::PlayerMessage { from, .. } => from.character_id == character_id,
|
||||
DeadChatContent::Death { character, .. } => character.character_id == character_id,
|
||||
DeadChatContent::TimeChange(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn message(&self) -> Option<&str> {
|
||||
match self {
|
||||
DeadChatContent::PlayerMessage { message, .. } => Some(message.as_str()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -30,7 +30,7 @@ use axum_extra::TypedHeader;
|
|||
use chrono::Utc;
|
||||
use colored::Colorize;
|
||||
use tokio::sync::broadcast::{Receiver, Sender};
|
||||
use werewolves_proto::message::{ClientMessage, Identification, ServerMessage, UpdateSelf};
|
||||
use werewolves_proto::message::{ClientMessage, Identification, ServerToClientMessage, UpdateSelf};
|
||||
|
||||
pub async fn handler(
|
||||
ws: WebSocketUpgrade,
|
||||
|
|
@ -142,7 +142,7 @@ struct Client {
|
|||
socket: WebSocket,
|
||||
who: String,
|
||||
sender: Sender<IdentifiedClientMessage>,
|
||||
receiver: Receiver<ServerMessage>,
|
||||
receiver: Receiver<ServerToClientMessage>,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
|
|
@ -152,7 +152,7 @@ impl Client {
|
|||
socket: WebSocket,
|
||||
who: String,
|
||||
sender: Sender<IdentifiedClientMessage>,
|
||||
receiver: Receiver<ServerMessage>,
|
||||
receiver: Receiver<ServerToClientMessage>,
|
||||
) -> Self {
|
||||
Self {
|
||||
ident,
|
||||
|
|
@ -240,7 +240,7 @@ impl Client {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_message(&mut self, message: ServerMessage) -> Result<(), anyhow::Error> {
|
||||
async fn handle_message(&mut self, message: ServerToClientMessage) -> Result<(), anyhow::Error> {
|
||||
self.socket
|
||||
.send({
|
||||
#[cfg(not(feature = "cbor"))]
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ use tokio::{
|
|||
time::Instant,
|
||||
};
|
||||
use werewolves_proto::{
|
||||
message::{PublicIdentity, ServerMessage},
|
||||
message::{PublicIdentity, ServerToClientMessage},
|
||||
player::PlayerId,
|
||||
};
|
||||
|
||||
|
|
@ -44,8 +44,8 @@ impl ConnectionId {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct JoinedPlayer {
|
||||
sender: Sender<ServerMessage>,
|
||||
receiver: Receiver<ServerMessage>,
|
||||
sender: Sender<ServerToClientMessage>,
|
||||
receiver: Receiver<ServerToClientMessage>,
|
||||
active_connection: ConnectionId,
|
||||
in_game: bool,
|
||||
pub name: String,
|
||||
|
|
@ -55,8 +55,8 @@ pub struct JoinedPlayer {
|
|||
|
||||
impl JoinedPlayer {
|
||||
pub const fn new(
|
||||
sender: Sender<ServerMessage>,
|
||||
receiver: Receiver<ServerMessage>,
|
||||
sender: Sender<ServerToClientMessage>,
|
||||
receiver: Receiver<ServerToClientMessage>,
|
||||
active_connection: ConnectionId,
|
||||
name: String,
|
||||
number: Option<NonZeroU8>,
|
||||
|
|
@ -72,7 +72,7 @@ impl JoinedPlayer {
|
|||
in_game: false,
|
||||
}
|
||||
}
|
||||
pub fn resubscribe_reciever(&self) -> Receiver<ServerMessage> {
|
||||
pub fn resubscribe_reciever(&self) -> Receiver<ServerToClientMessage> {
|
||||
self.receiver.resubscribe()
|
||||
}
|
||||
}
|
||||
|
|
@ -91,7 +91,7 @@ impl JoinedPlayers {
|
|||
|
||||
pub async fn send_to_all_filter(
|
||||
&self,
|
||||
message: ServerMessage,
|
||||
message: ServerToClientMessage,
|
||||
filter: impl Fn(PlayerId) -> bool,
|
||||
) {
|
||||
let players: tokio::sync::MutexGuard<'_, HashMap<PlayerId, JoinedPlayer>> =
|
||||
|
|
@ -107,7 +107,7 @@ impl JoinedPlayers {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn send_to(&self, player_ids: &[PlayerId], message: ServerMessage) {
|
||||
pub async fn send_to(&self, player_ids: &[PlayerId], message: ServerToClientMessage) {
|
||||
let players: tokio::sync::MutexGuard<'_, HashMap<PlayerId, JoinedPlayer>> =
|
||||
self.players.lock().await;
|
||||
let senders = players
|
||||
|
|
@ -130,7 +130,7 @@ impl JoinedPlayers {
|
|||
.collect::<Box<[_]>>();
|
||||
core::mem::drop(players);
|
||||
for (pid, send) in senders {
|
||||
send.send(ServerMessage::LobbyInfo {
|
||||
send.send(ServerToClientMessage::LobbyInfo {
|
||||
joined: in_lobby_ids.contains(&pid),
|
||||
players: in_lobby.clone(),
|
||||
})
|
||||
|
|
@ -181,7 +181,7 @@ impl JoinedPlayers {
|
|||
None
|
||||
}
|
||||
|
||||
pub async fn get_sender(&self, player_id: PlayerId) -> Option<Sender<ServerMessage>> {
|
||||
pub async fn get_sender(&self, player_id: PlayerId) -> Option<Sender<ServerToClientMessage>> {
|
||||
self.players
|
||||
.lock()
|
||||
.await
|
||||
|
|
@ -193,7 +193,7 @@ impl JoinedPlayers {
|
|||
&self,
|
||||
player_id: PlayerId,
|
||||
player: JoinedPlayer,
|
||||
) -> Receiver<ServerMessage> {
|
||||
) -> Receiver<ServerToClientMessage> {
|
||||
let mut map = self.players.lock().await;
|
||||
|
||||
if let Some(old) = map.insert(player_id, player) {
|
||||
|
|
|
|||
|
|
@ -27,7 +27,8 @@ use werewolves_proto::{
|
|||
error::GameError,
|
||||
game::{Game, GameOver, Village},
|
||||
message::{
|
||||
ClientMessage, Identification, ServerMessage,
|
||||
ClientDeadChat, ClientMessage, Identification, ServerToClientMessage,
|
||||
dead::DeadChatMessage,
|
||||
host::{HostGameMessage, HostMessage, PostGameMessage, ServerToHostMessage},
|
||||
},
|
||||
player::PlayerId,
|
||||
|
|
@ -79,7 +80,7 @@ impl GameRunner {
|
|||
for char in characters.iter() {
|
||||
match self.player_sender.send_if_present(
|
||||
char.player_id(),
|
||||
ServerMessage::GameStart {
|
||||
ServerToClientMessage::GameStart {
|
||||
role: char.initial_shown_role(),
|
||||
},
|
||||
) {
|
||||
|
|
@ -97,7 +98,7 @@ impl GameRunner {
|
|||
}
|
||||
}
|
||||
self.joined_players
|
||||
.send_to_all_filter(ServerMessage::GameInProgress, |pid| {
|
||||
.send_to_all_filter(ServerToClientMessage::GameInProgress, |pid| {
|
||||
!characters.iter().any(|c| c.player_id() == pid)
|
||||
})
|
||||
.await;
|
||||
|
|
@ -131,7 +132,7 @@ impl GameRunner {
|
|||
&& let Some(sender) = sender.get_sender(player_id).await
|
||||
{
|
||||
sender
|
||||
.send(ServerMessage::GameStart {
|
||||
.send(ServerToClientMessage::GameStart {
|
||||
role: char.initial_shown_role(),
|
||||
})
|
||||
.log_debug();
|
||||
|
|
@ -197,7 +198,7 @@ impl GameRunner {
|
|||
};
|
||||
if acks.iter().any(|(c, d)| c.player_id() == player_id && *d) {
|
||||
// already ack'd just sleep
|
||||
sender.send(ServerMessage::Sleep).log_debug();
|
||||
sender.send(ServerToClientMessage::Sleep).log_debug();
|
||||
continue;
|
||||
}
|
||||
if let Some(char) = self
|
||||
|
|
@ -208,13 +209,15 @@ impl GameRunner {
|
|||
.find(|c| c.player_id() == player_id)
|
||||
{
|
||||
sender
|
||||
.send(ServerMessage::GameStart {
|
||||
.send(ServerToClientMessage::GameStart {
|
||||
role: char.initial_shown_role(),
|
||||
})
|
||||
.log_debug();
|
||||
} else {
|
||||
log::info!("game in progress for {player_id}");
|
||||
sender.send(ServerMessage::GameInProgress).log_debug();
|
||||
sender
|
||||
.send(ServerToClientMessage::GameInProgress)
|
||||
.log_debug();
|
||||
}
|
||||
log::info!("player {player_id} end");
|
||||
}
|
||||
|
|
@ -231,12 +234,12 @@ impl GameRunner {
|
|||
{
|
||||
*ackd = true;
|
||||
self.player_sender
|
||||
.send_if_present(player_id, ServerMessage::Sleep)
|
||||
.send_if_present(player_id, ServerToClientMessage::Sleep)
|
||||
.log_debug();
|
||||
}
|
||||
(update_host)(&acks, &mut self.comms);
|
||||
if let Some(sender) = self.joined_players.get_sender(player_id).await {
|
||||
sender.send(ServerMessage::Sleep).log_debug();
|
||||
sender.send(ServerToClientMessage::Sleep).log_debug();
|
||||
}
|
||||
}
|
||||
Message::Client(IdentifiedClientMessage {
|
||||
|
|
@ -248,7 +251,7 @@ impl GameRunner {
|
|||
|
||||
for char in self.game.village().characters() {
|
||||
if let Some(sender) = self.joined_players.get_sender(char.player_id()).await {
|
||||
let _ = sender.send(ServerMessage::Sleep);
|
||||
let _ = sender.send(ServerToClientMessage::Sleep);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -261,13 +264,74 @@ impl GameRunner {
|
|||
update: ClientUpdate::ConnectStateUpdate,
|
||||
..
|
||||
})) => return None,
|
||||
Ok(Message::Client(IdentifiedClientMessage {
|
||||
identity: Identification { player_id, .. },
|
||||
update: ClientUpdate::Message(ClientMessage::DeadChat(chat_request)),
|
||||
})) => {
|
||||
if let ClientDeadChat::Send(msg) = &chat_request
|
||||
&& msg.trim().is_empty()
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let reply = match self.game.process_dead_chat_request(player_id, chat_request) {
|
||||
Ok(msg) => msg,
|
||||
Err(err) => ServerToClientMessage::Error(err),
|
||||
};
|
||||
match reply {
|
||||
ServerToClientMessage::DeadChatMessage(msg) => {
|
||||
if let Err(err) = self.send_dead_message(&msg).await {
|
||||
log::warn!("sending message {msg:?} to dead chat: {err}");
|
||||
}
|
||||
}
|
||||
other => {
|
||||
if let Some(sender) = self.joined_players.get_sender(player_id).await
|
||||
&& let Err(err) = sender.send(other.clone())
|
||||
{
|
||||
log::warn!("sending message {other:?} to [{player_id}]: {err}");
|
||||
}
|
||||
}
|
||||
}
|
||||
return None;
|
||||
}
|
||||
Ok(Message::Client(IdentifiedClientMessage {
|
||||
identity: Identification { player_id, .. },
|
||||
update: ClientUpdate::Message(ClientMessage::GetState),
|
||||
})) => {
|
||||
let Some(char) = self.game.village().character_by_player_id(player_id) else {
|
||||
if let Some(send) = self.joined_players.get_sender(player_id).await {
|
||||
send.send(ServerToClientMessage::GameInProgress).log_debug();
|
||||
}
|
||||
return None;
|
||||
};
|
||||
if !self
|
||||
.game
|
||||
.village()
|
||||
.dead_chat()
|
||||
.all_character_ids()
|
||||
.contains(&char.character_id())
|
||||
{
|
||||
if let Some(send) = self.joined_players.get_sender(player_id).await {
|
||||
send.send(ServerToClientMessage::GameInProgress).log_debug();
|
||||
}
|
||||
return None;
|
||||
}
|
||||
let msg = match self
|
||||
.game
|
||||
.process_dead_chat_request(player_id, ClientDeadChat::GetHistory)
|
||||
{
|
||||
Ok(msg) => msg,
|
||||
Err(err) => ServerToClientMessage::Error(err),
|
||||
};
|
||||
self.joined_players.send_to(&[player_id], msg).await;
|
||||
return None;
|
||||
}
|
||||
Ok(Message::Client(IdentifiedClientMessage {
|
||||
identity: Identification { player_id, .. },
|
||||
..
|
||||
})) => {
|
||||
log::info!("client message from player {player_id}");
|
||||
if let Some(send) = self.joined_players.get_sender(player_id).await {
|
||||
send.send(ServerMessage::GameInProgress).log_debug();
|
||||
send.send(ServerToClientMessage::GameInProgress).log_debug();
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
|
@ -278,6 +342,7 @@ impl GameRunner {
|
|||
}
|
||||
};
|
||||
|
||||
let pre_time = self.game.village().time();
|
||||
match self.host_message(msg) {
|
||||
Ok(resp) => {
|
||||
self.comms.host().send(resp).log_warn();
|
||||
|
|
@ -289,7 +354,53 @@ impl GameRunner {
|
|||
.log_warn();
|
||||
}
|
||||
}
|
||||
self.game.game_over()
|
||||
let post_time = self.game.village().time();
|
||||
if let Some(game_over) = self.game.game_over() {
|
||||
return Some(game_over);
|
||||
}
|
||||
if pre_time != post_time {
|
||||
let newly_dead = self
|
||||
.game
|
||||
.village()
|
||||
.dead_characters()
|
||||
.into_iter()
|
||||
.filter_map(|c| c.died_to().map(|_| (c.character_id(), c.player_id())));
|
||||
|
||||
for (char, player) in newly_dead {
|
||||
let msgs = self
|
||||
.game
|
||||
.village()
|
||||
.dead_chat()
|
||||
.get_since(self.game.started(), char);
|
||||
self.joined_players
|
||||
.send_to(&[player], ServerToClientMessage::DeadChat(msgs))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub async fn send_dead_message(&mut self, msg: &DeadChatMessage) -> Result<()> {
|
||||
let player_ids = self
|
||||
.game
|
||||
.village()
|
||||
.dead_chat()
|
||||
.all_character_ids()
|
||||
.into_iter()
|
||||
.map(|c| {
|
||||
self.game
|
||||
.village()
|
||||
.character_by_id(c)
|
||||
.map(|c| c.player_id())
|
||||
})
|
||||
.collect::<Result<Box<_>>>()?;
|
||||
self.joined_players
|
||||
.send_to(
|
||||
&player_ids,
|
||||
ServerToClientMessage::DeadChatMessage(msg.clone()),
|
||||
)
|
||||
.await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn host_message(&mut self, message: HostMessage) -> Result<ServerToHostMessage> {
|
||||
|
|
@ -310,7 +421,7 @@ impl GameRunner {
|
|||
|
||||
enum ProcessOutcome {
|
||||
Lobby(Lobby),
|
||||
SendPlayer(PlayerId, ServerMessage),
|
||||
SendPlayer(PlayerId, ServerToClientMessage),
|
||||
}
|
||||
|
||||
pub struct GameEnd {
|
||||
|
|
@ -351,7 +462,7 @@ impl GameEnd {
|
|||
.collect::<Box<[_]>>();
|
||||
|
||||
game.joined_players
|
||||
.send_to(&player_ids, ServerMessage::Story(story))
|
||||
.send_to(&player_ids, ServerToClientMessage::Story(story))
|
||||
.await;
|
||||
}
|
||||
let msg = match self.game().unwrap().comms.message().await {
|
||||
|
|
@ -452,7 +563,7 @@ impl GameEnd {
|
|||
let story = self.game().ok()?.game.story();
|
||||
return Some(ProcessOutcome::SendPlayer(
|
||||
identity.player_id,
|
||||
ServerMessage::Story(story),
|
||||
ServerToClientMessage::Story(story),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ use werewolves_proto::{
|
|||
error::GameError,
|
||||
game::{Game, GameSettings},
|
||||
message::{
|
||||
ClientMessage, Identification, PlayerState, PublicIdentity, ServerMessage,
|
||||
ClientMessage, Identification, PlayerState, PublicIdentity, ServerToClientMessage,
|
||||
host::{HostLobbyMessage, HostMessage, ServerToHostMessage},
|
||||
},
|
||||
player::PlayerId,
|
||||
|
|
@ -149,7 +149,7 @@ impl Lobby {
|
|||
)) => {
|
||||
let _ = self
|
||||
.players_in_lobby
|
||||
.send_if_present(player_id, ServerMessage::InvalidMessageForGameState);
|
||||
.send_if_present(player_id, ServerToClientMessage::InvalidMessageForGameState);
|
||||
}
|
||||
Err((
|
||||
Message::Client(IdentifiedClientMessage {
|
||||
|
|
@ -168,7 +168,7 @@ impl Lobby {
|
|||
log::error!("processing message from {public} [{player_id}]: {err}");
|
||||
let _ = self
|
||||
.players_in_lobby
|
||||
.send_if_present(player_id, ServerMessage::Reset);
|
||||
.send_if_present(player_id, ServerToClientMessage::Reset);
|
||||
}
|
||||
}
|
||||
None
|
||||
|
|
@ -176,6 +176,12 @@ impl Lobby {
|
|||
|
||||
async fn next_inner(&mut self, msg: Message) -> Result<Option<GameRunner>, GameError> {
|
||||
match msg {
|
||||
Message::Client(IdentifiedClientMessage {
|
||||
update: ClientUpdate::Message(ClientMessage::DeadChat(_)),
|
||||
..
|
||||
}) => {
|
||||
log::warn!("dead chat message in lobby? ignoring.");
|
||||
}
|
||||
Message::Host(HostMessage::Lobby(HostLobbyMessage::ManufacturePlayer(public))) => {
|
||||
log::info!("adding player {public:?} by host request");
|
||||
self.players_in_lobby.push((
|
||||
|
|
@ -284,7 +290,7 @@ impl Lobby {
|
|||
identity: Identification { player_id, .. },
|
||||
update: ClientUpdate::Message(ClientMessage::GetState),
|
||||
}) => {
|
||||
let msg = ServerMessage::LobbyInfo {
|
||||
let msg = ServerToClientMessage::LobbyInfo {
|
||||
joined: self
|
||||
.players_in_lobby
|
||||
.iter()
|
||||
|
|
@ -338,10 +344,10 @@ impl Lobby {
|
|||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct LobbyPlayers(Vec<(Identification, Option<Sender<ServerMessage>>)>);
|
||||
pub struct LobbyPlayers(Vec<(Identification, Option<Sender<ServerToClientMessage>>)>);
|
||||
|
||||
impl Deref for LobbyPlayers {
|
||||
type Target = Vec<(Identification, Option<Sender<ServerMessage>>)>;
|
||||
type Target = Vec<(Identification, Option<Sender<ServerToClientMessage>>)>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
|
|
@ -374,7 +380,7 @@ impl LobbyPlayers {
|
|||
.collect(),
|
||||
)
|
||||
}
|
||||
pub fn find(&self, player_id: PlayerId) -> Option<&Sender<ServerMessage>> {
|
||||
pub fn find(&self, player_id: PlayerId) -> Option<&Sender<ServerToClientMessage>> {
|
||||
self.iter()
|
||||
.filter_map(|(id, s)| s.as_ref().map(|s| (id, s)))
|
||||
.find_map(|(id, s)| (id.player_id == player_id).then_some(s))
|
||||
|
|
@ -383,7 +389,7 @@ impl LobbyPlayers {
|
|||
pub fn send_if_present(
|
||||
&self,
|
||||
player_id: PlayerId,
|
||||
message: ServerMessage,
|
||||
message: ServerToClientMessage,
|
||||
) -> Result<bool, GameError> {
|
||||
if let Some(sender) = self.find(player_id) {
|
||||
sender
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ wasm-bindgen-futures = "0.4"
|
|||
thiserror = { version = "2" }
|
||||
convert_case = { version = "0.10" }
|
||||
ciborium = { version = "0.2", optional = true }
|
||||
chrono-humanize = { version = "0.2.3", features = ["wasmbind"] }
|
||||
|
||||
[features]
|
||||
default = ["cbor"]
|
||||
|
|
|
|||
|
|
@ -2931,3 +2931,99 @@ dialog {
|
|||
color: $damned_color;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
.dead-chat {
|
||||
height: 100%;
|
||||
// width: 100%;
|
||||
// max-width: 100vw;
|
||||
padding: 0px 3% 0 3%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
flex-wrap: nowrap;
|
||||
flex-grow: 1;
|
||||
overflow-x: hidden;
|
||||
gap: 3px;
|
||||
|
||||
.chat-messages {
|
||||
user-select: text;
|
||||
padding-inline-start: 0px;
|
||||
overflow-y: scroll;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
margin: 0;
|
||||
flex-grow: 1;
|
||||
max-width: 100%;
|
||||
max-height: 95vh;
|
||||
justify-content: flex-end;
|
||||
// scrollbar-width: thin;
|
||||
scrollbar-width: none;
|
||||
scrollbar-color: rgba(255, 255, 255, 0.7) black;
|
||||
}
|
||||
|
||||
.message {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
flex-wrap: wrap;
|
||||
margin-left: 0;
|
||||
gap: 1ch;
|
||||
align-items: baseline;
|
||||
|
||||
.dead-ident {
|
||||
font-weight: bold;
|
||||
|
||||
&[pronouns]:hover::after {
|
||||
content: attr(pronouns);
|
||||
overflow-y: hidden;
|
||||
position: relative;
|
||||
color: white;
|
||||
background-color: black;
|
||||
border: 1px solid white;
|
||||
padding: 3px;
|
||||
z-index: 4;
|
||||
align-self: center;
|
||||
justify-self: center;
|
||||
}
|
||||
}
|
||||
|
||||
.time {
|
||||
flex-shrink: 1;
|
||||
opacity: 50%;
|
||||
font-size: 1em;
|
||||
}
|
||||
|
||||
.message-content {
|
||||
flex-grow: 1;
|
||||
font-size: 1.2em;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
flex-wrap: wrap;
|
||||
align-items: baseline;
|
||||
gap: 1ch;
|
||||
}
|
||||
|
||||
.time-change {
|
||||
width: 100%;
|
||||
text-align: center;
|
||||
}
|
||||
}
|
||||
|
||||
form {
|
||||
max-width: 100%;
|
||||
margin: 0;
|
||||
flex-grow: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
flex-wrap: nowrap;
|
||||
padding-bottom: 10px;
|
||||
max-height: 5ch;
|
||||
}
|
||||
|
||||
#message-input {
|
||||
padding: 5px 0px 5px 0px;
|
||||
padding-inline: 0px 3px 0px 3px;
|
||||
align-self: flex-end;
|
||||
width: 100%;
|
||||
flex-grow: 1;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,10 @@
|
|||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
use core::sync::atomic::{AtomicBool, AtomicI64, Ordering};
|
||||
use core::{
|
||||
ops::Not,
|
||||
sync::atomic::{AtomicBool, AtomicI64, Ordering},
|
||||
};
|
||||
use std::rc::Rc;
|
||||
|
||||
use chrono::{DateTime, TimeDelta, Utc};
|
||||
|
|
@ -20,7 +23,9 @@ use gloo::storage::errors::StorageError;
|
|||
use wasm_bindgen::{JsCast, prelude::Closure};
|
||||
use werewolves_proto::{
|
||||
game::story::GameStory,
|
||||
message::{ClientMessage, Identification, PublicIdentity},
|
||||
message::{
|
||||
ClientDeadChat, ClientMessage, Identification, PublicIdentity, dead::DeadChatMessage,
|
||||
},
|
||||
player::PlayerId,
|
||||
role::RoleTitle,
|
||||
};
|
||||
|
|
@ -51,6 +56,9 @@ pub enum ClientEvent2 {
|
|||
},
|
||||
Story(GameStory),
|
||||
GameInProgress,
|
||||
DeadChat {
|
||||
messages: Vec<DeadChatMessage>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, PartialEq)]
|
||||
|
|
@ -74,7 +82,11 @@ pub(super) fn time_spent_unfocused() -> Option<TimeDelta> {
|
|||
|
||||
#[function_component]
|
||||
pub fn Client2(ClientProps { auto_join }: &ClientProps) -> Html {
|
||||
let ident_state = use_state(|| Option::<(PlayerId, PublicIdentity)>::None);
|
||||
let ident_state = use_state(|| {
|
||||
PlayerId::load_from_storage()
|
||||
.and_then(|pid| PublicIdentity::load_from_storage().map(|ident| (pid, ident)))
|
||||
.ok()
|
||||
});
|
||||
|
||||
if gloo::utils::window().onfocus().is_none() {
|
||||
let on_focus = {
|
||||
|
|
@ -154,6 +166,24 @@ pub fn Client2(ClientProps { auto_join }: &ClientProps) -> Html {
|
|||
};
|
||||
|
||||
let content = match &*client_state {
|
||||
ClientEvent2::DeadChat { messages } => {
|
||||
let on_send = {
|
||||
let send = send.clone();
|
||||
move |msg: String| {
|
||||
if let Err(err) =
|
||||
send.send_now(ClientMessage::DeadChat(ClientDeadChat::Send(msg)))
|
||||
{
|
||||
log::error!("sending dead chat message: {err}");
|
||||
}
|
||||
}
|
||||
};
|
||||
html! {
|
||||
<crate::components::chat::DeadChat
|
||||
on_send={on_send}
|
||||
messages={messages.clone()}
|
||||
/>
|
||||
}
|
||||
}
|
||||
ClientEvent2::Signin => html! {
|
||||
<Signin callback={on_signin} />
|
||||
},
|
||||
|
|
@ -260,8 +290,9 @@ pub fn Client2(ClientProps { auto_join }: &ClientProps) -> Html {
|
|||
}
|
||||
}
|
||||
};
|
||||
let dead_chat = matches!(&*client_state, ClientEvent2::DeadChat { .. });
|
||||
|
||||
let nav = {
|
||||
let nav = dead_chat.not().then_some({
|
||||
let send = (*send).clone();
|
||||
let error_cb = error_cb.clone();
|
||||
let client_nav_msg_cb = move |msg| {
|
||||
|
|
@ -272,13 +303,16 @@ pub fn Client2(ClientProps { auto_join }: &ClientProps) -> Html {
|
|||
html! {
|
||||
<ClientNav identity={ident_state.clone()} message_callback={client_nav_msg_cb} />
|
||||
}
|
||||
};
|
||||
});
|
||||
let footer = dead_chat.not().then_some(html! {
|
||||
<Footer />
|
||||
});
|
||||
|
||||
html! {
|
||||
<>
|
||||
{nav}
|
||||
{content}
|
||||
<Footer />
|
||||
{footer}
|
||||
</>
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,8 @@ use gloo::net::websocket::{self, futures::WebSocket};
|
|||
use instant::Instant;
|
||||
use serde::Serialize;
|
||||
use thiserror::Error;
|
||||
use werewolves_proto::message::{PlayerUpdate, ServerMessage};
|
||||
use werewolves_proto::message::dead::DeadChatMessage;
|
||||
use werewolves_proto::message::{ClientDeadChat, PlayerUpdate, ServerToClientMessage};
|
||||
use werewolves_proto::{
|
||||
message::{ClientMessage, Identification, PublicIdentity},
|
||||
player::PlayerId,
|
||||
|
|
@ -48,6 +49,7 @@ pub struct Connection2 {
|
|||
ident: UseStateHandle<Option<(PlayerId, PublicIdentity)>>,
|
||||
receiver: Rc<RefCell<UnboundedReceiver<ClientMessage>>>,
|
||||
active: Rc<RefCell<()>>,
|
||||
dead_chat: Option<Vec<DeadChatMessage>>,
|
||||
}
|
||||
|
||||
impl Connection2 {
|
||||
|
|
@ -60,6 +62,7 @@ impl Connection2 {
|
|||
state,
|
||||
ident,
|
||||
receiver,
|
||||
dead_chat: None,
|
||||
active: Rc::new(RefCell::new(())),
|
||||
}
|
||||
}
|
||||
|
|
@ -156,8 +159,9 @@ impl Connection2 {
|
|||
let mut ws = Self::connect_ws().await.fuse();
|
||||
log::info!("connected to {url}");
|
||||
|
||||
log::debug!("sending self ident");
|
||||
if let Err(err) = ws.send(Self::encode_message(&self.identification())).await {
|
||||
let ident = self.identification();
|
||||
log::debug!("sending self ident: {ident}");
|
||||
if let Err(err) = ws.send(Self::encode_message(&ident)).await {
|
||||
log::error!("websocket identification send: {err}");
|
||||
continue 'outer;
|
||||
};
|
||||
|
|
@ -227,7 +231,7 @@ impl Connection2 {
|
|||
{
|
||||
match msg {
|
||||
websocket::Message::Text(text) => {
|
||||
serde_json::from_str::<ServerMessage>(&text)
|
||||
serde_json::from_str::<ServerToClientMessage>(&text)
|
||||
}
|
||||
websocket::Message::Bytes(items) => serde_json::from_slice(&items),
|
||||
}
|
||||
|
|
@ -240,14 +244,36 @@ impl Connection2 {
|
|||
continue;
|
||||
}
|
||||
websocket::Message::Bytes(bytes) => {
|
||||
ciborium::from_reader::<ServerMessage, _>(bytes.as_slice())
|
||||
ciborium::from_reader::<ServerToClientMessage, _>(bytes.as_slice())
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
match parse {
|
||||
Ok(ServerToClientMessage::DeadChat(msgs)) => {
|
||||
self.dead_chat.replace(msgs.to_vec());
|
||||
self.state.set(ClientEvent2::DeadChat {
|
||||
messages: msgs.to_vec(),
|
||||
});
|
||||
}
|
||||
Ok(ServerToClientMessage::DeadChatMessage(msg)) => {
|
||||
if let Some(dead_chat) = self.dead_chat.as_mut() {
|
||||
dead_chat.push(msg);
|
||||
dead_chat.sort_by_key(|k| k.timestamp);
|
||||
self.state.set(ClientEvent2::DeadChat {
|
||||
messages: dead_chat.clone(),
|
||||
});
|
||||
} else if let Err(err) = ws
|
||||
.send(Self::encode_message(&ClientMessage::DeadChat(
|
||||
ClientDeadChat::GetHistory,
|
||||
)))
|
||||
.await
|
||||
{
|
||||
log::error!("sending dead chat history request: {err}");
|
||||
}
|
||||
}
|
||||
Ok(msg) => {
|
||||
quit = matches!(msg, ServerMessage::Disconnect);
|
||||
quit = matches!(msg, ServerToClientMessage::Disconnect);
|
||||
if let Some(state) = self.message_to_client_state(msg) {
|
||||
self.state.set(state);
|
||||
}
|
||||
|
|
@ -260,13 +286,17 @@ impl Connection2 {
|
|||
}
|
||||
}
|
||||
|
||||
fn message_to_client_state(&self, msg: ServerMessage) -> Option<ClientEvent2> {
|
||||
fn message_to_client_state(&self, msg: ServerToClientMessage) -> Option<ClientEvent2> {
|
||||
log::debug!("received message: {msg:?}");
|
||||
Some(match msg {
|
||||
ServerMessage::Story(story) => ClientEvent2::Story(story),
|
||||
ServerMessage::Sleep => ClientEvent2::Sleep,
|
||||
ServerMessage::Disconnect => ClientEvent2::Disconnected,
|
||||
ServerMessage::LobbyInfo {
|
||||
ServerToClientMessage::Error(err) => {
|
||||
log::error!("server: {err}");
|
||||
return None;
|
||||
}
|
||||
ServerToClientMessage::Story(story) => ClientEvent2::Story(story),
|
||||
ServerToClientMessage::Sleep => ClientEvent2::Sleep,
|
||||
ServerToClientMessage::Disconnect => ClientEvent2::Disconnected,
|
||||
ServerToClientMessage::LobbyInfo {
|
||||
joined,
|
||||
mut players,
|
||||
} => {
|
||||
|
|
@ -277,16 +307,16 @@ impl Connection2 {
|
|||
players: players.into_iter().collect(),
|
||||
}
|
||||
}
|
||||
ServerMessage::GameStart { role } => ClientEvent2::ShowRole(role),
|
||||
ServerMessage::InvalidMessageForGameState => {
|
||||
ServerToClientMessage::GameStart { role } => ClientEvent2::ShowRole(role),
|
||||
ServerToClientMessage::InvalidMessageForGameState => {
|
||||
log::error!("invalid message for game state");
|
||||
return None;
|
||||
}
|
||||
ServerMessage::NoSuchTarget => {
|
||||
ServerToClientMessage::NoSuchTarget => {
|
||||
log::error!("no such target");
|
||||
return None;
|
||||
}
|
||||
ServerMessage::Update(PlayerUpdate::Number(new_num)) => {
|
||||
ServerToClientMessage::Update(PlayerUpdate::Number(new_num)) => {
|
||||
let Some((pid, mut ident)) = (*self.ident).clone() else {
|
||||
return None;
|
||||
};
|
||||
|
|
@ -294,11 +324,14 @@ impl Connection2 {
|
|||
self.ident.set(Some((pid, ident)));
|
||||
return None;
|
||||
}
|
||||
ServerMessage::GameInProgress => ClientEvent2::GameInProgress,
|
||||
ServerMessage::GameOver(_) | ServerMessage::Reset => {
|
||||
ServerToClientMessage::GameInProgress => ClientEvent2::GameInProgress,
|
||||
ServerToClientMessage::GameOver(_) | ServerToClientMessage::Reset => {
|
||||
log::info!("ignoring: {msg:?}");
|
||||
return None;
|
||||
}
|
||||
ServerToClientMessage::DeadChat(_) | ServerToClientMessage::DeadChatMessage(_) => {
|
||||
return None;
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -178,7 +178,6 @@ async fn worker(mut recv: Receiver<HostMessage>, scope: Scope<Host>) {
|
|||
Ok(ServerToHostMessage::Error(GameError::AwaitingResponse)) => {}
|
||||
Ok(msg) => {
|
||||
log::debug!("got message: {:?}", msg.title());
|
||||
log::trace!("message content: {msg:?}");
|
||||
scope.send_message::<HostEvent>(msg.into())
|
||||
}
|
||||
Err(err) => {
|
||||
|
|
@ -324,7 +323,6 @@ impl Component for Host {
|
|||
}
|
||||
|
||||
fn view(&self, _ctx: &Context<Self>) -> Html {
|
||||
log::trace!("state: {:?}", self.state);
|
||||
let content = match self.state.clone() {
|
||||
HostState::ScreenOverrides { .. } => {
|
||||
let send = {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,219 @@
|
|||
// Copyright (C) 2026 Emilis Bliūdžius
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as
|
||||
// published by the Free Software Foundation, either version 3 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
use chrono::{DateTime, Utc};
|
||||
use chrono_humanize::Humanize;
|
||||
use yew::prelude::*;
|
||||
|
||||
use wasm_bindgen::{JsCast, UnwrapThrowExt};
|
||||
use web_sys::{HtmlElement, HtmlInputElement};
|
||||
use werewolves_proto::message::{
|
||||
PublicIdentity,
|
||||
dead::{DeadChatContent, DeadChatMessage},
|
||||
};
|
||||
|
||||
use crate::components::{Icon, IconSource, IconType, attributes::DiedToSpan};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Properties)]
|
||||
pub struct DeadChatProperties {
|
||||
pub on_send: Callback<String>,
|
||||
pub messages: Vec<DeadChatMessage>,
|
||||
}
|
||||
|
||||
#[function_component]
|
||||
pub fn DeadChat(DeadChatProperties { on_send, messages }: &DeadChatProperties) -> Html {
|
||||
let messages = messages
|
||||
.iter()
|
||||
.map(|m| {
|
||||
html! {
|
||||
<ChatMessage message={m.clone()} />
|
||||
}
|
||||
})
|
||||
.collect::<Html>();
|
||||
let submit = {
|
||||
let on_send = on_send.clone();
|
||||
move |ev: SubmitEvent| {
|
||||
ev.prevent_default();
|
||||
let Some(target) = ev.target_dyn_into::<HtmlElement>() else {
|
||||
return;
|
||||
};
|
||||
let input = target
|
||||
.query_selector("#message-input")
|
||||
.expect_throw("could not find #message-input")
|
||||
.expect_throw("could not find #message-input")
|
||||
.dyn_into::<HtmlInputElement>()
|
||||
.expect_throw("#message-input is not HtmlInputElement");
|
||||
let value = input.value().trim().to_string();
|
||||
if !value.is_empty() {
|
||||
on_send.emit(value);
|
||||
}
|
||||
input.set_value("");
|
||||
}
|
||||
};
|
||||
|
||||
let node = use_node_ref();
|
||||
|
||||
use_effect_with(node.clone(), |node| {
|
||||
let Some(div) = node.cast::<HtmlElement>() else {
|
||||
log::warn!("chat-messages node not attached");
|
||||
return;
|
||||
};
|
||||
|
||||
let is_scrolled_to_bottom =
|
||||
div.scroll_height() - div.client_height() <= div.scroll_top() + 1;
|
||||
if !is_scrolled_to_bottom {
|
||||
div.set_scroll_top(div.scroll_height() - div.client_height());
|
||||
}
|
||||
});
|
||||
|
||||
html! {
|
||||
<div class="dead-chat">
|
||||
<ol class="chat-messages" ref={node}>
|
||||
{messages}
|
||||
</ol>
|
||||
<form onsubmit={submit}>
|
||||
<input
|
||||
type="text"
|
||||
id="message-input"
|
||||
placeholder="write to the dead"
|
||||
autocomplete="off"
|
||||
/>
|
||||
<input type="submit" hidden=true/>
|
||||
</form>
|
||||
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Properties)]
|
||||
pub struct DeadChatIdentProps {
|
||||
pub ident: PublicIdentity,
|
||||
}
|
||||
|
||||
#[function_component]
|
||||
pub fn DeadChatIdent(DeadChatIdentProps { ident }: &DeadChatIdentProps) -> Html {
|
||||
let pronouns = ident.pronouns.clone();
|
||||
html! {
|
||||
<span class="dead-ident" pronouns={pronouns}>
|
||||
{ident.name.clone()}
|
||||
</span>
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Properties)]
|
||||
pub struct TimestampProps {
|
||||
pub timestamp: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[function_component]
|
||||
pub fn Timestamp(TimestampProps { timestamp }: &TimestampProps) -> Html {
|
||||
let use_relative = use_state(|| false);
|
||||
let lock = use_state(|| false);
|
||||
|
||||
let enter = {
|
||||
let lock = lock.clone();
|
||||
let use_relative = use_relative.setter();
|
||||
move |_| {
|
||||
if !*lock {
|
||||
use_relative.set(true);
|
||||
}
|
||||
}
|
||||
};
|
||||
let leave = {
|
||||
let lock = lock.clone();
|
||||
let use_relative = use_relative.setter();
|
||||
move |_| {
|
||||
if !*lock {
|
||||
use_relative.set(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
let bare_timestamp = timestamp
|
||||
.naive_local()
|
||||
.time()
|
||||
.format("%H:%M:%S")
|
||||
.to_string();
|
||||
|
||||
let timestamp_str = if *use_relative {
|
||||
(*timestamp - Utc::now()).humanize()
|
||||
} else {
|
||||
bare_timestamp
|
||||
};
|
||||
let lock_set = {
|
||||
let lock = lock.clone();
|
||||
move |_| lock.set(!*lock)
|
||||
};
|
||||
|
||||
html! {
|
||||
<span
|
||||
class="time"
|
||||
onpointerenter={enter}
|
||||
onpointercancel={leave.clone()}
|
||||
onpointerleave={leave}
|
||||
onclick={lock_set}
|
||||
>
|
||||
{timestamp_str}
|
||||
</span>
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Properties)]
|
||||
pub struct ChatMessageProps {
|
||||
pub message: DeadChatMessage,
|
||||
}
|
||||
|
||||
#[function_component]
|
||||
pub fn ChatMessage(
|
||||
ChatMessageProps {
|
||||
message:
|
||||
DeadChatMessage {
|
||||
id: _,
|
||||
timestamp,
|
||||
message,
|
||||
},
|
||||
}: &ChatMessageProps,
|
||||
) -> Html {
|
||||
match message {
|
||||
DeadChatContent::PlayerMessage { from, message } => {
|
||||
html! {
|
||||
<li class="message">
|
||||
<Timestamp timestamp={*timestamp} />
|
||||
<DeadChatIdent ident={from.clone().into_public()}/>
|
||||
<span class="message-content">{message.clone()}</span>
|
||||
</li>
|
||||
}
|
||||
}
|
||||
DeadChatContent::Death { character, cause } => {
|
||||
html! {
|
||||
<li class="message">
|
||||
<Timestamp timestamp={*timestamp} />
|
||||
<Icon source={IconSource::Skull} icon_type={IconType::Fit}/>
|
||||
<DeadChatIdent ident={character.clone().into_public()}/>
|
||||
<span class="message-content">
|
||||
{"died to "}
|
||||
// <DiedToSpan died_to={cause.title()}/>
|
||||
{cause.title().to_string()}
|
||||
</span>
|
||||
</li>
|
||||
}
|
||||
}
|
||||
DeadChatContent::TimeChange(time) => {
|
||||
html! {
|
||||
<li class="message">
|
||||
<span class="time-change">{time.to_string()}</span>
|
||||
</li>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -116,12 +116,26 @@ pub fn DaytimePlayerList(
|
|||
}
|
||||
})
|
||||
.collect::<Html>();
|
||||
let button_text = if marked.is_empty() {
|
||||
"end day".to_string()
|
||||
let (button_text, confirmation_text) = if marked.is_empty() {
|
||||
(
|
||||
"end day".to_string(),
|
||||
"really end the day with no executions?".to_string(),
|
||||
)
|
||||
} else if marked.len() == 1 {
|
||||
"execute 1 player".to_string()
|
||||
(
|
||||
"execute 1 player".to_string(),
|
||||
characters
|
||||
.iter()
|
||||
.find(|c| c.identity.character_id == marked[0])
|
||||
.map(|c| c.identity.clone().into_public())
|
||||
.map(|id| format!("really execute {id}?"))
|
||||
.unwrap_or("really execute 1 player?".to_string()),
|
||||
)
|
||||
} else {
|
||||
format!("execute {} players", marked.len())
|
||||
(
|
||||
format!("execute {} players", marked.len()),
|
||||
format!("really execute {} players?", marked.len()),
|
||||
)
|
||||
};
|
||||
let parity = {
|
||||
let wolves = characters
|
||||
|
|
@ -151,10 +165,21 @@ pub fn DaytimePlayerList(
|
|||
.then_some(())
|
||||
.and_then(|_| on_execute.clone())
|
||||
.map(|on_execute| {
|
||||
let on_execute = Callback::from(move |_| {
|
||||
on_execute.emit(());
|
||||
crate::components::modal::close_modal_by_id("execute");
|
||||
});
|
||||
html! {
|
||||
<Button on_click={on_execute}>
|
||||
{button_text}
|
||||
</Button>
|
||||
<crate::components::modal::Dialog
|
||||
id="execute"
|
||||
button={html!{{button_text.clone()}}}
|
||||
close_button=false
|
||||
>
|
||||
<h3>{confirmation_text}</h3>
|
||||
<Button on_click={on_execute}>
|
||||
{button_text}
|
||||
</Button>
|
||||
</crate::components::modal::Dialog>
|
||||
}
|
||||
});
|
||||
let day = day.as_ref().map(|day| {
|
||||
|
|
|
|||
|
|
@ -40,6 +40,9 @@ mod components {
|
|||
pub mod story {
|
||||
werewolves_macros::include_path!("werewolves/src/components/story");
|
||||
}
|
||||
pub mod chat {
|
||||
werewolves_macros::include_path!("werewolves/src/components/chat");
|
||||
}
|
||||
}
|
||||
mod pages {
|
||||
werewolves_macros::include_path!("werewolves/src/pages");
|
||||
|
|
|
|||
Loading…
Reference in New Issue