Add character name and level to social window

This commit is contained in:
scott-c 2020-05-20 19:59:44 +08:00
parent 636fcafd2e
commit c23183b1ca
6 changed files with 44 additions and 29 deletions

View File

@ -40,6 +40,7 @@ fn complete_player(part: &str, client: &Client) -> Vec<String> {
client
.player_list
.values()
.map(|player_info| &player_info.player_alias)
.filter(|alias| alias.starts_with(part))
.cloned()
.collect()

View File

@ -23,7 +23,7 @@ use common::{
event::{EventBus, SfxEvent, SfxEventItem},
msg::{
validate_chat_msg, ChatMsgValidationError, ClientMsg, ClientState, Notification,
PlayerListUpdate, RegisterError, RequestStateError, ServerInfo, ServerMsg,
PlayerInfo, PlayerListUpdate, RegisterError, RequestStateError, ServerInfo, ServerMsg,
MAX_BYTES_CHAT_MSG,
},
net::PostBox,
@ -68,7 +68,7 @@ pub struct Client {
thread_pool: ThreadPool,
pub server_info: ServerInfo,
pub world_map: (Arc<DynamicImage>, Vec2<u32>),
pub player_list: HashMap<u64, String>,
pub player_list: HashMap<u64, PlayerInfo>,
pub character_list: CharacterList,
postbox: PostBox<ClientMsg, ServerMsg>,
@ -734,13 +734,15 @@ impl Client {
ServerMsg::PlayerListUpdate(PlayerListUpdate::Init(list)) => {
self.player_list = list
},
ServerMsg::PlayerListUpdate(PlayerListUpdate::Add(uid, name)) => {
if let Some(old_name) = self.player_list.insert(uid, name.clone()) {
ServerMsg::PlayerListUpdate(PlayerListUpdate::Add(uid, player_info)) => {
if let Some(old_player_info) =
self.player_list.insert(uid, player_info.clone())
{
warn!(
"Received msg to insert {} with uid {} into the player list but \
there was already an entry for {} with the same uid that was \
overwritten!",
name, uid, old_name
player_info.player_alias, uid, old_player_info.player_alias
);
}
},
@ -754,8 +756,8 @@ impl Client {
}
},
ServerMsg::PlayerListUpdate(PlayerListUpdate::Alias(uid, new_name)) => {
if let Some(name) = self.player_list.get_mut(&uid) {
*name = new_name;
if let Some(player_info) = self.player_list.get_mut(&uid) {
player_info.player_alias = new_name;
} else {
warn!(
"Received msg to alias player with uid {} to {} but this uid is \

View File

@ -7,7 +7,8 @@ pub use self::{
client::ClientMsg,
ecs_packet::EcsCompPacket,
server::{
Notification, PlayerListUpdate, RegisterError, RequestStateError, ServerInfo, ServerMsg,
Notification, PlayerInfo, PlayerListUpdate, RegisterError, RequestStateError, ServerInfo,
ServerMsg,
},
};

View File

@ -20,12 +20,19 @@ pub struct ServerInfo {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PlayerListUpdate {
Init(HashMap<u64, String>),
Add(u64, String),
Init(HashMap<u64, PlayerInfo>),
Add(u64, PlayerInfo),
Remove(u64),
Alias(u64, String),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlayerInfo {
pub player_alias: String,
pub character_name: String,
pub character_level: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Notification {
WaypointSaved,

View File

@ -10,8 +10,8 @@ use common::{
},
event::{EventBus, ServerEvent},
msg::{
validate_chat_msg, ChatMsgValidationError, ClientMsg, ClientState, PlayerListUpdate,
RequestStateError, ServerMsg, MAX_BYTES_CHAT_MSG,
validate_chat_msg, ChatMsgValidationError, ClientMsg, ClientState, PlayerInfo,
PlayerListUpdate, RequestStateError, ServerMsg, MAX_BYTES_CHAT_MSG,
},
state::{BlockChange, Time},
sync::Uid,
@ -83,9 +83,15 @@ impl<'a> System<'a> for Sys {
let mut new_chat_msgs = Vec::new();
// Player list to send new players.
let player_list = (&uids, &players)
let player_list = (&uids, &players, &stats)
.join()
.map(|(uid, player)| ((*uid).into(), player.alias.clone()))
.map(|(uid, player, stats)| {
((*uid).into(), PlayerInfo {
player_alias: player.alias.clone(),
character_name: stats.name.clone(),
character_level: stats.level.level(),
})
})
.collect::<HashMap<_, _>>();
// List of new players to update player lists of all clients.
let mut new_players = Vec::new();
@ -382,11 +388,15 @@ impl<'a> System<'a> for Sys {
// Handle new players.
// Tell all clients to add them to the player list.
for entity in new_players {
if let (Some(uid), Some(player)) = (uids.get(entity), players.get(entity)) {
let msg = ServerMsg::PlayerListUpdate(PlayerListUpdate::Add(
(*uid).into(),
player.alias.clone(),
));
if let (Some(uid), Some(player), Some(stats)) =
(uids.get(entity), players.get(entity), stats.get(entity))
{
let msg =
ServerMsg::PlayerListUpdate(PlayerListUpdate::Add((*uid).into(), PlayerInfo {
player_alias: player.alias.clone(),
character_name: stats.name.clone(),
character_level: stats.level.level(), // TODO: stats.level.amount,
}));
for client in (&mut clients).join().filter(|c| c.is_registered()) {
client.notify(msg.clone())
}

View File

@ -192,18 +192,12 @@ impl<'a> Widget for Social<'a> {
.font_id(self.fonts.cyri.conrod_id)
.color(TEXT_COLOR)
.set(ids.online_title, ui);
for (i, (_, player_alias)) in self.client.player_list.iter().enumerate() {
for (i, (_, player_info)) in self.client.player_list.iter().enumerate() {
Text::new(&format!(
"[{}] {} Lvl {}",
player_alias,
match self.client.character_list.characters.get(i) {
Some(s) => &s.character.alias,
None => "<Unknown>",
},
match self.client.character_list.characters.get(i) {
Some(s) => s.level.to_string(),
None => "<Unknown>".to_string(),
},
player_info.player_alias,
player_info.character_name,
player_info.character_level
))
.down(3.0)
.font_size(self.fonts.cyri.scale(15))