go_dreamfactory/modules/linestory/api_receive.go
2022-11-15 19:39:38 +08:00

56 lines
1.6 KiB
Go

package linestory
import (
"errors"
"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.LinestoryReceiveReq) (code pb.ErrorCode) {
if req.ChapterId == 0 {
code = pb.ErrorCode_ReqParameterError
}
return
}
func (this *apiComp) Receive(session comm.IUserSession, req *pb.LinestoryReceiveReq) (code pb.ErrorCode, data proto.Message) {
if code = this.ReceiveCheck(session, req); code != pb.ErrorCode_Success {
return
}
uid := session.GetUserId()
conf := this.moduleLinestory.configure.getChapterCfgById(req.ChapterId)
if conf == nil {
log.Errorf("config is nil uid:%v groupId:%v", uid, req.ChapterId)
code = pb.ErrorCode_ConfigNoFound
return
}
if err := this.moduleLinestory.modelLinestory.receive(uid, req.ChapterId); err != nil {
var customErr = new(comm.CustomError)
if errors.As(err, &customErr) {
code = customErr.Code
} else {
code = pb.ErrorCode_DBError
}
this.moduleLinestory.Error("章节奖励领取失败", log.Fields{"uid": uid, "chapterId": req.ChapterId, "code": code})
return
}
//发奖
if code := this.moduleLinestory.DispenseRes(session, conf.Reward, true); code != pb.ErrorCode_Success {
this.moduleLinestory.Errorf("奖励发放失败 uid:%v groupId:%v reward:%v", uid, req.ChapterId, conf.Reward)
}
rsp := &pb.LinestoryReceiveResp{
ChapterId: req.ChapterId,
}
if err := session.SendMsg(string(this.moduleLinestory.GetType()), LinestorySubTypeReceive, rsp); err != nil {
code = pb.ErrorCode_SystemError
}
return
}