136 lines
3.3 KiB
Go
136 lines
3.3 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
|
|
redRecord *pb.GameInviteQiecuoRecord
|
|
blueRecord *pb.GameInviteQiecuoRecord
|
|
sessions []comm.IUserSession
|
|
gamedata *pb.GameInviteData
|
|
rules string
|
|
roomid string
|
|
ok bool
|
|
keep bool
|
|
)
|
|
|
|
if errdata = this.AcceptCheck(session, req); errdata != nil {
|
|
return
|
|
}
|
|
sessions = append(sessions, session.Clone())
|
|
//校验切磋请求是否超时
|
|
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
|
|
}
|
|
rules = gamedata.Rules
|
|
for _, v := range gamedata.Targets {
|
|
if v.Uid == session.GetUserId() {
|
|
if configure.Now().Sub(time.Unix(v.Timestamp, 0)).Seconds() > 10 {
|
|
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(),
|
|
}
|
|
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)
|
|
}
|
|
switch req.Gtype {
|
|
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
|
|
}
|
|
redRecord.Status = 1
|
|
redRecord.Battid = roomid
|
|
gamedata.Member = []string{session.GetUserId(), req.Uid}
|
|
blueRecord.Status = 1
|
|
blueRecord.Battid = roomid
|
|
blueRecord.Invite[redRecord.Gtype] = &pb.GameInviteData{
|
|
Member: []string{session.GetUserId(), req.Uid},
|
|
Rules: gamedata.Rules,
|
|
}
|
|
this.module.model.Change(session.GetUserId(), map[string]interface{}{
|
|
"status": 1,
|
|
"invite": blueRecord.Invite,
|
|
"battid": blueRecord.Battid,
|
|
})
|
|
this.module.model.Change(req.Uid, map[string]interface{}{
|
|
"status": 1,
|
|
"invite": redRecord.Invite,
|
|
"battid": redRecord.Battid,
|
|
})
|
|
|
|
session.SendMsg(string(this.module.GetType()), "accept", &pb.GameInviteAcceptResp{
|
|
IsSucc: true,
|
|
})
|
|
return
|
|
}
|