feat(logic): 实现地图刷怪功能并优化数据库查询

- 在玩家结构中添加 StopChan 通道,用于停止刷怪协程
- 优化 MapEnter 和 MapLeave 函数,支持刷怪功能
- 新增 spawnMonsters 函数实现具体刷怪逻辑
- 优化多个模块的数据库查询语句,提高查询效率
- 调整 PlayerService 中的 Reg 函数,优化数据插入操作
This commit is contained in:
2025-08-23 17:44:12 +08:00
parent bc4bd7eba6
commit b6164f3b9e
11 changed files with 131 additions and 26 deletions

View File

@@ -7,6 +7,7 @@ import (
"blazing/logic/service/maphot"
"blazing/logic/service/maps"
"blazing/logic/service/space"
mservice "blazing/modules/blazing/service"
"time"
)
@@ -19,24 +20,31 @@ func (h *Controller) MapEnter(data *maps.InInfo, c *entity.Player) (result *maps
tt.Nick = c.Nick
tt.Pos = data.Point
data.Broadcast(c.MapId, *tt) //同步广播
go func() { //测试刷怪
// 如果是无怪地图,直接返回
if mservice.NewMonsterService().GetId(c.MapId) == 0 {
return nil, -1
}
// 创建新的停止通道
c.StopChan = make(chan struct{})
// 启动刷怪协程
go func(stopChan chan struct{}, currentMap int) {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
tt := handler.NewTomeeHeader()
tt.CMD = 2004
tt.Result = 0
tt.UserID = c.UserID
t1 := maps.OgreInfo{}
for i := 0; i < 9; i++ {
t1.Data[i] = 1
select {
case <-stopChan:
// 收到停止信号,退出协程
return
case <-ticker.C:
// 刷新当前地图的怪物
spawnMonsters(currentMap, c)
}
c.SendPack(tt.Pack(&t1))
<-time.After(10000 * time.Millisecond)
}
}()
}(c.StopChan, int(c.MapId))
return nil, -1
}
func (h Controller) MapHot(data *maphot.InInfo, c *entity.Player) (result *maphot.OutInfo, err errorcode.ErrorCode) {
@@ -52,6 +60,12 @@ func (h *Controller) MapLeave(data *maps.LeaveMapInboundInfo, c *entity.Player)
//result = &maps.LeaveMapOutboundInfo{UserID: c.GetUserID()}
data.Broadcast(c.MapId, maps.LeaveMapOutboundInfo{UserID: c.GetUserID()}) //同步广播
space.GetSpace(c.MapId).Delete(c.UserID)
// 如果有正在运行的刷怪协程,发送停止信号
if c.StopChan != nil {
close(c.StopChan)
c.StopChan = nil
}
c.MapId = 0 // 重置当前地图
return nil, -1
}
func (h *Controller) MapList(data *maps.ListMapPlayerInboundInfo, c *entity.Player) (result *maps.ListMapPlayerOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
@@ -66,3 +80,27 @@ func (h *Controller) MapList(data *maps.ListMapPlayerInboundInfo, c *entity.Play
result.Player = append(result.Player, *result1)
return
}
// 刷怪具体实现
func spawnMonsters(mapID int, c *entity.Player) {
// 获取当前地图的怪物配置
if mservice.NewMonsterService().GetId(c.MapId) == 0 {
return
}
// 创建数据包
tt := handler.NewTomeeHeader()
tt.CMD = 2004
tt.Result = 0
tt.UserID = c.UserID
// 设置怪物信息
t1 := maps.OgreInfo{}
//copy(t1.Data[:], monsterConfig) // 使用地图对应的怪物配置
for i := 0; i < 9; i++ {
t1.Data[i] = mservice.NewMonsterService().GetId(c.MapId)
}
// 发送数据包
c.SendPack(tt.Pack(&t1))
}