房间回收 离线处理

This commit is contained in:
meixiongfeng 2023-11-07 15:11:36 +08:00
parent ebee3aaa0c
commit dcd4818cb1
2 changed files with 82 additions and 110 deletions

View File

@ -2,6 +2,7 @@ package entertainment
import (
"context"
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/event"
@ -124,32 +125,33 @@ func (this *Entertainment) ConsumXxlCard(session comm.IUserSession, card string)
// 用户离线处理
func (this *Entertainment) EventUserOffline(uid, sessionid string) {
this.Debugf("user offline: %s", uid)
// if list, err := this.model.getEntertainmList(uid); err == nil {
// var (
// serverPath string = fmt.Sprintf("%s/%s", this.service.GetType(), this.service.GetId())
// )
// if serverPath != list.ServicePath { // RPC 通知房间所在的服务器
// _, err = this.service.RpcGo(
// context.Background(),
// list.ServicePath,
// string(comm.RPC_XXLOffLine),
// &pb.RPCGeneralReqA2{
// Param1: list.Roomid,
// Param2: list.Uid,
// },
// nil)
// if err != nil {
// this.Errorln(err)
// return
// }
// } else {
// this.useroffline(context.Background(), &pb.RPCGeneralReqA2{Param1: list.Roomid, Param2: list.Uid}, nil)
// }
// return
// }
if list, err := this.model.getEntertainmList(uid); err == nil {
if list.Roomid == "" { // 不在房间不处理
return
}
serverPath := fmt.Sprintf("%s/%s", this.service.GetType(), this.service.GetId())
if serverPath != list.ServicePath { // RPC 通知房间所在的服务器
_, err = this.service.RpcGo(
context.Background(),
list.ServicePath,
string(comm.RPC_XXLOffLine),
&pb.RPCGeneralReqA2{
Param1: list.Roomid,
Param2: list.Uid,
},
nil)
if err != nil {
this.Errorln(err)
return
}
} else {
this.useroffline(context.Background(), &pb.RPCGeneralReqA2{Param1: list.Roomid, Param2: list.Uid}, nil)
}
return
}
}
func (this *Entertainment) useroffline(ctx context.Context, req *pb.RPCGeneralReqA2, resp *pb.RPCGeneralReqA2) (err error) {
this.Debugf("user offline :%s,%s", req.Param1, req.Param2)
return
}

View File

@ -21,75 +21,29 @@ const (
//游戏房间
type Room struct {
Id string // 房间id
szSession []comm.IUserSession
player1 *pb.PlayerData // 玩家1
player2 *pb.PlayerData // 玩家2
chessboard *MapData
module *Entertainment
round int32 // 轮数
operatetimer *timewheel.Task //操作倒计时定时器
curPower string // 当前操作的玩家
NexPower string // 下一个操作的玩家
MaxRound int32
rd1 bool // 玩家1 是否准备
rd2 bool // 玩家2 是否准备
Status int32 //房间游戏状态 0未开始 1 已开始 2 已结束
RoomType int32 // 房间类型 1 是好友创房
MaxTime int32 // 操作时间
Playtype int32 //当前房间玩法
Id string // 房间id
szSession []comm.IUserSession
player1 *pb.PlayerData // 玩家1
player2 *pb.PlayerData // 玩家2
chessboard *MapData
module *Entertainment
round int32 // 轮数
closeRoomTimer *timewheel.Task //房间解散倒计时定时器
curPower string // 当前操作的玩家
NexPower string // 下一个操作的玩家
MaxRound int32
rd1 bool // 玩家1 是否准备
rd2 bool // 玩家2 是否准备
Status int32 //房间游戏状态 0未开始 1 已开始 2 已结束
RoomType int32 // 房间类型 1 是好友创房
MaxTime int32 // 操作时间
Playtype int32 //当前房间玩法
}
func (this *Room) operateTimeOut(task *timewheel.Task, args ...interface{}) {
fmt.Printf("超时+++++%d\n", time.Now().Unix())
if this.operatetimer != nil {
this.operatetimer = nil
}
if this.player1.Userinfo.Uid == this.curPower { // 给玩家2
this.NexPower = this.player2.Userinfo.Uid
this.player2.Ps = MaxPs // 恢复体力
this.player1.Ps = 0
} else { // 权限给1号玩家
this.NexPower = this.player1.Userinfo.Uid
this.player1.Ps = MaxPs // 恢复体力
this.player2.Ps = 0
this.round++
}
var szMap []*pb.MapData
szMap = append(szMap, &pb.MapData{
Data: this.chessboard.GetPalatData(),
})
fmt.Printf("===超时棋盘=============\n")
//this.chessboard.Debugf()
if err := this.module.SendMsgSyncToSession(string(this.module.GetType()), "operatorrst", &pb.EntertainOperatorRstPush{
//Mpadata: szMap,
Power: this.NexPower,
Curpower: this.curPower,
Score: 0,
Round: this.round,
User1: this.player1,
User2: this.player2,
Itype: 9999,
Curid: 0,
Targetid: 0,
}, this.szSession...); err != nil {
this.module.Errorln(err)
}
// 变更权限
this.curPower = this.NexPower
if this.round > this.MaxRound { // 游戏结束
if this.player1.Score == this.player2.Score {
this.MaxRound += 1 // 增加一回合
} else {
this.GameOver()
}
return
}
// 二号玩家是机器人
if len(this.szSession) == 1 {
this.AiOperator()
}
fmt.Printf("解散房间超时+++++%d\n", time.Now().Unix()) //倒计时结束还没结束基本是游戏异常 直接清理房间
this.ModifyUserRoomInfoData(false)
event.TriggerEvent(comm.EventCloseRoom, this.Id) // 释放房间
}
// 随机一个玩法
@ -442,13 +396,6 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr
}
return
}
if this.curPower != this.NexPower { // 下一次操作不是自己 清除定时器
if this.operatetimer != nil {
timewheel.Remove(this.operatetimer)
this.operatetimer = nil
}
}
// 变更权限
this.curPower = this.NexPower
@ -487,7 +434,6 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr
} else {
this.MaxTime = this.module.ModuleTools.GetGlobalConf().ConsumePvpTime
}
this.MaxTime = 30 // 测试用 后面走配置
this.Status = 1
this.round = 1
@ -503,8 +449,9 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr
}, this.szSession...); err != nil {
this.module.Errorln(err)
}
// 游戏开始开启一个定时器
//this.operatetimer = timewheel.Add(time.Second*time.Duration(this.MaxTime), this.operateTimeOut)
this.ModifyUserRoomInfoData(true)
// 游戏开始开启一个定时器 1小时如果还不结束 自动清理
this.closeRoomTimer = timewheel.Add(time.Hour, this.operateTimeOut)
}
case "reconnect": // 重连
@ -544,11 +491,9 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr
}, this.szSession...); err != nil {
this.module.Errorln(err)
}
this.ModifyUserRoomInfoData(false)
case "operatorover": // 操作完成 开启一个定时器
if this.operatetimer != nil {
timewheel.Remove(this.operatetimer)
this.operatetimer = nil
}
if this.player1.Userinfo.Uid == session.GetUserId() {
this.rd1 = true
} else if this.player2.Userinfo.Uid == session.GetUserId() {
@ -578,7 +523,6 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr
}
//this.operatetimer = timewheel.Add(time.Second*time.Duration(this.MaxTime), this.operateTimeOut)
}
return
}
@ -593,10 +537,7 @@ func (this *Room) GameOver() (errdata *pb.ErrorData) {
lostPlayer *pb.PlayerData // 输的玩家
box *pb.BoxData // 是否可以获得宝箱奖励
)
if this.operatetimer != nil {
timewheel.Remove(this.operatetimer)
this.operatetimer = nil
}
winner = this.player1
bReward = true
if this.player1.Score < this.player2.Score {
@ -673,8 +614,12 @@ func (this *Room) GameOver() (errdata *pb.ErrorData) {
Reward: atno,
Box: box,
}, this.szSession...)
this.ModifyUserRoomInfoData(false)
event.TriggerEvent(comm.EventCloseRoom, this.Id)
if this.closeRoomTimer != nil { // 游戏结束 清理定时器
timewheel.Remove(this.closeRoomTimer)
this.closeRoomTimer = nil
}
return
}
@ -722,3 +667,28 @@ func (this *Room) JoinRoom(module *Entertainment, p *pb.PlayerData) (room *Room,
}
return
}
// 修改玩家的房间信息
func (this *Room) ModifyUserRoomInfoData(bStart bool) {
var (
roomid string
path string
)
if bStart {
roomid = this.Id
path = fmt.Sprintf("%s/%s", this.module.service.GetType(), this.module.service.GetId())
}
// 修改房间记录信息
this.module.model.modifyEntertainmList(this.player1.Userinfo.Uid, map[string]interface{}{
"roomid": roomid,
"servicePath": path,
})
if this.player2.Userinfo.Uid != "999" {
this.module.model.modifyEntertainmList(this.player2.Userinfo.Uid, map[string]interface{}{
"roomid": roomid,
"servicePath": path,
})
}
return
}