AI计时器整理
This commit is contained in:
parent
1ea7fb31bb
commit
86111c04ea
@ -20,7 +20,7 @@ func (this *apiComp) MatchCheck(session comm.IUserSession, req *pb.EntertainMatc
|
|||||||
func (this *apiComp) Match(session comm.IUserSession, req *pb.EntertainMatchReq) (errdata *pb.ErrorData) {
|
func (this *apiComp) Match(session comm.IUserSession, req *pb.EntertainMatchReq) (errdata *pb.ErrorData) {
|
||||||
// 随便找个在线的人
|
// 随便找个在线的人
|
||||||
var (
|
var (
|
||||||
bMatch bool
|
//bMatch bool
|
||||||
s2 comm.IUserSession
|
s2 comm.IUserSession
|
||||||
p1 *pb.PlayerData // 玩家1
|
p1 *pb.PlayerData // 玩家1
|
||||||
p2 *pb.PlayerData // 玩家2
|
p2 *pb.PlayerData // 玩家2
|
||||||
@ -57,7 +57,7 @@ func (this *apiComp) Match(session comm.IUserSession, req *pb.EntertainMatchReq)
|
|||||||
// }
|
// }
|
||||||
roomid = this.module.gameMgr.CreateRoom(session, s2, p1, p2)
|
roomid = this.module.gameMgr.CreateRoom(session, s2, p1, p2)
|
||||||
session.SendMsg(string(this.module.GetType()), "match", &pb.EntertainMatchResp{
|
session.SendMsg(string(this.module.GetType()), "match", &pb.EntertainMatchResp{
|
||||||
Maych: bMatch,
|
Maych: true,
|
||||||
Player: p2,
|
Player: p2,
|
||||||
Roomid: roomid,
|
Roomid: roomid,
|
||||||
})
|
})
|
||||||
|
@ -30,7 +30,7 @@ func (this *gameMgrComp) Init(service core.IService, module core.IModule, comp c
|
|||||||
|
|
||||||
func (this *gameMgrComp) CreateRoom(s1 comm.IUserSession, s2 comm.IUserSession, p1 *pb.PlayerData, p2 *pb.PlayerData) (roomid string) {
|
func (this *gameMgrComp) CreateRoom(s1 comm.IUserSession, s2 comm.IUserSession, p1 *pb.PlayerData, p2 *pb.PlayerData) (roomid string) {
|
||||||
room := new(Room) //初始化房间
|
room := new(Room) //初始化房间
|
||||||
room.InitRoom(this.module, s1, s2, p1, p2)
|
room = room.InitRoom(this.module, s1, s2, p1, p2)
|
||||||
|
|
||||||
this.lock.Lock()
|
this.lock.Lock()
|
||||||
this.rooms[room.Id] = room
|
this.rooms[room.Id] = room
|
||||||
|
@ -100,6 +100,12 @@ func (this *Room) AiTimeOut(task *timewheel.Task, args ...interface{}) {
|
|||||||
szMap = append(szMap, m...)
|
szMap = append(szMap, m...)
|
||||||
}
|
}
|
||||||
this.player2.Score += curScore
|
this.player2.Score += curScore
|
||||||
|
|
||||||
|
// 清理旧的计时器 开启一个新的
|
||||||
|
if this.operatetimer != nil {
|
||||||
|
timewheel.Remove(this.operatetimer)
|
||||||
|
}
|
||||||
|
this.operatetimer = timewheel.Add(time.Second*8, this.operateTimeOut) // 开启新的定时器
|
||||||
// 广播消息
|
// 广播消息
|
||||||
if err := this.SendMsgToSession(string(this.module.GetType()), "operatorrst", &pb.EntertainOperatorRstPush{
|
if err := this.SendMsgToSession(string(this.module.GetType()), "operatorrst", &pb.EntertainOperatorRstPush{
|
||||||
Mpadata: szMap,
|
Mpadata: szMap,
|
||||||
@ -125,33 +131,8 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr
|
|||||||
if session.GetUserId() != this.power { // 校验是不是你的权限
|
if session.GetUserId() != this.power { // 校验是不是你的权限
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if this.operatetimer != nil {
|
||||||
if this.power == this.player1.Uid { //权限校验
|
timewheel.Remove(this.operatetimer)
|
||||||
this.player1.Ps--
|
|
||||||
if this.player1.Ps <= 0 { // 权限给下一个人
|
|
||||||
this.power = this.player2.Uid
|
|
||||||
|
|
||||||
if len(this.szSession) == 1 { // 校验2号玩家是不是AI
|
|
||||||
// 起一个定时器
|
|
||||||
if this.aiTimer != nil {
|
|
||||||
timewheel.Remove(this.aiTimer)
|
|
||||||
}
|
|
||||||
this.operatetimer = timewheel.Add(time.Second*3, this.AiTimeOut)
|
|
||||||
}
|
|
||||||
this.round++
|
|
||||||
}
|
|
||||||
this.player2.Ps = MaxPs
|
|
||||||
|
|
||||||
} else if this.power == this.player2.Uid {
|
|
||||||
|
|
||||||
this.player2.Ps--
|
|
||||||
if this.player2.Ps <= 0 { // 权限给下一个人
|
|
||||||
this.power = this.player1.Uid
|
|
||||||
this.round++
|
|
||||||
}
|
|
||||||
this.player1.Ps = MaxPs
|
|
||||||
} else { // err 未知权限
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
// 交换元素
|
// 交换元素
|
||||||
this.chessboard.SwapGirde(req.Curid, req.Targetid) // 交换格子
|
this.chessboard.SwapGirde(req.Curid, req.Targetid) // 交换格子
|
||||||
@ -162,7 +143,7 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr
|
|||||||
curScore += score
|
curScore += score
|
||||||
szMap = append(szMap, m...)
|
szMap = append(szMap, m...)
|
||||||
}
|
}
|
||||||
this.player2.Score += curScore
|
//this.player2.Score += curScore
|
||||||
// 开启新的定时器
|
// 开启新的定时器
|
||||||
if this.operatetimer != nil {
|
if this.operatetimer != nil {
|
||||||
timewheel.Remove(this.operatetimer)
|
timewheel.Remove(this.operatetimer)
|
||||||
@ -172,7 +153,33 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr
|
|||||||
session.SendMsg(string(this.module.GetType()), "operator", &pb.EntertainOperatorResp{
|
session.SendMsg(string(this.module.GetType()), "operator", &pb.EntertainOperatorResp{
|
||||||
Success: true,
|
Success: true,
|
||||||
})
|
})
|
||||||
|
if this.power == this.player1.Uid { //权限校验
|
||||||
|
this.player1.Score += curScore
|
||||||
|
this.player1.Ps--
|
||||||
|
if this.player1.Ps <= 0 { // 权限给下一个人
|
||||||
|
this.power = this.player2.Uid
|
||||||
|
|
||||||
|
if len(this.szSession) == 1 { // 校验2号玩家是不是AI
|
||||||
|
// 起一个定时器
|
||||||
|
if this.aiTimer != nil {
|
||||||
|
timewheel.Remove(this.aiTimer)
|
||||||
|
}
|
||||||
|
this.aiTimer = timewheel.Add(time.Second*3, this.AiTimeOut)
|
||||||
|
}
|
||||||
|
this.round++
|
||||||
|
}
|
||||||
|
this.player2.Ps = MaxPs
|
||||||
|
} else if this.power == this.player2.Uid {
|
||||||
|
this.player2.Score += curScore
|
||||||
|
this.player2.Ps--
|
||||||
|
if this.player2.Ps <= 0 { // 权限给下一个人
|
||||||
|
this.power = this.player1.Uid
|
||||||
|
this.round++
|
||||||
|
}
|
||||||
|
this.player1.Ps = MaxPs
|
||||||
|
} else { // err 未知权限
|
||||||
|
return
|
||||||
|
}
|
||||||
// 广播消息
|
// 广播消息
|
||||||
if err := this.SendMsgToSession(string(this.module.GetType()), "operatorrst", &pb.EntertainOperatorRstPush{
|
if err := this.SendMsgToSession(string(this.module.GetType()), "operatorrst", &pb.EntertainOperatorRstPush{
|
||||||
Mpadata: szMap,
|
Mpadata: szMap,
|
||||||
@ -218,7 +225,12 @@ func (this *Room) StartGame() (errdata *pb.ErrorData) {
|
|||||||
|
|
||||||
// 游戏结束
|
// 游戏结束
|
||||||
func (this *Room) GameOver() (errdata *pb.ErrorData) {
|
func (this *Room) GameOver() (errdata *pb.ErrorData) {
|
||||||
|
if this.aiTimer != nil {
|
||||||
|
timewheel.Remove(this.aiTimer)
|
||||||
|
}
|
||||||
|
if this.operatetimer != nil {
|
||||||
|
timewheel.Remove(this.operatetimer)
|
||||||
|
}
|
||||||
this.SendMsgToSession(string(this.module.GetType()), "gameover", &pb.EntertainGameOverPush{
|
this.SendMsgToSession(string(this.module.GetType()), "gameover", &pb.EntertainGameOverPush{
|
||||||
User1: this.player1,
|
User1: this.player1,
|
||||||
User2: this.player2,
|
User2: this.player2,
|
||||||
|
Loading…
Reference in New Issue
Block a user