Skip to content

Commit eca2532

Browse files
committed
feat(redpacket): 添加红包信息命令并优化输出格式
1 parent 1078b33 commit eca2532

2 files changed

Lines changed: 223 additions & 7 deletions

File tree

client/src/app.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,7 @@ impl App {
206206
}
207207
":n" | ":notice" => {
208208
self.client.notice.remove_all_listeners().await;
209-
if !self.client.notice.is_connected().await {
210-
self.client.notice.connect(None).await;
211-
}
209+
self.client.notice.connect(None).await;
212210
match self.command_registry.execute(&context, "notice", &[]).await {
213211
Ok(_) => {}
214212
Err(e) => println!("进入通知失败: {}", e),

client/src/commands/handlers/redpacket.rs

Lines changed: 222 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ impl RedpacketCommand {
4343
"gesture" | "g" => self.handle_gesture_command(&parts[2..]).await?,
4444
"list" | "l" => self.handle_list_command().await?,
4545
"." => self.handle_auto_open_command().await?,
46+
"i" | "info" => self.handle_info_command(&parts[2..]).await?,
4647
"help" | "-h" | "--help" => println!("{}", self.help().green()),
4748
_ => {
4849
println!("{}: {}", "未知红包命令".red(), parts[1]);
@@ -104,7 +105,7 @@ impl RedpacketCommand {
104105
let user_name = self.context.auth.get_user_name().await?;
105106
if let Some(got) = info.who.iter().find(|got| got.user_name == user_name) {
106107
println!(
107-
"你领取了 {} 积分 {} / {}",
108+
"\r你领取了 {} 积分 {} / {}",
108109
got.money.to_string().yellow().bold(),
109110
info.info.got.to_string().cyan(),
110111
info.info.count.to_string().cyan()
@@ -136,7 +137,7 @@ impl RedpacketCommand {
136137

137138
if let Some(info) = &result.data {
138139
if info.info.count == info.info.got {
139-
println!("{}", "红包已领完".yellow());
140+
println!("\r{}", "红包已领完".yellow());
140141
println!(
141142
"{}",
142143
"红包详情:\n===============================".red().bold()
@@ -155,13 +156,13 @@ impl RedpacketCommand {
155156
let user_name = self.context.auth.get_user_name().await?;
156157
if let Some(got) = info.who.iter().find(|got| got.user_name == user_name) {
157158
println!(
158-
"你领取了 {} 积分 {} / {}",
159+
"\r你领取了 {} 积分 {} / {}",
159160
got.money.to_string().yellow().bold(),
160161
info.info.got.to_string().cyan(),
161162
info.info.count.to_string().cyan()
162163
);
163164
} else {
164-
println!("{}", "红包已领完".yellow());
165+
println!("\r{}", "红包已领完".yellow());
165166
}
166167
}
167168
Ok(())
@@ -591,6 +592,223 @@ impl RedpacketCommand {
591592

592593
Ok(())
593594
}
595+
596+
/// 显示红包详细信息
597+
async fn handle_info_command(&self, args: &[&str]) -> Result<()> {
598+
if args.is_empty() {
599+
println!("{}", "用法: :rp i <红包ID>".yellow());
600+
return Ok(());
601+
}
602+
603+
let oid = args[0];
604+
let result = self.context.client.redpacket.open(oid).await;
605+
606+
if !result.success {
607+
println!(
608+
"{}: {}",
609+
"查询红包失败".red(),
610+
result.message.unwrap_or("未知错误".to_string())
611+
);
612+
return Ok(());
613+
}
614+
615+
if let Some(info) = &result.data {
616+
// 显示红包基本信息
617+
println!("\n{}", "===============================".cyan().bold());
618+
println!(
619+
"发送者: {} | 消息: {}",
620+
info.info.user_name.green().bold(),
621+
info.info.msg.yellow()
622+
);
623+
624+
// 判断红包类型
625+
let redpacket_type = if info.info.gesture.is_some() {
626+
"猜拳红包".magenta().bold()
627+
} else if !info.receivers.is_empty() {
628+
"专属红包".blue().bold()
629+
} else {
630+
let has_negative = info.who.iter().any(|w| w.money < 0);
631+
let all_same = info.who.iter().all(|w| w.money == info.who[0].money);
632+
633+
if has_negative {
634+
"心跳红包".red().bold()
635+
} else if all_same {
636+
"平分红包".blue().bold()
637+
} else {
638+
"拼手气红包".green().bold()
639+
}
640+
};
641+
642+
println!("红包类型: {}", redpacket_type);
643+
644+
// 显示专属红包的接收者列表
645+
if !info.receivers.is_empty() {
646+
println!(
647+
"指定接收者: {}",
648+
info.receivers.join(", ").cyan().bold()
649+
);
650+
}
651+
652+
println!(
653+
"红包数量: {} 个 | 已领取: {} / {}",
654+
info.info.count.to_string().cyan(),
655+
info.info.got.to_string().green().bold(),
656+
info.info.count.to_string().cyan()
657+
);
658+
659+
// 计算总金额
660+
let total_money: i32 = info.who.iter().map(|w| w.money.abs()).sum();
661+
println!("总金额: {} 积分", total_money.to_string().yellow().bold());
662+
663+
// 显示猜拳红包的发送者手势
664+
if let Some(gesture) = info.info.gesture {
665+
let sender_gesture_name = GestureType::from_i32(gesture)
666+
.map(|g| g.name())
667+
.unwrap_or("未知");
668+
println!(
669+
"{}",
670+
sender_gesture_name.yellow().bold()
671+
);
672+
}
673+
674+
println!("{}", "===============================".cyan().bold());
675+
676+
// 根据红包类型显示详情
677+
if info.info.gesture.is_some() {
678+
self.show_gesture_redpacket_details(info, info.info.gesture.unwrap());
679+
} else if !info.receivers.is_empty() {
680+
self.show_specify_redpacket_details(info);
681+
} else {
682+
let has_negative = info.who.iter().any(|w| w.money < 0);
683+
let all_same = info.who.iter().all(|w| w.money == info.who[0].money);
684+
685+
if has_negative {
686+
self.show_heartbeat_redpacket_details(info);
687+
} else if all_same {
688+
self.show_average_redpacket_details(info);
689+
} else {
690+
self.show_random_redpacket_details(info);
691+
}
692+
}
693+
694+
println!("{}\n", "===============================".cyan().bold());
695+
}
696+
697+
Ok(())
698+
}
699+
700+
/// 显示猜拳红包详情
701+
fn show_gesture_redpacket_details(&self, info: &fishpi_rust::RedPacketInfo, sender_gesture: i32) {
702+
for w in &info.who {
703+
let receiver_gesture = if w.money == 0 {
704+
sender_gesture
705+
} else if w.money > 0 {
706+
match sender_gesture {
707+
0 => 2, 1 => 0, 2 => 1, _ => sender_gesture,
708+
}
709+
} else {
710+
match sender_gesture {
711+
0 => 1, 1 => 2, 2 => 0, _ => sender_gesture,
712+
}
713+
};
714+
715+
let receiver_gesture_name = GestureType::from_i32(receiver_gesture)
716+
.map(|g| g.name())
717+
.unwrap_or("未知");
718+
719+
let result_text = match w.money {
720+
0 => format!("出 {} 平局", receiver_gesture_name.yellow()),
721+
m if m > 0 => format!(
722+
"{} {} 积分",
723+
receiver_gesture_name.green().bold(),
724+
m.to_string().cyan().bold()
725+
),
726+
m => format!(
727+
"{} -{} 积分",
728+
receiver_gesture_name.red(),
729+
m.abs().to_string().cyan().bold()
730+
),
731+
};
732+
733+
println!(
734+
"[{}] {}: {}",
735+
w.time.blue(),
736+
w.user_name.green(),
737+
result_text
738+
);
739+
}
740+
}
741+
742+
/// 显示心跳红包详情
743+
fn show_heartbeat_redpacket_details(&self, info: &fishpi_rust::RedPacketInfo) {
744+
for w in &info.who {
745+
let result_text = match w.money {
746+
0 => "0溢事件".yellow().to_string(),
747+
m if m > 0 => format!(
748+
"{} 积分",
749+
m.to_string().green().bold()
750+
),
751+
m => format!(
752+
"-{} 积分",
753+
m.abs().to_string().red()
754+
),
755+
};
756+
757+
println!(
758+
"[{}] {}: {}",
759+
w.time.blue(),
760+
w.user_name.green(),
761+
result_text
762+
);
763+
}
764+
}
765+
766+
/// 显示拼手气红包详情
767+
fn show_random_redpacket_details(&self, info: &fishpi_rust::RedPacketInfo) {
768+
// 找出最佳手气
769+
let max_money = info.who.iter().map(|w| w.money).max().unwrap_or(0);
770+
771+
for w in &info.who {
772+
let is_best = w.money == max_money && info.who.len() > 1;
773+
let money_text = if is_best {
774+
format!("{} 积分 🎉", w.money.to_string().yellow().bold())
775+
} else {
776+
format!("{} 积分", w.money.to_string().cyan())
777+
};
778+
779+
println!(
780+
"[{}] {}: {}{}",
781+
w.time.blue(),
782+
w.user_name.green(),
783+
money_text,
784+
if is_best { " (最佳手气)".red().bold().to_string() } else { "".to_string() }
785+
);
786+
}
787+
}
788+
789+
/// 显示平分红包详情
790+
fn show_average_redpacket_details(&self, info: &fishpi_rust::RedPacketInfo) {
791+
for w in &info.who {
792+
println!(
793+
"[{}] {}: {} 积分",
794+
w.time.blue(),
795+
w.user_name.green(),
796+
w.money.to_string().cyan().bold()
797+
);
798+
}
799+
}
800+
801+
/// 显示专属红包详情
802+
fn show_specify_redpacket_details(&self, info: &fishpi_rust::RedPacketInfo) {
803+
for w in &info.who {
804+
println!(
805+
"[{}] {}: {} 积分",
806+
w.time.blue(),
807+
w.user_name.green(),
808+
w.money.to_string().cyan().bold()
809+
);
810+
}
811+
}
594812
}
595813

596814
#[async_trait]

0 commit comments

Comments
 (0)