Fix social list having incorrect player count

This commit is contained in:
scott-c 2020-05-24 17:20:54 +08:00
parent c23183b1ca
commit 136bf271b8
5 changed files with 42 additions and 16 deletions

View File

@ -746,6 +746,23 @@ impl Client {
); );
} }
}, },
ServerMsg::PlayerListUpdate(PlayerListUpdate::SelectedCharacter(
uid,
char_info,
)) => {
warn!(
"ServerMsg::PlayerListUpdate PlayerListUpdate::LevelChange not \
implemented: {} {:?}",
uid, char_info
);
},
ServerMsg::PlayerListUpdate(PlayerListUpdate::LevelChange(uid, next_level)) => {
warn!(
"ServerMsg::PlayerListUpdate PlayerListUpdate::LevelChange not \
implemented: {} {}",
uid, next_level
);
},
ServerMsg::PlayerListUpdate(PlayerListUpdate::Remove(uid)) => { ServerMsg::PlayerListUpdate(PlayerListUpdate::Remove(uid)) => {
if self.player_list.remove(&uid).is_none() { if self.player_list.remove(&uid).is_none() {
warn!( warn!(

View File

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

View File

@ -22,6 +22,8 @@ pub struct ServerInfo {
pub enum PlayerListUpdate { pub enum PlayerListUpdate {
Init(HashMap<u64, PlayerInfo>), Init(HashMap<u64, PlayerInfo>),
Add(u64, PlayerInfo), Add(u64, PlayerInfo),
SelectedCharacter(u64, CharacterInfo),
LevelChange(u64, u32),
Remove(u64), Remove(u64),
Alias(u64, String), Alias(u64, String),
} }
@ -29,8 +31,13 @@ pub enum PlayerListUpdate {
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlayerInfo { pub struct PlayerInfo {
pub player_alias: String, pub player_alias: String,
pub character_name: String, pub character: Option<CharacterInfo>,
pub character_level: u32, }
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CharacterInfo {
pub name: String,
pub level: u32,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]

View File

@ -10,8 +10,8 @@ use common::{
}, },
event::{EventBus, ServerEvent}, event::{EventBus, ServerEvent},
msg::{ msg::{
validate_chat_msg, ChatMsgValidationError, ClientMsg, ClientState, PlayerInfo, validate_chat_msg, CharacterInfo, ChatMsgValidationError, ClientMsg, ClientState,
PlayerListUpdate, RequestStateError, ServerMsg, MAX_BYTES_CHAT_MSG, PlayerInfo, PlayerListUpdate, RequestStateError, ServerMsg, MAX_BYTES_CHAT_MSG,
}, },
state::{BlockChange, Time}, state::{BlockChange, Time},
sync::Uid, sync::Uid,
@ -88,8 +88,11 @@ impl<'a> System<'a> for Sys {
.map(|(uid, player, stats)| { .map(|(uid, player, stats)| {
((*uid).into(), PlayerInfo { ((*uid).into(), PlayerInfo {
player_alias: player.alias.clone(), player_alias: player.alias.clone(),
character_name: stats.name.clone(), // TODO: player might not have a character selected
character_level: stats.level.level(), character: Some(CharacterInfo {
name: stats.name.clone(),
level: stats.level.level(),
}),
}) })
}) })
.collect::<HashMap<_, _>>(); .collect::<HashMap<_, _>>();
@ -388,14 +391,11 @@ impl<'a> System<'a> for Sys {
// Handle new players. // Handle new players.
// Tell all clients to add them to the player list. // Tell all clients to add them to the player list.
for entity in new_players { for entity in new_players {
if let (Some(uid), Some(player), Some(stats)) = if let (Some(uid), Some(player)) = (uids.get(entity), players.get(entity)) {
(uids.get(entity), players.get(entity), stats.get(entity))
{
let msg = let msg =
ServerMsg::PlayerListUpdate(PlayerListUpdate::Add((*uid).into(), PlayerInfo { ServerMsg::PlayerListUpdate(PlayerListUpdate::Add((*uid).into(), PlayerInfo {
player_alias: player.alias.clone(), player_alias: player.alias.clone(),
character_name: stats.name.clone(), character: None, // new players will be on character select.
character_level: stats.level.level(), // TODO: stats.level.amount,
})); }));
for client in (&mut clients).join().filter(|c| c.is_registered()) { for client in (&mut clients).join().filter(|c| c.is_registered()) {
client.notify(msg.clone()) client.notify(msg.clone())

View File

@ -194,10 +194,12 @@ impl<'a> Widget for Social<'a> {
.set(ids.online_title, ui); .set(ids.online_title, ui);
for (i, (_, player_info)) in self.client.player_list.iter().enumerate() { for (i, (_, player_info)) in self.client.player_list.iter().enumerate() {
Text::new(&format!( Text::new(&format!(
"[{}] {} Lvl {}", "[{}] {}",
player_info.player_alias, player_info.player_alias,
player_info.character_name, match &player_info.character {
player_info.character_level Some(character) => format!("{} Lvl {}", &character.name, &character.level),
None => "<None>".to_string(), // character select or spectating
}
)) ))
.down(3.0) .down(3.0)
.font_size(self.fonts.cyri.scale(15)) .font_size(self.fonts.cyri.scale(15))