refactor(space): 将GetPlanet重命名为GetSpace并提取地图热度逻辑到hot.go
This commit is contained in:
@@ -9,8 +9,8 @@ import (
|
||||
|
||||
func (h *Controller) MapIn(data *maps.InInfo, c *entity.Player) (result *maps.OutInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
|
||||
|
||||
c.MapId = data.MapId //登录地图
|
||||
space.GetPlanet(c.MapId).Set(c.UserID, c) //添加玩家
|
||||
c.MapId = data.MapId //登录地图
|
||||
space.GetSpace(c.MapId).Set(c.UserID, c) //添加玩家
|
||||
//result = &maps.OutInfo{UserID: c.UserID} //设置广播信息
|
||||
data.Broadcast(c.MapId, maps.OutInfo{UserID: c.UserID}) //同步广播
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ type InInfo struct {
|
||||
|
||||
func (t *InInfo) Broadcast(mapid uint32, o OutInfo) {
|
||||
|
||||
space.GetPlanet(mapid).Range(func(playerID uint32, player *entity.Player) bool {
|
||||
space.GetSpace(mapid).Range(func(playerID uint32, player *entity.Player) bool {
|
||||
|
||||
player.SendPack(t.Head.Pack(o))
|
||||
return true
|
||||
|
||||
@@ -17,7 +17,7 @@ type LeaveMapInboundInfo struct {
|
||||
|
||||
func (t *LeaveMapInboundInfo) Broadcast(mapid uint32, o LeaveMapOutboundInfo) {
|
||||
|
||||
space.GetPlanet(mapid).Range(func(playerID uint32, player *entity.Player) bool {
|
||||
space.GetSpace(mapid).Range(func(playerID uint32, player *entity.Player) bool {
|
||||
|
||||
player.SendPack(t.Head.Pack(o))
|
||||
return true
|
||||
|
||||
35
logic/service/space/hot.go
Normal file
35
logic/service/space/hot.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package space
|
||||
|
||||
import xml "blazing/common/data/xml/map"
|
||||
|
||||
// MapHotInfo 表示地图热度信息
|
||||
type MapHotInfo struct {
|
||||
MapID uint32 `json:"mapId"` // 地图ID
|
||||
Count uint32 `json:"count"` // 地图里的人数
|
||||
}
|
||||
|
||||
func GetMapHot() []MapHotInfo {
|
||||
|
||||
tt := make(map[uint32]uint32)
|
||||
|
||||
for _, v := range xml.MapConfig.Maps {
|
||||
|
||||
t1, ok := tt[uint32(v.Super)]
|
||||
if ok {
|
||||
tt[uint32(v.Super)] = uint32(int(t1) + GetSpace(uint32(v.ID)).Len())
|
||||
|
||||
} else {
|
||||
tt[uint32(v.Super)] = uint32(GetSpace(uint32(v.ID)).Len())
|
||||
}
|
||||
|
||||
}
|
||||
var result = make([]MapHotInfo, 0)
|
||||
for k, v := range tt {
|
||||
result = append(result, MapHotInfo{
|
||||
MapID: uint32(k),
|
||||
Count: uint32(GetSpace(uint32(v)).Len()),
|
||||
})
|
||||
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package space
|
||||
|
||||
import (
|
||||
"blazing/common/data/entity"
|
||||
xml "blazing/common/data/xml/map"
|
||||
"blazing/common/utils"
|
||||
"blazing/modules/blazing/model"
|
||||
"sync"
|
||||
)
|
||||
@@ -69,3 +71,36 @@ func (m *Space) Range(f func(playerID uint32, player *entity.Player) bool) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取星球
|
||||
func GetSpace(id uint32) *Space {
|
||||
|
||||
planet, ok := planetmap.Load(id)
|
||||
if ok {
|
||||
return planet
|
||||
}
|
||||
|
||||
//如果不ok,说明星球未创建,那就新建星球
|
||||
|
||||
for _, v := range xml.MapConfig.Maps {
|
||||
if v.ID == int(id) { //找到这个地图
|
||||
t := NewSpace()
|
||||
t.DefaultPos = model.Pos{X: uint32(v.X), Y: uint32(v.Y)}
|
||||
t.ID = uint32(v.ID)
|
||||
t.Name = v.Name
|
||||
t.Positions = make(map[uint32]model.Pos)
|
||||
for _, v := range v.Entries.Entries { //添加地图入口
|
||||
t.Positions[uint32(v.FromMap)] = model.Pos{X: uint32(v.PosX), Y: uint32(v.PosY)}
|
||||
|
||||
}
|
||||
planetmap.Store(id, t)
|
||||
return t
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var planetmap = &utils.SyncMap[uint32, *Space]{} //玩家数据
|
||||
|
||||
@@ -2,78 +2,12 @@ package space
|
||||
|
||||
import (
|
||||
"blazing/common/data/entity"
|
||||
xml "blazing/common/data/xml/map"
|
||||
"blazing/common/socket/handler"
|
||||
"blazing/common/utils"
|
||||
"blazing/modules/blazing/model"
|
||||
)
|
||||
|
||||
// MapHotInfo 表示地图热度信息
|
||||
type MapHotInfo struct {
|
||||
MapID uint32 `json:"mapId"` // 地图ID
|
||||
Count uint32 `json:"count"` // 地图里的人数
|
||||
}
|
||||
|
||||
func GetMapHot() []MapHotInfo {
|
||||
|
||||
tt := make(map[uint32]uint32)
|
||||
|
||||
for _, v := range xml.MapConfig.Maps {
|
||||
|
||||
t1, ok := tt[uint32(v.Super)]
|
||||
if ok {
|
||||
tt[uint32(v.Super)] = uint32(int(t1) + GetPlanet(uint32(v.ID)).Len())
|
||||
|
||||
} else {
|
||||
tt[uint32(v.Super)] = uint32(GetPlanet(uint32(v.ID)).Len())
|
||||
}
|
||||
|
||||
}
|
||||
var result=make([]MapHotInfo,0)
|
||||
for k, v := range tt {
|
||||
result = append(result, MapHotInfo{
|
||||
MapID: uint32(k),
|
||||
Count: uint32(GetPlanet(uint32(v)).Len()),
|
||||
})
|
||||
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// 获取星球
|
||||
func GetPlanet(id uint32) *Space {
|
||||
|
||||
planet, ok := planetmap.Load(id)
|
||||
if ok {
|
||||
return planet
|
||||
}
|
||||
|
||||
//如果不ok,说明星球未创建,那就新建星球
|
||||
|
||||
for _, v := range xml.MapConfig.Maps {
|
||||
if v.ID == int(id) { //找到这个地图
|
||||
t := NewSpace()
|
||||
t.DefaultPos = model.Pos{X: uint32(v.X), Y: uint32(v.Y)}
|
||||
t.ID = uint32(v.ID)
|
||||
t.Name = v.Name
|
||||
t.Positions = make(map[uint32]model.Pos)
|
||||
for _, v := range v.Entries.Entries { //添加地图入口
|
||||
t.Positions[uint32(v.FromMap)] = model.Pos{X: uint32(v.PosX), Y: uint32(v.PosY)}
|
||||
|
||||
}
|
||||
planetmap.Store(id, t)
|
||||
return t
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var planetmap = &utils.SyncMap[uint32, *Space]{} //玩家数据
|
||||
type InInfo struct {
|
||||
Head handler.TomeeHeader `cmd:"2101" struc:"[0]pad"` //玩家登录
|
||||
Head handler.TomeeHeader `cmd:"2101" struc:"[0]pad"` //走路包
|
||||
// Flag: 0为走,1为飞行模式,对应Java的@UInt long
|
||||
Flag uint32
|
||||
|
||||
@@ -86,7 +20,7 @@ type InInfo struct {
|
||||
|
||||
func (t *InInfo) Broadcast(mapid uint32, o OutInfo) {
|
||||
|
||||
GetPlanet(mapid).Range(func(playerID uint32, player *entity.Player) bool {
|
||||
GetSpace(mapid).Range(func(playerID uint32, player *entity.Player) bool {
|
||||
|
||||
player.SendPack(t.Head.Pack(o))
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user