上传pvp 倒计时实现

This commit is contained in:
liwei1dao 2023-02-13 16:06:43 +08:00
parent 6a968e9c4d
commit 3a13fb79be
5 changed files with 128 additions and 31 deletions

View File

@ -225,9 +225,11 @@ func (this *Battle) CreateBattleServer(req *pb.BattleInfo) (code pb.ErrorCode) {
this.Debug("CreateBattleServer", log.Field{Key: "req", Value: req}) this.Debug("CreateBattleServer", log.Field{Key: "req", Value: req})
var ( var (
resp *pb.BattleCreateServerResp resp *pb.BattleCreateServerResp
ctx context.Context
err error err error
) )
if resp, err = this.clientmgr.CreateBattle(context.Background(), &pb.BattleCreateServerReq{Info: req}); err != nil { ctx, _ = context.WithTimeout(context.Background(), time.Second*5)
if resp, err = this.clientmgr.CreateBattle(ctx, &pb.BattleCreateServerReq{Info: req}); err != nil {
this.Errorln(err) this.Errorln(err)
code = pb.ErrorCode_RpcFuncExecutionError code = pb.ErrorCode_RpcFuncExecutionError
} }
@ -240,9 +242,11 @@ func (this *Battle) CreateBattleServer(req *pb.BattleInfo) (code pb.ErrorCode) {
func (this *Battle) InCmdBattle(req *pb.BattleInCmdReq) (code pb.ErrorCode) { func (this *Battle) InCmdBattle(req *pb.BattleInCmdReq) (code pb.ErrorCode) {
var ( var (
resp *pb.BattleInCmdResp resp *pb.BattleInCmdResp
ctx context.Context
err error err error
) )
if resp, err = this.clientmgr.InCmdBattle(context.Background(), req); err != nil { ctx, _ = context.WithTimeout(context.Background(), time.Second*5)
if resp, err = this.clientmgr.InCmdBattle(ctx, req); err != nil {
this.Errorln(err) this.Errorln(err)
code = pb.ErrorCode_RpcFuncExecutionError code = pb.ErrorCode_RpcFuncExecutionError
} }
@ -272,6 +276,7 @@ func (this *Battle) ConcedeBattle(req *pb.BattleConcedeReq) (code pb.ErrorCode)
func (this *Battle) CheckBattleReport(session comm.IUserSession, report *pb.BattleReport) (code pb.ErrorCode, iswin bool) { func (this *Battle) CheckBattleReport(session comm.IUserSession, report *pb.BattleReport) (code pb.ErrorCode, iswin bool) {
var ( var (
reply *pb.BattleCheckResults reply *pb.BattleCheckResults
ctx context.Context
err error err error
) )
if report == nil { if report == nil {
@ -281,7 +286,8 @@ func (this *Battle) CheckBattleReport(session comm.IUserSession, report *pb.Batt
if this.options.OpenCheck { if this.options.OpenCheck {
stime := time.Now() stime := time.Now()
if reply, err = this.clientmgr.CheckBattle(context.Background(), report); err != nil || !reply.Ischeck { ctx, _ = context.WithTimeout(context.Background(), time.Second*5)
if reply, err = this.clientmgr.CheckBattle(ctx, report); err != nil || !reply.Ischeck {
code = pb.ErrorCode_BattleValidationFailed code = pb.ErrorCode_BattleValidationFailed
this.Error("[Battle Check]", this.Error("[Battle Check]",
log.Field{Key: "t", Value: time.Since(stime).Milliseconds()}, log.Field{Key: "t", Value: time.Since(stime).Milliseconds()},

View File

@ -2,6 +2,7 @@ package pvp
import ( import (
"go_dreamfactory/comm" "go_dreamfactory/comm"
"go_dreamfactory/lego/sys/timewheel"
"go_dreamfactory/pb" "go_dreamfactory/pb"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
@ -33,6 +34,7 @@ func (this *apiComp) Formation(session comm.IUserSession, req *pb.PvpFormationRe
} }
if battle.Redformation != nil && battle.Blueformation != nil { //都设置了战斗阵型 if battle.Redformation != nil && battle.Blueformation != nil { //都设置了战斗阵型
battle.State = pb.PvpState_battle battle.State = pb.PvpState_battle
timewheel.Remove(battle.readytimer)
go this.module.startBattle(battle) go this.module.startBattle(battle)
} }
battle.lock.Unlock() battle.lock.Unlock()

View File

@ -2,6 +2,7 @@ package pvp
import ( import (
"go_dreamfactory/comm" "go_dreamfactory/comm"
"go_dreamfactory/lego/sys/timewheel"
"go_dreamfactory/pb" "go_dreamfactory/pb"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
@ -37,6 +38,8 @@ func (this *apiComp) InCmd(session comm.IUserSession, req *pb.PvpInCmdReq) (code
In: req.Cmd, In: req.Cmd,
}); code != pb.ErrorCode_Success { }); code != pb.ErrorCode_Success {
return return
} else {
timewheel.Remove(battle.operatetimer)
} }
} else { } else {
code = pb.ErrorCode_BattleInCmdFailed code = pb.ErrorCode_BattleInCmdFailed

View File

@ -22,6 +22,6 @@ type BattleItem struct {
BlueOffline bool //蓝方离线 BlueOffline bool //蓝方离线
readytimer *timewheel.Task //准备倒计时定时器 readytimer *timewheel.Task //准备倒计时定时器
operatetimer *timewheel.Task //操作倒计时定时器 operatetimer *timewheel.Task //操作倒计时定时器
curroperate int32 //当前操作玩家 1 红方 2 蓝方 curroperate *pb.ComWaitInputSkill //当前操作玩家 1 红方 2 蓝方
lock sync.Mutex //战斗锁 防止计时器和消息同时操作对象 lock sync.Mutex //战斗锁 防止计时器和消息同时操作对象
} }

View File

@ -14,6 +14,7 @@ import (
"time" "time"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"google.golang.org/protobuf/proto"
) )
/* /*
@ -88,6 +89,7 @@ func (this *Pvp) CreatePvp(red, blue *pb.PvpUserInfo, ptype pb.PvpType) (battleI
Red: red, Red: red,
Blue: blue, Blue: blue,
readytimer: timewheel.Add(time.Second*60, this.readyTimeOut, battleId), readytimer: timewheel.Add(time.Second*60, this.readyTimeOut, battleId),
curroperate: &pb.ComWaitInputSkill{},
} }
if battle.RedSession, ok = this.GetUserSession(red.Uid); !ok { if battle.RedSession, ok = this.GetUserSession(red.Uid); !ok {
@ -120,6 +122,8 @@ func (this *Pvp) PvpOutCmdPush(out *pb.PvpOutCmdPush) {
var ( var (
battle *BattleItem battle *BattleItem
ok bool ok bool
data []byte
code pb.ErrorCode
err error err error
) )
this.lock.RLock() this.lock.RLock()
@ -132,6 +136,45 @@ func (this *Pvp) PvpOutCmdPush(out *pb.PvpOutCmdPush) {
}, battle.RedSession, battle.BlueSession); err != nil { }, battle.RedSession, battle.BlueSession); err != nil {
this.Errorln(err) this.Errorln(err)
} }
for _, v := range out.Cmd {
if v.Cmdtype == "ComWaitInputSkill" {
if err = proto.Unmarshal(v.Value, battle.curroperate); err != nil {
this.Errorln(err)
}
if battle.curroperate.Side == 1 && battle.RedOffline { //已经离线
battle.curroperate.Auto = true
data, _ = proto.Marshal(battle.curroperate)
if code = this.battle.InCmdBattle(&pb.BattleInCmdReq{
Battleid: battle.Id,
Side: battle.curroperate.Side,
In: &pb.BattleCmd{
Cmdtype: "ComWaitInputSkill",
Value: data,
},
}); code != pb.ErrorCode_Success {
return
}
return
}
if battle.curroperate.Side == 2 && battle.BlueOffline { //已经离线
battle.curroperate.Auto = true
data, _ = proto.Marshal(battle.curroperate)
if code = this.battle.InCmdBattle(&pb.BattleInCmdReq{
Battleid: battle.Id,
Side: battle.curroperate.Side,
In: &pb.BattleCmd{
Cmdtype: "ComWaitInputSkill",
Value: data,
},
}); code != pb.ErrorCode_Success {
return
}
return
}
battle.operatetimer = timewheel.Add(time.Second*60, this.operateTimeOut, battle.Id)
}
}
} }
} }
@ -158,7 +201,7 @@ func (this *Pvp) PvpFinishPush(battleId string) {
//准备超时 取消战斗 //准备超时 取消战斗
func (this *Pvp) readyTimeOut(task *timewheel.Task, args ...interface{}) { func (this *Pvp) readyTimeOut(task *timewheel.Task, args ...interface{}) {
this.Debug("PVPTimeOut", log.Field{Key: "args", Value: args}) this.Debug("readyTimeOut", log.Field{Key: "args", Value: args})
var ( var (
id string id string
battle *BattleItem battle *BattleItem
@ -186,6 +229,37 @@ func (this *Pvp) readyTimeOut(task *timewheel.Task, args ...interface{}) {
} }
} }
//操作倒计时
func (this *Pvp) operateTimeOut(task *timewheel.Task, args ...interface{}) {
this.Debug("operateTimeOut", log.Field{Key: "args", Value: args})
var (
id string
battle *BattleItem
ok bool
code pb.ErrorCode
data []byte
)
id = args[0].(string)
this.lock.RLock()
battle, ok = this.battles[id]
this.lock.RUnlock()
if ok && battle.State == pb.PvpState_battle {
battle.curroperate.Auto = true
data, _ = proto.Marshal(battle.curroperate)
if code = this.battle.InCmdBattle(&pb.BattleInCmdReq{
Battleid: battle.Id,
Side: battle.curroperate.Side,
In: &pb.BattleCmd{
Cmdtype: "ComWaitInputSkill",
Value: data,
},
}); code != pb.ErrorCode_Success {
return
}
}
}
//开始战斗 //开始战斗
func (this *Pvp) startBattle(battle *BattleItem) { func (this *Pvp) startBattle(battle *BattleItem) {
this.Debug("PVPStart", log.Field{Key: "battleId", Value: battle.Id}) this.Debug("PVPStart", log.Field{Key: "battleId", Value: battle.Id})
@ -248,23 +322,35 @@ func (this *Pvp) startBattle(battle *BattleItem) {
func (this *Pvp) useroffline(session comm.IUserSession) { func (this *Pvp) useroffline(session comm.IUserSession) {
var ( var (
side int32 side int32
data []byte
code pb.ErrorCode
) )
this.lock.RLock() this.lock.RLock()
for _, v := range this.battles { for _, v := range this.battles {
if v.Red.Uid == session.GetUserId() || v.Blue.Uid == session.GetUserId() { if v.Red.Uid == session.GetUserId() || v.Blue.Uid == session.GetUserId() {
if session.GetUserId() == v.Red.Uid { if session.GetUserId() == v.Red.Uid {
side = 1 side = 1
v.RedOffline = true
} else { } else {
side = 2 side = 2
v.BlueOffline = true
} }
v.lock.Lock()
if v.State == pb.PvpState_battle { if v.curroperate.Side == side {
go this.battle.ConcedeBattle(&pb.BattleConcedeReq{ timewheel.Remove(v.operatetimer)
v.curroperate.Auto = true
data, _ = proto.Marshal(v.curroperate)
if code = this.battle.InCmdBattle(&pb.BattleInCmdReq{
Battleid: v.Id, Battleid: v.Id,
Side: side, Side: v.curroperate.Side,
}) In: &pb.BattleCmd{
Cmdtype: "ComWaitInputSkill",
Value: data,
},
}); code != pb.ErrorCode_Success {
return
}
} }
v.lock.Unlock()
} }
} }
this.lock.RUnlock() this.lock.RUnlock()