use crate::{
comp::{group::Group, BuffKind},
uid::Uid,
};
use common_i18n::Content;
use serde::{Deserialize, Serialize};
use specs::{Component, DenseVecStorage};
use std::time::{Duration, Instant};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ChatMode {
Tell(Uid),
Say,
Region,
Group,
Faction(String),
World,
}
impl Component for ChatMode {
type Storage = DenseVecStorage<Self>;
}
impl ChatMode {
pub fn to_msg(
&self,
from: Uid,
content: Content,
group: Option<Group>,
) -> Result<UnresolvedChatMsg, Content> {
let chat_type = match self {
ChatMode::Tell(to) => ChatType::Tell(from, *to),
ChatMode::Say => ChatType::Say(from),
ChatMode::Region => ChatType::Region(from),
ChatMode::Group => ChatType::Group(
from,
group.ok_or(Content::localized("command-message-group-missing"))?,
),
ChatMode::Faction(faction) => ChatType::Faction(from, faction.clone()),
ChatMode::World => ChatType::World(from),
};
Ok(UnresolvedChatMsg { chat_type, content })
}
}
impl ChatMode {
pub const fn default() -> Self { Self::World }
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum KillType {
Buff(BuffKind),
Melee,
Projectile,
Explosion,
Energy,
Other,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum KillSource {
Player(Uid, KillType),
NonPlayer(String, KillType),
NonExistent(KillType),
FallDamage,
Suicide,
Other,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ChatType<G> {
Online(Uid),
Offline(Uid),
CommandInfo,
CommandError,
Kill(KillSource, Uid),
GroupMeta(G),
FactionMeta(String),
Tell(Uid, Uid),
Say(Uid),
Group(Uid, G),
Faction(Uid, String),
Region(Uid),
World(Uid),
Npc(Uid),
NpcSay(Uid),
NpcTell(Uid, Uid),
Meta,
}
impl<G> ChatType<G> {
pub fn into_plain_msg(self, text: impl ToString) -> GenericChatMsg<G> {
GenericChatMsg {
chat_type: self,
content: Content::Plain(text.to_string()),
}
}
pub fn into_msg(self, content: Content) -> GenericChatMsg<G> {
GenericChatMsg {
chat_type: self,
content,
}
}
pub fn uid(&self) -> Option<Uid> {
match self {
ChatType::Online(_) => None,
ChatType::Offline(_) => None,
ChatType::CommandInfo => None,
ChatType::CommandError => None,
ChatType::FactionMeta(_) => None,
ChatType::GroupMeta(_) => None,
ChatType::Kill(_, _) => None,
ChatType::Tell(u, _t) => Some(*u),
ChatType::Say(u) => Some(*u),
ChatType::Group(u, _s) => Some(*u),
ChatType::Faction(u, _s) => Some(*u),
ChatType::Region(u) => Some(*u),
ChatType::World(u) => Some(*u),
ChatType::Npc(u) => Some(*u),
ChatType::NpcSay(u) => Some(*u),
ChatType::NpcTell(u, _t) => Some(*u),
ChatType::Meta => None,
}
}
pub fn is_private(&self) -> Option<bool> {
match self {
ChatType::Online(_)
| ChatType::Offline(_)
| ChatType::CommandInfo
| ChatType::CommandError
| ChatType::FactionMeta(_)
| ChatType::GroupMeta(_)
| ChatType::Npc(_)
| ChatType::NpcSay(_)
| ChatType::NpcTell(_, _)
| ChatType::Meta
| ChatType::Kill(_, _) => None,
ChatType::Tell(_, _) | ChatType::Group(_, _) | ChatType::Faction(_, _) => Some(true),
ChatType::Say(_) | ChatType::Region(_) | ChatType::World(_) => Some(false),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenericChatMsg<G> {
pub chat_type: ChatType<G>,
content: Content,
}
pub type ChatMsg = GenericChatMsg<String>;
pub type UnresolvedChatMsg = GenericChatMsg<Group>;
impl<G> GenericChatMsg<G> {
pub const MAX_BYTES_PLAYER_CHAT_MSG: usize = 256;
pub const NPC_DISTANCE: f32 = 100.0;
pub const NPC_SAY_DISTANCE: f32 = 30.0;
pub const REGION_DISTANCE: f32 = 1000.0;
pub const SAY_DISTANCE: f32 = 100.0;
pub fn npc(uid: Uid, content: Content) -> Self {
let chat_type = ChatType::Npc(uid);
Self { chat_type, content }
}
pub fn npc_say(uid: Uid, content: Content) -> Self {
let chat_type = ChatType::NpcSay(uid);
Self { chat_type, content }
}
pub fn npc_tell(from: Uid, to: Uid, content: Content) -> Self {
let chat_type = ChatType::NpcTell(from, to);
Self { chat_type, content }
}
pub fn death(kill_source: KillSource, victim: Uid) -> Self {
Self {
chat_type: ChatType::Kill(kill_source, victim),
content: Content::Plain(String::new()),
}
}
pub fn map_group<T>(self, mut f: impl FnMut(G) -> T) -> GenericChatMsg<T> {
let chat_type = match self.chat_type {
ChatType::Online(a) => ChatType::Online(a),
ChatType::Offline(a) => ChatType::Offline(a),
ChatType::CommandInfo => ChatType::CommandInfo,
ChatType::CommandError => ChatType::CommandError,
ChatType::FactionMeta(a) => ChatType::FactionMeta(a),
ChatType::GroupMeta(g) => ChatType::GroupMeta(f(g)),
ChatType::Kill(a, b) => ChatType::Kill(a, b),
ChatType::Tell(a, b) => ChatType::Tell(a, b),
ChatType::Say(a) => ChatType::Say(a),
ChatType::Group(a, g) => ChatType::Group(a, f(g)),
ChatType::Faction(a, b) => ChatType::Faction(a, b),
ChatType::Region(a) => ChatType::Region(a),
ChatType::World(a) => ChatType::World(a),
ChatType::Npc(a) => ChatType::Npc(a),
ChatType::NpcSay(a) => ChatType::NpcSay(a),
ChatType::NpcTell(a, b) => ChatType::NpcTell(a, b),
ChatType::Meta => ChatType::Meta,
};
GenericChatMsg {
chat_type,
content: self.content,
}
}
pub fn get_group(&self) -> Option<&G> {
match &self.chat_type {
ChatType::GroupMeta(g) => Some(g),
ChatType::Group(_, g) => Some(g),
_ => None,
}
}
pub fn to_bubble(&self) -> Option<(SpeechBubble, Uid)> {
self.uid()
.map(|from| (SpeechBubble::new(self.content.clone(), self.icon()), from))
}
pub fn icon(&self) -> SpeechBubbleType {
match &self.chat_type {
ChatType::Online(_) => SpeechBubbleType::None,
ChatType::Offline(_) => SpeechBubbleType::None,
ChatType::CommandInfo => SpeechBubbleType::None,
ChatType::CommandError => SpeechBubbleType::None,
ChatType::FactionMeta(_) => SpeechBubbleType::None,
ChatType::GroupMeta(_) => SpeechBubbleType::None,
ChatType::Kill(_, _) => SpeechBubbleType::None,
ChatType::Tell(_u, _) => SpeechBubbleType::Tell,
ChatType::Say(_u) => SpeechBubbleType::Say,
ChatType::Group(_u, _s) => SpeechBubbleType::Group,
ChatType::Faction(_u, _s) => SpeechBubbleType::Faction,
ChatType::Region(_u) => SpeechBubbleType::Region,
ChatType::World(_u) => SpeechBubbleType::World,
ChatType::Npc(_u) => SpeechBubbleType::None,
ChatType::NpcSay(_u) => SpeechBubbleType::Say,
ChatType::NpcTell(_f, _t) => SpeechBubbleType::Say,
ChatType::Meta => SpeechBubbleType::None,
}
}
pub fn uid(&self) -> Option<Uid> { self.chat_type.uid() }
pub fn content(&self) -> &Content { &self.content }
pub fn into_content(self) -> Content { self.content }
pub fn set_content(&mut self, content: Content) { self.content = content; }
}
#[derive(Clone, Debug)]
pub struct Faction(pub String);
impl Component for Faction {
type Storage = DenseVecStorage<Self>;
}
impl From<String> for Faction {
fn from(s: String) -> Self { Faction(s) }
}
pub enum SpeechBubbleType {
Tell,
Say,
Region,
Group,
Faction,
World,
Quest, Trade, None, }
pub struct SpeechBubble {
pub content: Content,
pub icon: SpeechBubbleType,
pub timeout: Instant,
}
impl SpeechBubble {
pub const DEFAULT_DURATION: f64 = 5.0;
pub fn new(content: Content, icon: SpeechBubbleType) -> Self {
let timeout = Instant::now() + Duration::from_secs_f64(SpeechBubble::DEFAULT_DURATION);
Self {
content,
icon,
timeout,
}
}
pub fn content(&self) -> &Content { &self.content }
}