aozhiwei 73e374da5b 1
2021-09-06 07:07:27 +00:00

257 lines
8.3 KiB
C++

#include "precompile.h"
#include "killmgr.h"
#include "human.h"
#include "metamgr.h"
#include "room.h"
#include "player.h"
#include "cs_proto.pb.h"
struct RollMsgHintInfo
{
std::string master_name;
std::string killer_name;
std::string dead_name;
std::string weapon_text_icon;
std::vector<std::tuple<int, std::string>>* hint_template = nullptr;
bool Replace(int idx, std::string& text)
{
switch (idx) {
case kFieldIdxMasterName:
{
text = master_name;
}
break;
case kFieldIdxKillerName:
{
text = killer_name;
}
break;
case kFieldIdxDeadName:
{
text = dead_name;
}
break;
case kFieldIdxWeaponTextIcon:
{
text = weapon_text_icon;
}
break;
default:
{
return false;
}
break;
}
return true;
}
};
void KillMgr::Init()
{
}
void KillMgr::UnInit()
{
}
/*
master.name
killer.name
dead.name
weapon.name
weapon.text_icon
image.id:
*/
void KillMgr::OnHumanDead(Human* dead_hum, KillInfo* info)
{
RollMsgHintInfo hint_info;
std::shared_ptr<cs::SMRollMsg> pb_msg = std::make_shared<cs::SMRollMsg>();
FillHintInfo(dead_hum, info, hint_info);
PreprocessRollMsg(pb_msg, hint_info);
BoradcastRollMsg(dead_hum, info, 0, pb_msg);
}
void KillMgr::FillHintInfo(Human* dead_hum, KillInfo* info, RollMsgHintInfo& hint_info)
{
hint_info.dead_name = dead_hum->GetName();
switch (info->killer_id) {
case VP_Gas:
{
hint_info.killer_name = "";
hint_info.hint_template = MetaMgr::Instance()->GetTextElements
("battle_server_dead_text_gas");
}
break;
case VP_Buff:
{
hint_info.killer_name = "";
hint_info.hint_template = MetaMgr::Instance()->GetTextElements
(a8::Format("battle_server_dead_text_buff_%d", {info->weapon_id}));
if (!hint_info.hint_template) {
hint_info.hint_template = MetaMgr::Instance()->GetTextElements
("battle_server_dead_text_buff_default");
}
}
break;
case VP_Explosion:
{
hint_info.killer_name = "";
hint_info.hint_template = MetaMgr::Instance()->GetTextElements
(a8::Format("battle_server_dead_text_explosion_%d", {info->weapon_id}));
if (!hint_info.hint_template) {
hint_info.hint_template = MetaMgr::Instance()->GetTextElements
("battle_server_dead_text_explosion_default");
}
}
break;
default:
{
Entity* killer = dead_hum->room->GetEntityByUniId(info->killer_id);
if (killer) {
MetaData::Equip* equip_meta = MetaMgr::Instance()->GetEquip(info->weapon_id);
if (equip_meta) {
hint_info.weapon_text_icon = equip_meta->i->text_icon();
}
hint_info.killer_name = killer->GetName();
if (info->killer_id == dead_hum->GetUniId()) {
//${dead.name} 自杀
hint_info.hint_template = MetaMgr::Instance()->GetTextElements
("battle_server_dead_text_specate");
} else {
//${killer.name} 使用 ${weapon.text_icon} 干掉了 ${dead.name}
hint_info.hint_template = MetaMgr::Instance()->GetTextElements
("battle_server_dead_text_weapon");
}
} else {
}
}
break;
}
}
void KillMgr::PreprocessRollMsg(std::shared_ptr<cs::SMRollMsg> pb_msg,
RollMsgHintInfo& hint_info)
{
if (hint_info.hint_template) {
for (auto& tuple : *hint_info.hint_template) {
int el_type = std::get<0>(tuple);
std::string el_val = std::get<1>(tuple);
switch (el_type) {
case kTextElement:
{
auto e = pb_msg->add_elements();
e->set_element_type(el_type);
e->mutable_union_obj_1()->set_text(el_val);
}
break;
case kImageElement:
{
auto e = pb_msg->add_elements();
e->set_element_type(el_type);
e->mutable_union_obj_2()->set_id(a8::XValue(el_val));
}
break;
default:
{
std::string text;
if (hint_info.Replace(el_type, text)) {
if (el_type == kFieldIdxWeaponTextIcon) {
auto e = pb_msg->add_elements();
e->set_element_type(el_type);
e->mutable_union_obj_2()->set_id(a8::XValue(text));
} else {
auto e = pb_msg->add_elements();
e->set_element_type(el_type);
e->mutable_union_obj_1()->set_text(text);
}
}
}
break;
}
}
}
}
void KillMgr::BoradcastRollMsg(Human* dead_hum,
KillInfo* info,
int killer_team_id,
std::shared_ptr<cs::SMRollMsg> pb_msg)
{
KillInfo* info_copy = new KillInfo();
*info_copy = *info;
std::shared_ptr<cs::SMRollMsg>* pb_msg_copy = new std::shared_ptr<cs::SMRollMsg>;
*pb_msg_copy = pb_msg;
dead_hum->room->xtimer.AddDeadLineTimerAndAttach
(
0,
a8::XParams()
.SetSender(dead_hum)
.SetParam1(info_copy)
.SetParam2(pb_msg_copy)
.SetParam3(killer_team_id),
[] (const a8::XParams& param)
{
Human* dead_hum = (Human*)param.sender.GetUserData();
KillInfo* info = (KillInfo*)param.param1.GetUserData();
std::shared_ptr<cs::SMRollMsg>* pb_msg = (std::shared_ptr<cs::SMRollMsg>*)
param.param2.GetUserData();
int killer_team_id = param.param3;
KillMgr::Instance()->BoradcastRollMsgCb
(dead_hum,
info,
killer_team_id,
*pb_msg);
},
&dead_hum->xtimer_attacher.timer_list_,
[] (const a8::XParams& param)
{
KillInfo* info = (KillInfo*)param.param1.GetUserData();
std::shared_ptr<cs::SMRollMsg>* pb_msg = (std::shared_ptr<cs::SMRollMsg>*)
param.param2.GetUserData();
delete pb_msg;
delete info;
}
);
}
void KillMgr::BoradcastRollMsgCb(Human* dead_hum,
KillInfo* info,
int killer_team_id,
std::shared_ptr<cs::SMRollMsg> pb_msg)
{
dead_hum->room->TraversePlayerList
(a8::XParams(),
[dead_hum, info, &pb_msg, killer_team_id]
(Player* hum, a8::XParams& param) -> bool
{
for (int i = 0; i < pb_msg->elements_size(); ++i) {
auto element = pb_msg->mutable_elements(i);
if (element->element_type() == kTextElement) {
int color = element->mutable_union_obj_1()->color();
if (info->killer_id == hum->GetUniId()){
color = MetaMgr::Instance()->self_kill_color;
} else if (killer_team_id == hum->team_id) {
color = MetaMgr::Instance()->teammate_kill_color;
} else if (dead_hum == hum) {
color = MetaMgr::Instance()->self_bekill_color;
} else if (dead_hum->team_id == hum->team_id) {
color = MetaMgr::Instance()->teammate_bekill_color;
}
if (element->mutable_union_obj_1()->color() != color) {
element->mutable_union_obj_1()->set_color(color);
}
}
}
hum->SendNotifyMsg(*pb_msg);
return true;
});
}