This commit is contained in:
liwei1dao 2023-11-07 20:29:47 +08:00
commit b931ab9da5
3 changed files with 260 additions and 208 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,40 @@ 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)
// 房间不在直接返回
if _, err = this.gameMgr.GetRoomInfo(req.Param1); err != nil {
return
}
this.gameMgr.RoomDistribute(req.Param1, nil, "offline", req)
return
}

View File

@ -21,75 +21,28 @@ 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 是好友创房 0对战 2 AI 对战
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()
}
func (this *Room) CloseRoomTimeOut(task *timewheel.Task, args ...interface{}) {
fmt.Printf("解散房间超时+++++%d\n", time.Now().Unix()) //倒计时结束还没结束基本是游戏异常 直接清理房间
this.ModifyUserRoomInfoData(false)
}
// 随机一个玩法
@ -124,6 +77,8 @@ func (this *Room) InitRoom(module *Entertainment, p1 *pb.PlayerData, p2 *pb.Play
} else {
this.szSession = append(this.szSession, s2.Clone())
}
} else {
this.RoomType = 2
}
this.MaxRound = module.ModuleTools.GetGlobalConf().ConsumeRounds
room = &Room{
@ -137,6 +92,7 @@ func (this *Room) InitRoom(module *Entertainment, p1 *pb.PlayerData, p2 *pb.Play
MaxRound: this.MaxRound,
Status: 0,
Playtype: this.Playtype,
RoomType: this.RoomType,
}
if err := this.module.SendMsgSyncToSession(string(this.module.GetType()), "enterroom", &pb.EntertainEnterRoomPush{
@ -339,22 +295,28 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr
this.player2.Ps = 0
}
} else {
bCanChange := false
oid1 = this.chessboard.Plat[req.Curid].Oid
oid2 = this.chessboard.Plat[req.Targetid].Oid
// 交换元素
if b := this.chessboard.SwapGirde(req.Curid, req.Targetid); !b { // 交换格子
errdata = &pb.ErrorData{
Code: pb.ErrorCode_EntertainCantSwap, // 不能交换 直接返回
Title: pb.ErrorCode_EntertainCantSwap.ToString(),
if b, m := this.chessboard.CheckSpecialElemChange(req.Curid, req.Targetid, color); !b {
// 交换元素
if b = this.chessboard.SwapGirde(req.Curid, req.Targetid); !b { // 交换格子
errdata = &pb.ErrorData{
Code: pb.ErrorCode_EntertainCantSwap, // 不能交换 直接返回
Title: pb.ErrorCode_EntertainCantSwap.ToString(),
}
return
}
return
} else {
bCanChange = true
szMap = append(szMap, m...)
}
if m, b := this.chessboard.CheckMap(color, true); len(m) > 0 {
// curScore += score
szMap = append(szMap, m...)
bAddPs = b
} else { // 不能消除
bCanChange = true
}
if !bCanChange { // 不能消除
this.chessboard.SwapGirde(req.Targetid, req.Curid) // 换到原来的位置
errdata = &pb.ErrorData{
Code: pb.ErrorCode_EntertainCantSwap, // 不能交换 直接返回
@ -442,13 +404,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 +442,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 +457,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.CloseRoomTimeOut)
}
case "reconnect": // 重连
@ -544,11 +499,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() {
@ -577,8 +530,15 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr
}
}
//this.operatetimer = timewheel.Add(time.Second*time.Duration(this.MaxTime), this.operateTimeOut)
}
case "offline":
req := msg.(*pb.RPCGeneralReqA2)
fmt.Printf("useroffline: %v\n", req)
if this.RoomType == 2 { // AI 对战直接结束
this.ModifyUserRoomInfoData(false)
return
}
}
return
}
@ -593,10 +553,6 @@ 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 {
@ -650,7 +606,7 @@ func (this *Room) GameOver() (errdata *pb.ErrorData) {
Opentime: int64(c.Cd) + configure.Now().Unix(),
}
list.Box = append(list.Box, box)
// 写库
this.module.model.modifyEntertainmList(winner.Userinfo.Uid, map[string]interface{}{
"box": list.Box,
})
@ -659,6 +615,7 @@ func (this *Room) GameOver() (errdata *pb.ErrorData) {
}
}
// 修改房间状态
this.Status = 2
this.module.SendMsgSyncToSession(string(this.module.GetType()), "gameover", &pb.EntertainGameOverPush{
@ -673,8 +630,11 @@ func (this *Room) GameOver() (errdata *pb.ErrorData) {
Reward: atno,
Box: box,
}, this.szSession...)
event.TriggerEvent(comm.EventCloseRoom, this.Id)
this.ModifyUserRoomInfoData(false)
if this.closeRoomTimer != nil { // 游戏结束 清理定时器
timewheel.Remove(this.closeRoomTimer)
this.closeRoomTimer = nil
}
return
}
@ -722,3 +682,32 @@ 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())
} else {
event.TriggerEvent(comm.EventCloseRoom, this.Id) // 释放房间
}
// 修改房间记录信息
if this.player1 != nil && this.player1.Userinfo != nil {
this.module.model.modifyEntertainmList(this.player1.Userinfo.Uid, map[string]interface{}{
"roomid": roomid,
"servicePath": path,
})
}
if this.RoomType != 2 && this.player2 != nil && this.player2.Userinfo != nil {
this.module.model.modifyEntertainmList(this.player2.Userinfo.Uid, map[string]interface{}{
"roomid": roomid,
"servicePath": path,
})
}
return
}

View File

@ -51,73 +51,32 @@ func (this *MapData) GetRandType() int32 {
}
// 玩法带入
func (this *MapData) CreateGride(index int32, bDrop bool) (girde *pb.GirdeData) {
func (this *MapData) CreateGride(count int) (girdes []*pb.GirdeData) {
var (
id int32
conf *cfg.GameBlockData
err error
) // 权重带入
this.oid++
girde = &pb.GirdeData{ // 默认值
Oid: this.oid,
Color: 1,
Cid: 1,
Score: 1,
Special: 0,
}
if this.iType == 4 && bDrop {
var count int32
for _, v := range this.Plat {
if v.Special == 5 || v.Special == 6 ||
v.Special == 7 || v.Special == 8 {
count++
if count >= 2 {
break
}
}
}
if count < 2 {
n, _ := rand.Int(rand.Reader, big.NewInt(6)) // 随机一个颜色
n1, _ := rand.Int(rand.Reader, big.NewInt(4)) // 随机一个消除类型
if conf, err := this.module.configure.GetGameBlock(int32(n.Int64()+1), int32(n1.Int64()+5)); err == nil {
id = conf.Key
}
} else {
id = this.GetRandType()
for i := 0; i < count; i++ {
this.oid++
girde := &pb.GirdeData{ // 默认值
Oid: this.oid,
Color: 1,
Cid: 1,
Score: 1,
Special: 0,
}
} else if this.iType == 3 && bDrop { // 创建棋盘的时候此玩法类型执行效率低下 稍后采用同颜色替换模式处理
// 最多2个
var count int32
for _, v := range this.Plat {
if v.Special == 4 {
count++
if count >= 2 {
break
}
}
}
if count < 2 {
n, _ := rand.Int(rand.Reader, big.NewInt(6)) // 随机一个颜色
if conf, err := this.module.configure.GetGameBlock(int32(n.Int64()+1), 4); err == nil {
id = conf.Key
}
} else {
id = this.GetRandType()
}
} else {
id = this.GetRandType()
conf, err = this.module.configure.GetGameBlockByKey(id)
if err == nil {
girde.Color = conf.Color
girde.Cid = conf.Key
girde.Special = conf.Type
girde.Score = conf.Score
girdes = append(girdes, girde)
}
}
conf, err = this.module.configure.GetGameBlockByKey(id)
if err != nil {
return
}
girde.Color = conf.Color
girde.Cid = conf.Key
girde.Special = conf.Type
girde.Score = conf.Score
return
}
@ -127,8 +86,8 @@ func (this *MapData) InitMap(module *Entertainment, iType int32) {
this.iType = iType
this.oid = 1000 // 方便观察 从1000开始
this.Plat = make([]*pb.GirdeData, Width*Height)
for i := 0; i < Width*Height; i++ {
this.Plat[i] = this.CreateGride(int32(i), false)
for i, v := range this.CreateGride(Total) {
this.Plat[i] = v
}
if iType > 1 {
var sp int32
@ -158,7 +117,61 @@ func (this *MapData) InitMap(module *Entertainment, iType int32) {
this.SetIndelibilityPlat()
this.Plat = this.GetPalatData()
//this.SetMap() // 方便测试固定地图
// this.SetMap() // 方便测试固定地图
}
// 交换之前先判断是不是特殊元素的交换
func (this *MapData) CheckSpecialElemChange(oldId, newId int32, color int32) (bSwap bool, szMap []*pb.MapData) {
this.operElem = []int32{} // 初始化
if (oldId%Height+1 < Total && oldId+1 == newId) ||
(oldId%Height > 0 && oldId-1 == newId) ||
(oldId+Width < Total && oldId+Width == newId) ||
(oldId-Width >= 0 && oldId-Width == newId) {
bSwap = true
x := make(map[int]struct{})
if this.Plat[newId].Special != 0 && this.Plat[oldId].Special != 0 { // 都不为0
tmp := new(pb.GirdeData)
*tmp = *this.Plat[newId]
this.Plat[newId] = this.Plat[oldId]
this.Plat[oldId] = tmp
this.operElem = append(this.operElem, newId)
this.operElem = append(this.operElem, oldId)
x = this.SpecialElem(int(oldId), this.Plat[oldId].Special)
xc := this.SpecialElem(int(newId), this.Plat[newId].Special)
for k := range xc {
x[k] = struct{}{}
}
var energy int32
var curScore int32
for id := range x {
if this.Plat[id].Color == color {
energy++
}
curScore += this.Plat[id].Score
this.Plat[id] = &pb.GirdeData{}
this.operElem = append(this.operElem, int32(id))
}
for id := range x {
if this.Plat[id].Color == color {
energy++
}
curScore += this.Plat[id].Score
this.Plat[id] = &pb.GirdeData{}
this.operElem = append(this.operElem, int32(id))
}
if this.DropGirde() {
szMap = append(szMap, &pb.MapData{
Data: this.GetPalatData(),
CurSocre: curScore,
CurEnergy: energy,
})
}
} else {
bSwap = false
}
}
return
}
// 交换2个元素(参数 oid )
@ -198,13 +211,7 @@ func (this *MapData) Check5X() (bEliminate bool, xiaochu []int, s map[int]int) {
//var xiaochu []int // 即将消除的key
s = make(map[int]int)
for k, v := range this.Plat {
// b := false
// for _, e := range this.operElem {
// if e == int32(k) {
// b = true
// break
// }
// }
if v.Cid == 0 {
continue
}
@ -256,15 +263,8 @@ func (this *MapData) Check4X() (bEliminate bool, xiaochu []int, s map[int]int) {
)
s = make(map[int]int)
//var xiaochu []int // 即将消除的key
for k, v := range this.Plat {
// b := false
// for _, e := range this.operElem {
// if e == int32(k) {
// b = true
// break
// }
// }
if v.Cid == 0 {
continue
}
@ -478,8 +478,6 @@ func (this *MapData) CheckMap(color int32, bSkill bool) (szMap []*pb.MapData, xc
CurSocre: curScore,
CurEnergy: energy,
})
//this.Debugf()
}
// 检查掉落
@ -493,6 +491,8 @@ func (this *MapData) CheckMap(color int32, bSkill bool) (szMap []*pb.MapData, xc
// 下落 生成新的格子 (返回掉落所获得的分数)
func (this *MapData) DropGirde() bool {
// 需要填充的格子
var fill []int
bDrop := false
for i := 0; i < Width; i++ {
for j := 0; j < Height; j++ {
@ -510,12 +510,66 @@ func (this *MapData) DropGirde() bool {
}
for m := j + add; m < Height; m++ {
k1 := i*Width + m
this.Plat[k1] = this.CreateGride(int32(k1), true)
fill = append(fill, k1)
//this.Plat[k1] = this.CreateGride(true)
}
break
}
}
}
sz := this.CreateGride(len(fill))
for pos, id := range fill {
this.Plat[id] = sz[pos]
}
if this.iType > 1 {
var sp int32
var mp map[int]struct{}
mp = make(map[int]struct{}, 0)
var count int32
for pos, v := range this.Plat {
if this.iType == 4 {
if v.Special == 5 || v.Special == 6 ||
v.Special == 7 || v.Special == 8 {
mp[pos] = struct{}{}
count++
if count >= 2 {
break
}
}
} else if this.iType == 3 {
if v.Special == 4 {
mp[pos] = struct{}{}
count++
if count >= 2 {
break
}
}
}
}
if len(mp) < 2 {
for {
n1, _ := rand.Int(rand.Reader, big.NewInt(int64(len(fill))))
mp[fill[n1.Int64()]] = struct{}{}
if len(mp) >= 2 {
break
}
}
if this.iType == 3 {
sp = 4
} else if this.iType == 4 {
n1, _ := rand.Int(rand.Reader, big.NewInt(4)) // 随机一个消除类型
sp = int32(n1.Int64()) + 5
}
for key := range mp {
if conf, err := this.module.configure.GetGameBlock(this.Plat[key].Color, sp); err == nil {
this.Plat[key].Cid = conf.Key
this.Plat[key].Special = conf.Type
this.Plat[key].Score = conf.Score
}
}
}
}
return bDrop
}
@ -687,12 +741,12 @@ func (this *MapData) SkillUp(pos int32, color int32, skillid int32, value int32,
func (this *MapData) SetMap() {
sz2 := []int32{
3, 3, 2, 2, 1, 3, 1,
5, 1, 2, 3, 1, 2, 2,
3, 4, 3, 5, 6, 1, 6,
1, 4, 1, 4, 2, 3, 6,
1, 5, 1, 5, 6, 1, 4,
6, 6, 4, 6, 4, 3, 3,
3, 3, 1, 2, 1, 2, 5,
5, 1, 3, 3, 1, 2, 2,
3, 2, 1, 4, 3, 1, 6,
1, 4, 3, 4, 2, 3, 6,
1, 5, 6, 5, 6, 1, 4,
6, 6, 3, 6, 3, 1, 3,
3, 3, 1, 3, 3, 2, 5,
// 1, 4, 2, 5, 4, 5, 2,
// 3, 2, 3, 5, 2, 1, 4,
// 2, 5, 5, 4, 5, 4, 1,
@ -1125,8 +1179,8 @@ func (this *MapData) SpecialElem(id int, s int32) (x map[int]struct{}) {
// 获取配置
if c, _ := this.module.configure.GetGameBlock(this.Plat[id].Color, FiveType); c != nil {
ids := utils.RandomNumbers(0, Total-1, int(c.Value))
for key := range ids {
x[key] = struct{}{}
for _, v := range ids {
x[v] = struct{}{}
}
}
} else if s == BoomType { // 炸弹类型 带走周围一圈+ 上下左右