go_dreamfactory/modules/friend/api_cross_accept.go
2023-06-06 11:08:14 +08:00

84 lines
2.4 KiB
Go

package friend
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
)
//接受切磋
func (this *apiComp) AcceptCheck(session comm.IUserSession, req *pb.FriendAcceptReq) (errdata *pb.ErrorData) {
if req.Uid == "" {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
}
return
}
func (this *apiComp) Accept(session comm.IUserSession, req *pb.FriendAcceptReq) (errdata *pb.ErrorData) {
if errdata = this.AcceptCheck(session, req); errdata != nil {
return
}
//校验切磋请求是否超时
if qr := this.module.ModelFriendQiecuo.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.ModelFriendQiecuo.updateQiecuoRecord(req.Uid, session.GetUserId(), matchId)
}
}
resp := &pb.FriendAcceptResp{
IsSucc: true,
}
if err := session.SendMsg(string(this.module.GetType()), FriendSubTypeAccept, resp); 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
}