这以前写的代码

之前是玩家背包每个东西都是满的,副本掉落,会掉很多东西,因为玩家背包都满了,需要发送邮件,当时主程就要求这些东西作为一封邮件发出去

首先是获取之前db_req中有没有邮件

bool bDBMailExist =  m_userMailStrategy->GetExistMailWithAttach(db_req->mutable_mails(), pDBMail);

如果存在给这些邮件直接添加附件

m_userMailStrategy->AddAttachTo(gameEle, pDBMail);
m_userMailStrategy->AddAttachTo(gameEle, pUserMail);
bool CUserMailStrategy::AddAttachTo(const struct_game_element& gameEle, db_user_mail* pDBMail)
{
    ASSERT(pDBMail);
    
    ::google::protobuf::RepeatedPtrField< ::db_mail_attach >::iterator iter = pDBMail->mutable_attachs()->begin();
    for (; iter != pDBMail->mutable_attachs()->end(); ++iter)
    {
        if (iter->mutable_element()->type() == gameEle.type())
        {
            if (gameEle.type() != struct_game_element::EELEMENTTYPE_ITEM || iter->mutable_element()->id() == gameEle.id())//非道具类型直接叠加
            {
                iter->mutable_element()->set_num(iter->mutable_element()->num() + gameEle.num());
                return true;
            }
        }
    }

    db_mail_attach* pAttach = pDBMail->add_attachs();
    pAttach->set_attach_id(CMailMgr::GetInstance()->CreateId(eIdType_MailAttach));
    pAttach->mutable_element()->CopyFrom(gameEle);

    return true;
}

说实话我不喜欢写这些代码,在某些特殊出现的情况下的优化,实在是太花时间,现在我要优化,看着这些我都蛋疼,这样的代码很容易出错,写的时候要小心,哎,还是按照这个模式来吧。

不过如果刚开始设计的好,这些需求实现起来应该也简单

posted @ 2015-09-29 16:17  zzyoucan  阅读(213)  评论(0编辑  收藏  举报