go_dreamfactory/modules/sociaty/api_cross_receive.go
2022-11-07 11:52:00 +08:00

93 lines
2.6 KiB
Go

package sociaty
import (
"go_dreamfactory/comm"
"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
}
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.Id == "" {
code = pb.ErrorCode_SociatyNoFound
this.module.Errorf("uid:%s not in sociaty", 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.Errorf("任务 taskId:%v 验证未通过", req.TaskId)
return
}
// 领取
if err := this.module.modelSociatyTask.receive(req.TaskId, sociaty.Id, uid); err != nil {
code = pb.ErrorCode_SociatyRewardReceive
this.module.Errorf("领取任务奖励 err:%v", err)
return
}
//获取奖励配置
ggt, err := this.module.configure.getSociatyTaskCfg()
if err != nil || ggt == nil {
code = pb.ErrorCode_ConfigNoFound
return
}
conf, ok := ggt.GetDataMap()[req.TaskId]
if ok {
// 发放个人奖励
if code = this.module.DispenseRes(session, conf.Reward, true); code != pb.ErrorCode_Success {
this.module.Errorf("发放个人奖励失败 sociatyId:%s uid:%s taskId:%d code:%v", sociaty.Id, uid, req.TaskId, code)
}
}
// 更新公会经验和活跃度
if err := this.module.modelSociaty.updateResourceFromTask(sociaty, conf); err != nil {
this.module.Errorf("更新公会资源 sociatyId:%s uid:%s taskId:%d err:%v", sociaty.Id, uid, req.TaskId, err)
return
}
// 更新成员贡献值
if err := this.module.modelSociaty.updateMemberContribution(uid, conf.Contribution, sociaty); err != nil {
this.module.Errorf("更新成员贡献值 sociatyId:%s uid:%s taskId:%d err:%v", sociaty.Id, uid, req.TaskId, err)
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
}