From 86111c04ea70c36410d350f14df509a28df1d455 Mon Sep 17 00:00:00 2001 From: meixiongfeng <766881921@qq.com> Date: Fri, 20 Oct 2023 11:37:08 +0800 Subject: [PATCH] =?UTF-8?q?AI=E8=AE=A1=E6=97=B6=E5=99=A8=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/entertainment/api_match.go | 4 +- modules/entertainment/gamemgr.go | 2 +- modules/entertainment/room.go | 70 +++++++++++++++++------------- 3 files changed, 44 insertions(+), 32 deletions(-) diff --git a/modules/entertainment/api_match.go b/modules/entertainment/api_match.go index cd6a9b567..e33bfc875 100644 --- a/modules/entertainment/api_match.go +++ b/modules/entertainment/api_match.go @@ -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) { // 随便找个在线的人 var ( - bMatch bool + //bMatch bool s2 comm.IUserSession p1 *pb.PlayerData // 玩家1 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) session.SendMsg(string(this.module.GetType()), "match", &pb.EntertainMatchResp{ - Maych: bMatch, + Maych: true, Player: p2, Roomid: roomid, }) diff --git a/modules/entertainment/gamemgr.go b/modules/entertainment/gamemgr.go index 87a26a83f..2548324ec 100644 --- a/modules/entertainment/gamemgr.go +++ b/modules/entertainment/gamemgr.go @@ -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) { room := new(Room) //初始化房间 - room.InitRoom(this.module, s1, s2, p1, p2) + room = room.InitRoom(this.module, s1, s2, p1, p2) this.lock.Lock() this.rooms[room.Id] = room diff --git a/modules/entertainment/room.go b/modules/entertainment/room.go index f7f26f8fd..eaa7570da 100644 --- a/modules/entertainment/room.go +++ b/modules/entertainment/room.go @@ -100,6 +100,12 @@ func (this *Room) AiTimeOut(task *timewheel.Task, args ...interface{}) { szMap = append(szMap, m...) } 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{ Mpadata: szMap, @@ -125,33 +131,8 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr if session.GetUserId() != this.power { // 校验是不是你的权限 return } - - if this.power == this.player1.Uid { //权限校验 - 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 + if this.operatetimer != nil { + timewheel.Remove(this.operatetimer) } // 交换元素 this.chessboard.SwapGirde(req.Curid, req.Targetid) // 交换格子 @@ -162,7 +143,7 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr curScore += score szMap = append(szMap, m...) } - this.player2.Score += curScore + //this.player2.Score += curScore // 开启新的定时器 if this.operatetimer != nil { 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{ 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{ Mpadata: szMap, @@ -218,7 +225,12 @@ func (this *Room) StartGame() (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{ User1: this.player1, User2: this.player2,