go_dreamfactory/modules/practice/api_accept.go
2023-03-02 18:12:55 +08:00

81 lines
2.3 KiB
Go

package practice
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
"google.golang.org/protobuf/proto"
)
//接受切磋
func (this *apiComp) AcceptCheck(session comm.IUserSession, req *pb.PracticeAcceptReq) (code pb.ErrorCode) {
if req.Uid == "" {
code = pb.ErrorCode_ReqParameterError
}
return
}
func (this *apiComp) Accept(session comm.IUserSession, req *pb.PracticeAcceptReq) (code pb.ErrorCode, data proto.Message) {
if code = this.AcceptCheck(session, req); code != pb.ErrorCode_Success {
return
}
//校验切磋请求是否超时
if qr := this.module.modelQiecuo.getQiecuo(req.Uid); qr != nil {
now := configure.Now().Unix()
if now-qr.Timestamp > 10 { //大于10s 切磋超时
code = pb.ErrorCode_FriendQiecuoTimeout
this.module.Debug("切磋接受超时", log.Field{Key: "uid", Value: session.GetUserId()})
return
}
} else {
code = pb.ErrorCode_FriendQiecuoNoRequest
return
}
if imodule, err := this.module.service.GetModule(comm.ModulePvp); err == nil {
if ipvp, ok := imodule.(comm.IPvp); ok {
//发起者 red
red := this.module.ModuleUser.GetUser(req.Uid)
if red == nil {
code = pb.ErrorCode_UserNofound
this.module.Error("未找到红方信息", log.Field{Key: "uid", Value: req.Uid})
return
}
blue := this.module.ModuleUser.GetUser(session.GetUserId())
if blue == nil {
code = pb.ErrorCode_UserNofound
this.module.Error("未找到蓝方信息", log.Field{Key: "uid", Value: session.GetUserId()})
return
}
matchId, c := ipvp.CreatePvp(
&pb.PvpUserInfo{Uid: red.Uid, Name: red.Name, Avatar: red.Avatar, Lv: red.Lv},
&pb.PvpUserInfo{Uid: blue.Uid, Name: blue.Name, Avatar: blue.Avatar, Lv: blue.Lv},
pb.PvpType_friends,
)
if c != pb.ErrorCode_Success {
this.module.Debug("createPvp code:", log.Field{Key: "code", Value: c})
return
}
//更新状态
this.module.modelQiecuo.updateQiecuoRecord(req.Uid, session.GetUserId(), matchId)
}
}
if err := session.SendMsg(string(this.module.GetType()), "accept", &pb.FriendAcceptResp{
IsSucc: true,
}); err != nil {
code = pb.ErrorCode_SystemError
return
}
this.module.SendMsgToUser(string(this.module.GetType()), "qiecuonotify",
&pb.FriendQiecuonotifyPush{Uid: session.GetUserId(), NotifyType: 2}, req.Uid)
return
}