go_dreamfactory/modules/sociaty/api_cross_receive.go
2022-11-17 20:33:44 +08:00

90 lines
2.9 KiB
Go

package sociaty
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb"
"google.golang.org/protobuf/proto"
)
// 公会任务奖励领取
func (this *apiComp) ReceiveCheck(session comm.IUserSession, req *pb.SociatyReceiveReq) (code pb.ErrorCode) {
if req.TaskId == 0 {
code = pb.ErrorCode_ReqParameterError
this.module.Error("公会任务奖励领取参数错误", log.Fields{"uid": session.GetUserId(), "params": req})
}
return
}
func (this *apiComp) Receive(session comm.IUserSession, req *pb.SociatyReceiveReq) (code pb.ErrorCode, data proto.Message) {
if code = this.ReceiveCheck(session, req); code != pb.ErrorCode_Success {
return
}
uid := session.GetUserId()
sociaty := this.module.modelSociaty.getUserSociaty(uid)
if sociaty != nil && sociaty.Id == "" {
code = pb.ErrorCode_SociatyNoFound
this.module.Error("当前玩家所在的公会未找到", log.Fields{"uid": uid})
return
}
// 判断奖励是否已领
sociatyTask := this.module.modelSociaty.getUserTaskList(uid, sociaty.Id)
for _, v := range sociatyTask.TaskList {
if v.TaskId == req.TaskId {
if v.Status == 1 { //已领取
code = pb.ErrorCode_SociatyRewardReceived
return
}
break
}
}
// 验证任务是否完成
if err, ok := this.module.modelSociaty.validTask(uid, req.TaskId); err != nil || !ok {
code = pb.ErrorCode_SociatyTaskValidation
this.module.Error("公会任务未通过", log.Fields{"uid": uid, "sociatyId": sociaty.Id, "taskId": req.TaskId, "err": err.Error()})
return
}
// 奖励领取
if err := this.module.modelSociatyTask.receive(req.TaskId, sociaty.Id, uid); err != nil {
code = pb.ErrorCode_SociatyRewardReceive
this.module.Error("领取公会任务奖励", log.Fields{"uid": uid, "sociatyId": sociaty.Id, "taskId": req.TaskId, "err": err.Error()})
return
}
//获取奖励配置
conf, ok := this.module.sociatyTaskConf.GetDataMap()[req.TaskId]
if ok {
// 发放个人奖励
if code = this.module.DispenseRes(session, conf.Reward, true); code != pb.ErrorCode_Success {
this.module.Error("发放公会个人奖励失败", log.Fields{"uid": uid, "sociatyId": sociaty.Id, "taskId": req.TaskId, "code": code})
}
}
// 更新公会经验和活跃度
if err := this.module.modelSociaty.updateResourceFromTask(sociaty, conf); err != nil {
this.module.Error("更新公会资源", log.Fields{"uid": uid, "sociatyId": sociaty.Id, "taskId": req.TaskId, "err": err.Error()})
return
}
// 更新成员贡献值
if err := this.module.modelSociaty.updateMemberContribution(uid, conf.Contribution, sociaty); err != nil {
this.module.Error("更新公会成员贡献值", log.Fields{"uid": uid, "sociatyId": sociaty.Id, "taskId": req.TaskId, "err": err.Error()})
return
}
rsp := &pb.SociatyReceiveResp{
SociatyId: sociaty.Id,
TaskId: req.TaskId,
}
if err := session.SendMsg(string(this.module.GetType()), SociatySubTypeReceive, rsp); err != nil {
code = pb.ErrorCode_SystemError
}
return
}