2021-01-18 05:46:53 +00:00
|
|
|
use crate::{
|
|
|
|
comp::{group::Group, BuffKind},
|
|
|
|
uid::Uid,
|
|
|
|
};
|
2020-07-06 14:23:08 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-06-02 02:42:26 +00:00
|
|
|
use specs::Component;
|
2020-07-06 05:56:02 +00:00
|
|
|
use specs_idvs::IdvStorage;
|
2020-06-04 07:11:35 +00:00
|
|
|
use std::time::{Duration, Instant};
|
2020-06-01 04:33:39 +00:00
|
|
|
|
2020-06-12 07:43:20 +00:00
|
|
|
/// A player's current chat mode. These are chat types that can only be sent by
|
|
|
|
/// the player.
|
2021-02-10 19:42:59 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2020-06-01 04:33:39 +00:00
|
|
|
pub enum ChatMode {
|
2020-06-02 02:42:26 +00:00
|
|
|
/// Private message to another player (by uuid)
|
|
|
|
Tell(Uid),
|
2020-06-01 04:33:39 +00:00
|
|
|
/// Talk to players within shouting distance
|
|
|
|
Say,
|
|
|
|
/// Talk to players in your region of the world
|
|
|
|
Region,
|
|
|
|
/// Talk to your current group of players
|
2020-07-12 20:18:57 +00:00
|
|
|
Group(Group),
|
2020-06-01 04:33:39 +00:00
|
|
|
/// Talk to your faction
|
2020-06-04 09:40:05 +00:00
|
|
|
Faction(String),
|
2020-06-01 04:33:39 +00:00
|
|
|
/// Talk to every player on the server
|
|
|
|
World,
|
|
|
|
}
|
2020-06-04 07:11:35 +00:00
|
|
|
|
2020-06-01 04:33:39 +00:00
|
|
|
impl Component for ChatMode {
|
2020-07-06 05:56:02 +00:00
|
|
|
type Storage = IdvStorage<Self>;
|
2020-06-01 04:33:39 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 07:11:35 +00:00
|
|
|
impl ChatMode {
|
|
|
|
/// Create a message from your current chat mode and uuid.
|
2020-07-12 20:18:57 +00:00
|
|
|
pub fn new_message(&self, from: Uid, message: String) -> UnresolvedChatMsg {
|
2020-06-04 07:11:35 +00:00
|
|
|
let chat_type = match self {
|
|
|
|
ChatMode::Tell(to) => ChatType::Tell(from, *to),
|
|
|
|
ChatMode::Say => ChatType::Say(from),
|
|
|
|
ChatMode::Region => ChatType::Region(from),
|
2020-07-12 20:18:57 +00:00
|
|
|
ChatMode::Group(group) => ChatType::Group(from, *group),
|
|
|
|
ChatMode::Faction(faction) => ChatType::Faction(from, faction.clone()),
|
2020-06-04 07:11:35 +00:00
|
|
|
ChatMode::World => ChatType::World(from),
|
|
|
|
};
|
2020-07-12 20:18:57 +00:00
|
|
|
UnresolvedChatMsg { chat_type, message }
|
2020-06-04 07:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 07:34:58 +00:00
|
|
|
impl ChatMode {
|
|
|
|
pub const fn default() -> Self { Self::World }
|
2020-06-04 09:40:05 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 19:42:32 +00:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub enum KillType {
|
2021-01-18 05:46:53 +00:00
|
|
|
Buff(BuffKind),
|
2020-09-06 19:42:32 +00:00
|
|
|
Melee,
|
|
|
|
Projectile,
|
2020-09-09 20:26:20 +00:00
|
|
|
Explosion,
|
2020-09-13 05:32:50 +00:00
|
|
|
Energy,
|
2020-11-05 01:21:42 +00:00
|
|
|
Other,
|
2020-09-06 19:42:32 +00:00
|
|
|
// Projectile(String), TODO: add projectile name when available
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub enum KillSource {
|
|
|
|
Player(Uid, KillType),
|
|
|
|
NonPlayer(String, KillType),
|
2021-01-18 05:46:53 +00:00
|
|
|
NonExistent(KillType),
|
2020-09-06 19:42:32 +00:00
|
|
|
Environment(String),
|
|
|
|
FallDamage,
|
|
|
|
Suicide,
|
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
2020-06-12 07:43:20 +00:00
|
|
|
/// List of chat types. Each one is colored differently and has its own icon.
|
|
|
|
///
|
|
|
|
/// This is a superset of `SpeechBubbleType`, which is a superset of `ChatMode`
|
2020-06-04 09:40:05 +00:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2020-07-12 20:18:57 +00:00
|
|
|
pub enum ChatType<G> {
|
2020-06-12 07:43:20 +00:00
|
|
|
/// A player came online
|
2020-09-06 19:42:32 +00:00
|
|
|
Online(Uid),
|
2020-06-12 07:43:20 +00:00
|
|
|
/// A player went offline
|
2020-09-06 19:42:32 +00:00
|
|
|
Offline(Uid),
|
2020-06-12 07:43:20 +00:00
|
|
|
/// The result of chat commands
|
|
|
|
CommandInfo,
|
|
|
|
/// A chat command failed
|
|
|
|
CommandError,
|
2020-09-06 19:42:32 +00:00
|
|
|
/// Inform players that someone died (Source, Victim) Source may be None
|
|
|
|
/// (ex: fall damage)
|
|
|
|
Kill(KillSource, Uid),
|
2020-06-12 07:43:20 +00:00
|
|
|
/// Server notifications to a group, such as player join/leave
|
2020-07-12 20:18:57 +00:00
|
|
|
GroupMeta(G),
|
2020-06-12 07:43:20 +00:00
|
|
|
/// Server notifications to a faction, such as player join/leave
|
2020-06-12 17:44:29 +00:00
|
|
|
FactionMeta(String),
|
2020-06-02 02:42:26 +00:00
|
|
|
/// One-on-one chat (from, to)
|
|
|
|
Tell(Uid, Uid),
|
|
|
|
/// Chat with nearby players
|
|
|
|
Say(Uid),
|
|
|
|
/// Group chat
|
2020-07-12 20:18:57 +00:00
|
|
|
Group(Uid, G),
|
2020-06-02 02:42:26 +00:00
|
|
|
/// Factional chat
|
2020-06-04 09:40:05 +00:00
|
|
|
Faction(Uid, String),
|
2020-06-02 02:42:26 +00:00
|
|
|
/// Regional chat
|
|
|
|
Region(Uid),
|
|
|
|
/// World chat
|
|
|
|
World(Uid),
|
2020-06-04 07:11:35 +00:00
|
|
|
/// Messages sent from NPCs (Not shown in chat but as speech bubbles)
|
|
|
|
///
|
|
|
|
/// The u16 field is a random number for selecting localization variants.
|
|
|
|
Npc(Uid, u16),
|
2021-03-16 01:30:35 +00:00
|
|
|
/// From NPCs but in the chat for clients in the near vicinity
|
|
|
|
NpcSay(Uid, u16),
|
2021-05-11 17:26:22 +00:00
|
|
|
/// From NPCs but in the chat for a specific client. Shows a chat bubble.
|
|
|
|
/// (from, to, localization variant)
|
|
|
|
NpcTell(Uid, Uid, u16),
|
2020-06-28 17:10:01 +00:00
|
|
|
/// Anything else
|
|
|
|
Meta,
|
2020-06-02 02:42:26 +00:00
|
|
|
}
|
|
|
|
|
2020-07-12 20:18:57 +00:00
|
|
|
impl<G> ChatType<G> {
|
|
|
|
pub fn chat_msg<S>(self, msg: S) -> GenericChatMsg<G>
|
2020-06-12 07:43:20 +00:00
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
2020-07-12 20:18:57 +00:00
|
|
|
GenericChatMsg {
|
2020-06-12 07:43:20 +00:00
|
|
|
chat_type: self,
|
|
|
|
message: msg.into(),
|
2020-06-12 17:44:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-12 20:18:57 +00:00
|
|
|
}
|
2020-12-13 17:11:55 +00:00
|
|
|
|
2020-09-06 19:42:32 +00:00
|
|
|
// Stores chat text, type
|
2020-06-02 02:42:26 +00:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2020-07-12 20:18:57 +00:00
|
|
|
pub struct GenericChatMsg<G> {
|
|
|
|
pub chat_type: ChatType<G>,
|
2020-06-02 02:42:26 +00:00
|
|
|
pub message: String,
|
|
|
|
}
|
|
|
|
|
2020-07-12 20:18:57 +00:00
|
|
|
pub type ChatMsg = GenericChatMsg<String>;
|
|
|
|
pub type UnresolvedChatMsg = GenericChatMsg<Group>;
|
|
|
|
|
|
|
|
impl<G> GenericChatMsg<G> {
|
2020-06-05 01:48:26 +00:00
|
|
|
pub const NPC_DISTANCE: f32 = 100.0;
|
2021-03-16 01:30:35 +00:00
|
|
|
pub const NPC_SAY_DISTANCE: f32 = 30.0;
|
2020-06-05 01:48:26 +00:00
|
|
|
pub const REGION_DISTANCE: f32 = 1000.0;
|
|
|
|
pub const SAY_DISTANCE: f32 = 100.0;
|
|
|
|
|
2020-06-04 07:11:35 +00:00
|
|
|
pub fn npc(uid: Uid, message: String) -> Self {
|
|
|
|
let chat_type = ChatType::Npc(uid, rand::random());
|
|
|
|
Self { chat_type, message }
|
|
|
|
}
|
|
|
|
|
2021-03-16 01:30:35 +00:00
|
|
|
pub fn npc_say(uid: Uid, message: String) -> Self {
|
|
|
|
let chat_type = ChatType::NpcSay(uid, rand::random());
|
|
|
|
Self { chat_type, message }
|
|
|
|
}
|
|
|
|
|
2021-05-11 17:26:22 +00:00
|
|
|
pub fn npc_tell(from: Uid, to: Uid, message: String) -> Self {
|
|
|
|
let chat_type = ChatType::NpcTell(from, to, rand::random());
|
|
|
|
Self { chat_type, message }
|
|
|
|
}
|
|
|
|
|
2020-07-12 20:18:57 +00:00
|
|
|
pub fn map_group<T>(self, mut f: impl FnMut(G) -> T) -> GenericChatMsg<T> {
|
|
|
|
let chat_type = match self.chat_type {
|
2020-09-06 19:42:32 +00:00
|
|
|
ChatType::Online(a) => ChatType::Online(a),
|
|
|
|
ChatType::Offline(a) => ChatType::Offline(a),
|
2020-07-12 20:18:57 +00:00
|
|
|
ChatType::CommandInfo => ChatType::CommandInfo,
|
|
|
|
ChatType::CommandError => ChatType::CommandError,
|
|
|
|
ChatType::FactionMeta(a) => ChatType::FactionMeta(a),
|
|
|
|
ChatType::GroupMeta(g) => ChatType::GroupMeta(f(g)),
|
2020-09-06 19:42:32 +00:00
|
|
|
ChatType::Kill(a, b) => ChatType::Kill(a, b),
|
2020-07-12 20:18:57 +00:00
|
|
|
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, b) => ChatType::Npc(a, b),
|
2021-03-16 01:30:35 +00:00
|
|
|
ChatType::NpcSay(a, b) => ChatType::NpcSay(a, b),
|
2021-05-11 17:26:22 +00:00
|
|
|
ChatType::NpcTell(a, b, c) => ChatType::NpcTell(a, b, c),
|
2020-07-12 20:18:57 +00:00
|
|
|
ChatType::Meta => ChatType::Meta,
|
|
|
|
};
|
|
|
|
|
|
|
|
GenericChatMsg {
|
|
|
|
chat_type,
|
|
|
|
message: self.message,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-27 16:40:43 +00:00
|
|
|
pub fn get_group(&self) -> Option<&G> {
|
|
|
|
match &self.chat_type {
|
2021-07-11 18:41:52 +00:00
|
|
|
ChatType::GroupMeta(g) => Some(g),
|
|
|
|
ChatType::Group(_, g) => Some(g),
|
2021-03-27 16:40:43 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 07:11:35 +00:00
|
|
|
pub fn to_bubble(&self) -> Option<(SpeechBubble, Uid)> {
|
2020-06-05 22:36:31 +00:00
|
|
|
let icon = self.icon();
|
2021-05-11 17:26:22 +00:00
|
|
|
if let ChatType::Npc(from, r) | ChatType::NpcSay(from, r) | ChatType::NpcTell(from, _, r) =
|
|
|
|
self.chat_type
|
|
|
|
{
|
2020-06-11 05:16:42 +00:00
|
|
|
Some((SpeechBubble::npc_new(&self.message, r, icon), from))
|
2020-06-05 22:36:31 +00:00
|
|
|
} else {
|
|
|
|
self.uid()
|
2020-06-11 05:16:42 +00:00
|
|
|
.map(|from| (SpeechBubble::player_new(&self.message, icon), from))
|
2020-06-05 22:36:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 18:25:29 +00:00
|
|
|
pub fn icon(&self) -> SpeechBubbleType {
|
2020-06-05 22:36:31 +00:00
|
|
|
match &self.chat_type {
|
2020-09-06 19:42:32 +00:00
|
|
|
ChatType::Online(_) => SpeechBubbleType::None,
|
|
|
|
ChatType::Offline(_) => SpeechBubbleType::None,
|
2020-06-12 07:43:20 +00:00
|
|
|
ChatType::CommandInfo => SpeechBubbleType::None,
|
|
|
|
ChatType::CommandError => SpeechBubbleType::None,
|
2020-06-12 17:44:29 +00:00
|
|
|
ChatType::FactionMeta(_) => SpeechBubbleType::None,
|
|
|
|
ChatType::GroupMeta(_) => SpeechBubbleType::None,
|
2020-09-06 19:42:32 +00:00
|
|
|
ChatType::Kill(_, _) => SpeechBubbleType::None,
|
2020-06-11 22:39:57 +00:00
|
|
|
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,
|
2020-06-11 18:25:29 +00:00
|
|
|
ChatType::Npc(_u, _r) => SpeechBubbleType::None,
|
2021-03-16 01:30:35 +00:00
|
|
|
ChatType::NpcSay(_u, _r) => SpeechBubbleType::Say,
|
2021-05-11 17:26:22 +00:00
|
|
|
ChatType::NpcTell(_f, _t, _) => SpeechBubbleType::Say,
|
2020-06-28 17:10:01 +00:00
|
|
|
ChatType::Meta => SpeechBubbleType::None,
|
2020-06-05 22:36:31 +00:00
|
|
|
}
|
2020-06-02 02:42:26 +00:00
|
|
|
}
|
2020-06-05 01:48:26 +00:00
|
|
|
|
|
|
|
pub fn uid(&self) -> Option<Uid> {
|
|
|
|
match &self.chat_type {
|
2020-09-06 19:42:32 +00:00
|
|
|
ChatType::Online(_) => None,
|
|
|
|
ChatType::Offline(_) => None,
|
2020-06-12 07:43:20 +00:00
|
|
|
ChatType::CommandInfo => None,
|
|
|
|
ChatType::CommandError => None,
|
2020-06-12 17:44:29 +00:00
|
|
|
ChatType::FactionMeta(_) => None,
|
|
|
|
ChatType::GroupMeta(_) => None,
|
2020-09-06 19:42:32 +00:00
|
|
|
ChatType::Kill(_, _) => None,
|
2020-06-05 01:48:26 +00:00
|
|
|
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, _r) => Some(*u),
|
2021-03-16 01:30:35 +00:00
|
|
|
ChatType::NpcSay(u, _r) => Some(*u),
|
2021-05-11 17:26:22 +00:00
|
|
|
ChatType::NpcTell(u, _t, _r) => Some(*u),
|
2020-06-28 17:10:01 +00:00
|
|
|
ChatType::Meta => None,
|
2020-06-05 01:48:26 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-02 02:42:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-01 04:33:39 +00:00
|
|
|
/// Player factions are used to coordinate pvp vs hostile factions or segment
|
|
|
|
/// chat from the world
|
|
|
|
///
|
|
|
|
/// Factions are currently just an associated String (the faction's name)
|
2020-06-04 09:40:05 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Faction(pub String);
|
2020-06-01 04:33:39 +00:00
|
|
|
impl Component for Faction {
|
2020-07-06 05:56:02 +00:00
|
|
|
type Storage = IdvStorage<Self>;
|
2020-06-01 04:33:39 +00:00
|
|
|
}
|
2020-06-04 09:40:05 +00:00
|
|
|
impl From<String> for Faction {
|
|
|
|
fn from(s: String) -> Self { Faction(s) }
|
|
|
|
}
|
2020-06-04 07:11:35 +00:00
|
|
|
|
|
|
|
/// The contents of a speech bubble
|
|
|
|
pub enum SpeechBubbleMessage {
|
|
|
|
/// This message was said by a player and needs no translation
|
|
|
|
Plain(String),
|
|
|
|
/// This message was said by an NPC. The fields are a i18n key and a random
|
|
|
|
/// u16 index
|
|
|
|
Localized(String, u16),
|
|
|
|
}
|
|
|
|
|
2020-06-12 07:43:20 +00:00
|
|
|
/// List of chat types for players and NPCs. Each one has its own icon.
|
|
|
|
///
|
|
|
|
/// This is a subset of `ChatType`, and a superset of `ChatMode`
|
2020-06-11 18:25:29 +00:00
|
|
|
pub enum SpeechBubbleType {
|
2020-06-04 07:11:35 +00:00
|
|
|
// One for each chat mode
|
|
|
|
Tell,
|
|
|
|
Say,
|
|
|
|
Region,
|
|
|
|
Group,
|
|
|
|
Faction,
|
|
|
|
World,
|
|
|
|
// For NPCs
|
|
|
|
Quest, // TODO not implemented
|
|
|
|
Trade, // TODO not implemented
|
|
|
|
None, // No icon (default for npcs)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a speech bubble above the character
|
|
|
|
pub struct SpeechBubble {
|
|
|
|
pub message: SpeechBubbleMessage,
|
2020-06-11 18:25:29 +00:00
|
|
|
pub icon: SpeechBubbleType,
|
2020-06-04 07:11:35 +00:00
|
|
|
pub timeout: Instant,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SpeechBubble {
|
|
|
|
/// Default duration in seconds of speech bubbles
|
|
|
|
pub const DEFAULT_DURATION: f64 = 5.0;
|
|
|
|
|
2020-06-11 18:25:29 +00:00
|
|
|
pub fn npc_new(i18n_key: &str, r: u16, icon: SpeechBubbleType) -> Self {
|
2020-06-11 05:16:42 +00:00
|
|
|
let message = SpeechBubbleMessage::Localized(i18n_key.to_string(), r);
|
2020-06-04 07:11:35 +00:00
|
|
|
let timeout = Instant::now() + Duration::from_secs_f64(SpeechBubble::DEFAULT_DURATION);
|
|
|
|
Self {
|
|
|
|
message,
|
|
|
|
icon,
|
2021-03-12 11:10:42 +00:00
|
|
|
timeout,
|
2020-06-04 07:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 18:25:29 +00:00
|
|
|
pub fn player_new(message: &str, icon: SpeechBubbleType) -> Self {
|
2020-06-11 05:16:42 +00:00
|
|
|
let message = SpeechBubbleMessage::Plain(message.to_string());
|
2020-06-04 07:11:35 +00:00
|
|
|
let timeout = Instant::now() + Duration::from_secs_f64(SpeechBubble::DEFAULT_DURATION);
|
|
|
|
Self {
|
|
|
|
message,
|
|
|
|
icon,
|
2021-03-12 11:10:42 +00:00
|
|
|
timeout,
|
2020-06-04 07:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn message<F>(&self, i18n_variation: F) -> String
|
|
|
|
where
|
2020-06-11 05:16:42 +00:00
|
|
|
F: Fn(&str, u16) -> String,
|
2020-06-04 07:11:35 +00:00
|
|
|
{
|
|
|
|
match &self.message {
|
|
|
|
SpeechBubbleMessage::Plain(m) => m.to_string(),
|
2021-07-11 18:41:52 +00:00
|
|
|
SpeechBubbleMessage::Localized(k, i) => i18n_variation(k, *i),
|
2020-06-04 07:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|