完善对战

This commit is contained in:
meixiongfeng 2023-10-25 11:10:00 +08:00
parent ad64ecc289
commit a0c6b5485e
4 changed files with 134 additions and 82 deletions

View File

@ -28,10 +28,11 @@ type Room struct {
module *Entertainment
round int32 // 轮数
operatetimer *timewheel.Task //操作倒计时定时器
//aiTimer *timewheel.Task //AI操作随机做个延时
curPower string // 当前操作的玩家
NexPower string // 下一个操作的玩家
MaxRound int32
curPower string // 当前操作的玩家
NexPower string // 下一个操作的玩家
MaxRound int32
rd1 bool
rd2 bool
}
func (this *Room) operateTimeOut(task *timewheel.Task, args ...interface{}) {
@ -111,9 +112,9 @@ func (this *Room) AiOperator() {
var (
curScore int32
szMap []*pb.MapData
oid1 int32
oid2 int32
bAddPs bool
oid1 int32
oid2 int32
)
this.module.Debugf("AI 操作\n")
@ -124,16 +125,22 @@ func (this *Room) AiOperator() {
this.player1.Ps = MaxPs
// 交换元素
curScore, szMap, oid1, oid2 = this.chessboard.AiSwapGirde()
curScore, szMap, oid1, oid2, bAddPs = this.chessboard.AiSwapGirde()
if this.NexPower != this.curPower {
this.round++
}
for _, v := range szMap { //
if v.Xgrid > 0 {
this.player2.Energy += v.Xgrid
if v.CurEnergy > 0 {
this.player2.Energy += v.CurEnergy
}
}
if bAddPs {
this.player2.Ps++
if this.player2.Ps > MaxPs {
this.player2.Ps = MaxPs
}
}
this.player2.Score += curScore
// 广播消息
if err := this.module.SendMsgSyncToSession(string(this.module.GetType()), "operatorrst", &pb.EntertainOperatorRstPush{
@ -169,6 +176,7 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr
oid1 int32 // 唯一id
oid2 int32
color int32 // 校验消除的颜色
bAddPs bool
)
var szMap []*pb.MapData
req := msg.(*pb.EntertainOperatorReq)
@ -209,6 +217,36 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr
}
this.player1.Score += curScore
} else {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_EntertainNoEnergy,
Title: pb.ErrorCode_EntertainNoEnergy.ToString(),
}
return
}
this.NexPower = this.curPower
} else {
conf, err := this.module.configure.GetGameConsumeHero(this.player2.Cardid)
if err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_EntertainNoHeroSkill,
Title: pb.ErrorCode_EntertainNoHeroSkill.ToString(),
}
return
}
if this.player2.Energy >= conf.Skillload {
this.player2.Energy = 0 // 清零
if score, m := this.chessboard.SkillUp(conf.Skilleffect, conf.Skillvalue); score > 0 {
curScore += score
szMap = append(szMap, m...)
} else {
szMap = append(szMap, &pb.MapData{
Data: this.chessboard.GetPalatData(),
})
}
this.player2.Score += curScore
} else {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_EntertainNoEnergy,
@ -245,9 +283,10 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr
return
}
if score, m := this.chessboard.CheckMap(color); score > 0 {
if score, m, b := this.chessboard.CheckMap(color); score > 0 {
curScore += score
szMap = append(szMap, m...)
bAddPs = b
} else { // 不能消除
this.chessboard.SwapGirde(req.Targetid, req.Curid) // 换到原来的位置
errdata = &pb.ErrorData{
@ -260,30 +299,30 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr
if this.curPower == this.player1.Uid { //权限校验
this.player1.Score += curScore
this.player1.Ps--
// if curScore >= 4 {
// this.player1.Ps++
// if this.player1.Ps > MaxPs {
// this.player1.Ps = MaxPs
// }
// }
if bAddPs {
this.player1.Ps++
if this.player1.Ps > MaxPs {
this.player1.Ps = MaxPs
}
}
this.player2.Ps = MaxPs
} else { // this.curPower == this.player2.Uid
this.player2.Score += curScore
this.player2.Ps--
// if curScore >= 4 {
// this.player2.Ps++
// if this.player2.Ps > MaxPs {
// this.player2.Ps = MaxPs
// }
// }
if bAddPs {
this.player2.Ps++
if this.player2.Ps > MaxPs {
this.player2.Ps = MaxPs
}
}
this.player1.Ps = MaxPs
}
for _, v := range szMap { //
if v.Xgrid > 0 {
if v.CurEnergy > 0 {
if color == 1 {
this.player1.Energy += v.Xgrid
this.player1.Energy += v.CurEnergy
} else {
this.player2.Energy += v.Xgrid
this.player2.Energy += v.CurEnergy
}
}
}
@ -304,15 +343,6 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr
}
}
// for _, v := range szMap {
// fmt.Printf("======szMap=======\n")
// for index := Width - 1; index >= 0; index-- {
// for j := 0; j < Height; j++ {
// fmt.Printf("%d:%d ", v.Data[index+j*Height].Oid, v.Data[index+j*Height].Color)
// }
// fmt.Printf("\n")
// }
// }
// 广播消息
if err := this.module.SendMsgSyncToSession(string(this.module.GetType()), "operatorrst", &pb.EntertainOperatorRstPush{
Mpadata: szMap,
@ -349,12 +379,30 @@ func (this *Room) ReceiveMessage(session comm.IUserSession, stype string, msg pr
Mpadata: &pb.MapData{Data: this.chessboard.Plat},
Power: this.NexPower,
Round: this.round,
Roomid: "",
Roomid: this.Id, // 房间ID
}, this.szSession...); err != nil {
this.Errorln(err)
}
this.operatetimer = timewheel.Add(time.Second*MaxTime, this.operateTimeOut)
} else {
if this.player1.Uid == session.GetUserId() {
this.rd1 = true
} else if this.player2.Uid == session.GetUserId() {
this.rd2 = true
}
if this.rd1 && this.rd2 { // 两个玩家都准备好了 那么就开始游戏
if err := this.module.SendMsgSyncToSession(string(this.module.GetType()), "startgame", &pb.EntertainStartGamePush{
User1: this.player1,
User2: this.player2,
Mpadata: &pb.MapData{Data: this.chessboard.Plat},
Power: this.NexPower,
Round: this.round,
Roomid: this.Id,
}, this.szSession...); err != nil {
this.Errorln(err)
}
this.operatetimer = timewheel.Add(time.Second*MaxTime, this.operateTimeOut)
}
}
}

View File

@ -79,8 +79,7 @@ func (this *MapData) SwapGirde(oldId, newId int32) (bSwap bool) {
this.operElem = append(this.operElem, newId)
this.operElem = append(this.operElem, oldId)
}
this.Debugf()
//this.CheckMap(color)
//this.Debugf()
return
}
@ -89,10 +88,10 @@ func (this *MapData) SetMap() {
1, 1, 2, 5, 1, 5, 2,
3, 2, 3, 1, 2, 4, 4,
2, 1, 1, 3, 6, 4, 1,
1, 3, 1, 4, 3, 6, 3,
1, 3, 3, 5, 1, 6, 1,
5, 1, 2, 5, 1, 3, 1,
1, 1, 5, 1, 2, 1, 4,
4, 1, 1, 4, 3, 6, 3,
4, 3, 4, 5, 1, 6, 1,
5, 4, 2, 5, 1, 3, 1,
4, 1, 5, 1, 2, 1, 4,
}
var pos int
for index := Width - 1; index >= 0; index-- {
@ -440,33 +439,36 @@ func (this *MapData) Check3X(color int32) (bEliminate bool, score int32, count i
}
// 校验地图可消除的 判断各组上面2个和右边两个是否三个相等
func (this *MapData) CheckMap(color int32) (score int32, szMap []*pb.MapData) {
// xc 判断用来是否加体力
func (this *MapData) CheckMap(color int32) (score int32, szMap []*pb.MapData, xc bool) {
var curScore int32
for {
curScore = 0
var count int32
var energy int32
if bRet, s, c := this.Check5X(color); bRet {
fmt.Printf("=====检测消除5x===========\n")
curScore += s
count += c
energy += c
xc = true // 只要有 4x 5x 就标记ture
}
if bRet, s, c := this.Check4X(color); bRet {
fmt.Printf("=====检测消除4x===========\n")
curScore += s
count += c
energy += c
xc = true
}
if bRet, s, c := this.Check3X(color); bRet {
fmt.Printf("=====检测消除3x===========\n")
curScore += s
count += c
energy += c
}
score += curScore // 总分
if this.DropGirde() {
szMap = append(szMap, &pb.MapData{
Data: this.GetPalatData(),
CurSocre: curScore,
Xgrid: count,
Data: this.GetPalatData(),
CurSocre: curScore,
CurEnergy: energy,
})
}
@ -522,7 +524,7 @@ func (this *MapData) GetPalatData() (data []*pb.GirdeData) {
}
// ai操作
func (this *MapData) AiSwapGirde() (score int32, szMap []*pb.MapData, oid1 int32, oid2 int32) {
func (this *MapData) AiSwapGirde() (score int32, szMap []*pb.MapData, oid1 int32, oid2 int32, bAddPs bool) {
for pos := 0; pos < Total; pos++ {
y := pos % Height
@ -530,7 +532,7 @@ func (this *MapData) AiSwapGirde() (score int32, szMap []*pb.MapData, oid1 int32
if b := this.SwapGirde(int32(pos), int32(pos+1)); b {
oid1 = this.Plat[pos+1].Oid
oid2 = this.Plat[pos].Oid
if s, m := this.CheckMap(2); s == 0 {
if s, m, b := this.CheckMap(2); s == 0 {
this.SwapGirde(int32(pos+1), int32(pos))
this.operElem = []int32{}
oid1 = 0
@ -538,7 +540,7 @@ func (this *MapData) AiSwapGirde() (score int32, szMap []*pb.MapData, oid1 int32
} else {
score += s
szMap = append(szMap, m...)
bAddPs = b
break
}
}
@ -547,7 +549,7 @@ func (this *MapData) AiSwapGirde() (score int32, szMap []*pb.MapData, oid1 int32
if b := this.SwapGirde(int32(pos), int32(pos+Width)); b {
oid1 = this.Plat[pos+Width].Oid
oid2 = this.Plat[pos].Oid
if s, m := this.CheckMap(2); s == 0 {
if s, m, b := this.CheckMap(2); s == 0 {
this.SwapGirde(int32(pos+Width), int32(pos))
this.operElem = []int32{}
oid1 = 0
@ -555,6 +557,7 @@ func (this *MapData) AiSwapGirde() (score int32, szMap []*pb.MapData, oid1 int32
} else {
szMap = append(szMap, m...)
score += s
bAddPs = b
break
}
}
@ -571,7 +574,7 @@ func (this *MapData) SkillUp(skillid int32, value int32) (score int32, szMap []*
this.Plat[id] = &pb.GirdeData{}
}
if this.DropGirde() {
score, szMap = this.CheckMap(1)
score, szMap, _ = this.CheckMap(1)
}
}

View File

@ -96,7 +96,7 @@ func Test_Main(t *testing.T) {
m.DropGirde()
if score, m := m.CheckMap(1); score > 0 {
if score, m, _ := m.CheckMap(1); score > 0 {
szMap = append(szMap, m...)
}

View File

@ -26,9 +26,9 @@ type MapData struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Data []*GirdeData `protobuf:"bytes,1,rep,name=data,proto3" json:"data"` // 地图数据
CurSocre int32 `protobuf:"varint,2,opt,name=curSocre,proto3" json:"curSocre"` // 本轮得分
Xgrid int32 `protobuf:"varint,3,opt,name=xgrid,proto3" json:"xgrid"` // 本次掉落消除的格子(客户端不用)
Data []*GirdeData `protobuf:"bytes,1,rep,name=data,proto3" json:"data"` // 地图数据
CurSocre int32 `protobuf:"varint,2,opt,name=curSocre,proto3" json:"curSocre"` // 本次地图更新得分
CurEnergy int32 `protobuf:"varint,3,opt,name=curEnergy,proto3" json:"curEnergy"` // 本次掉落获得的能量
}
func (x *MapData) Reset() {
@ -77,9 +77,9 @@ func (x *MapData) GetCurSocre() int32 {
return 0
}
func (x *MapData) GetXgrid() int32 {
func (x *MapData) GetCurEnergy() int32 {
if x != nil {
return x.Xgrid
return x.CurEnergy
}
return 0
}
@ -255,30 +255,31 @@ var File_entertain_entertain_db_proto protoreflect.FileDescriptor
var file_entertain_entertain_db_proto_rawDesc = []byte{
0x0a, 0x1c, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x74, 0x61, 0x69, 0x6e, 0x2f, 0x65, 0x6e, 0x74, 0x65,
0x72, 0x74, 0x61, 0x69, 0x6e, 0x5f, 0x64, 0x62, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x5b,
0x72, 0x74, 0x61, 0x69, 0x6e, 0x5f, 0x64, 0x62, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x63,
0x0a, 0x07, 0x4d, 0x61, 0x70, 0x44, 0x61, 0x74, 0x61, 0x12, 0x1e, 0x0a, 0x04, 0x64, 0x61, 0x74,
0x61, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0a, 0x2e, 0x47, 0x69, 0x72, 0x64, 0x65, 0x44,
0x61, 0x74, 0x61, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x75, 0x72,
0x53, 0x6f, 0x63, 0x72, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x63, 0x75, 0x72,
0x53, 0x6f, 0x63, 0x72, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x78, 0x67, 0x72, 0x69, 0x64, 0x18, 0x03,
0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x78, 0x67, 0x72, 0x69, 0x64, 0x22, 0x75, 0x0a, 0x09, 0x47,
0x69, 0x72, 0x64, 0x65, 0x44, 0x61, 0x74, 0x61, 0x12, 0x10, 0x0a, 0x03, 0x6f, 0x69, 0x64, 0x18,
0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x6f, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x63, 0x6f,
0x6c, 0x6f, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x63, 0x6f, 0x6c, 0x6f, 0x72,
0x12, 0x10, 0x0a, 0x03, 0x63, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x63,
0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x63, 0x6f, 0x72, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28,
0x05, 0x52, 0x05, 0x73, 0x63, 0x6f, 0x72, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x70, 0x65, 0x63,
0x69, 0x61, 0x6c, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x73, 0x70, 0x65, 0x63, 0x69,
0x61, 0x6c, 0x22, 0x88, 0x01, 0x0a, 0x0a, 0x50, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x44, 0x61, 0x74,
0x61, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03,
0x75, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28,
0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x63, 0x6f, 0x72, 0x65,
0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x73, 0x63, 0x6f, 0x72, 0x65, 0x12, 0x0e, 0x0a,
0x02, 0x70, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02, 0x70, 0x73, 0x12, 0x16, 0x0a,
0x06, 0x63, 0x61, 0x72, 0x64, 0x69, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63,
0x61, 0x72, 0x64, 0x69, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x65, 0x6e, 0x65, 0x72, 0x67, 0x79, 0x18,
0x06, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x65, 0x6e, 0x65, 0x72, 0x67, 0x79, 0x42, 0x06, 0x5a,
0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x53, 0x6f, 0x63, 0x72, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x63, 0x75, 0x72, 0x45, 0x6e, 0x65, 0x72,
0x67, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x09, 0x63, 0x75, 0x72, 0x45, 0x6e, 0x65,
0x72, 0x67, 0x79, 0x22, 0x75, 0x0a, 0x09, 0x47, 0x69, 0x72, 0x64, 0x65, 0x44, 0x61, 0x74, 0x61,
0x12, 0x10, 0x0a, 0x03, 0x6f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x6f,
0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28,
0x05, 0x52, 0x05, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x12, 0x10, 0x0a, 0x03, 0x63, 0x69, 0x64, 0x18,
0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x63, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x63,
0x6f, 0x72, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x73, 0x63, 0x6f, 0x72, 0x65,
0x12, 0x18, 0x0a, 0x07, 0x73, 0x70, 0x65, 0x63, 0x69, 0x61, 0x6c, 0x18, 0x05, 0x20, 0x01, 0x28,
0x05, 0x52, 0x07, 0x73, 0x70, 0x65, 0x63, 0x69, 0x61, 0x6c, 0x22, 0x88, 0x01, 0x0a, 0x0a, 0x50,
0x6c, 0x61, 0x79, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64,
0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e,
0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12,
0x14, 0x0a, 0x05, 0x73, 0x63, 0x6f, 0x72, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05,
0x73, 0x63, 0x6f, 0x72, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x70, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28,
0x05, 0x52, 0x02, 0x70, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x63, 0x61, 0x72, 0x64, 0x69, 0x64, 0x18,
0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x61, 0x72, 0x64, 0x69, 0x64, 0x12, 0x16, 0x0a,
0x06, 0x65, 0x6e, 0x65, 0x72, 0x67, 0x79, 0x18, 0x06, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x65,
0x6e, 0x65, 0x72, 0x67, 0x79, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (