201 lines
5.1 KiB
Go
201 lines
5.1 KiB
Go
package gameinvite
|
|
|
|
import (
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/configure"
|
|
"time"
|
|
)
|
|
|
|
//接受切磋
|
|
|
|
func (this *apiComp) AcceptCheck(session comm.IUserSession, req *pb.GameInviteAcceptReq) (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.GameInviteAcceptReq) (errdata *pb.ErrorData) {
|
|
var (
|
|
err error
|
|
user *pb.DBUser
|
|
redRecord *pb.GameInviteQiecuoRecord
|
|
blueRecord *pb.GameInviteQiecuoRecord
|
|
sessions []comm.IUserSession
|
|
gamedata *pb.GameInviteData
|
|
timeout int32 = 10
|
|
rules string
|
|
roomid string
|
|
ok bool
|
|
keep bool
|
|
)
|
|
|
|
if errdata = this.AcceptCheck(session, req); errdata != nil {
|
|
return
|
|
}
|
|
|
|
//校验切磋请求是否超时
|
|
if redRecord, err = this.module.model.queryQiecuo(req.Uid); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
|
|
if blueRecord, err = this.module.model.queryQiecuo(session.GetUserId()); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
keep = false
|
|
|
|
if gamedata, ok = redRecord.Invite[req.Gtype]; !ok {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: fmt.Sprintf("no found gameInvite:%d", req.Gtype),
|
|
}
|
|
return
|
|
}
|
|
|
|
switch req.Gtype {
|
|
case 0, 1, 2, 3, 4:
|
|
timeout = 10
|
|
case 5:
|
|
timeout = 60
|
|
}
|
|
|
|
rules = gamedata.Rules
|
|
for _, v := range gamedata.Targets {
|
|
if v.Uid == session.GetUserId() {
|
|
if configure.Now().Sub(time.Unix(v.Timestamp, 0)).Seconds() > float64(timeout) {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_PracticeInviteTimeOut,
|
|
Title: pb.ErrorCode_PracticeInviteTimeOut.ToString(),
|
|
}
|
|
return
|
|
}
|
|
keep = true
|
|
}
|
|
}
|
|
if !keep {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
|
Message: "no fund Invite Data",
|
|
}
|
|
return
|
|
}
|
|
|
|
if redsession, ok := this.module.GetUserSession(req.Uid); !ok {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_BattleUserOff,
|
|
Title: pb.ErrorCode_BattleUserOff.ToString(),
|
|
Message: "req.uid is Off!",
|
|
}
|
|
return
|
|
} else {
|
|
sessions = append(sessions, redsession)
|
|
}
|
|
sessions = append(sessions, session.Clone())
|
|
if user, err = this.module.GetUserForSession(session); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
this.module.SendMsgToUser(string(this.module.GetType()), "qiecuonotify",
|
|
&pb.GameInviteQiecuonotifyPush{User: comm.GetUserBaseInfo(user), NotifyType: 2, Gtype: req.Gtype}, req.Uid)
|
|
|
|
switch req.Gtype {
|
|
case 1:
|
|
if roomid, err = this.module.pvp.CreateRoom(sessions, rules); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
break
|
|
case 2:
|
|
if roomid, err = this.module.caninerabbit.CreateRoom(sessions, rules); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
break
|
|
case 3:
|
|
if roomid, err = this.module.dcolor.CreateRoom(sessions, rules); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
break
|
|
case 4:
|
|
if roomid, err = this.module.catchBugs.CreateRoom(sessions, rules); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
break
|
|
case 5: // 三消类型
|
|
if roomid, err = this.module.entertain.CreateRoom(sessions, req.Rules); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
break
|
|
}
|
|
redRecord.Status = 1
|
|
redRecord.Roomid = roomid
|
|
redRecord.Spath = fmt.Sprintf("%s/%s", this.module.service.GetType(), this.module.service.GetId())
|
|
gamedata.Member = []string{session.GetUserId(), req.Uid}
|
|
blueRecord.Status = 1
|
|
blueRecord.Roomid = roomid
|
|
blueRecord.Spath = fmt.Sprintf("%s/%s", this.module.service.GetType(), this.module.service.GetId())
|
|
blueRecord.Invite[redRecord.Gtype] = gamedata
|
|
this.module.model.Change(session.GetUserId(), map[string]interface{}{
|
|
"status": redRecord.Status,
|
|
"gtype": req.Gtype,
|
|
"invite": blueRecord.Invite,
|
|
"roomid": blueRecord.Roomid,
|
|
"spath": blueRecord.Spath,
|
|
})
|
|
this.module.model.Change(req.Uid, map[string]interface{}{
|
|
"status": blueRecord.Status,
|
|
"gtype": req.Gtype,
|
|
"invite": redRecord.Invite,
|
|
"roomid": redRecord.Roomid,
|
|
"spath": redRecord.Spath,
|
|
})
|
|
session.SendMsg(string(this.module.GetType()), "accept", &pb.GameInviteAcceptResp{
|
|
IsSucc: true,
|
|
})
|
|
return
|
|
}
|