veloren/common/src/msg/server.rs

140 lines
3.8 KiB
Rust
Raw Normal View History

use super::{ClientState, EcsCompPacket};
2019-07-21 17:03:06 +00:00
use crate::{
character::CharacterItem,
comp, state, sync,
sync::Uid,
2019-07-21 17:03:06 +00:00
terrain::{Block, TerrainChunk},
};
2020-01-01 20:17:43 +00:00
use authc::AuthClientError;
2019-08-11 20:38:28 +00:00
use hashbrown::HashMap;
use vek::*;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerInfo {
pub name: String,
pub description: String,
2019-07-21 17:45:31 +00:00
pub git_hash: String,
2019-10-18 13:32:26 +00:00
pub git_date: String,
2019-12-21 17:02:39 +00:00
pub auth_provider: Option<String>,
}
/// Inform the client of updates to the player list.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PlayerListUpdate {
Init(HashMap<Uid, PlayerInfo>),
Add(Uid, PlayerInfo),
SelectedCharacter(Uid, CharacterInfo),
LevelChange(Uid, u32),
Admin(Uid, bool),
Remove(Uid),
Alias(Uid, String),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlayerInfo {
pub is_admin: bool,
pub player_alias: String,
pub character: Option<CharacterInfo>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CharacterInfo {
pub name: String,
pub level: u32,
}
2020-05-14 16:56:10 +00:00
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Notification {
WaypointSaved,
}
2020-06-02 02:42:26 +00:00
/// Messages sent from the server to the client
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ServerMsg {
InitialSync {
entity_package: sync::EntityPackage<EcsCompPacket>,
server_info: ServerInfo,
time_of_day: state::TimeOfDay,
world_map: (Vec2<u32>, Vec<u32>),
},
/// An error occurred while loading character data
CharacterDataLoadError(String),
/// A list of characters belonging to the a authenticated player was sent
CharacterListUpdate(Vec<CharacterItem>),
/// An error occured while creating or deleting a character
CharacterActionError(String),
PlayerListUpdate(PlayerListUpdate),
StateAnswer(Result<ClientState, (RequestStateError, ClientState)>),
/// Trigger cleanup for when the client goes back to the `Registered` state
/// from an ingame state
ExitIngameCleanup,
Ping,
Pong,
2020-06-02 02:42:26 +00:00
/// A message to go into the client chat box. The client is responsible for
/// formatting the message and turning it into a speech bubble.
2020-06-02 02:42:26 +00:00
ChatMsg(comp::ChatMsg),
SetPlayerEntity(Uid),
TimeOfDay(state::TimeOfDay),
EntitySync(sync::EntitySyncPackage),
CompSync(sync::CompSyncPackage<EcsCompPacket>),
2019-11-24 20:12:03 +00:00
CreateEntity(sync::EntityPackage<EcsCompPacket>),
DeleteEntity(Uid),
InventoryUpdate(comp::Inventory, comp::InventoryUpdateEvent),
TerrainChunkUpdate {
key: Vec2<i32>,
chunk: Result<Box<TerrainChunk>, ()>,
},
2019-08-11 20:38:28 +00:00
TerrainBlockUpdates(HashMap<Vec3<i32>, Block>),
Disconnect,
Shutdown,
TooManyPlayers,
2020-05-14 16:56:10 +00:00
/// Send a popup notification such as "Waypoint Saved"
Notification(Notification),
SetViewDistance(u32),
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum RequestStateError {
RegisterDenied(RegisterError),
Denied,
Already,
Impossible,
WrongMessage,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum RegisterError {
2019-12-21 17:02:39 +00:00
AlreadyLoggedIn,
2020-01-01 20:17:43 +00:00
AuthError(String),
InvalidCharacter,
2020-06-24 15:27:18 +00:00
NotOnWhitelist,
//TODO: InvalidAlias,
}
2019-07-17 22:10:42 +00:00
impl From<AuthClientError> for RegisterError {
2020-01-01 20:17:43 +00:00
fn from(err: AuthClientError) -> Self { Self::AuthError(err.to_string()) }
}
2019-07-17 22:10:42 +00:00
impl ServerMsg {
pub fn broadcast(message: String) -> ServerMsg {
2020-06-02 02:42:26 +00:00
ServerMsg::ChatMsg(comp::ChatMsg {
chat_type: comp::ChatType::Broadcast,
message,
2020-06-02 02:42:26 +00:00
})
}
pub fn private(message: String) -> ServerMsg {
2020-06-02 02:42:26 +00:00
ServerMsg::ChatMsg(comp::ChatMsg {
chat_type: comp::ChatType::Private,
message,
2020-06-02 02:42:26 +00:00
})
}
2019-07-29 14:40:46 +00:00
pub fn kill(message: String) -> ServerMsg {
2020-06-02 02:42:26 +00:00
ServerMsg::ChatMsg(comp::ChatMsg {
chat_type: comp::ChatType::Kill,
2019-07-29 14:40:46 +00:00
message,
2020-06-02 02:42:26 +00:00
})
2019-07-29 14:40:46 +00:00
}
2019-07-17 22:10:42 +00:00
}