refactor(logic): 重构登录和地图相关逻辑
- 更新 Player 结构体,添加 Nick 字段 - 修改 ErrorCode 类型从 uint32 改为 int32 - 优化 SocketHandler 处理逻辑 - 重构 Controller 中的 Login 和地图相关方法 - 更新地图服务中的 MapIn 和 MapOut 方法 - 调整空间服务中的 Hot 和 Walk 方法
This commit is contained in:
@@ -17,6 +17,7 @@ type Player struct {
|
||||
MapId uint32 //当前所在的地图ID
|
||||
|
||||
loginChan chan struct{} // 登录完成通知通道
|
||||
Nick string //昵称
|
||||
context.Context
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
// ErrorCode 错误码枚举类型
|
||||
type ErrorCode uint32
|
||||
type ErrorCode int32
|
||||
|
||||
func (t ErrorCode) Code() string { return fmt.Sprintf("%d", t) }
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ func (h *TomeeHandler) Handle(c gnet.Conn, data []byte) { //处理接收到的
|
||||
header.Result, _ = tempdata.ReadUInt32()
|
||||
header.Data = tempdata.BytesAvailable()
|
||||
//fmt.Println("接收封包", header)
|
||||
h.Callback( entity.NewConn(c), header)
|
||||
h.Callback(entity.NewConn(c), header)
|
||||
//return header
|
||||
}
|
||||
|
||||
@@ -69,6 +69,7 @@ func (h *TomeeHandler) Handle(c gnet.Conn, data []byte) { //处理接收到的
|
||||
// 结构体类型使用struc库进行序列化。
|
||||
// 最后,将数据长度、版本号、命令码、用户ID和结果代码一并打包进返回的字节切片中。
|
||||
func (h *TomeeHeader) Pack(data any) []byte { //组包
|
||||
//h.Result = 0//默认置0
|
||||
|
||||
//t := reflect.TypeOf(data)
|
||||
tv := reflect.ValueOf(data)
|
||||
|
||||
@@ -210,17 +210,23 @@ func Recv(c *entity.Conn, data handler.TomeeHeader) {
|
||||
}
|
||||
|
||||
aa, ok := ret[1].Interface().(errorcode.ErrorCode) //判断错误
|
||||
data.Result = uint32(aa)
|
||||
if aa == -1 {
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
if ok && aa != 0 { //这里实现回复错误包
|
||||
|
||||
cool.Loger.Error(context.Background(), aa.Code())
|
||||
|
||||
data.Result = uint32(aa)
|
||||
c.SendPack(data.Pack(nil))
|
||||
return
|
||||
|
||||
}
|
||||
data.Result = 0 //默认成功
|
||||
|
||||
data.Version = "7"
|
||||
|
||||
c.SendPack(data.Pack(ret[0].Interface()))
|
||||
|
||||
}
|
||||
|
||||
@@ -6,8 +6,12 @@ import (
|
||||
"blazing/common/socket/errorcode"
|
||||
"blazing/logic/service"
|
||||
"blazing/logic/service/login"
|
||||
"blazing/logic/service/space"
|
||||
"blazing/modules/blazing/model"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
)
|
||||
|
||||
// 处理命令: 1001
|
||||
@@ -22,17 +26,20 @@ func (h *Controller) Login(data *login.InInfo, c *entity.Conn) (result *login.Ou
|
||||
share.ShareManager.SetUserOnline(data.Head.UserID, h.Port) //设置用户登录服务器
|
||||
t.CompleteLogin() //通知客户端登录成功
|
||||
t.MapId = 1
|
||||
space.GetSpace(t.MapId).Set(t.UserID, t) //添加玩家
|
||||
glog.Debug(context.Background(), "登录成功,初始地图 人数:", space.GetSpace(1).Len())
|
||||
|
||||
c.SendPack(data.Def())
|
||||
return
|
||||
t.Nick = "测试"
|
||||
// c.SendPack(data.Def())
|
||||
// return
|
||||
result = login.NewOutInfo() //设置登录消息
|
||||
result.UserID = 10002 //注释掉就能进入
|
||||
result.UserID = 10001 //注释掉就能进入
|
||||
result.RegisterTime = uint32(time.Now().Unix())
|
||||
result.Nick = "骄阳号"
|
||||
result.Color = 0x00FF00
|
||||
result.Pos = model.Pos{X: 100, Y: 200}
|
||||
result.Pos = model.Pos{X: 500, Y: 400}
|
||||
result.Clothes = append(result.Clothes, model.PeopleItemInfo{ID: 100717, Level: 1})
|
||||
result.MapID = 1
|
||||
result.MapID = t.MapId
|
||||
result.TimeLimit = 43200
|
||||
result.TimeToday = 339
|
||||
|
||||
|
||||
@@ -11,8 +11,8 @@ func (h *Controller) MapIn(data *maps.InInfo, c *entity.Player) (result *maps.Ou
|
||||
|
||||
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}) //同步广播
|
||||
|
||||
return
|
||||
data.Broadcast(c.MapId, maps.OutInfo{UserID: c.UserID, Nick: c.Nick}) //同步广播
|
||||
|
||||
return nil, -1
|
||||
}
|
||||
@@ -1,20 +1,20 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"blazing/common/data/entity"
|
||||
"blazing/common/socket/errorcode"
|
||||
"blazing/logic/service/maphot"
|
||||
"blazing/logic/service/space"
|
||||
)
|
||||
|
||||
func (h Controller) MapHot(data *maphot.InInfo, c *entity.Player) (result *maphot.OutInfo, err errorcode.ErrorCode) {
|
||||
|
||||
result = &maphot.OutInfo{
|
||||
|
||||
HotInfos: space.GetMapHot(),
|
||||
}
|
||||
|
||||
|
||||
|
||||
return
|
||||
}
|
||||
package controller
|
||||
|
||||
import (
|
||||
"blazing/common/data/entity"
|
||||
"blazing/common/socket/errorcode"
|
||||
"blazing/logic/service/maphot"
|
||||
"blazing/logic/service/space"
|
||||
)
|
||||
|
||||
func (h Controller) MapHot(data *maphot.InInfo, c *entity.Player) (result *maphot.OutInfo, err errorcode.ErrorCode) {
|
||||
|
||||
result = &maphot.OutInfo{
|
||||
|
||||
HotInfos: space.GetMapHot(),
|
||||
}
|
||||
|
||||
|
||||
|
||||
return
|
||||
}
|
||||
@@ -4,10 +4,12 @@ import (
|
||||
"blazing/common/data/entity"
|
||||
"blazing/common/socket/errorcode"
|
||||
"blazing/logic/service/maps"
|
||||
"blazing/logic/service/space"
|
||||
)
|
||||
|
||||
func (h *Controller) MapOut(data *maps.LeaveMapInboundInfo, c *entity.Player) (result *maps.LeaveMapOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
|
||||
result = &maps.LeaveMapOutboundInfo{UserID: c.GetUserID()}
|
||||
//result = &maps.LeaveMapOutboundInfo{UserID: c.GetUserID()}
|
||||
data.Broadcast(c.MapId, maps.LeaveMapOutboundInfo{UserID: c.GetUserID()}) //同步广播
|
||||
return
|
||||
space.GetSpace(c.MapId).Delete(c.UserID)
|
||||
return nil, -1
|
||||
}
|
||||
20
logic/controller/map_list.go
Normal file
20
logic/controller/map_list.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"blazing/common/data/entity"
|
||||
"blazing/common/socket/errorcode"
|
||||
"blazing/logic/service/maps"
|
||||
)
|
||||
|
||||
func (h *Controller) MapList(data *maps.ListMapPlayerInboundInfo, c *entity.Player) (result *maps.ListMapPlayerOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
|
||||
|
||||
result = &maps.ListMapPlayerOutboundInfo{}
|
||||
result.Player = make([]maps.OutInfo, 0)
|
||||
result1 := maps.NewOutInfo()
|
||||
|
||||
result1.UserID = c.UserID
|
||||
//result.Pos = model.Pos{X: 500, Y: 400}
|
||||
result1.Nick = c.Nick
|
||||
result.Player = append(result.Player, *result1)
|
||||
return
|
||||
}
|
||||
@@ -8,12 +8,10 @@ import (
|
||||
|
||||
func (h Controller) Walk(data *space.InInfo, c *entity.Player) (result *space.OutInfo, err errorcode.ErrorCode) {
|
||||
|
||||
result = &space.OutInfo{Flag: data.Flag,
|
||||
data.Broadcast(c.MapId, space.OutInfo{Flag: data.Flag,
|
||||
UserID: c.GetUserID(),
|
||||
Reserve2: data.Reverse2,
|
||||
Point: data.Point}
|
||||
Point: data.Point}) //走路的广播
|
||||
|
||||
data.Broadcast(c.MapId, *result) //走路的广播
|
||||
|
||||
return
|
||||
return nil, -1
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"blazing/common/socket/handler"
|
||||
"blazing/logic/service/space"
|
||||
"blazing/modules/blazing/model"
|
||||
|
||||
"github.com/creasty/defaults"
|
||||
)
|
||||
|
||||
type InInfo struct {
|
||||
@@ -23,8 +25,9 @@ type InInfo struct {
|
||||
func (t *InInfo) Broadcast(mapid uint32, o OutInfo) {
|
||||
|
||||
space.GetSpace(mapid).Range(func(playerID uint32, player *entity.Player) bool {
|
||||
t.Head.Result = 0
|
||||
|
||||
player.SendPack(t.Head.Pack(o))
|
||||
player.SendPack(t.Head.Pack(&o))
|
||||
return true
|
||||
})
|
||||
}
|
||||
@@ -34,10 +37,6 @@ func (t *InInfo) Broadcast(mapid uint32, o OutInfo) {
|
||||
|
||||
// PeopleInfo 对应Java的PeopleInfo类,实现OutboundMessage接口
|
||||
type OutInfo struct {
|
||||
// 系统时间
|
||||
SystemTime uint32 `struc:"uint32" fieldDesc:"系统时间" json:"system_time"`
|
||||
|
||||
// 米米号
|
||||
UserID uint32 `struc:"uint32" fieldDesc:"米米号" json:"user_id"`
|
||||
|
||||
// 16字节昵称
|
||||
@@ -50,10 +49,10 @@ type OutInfo struct {
|
||||
Texture uint32 `struc:"uint32" fieldDesc:"固定值0" json:"texture"`
|
||||
|
||||
// 暂时不明建议先给固定值0
|
||||
Vip uint16 `struc:"uint16" fieldDesc:"暂时不明建议先给固定值0" json:"vip"`
|
||||
Vip uint32 `struc:"uint32" fieldDesc:"暂时不明建议先给固定值0" json:"vip"`
|
||||
|
||||
// 暂时不明建议先给固定值15
|
||||
Viped uint16 `struc:"uint16" default:"15" fieldDesc:"暂时不明建议先给固定值15" json:"viped"`
|
||||
Viped uint32 `struc:"uint32" default:"15" fieldDesc:"暂时不明建议先给固定值15" json:"viped"`
|
||||
|
||||
// 暂时不明建议先给固定值1
|
||||
VipStage uint32 `struc:"uint32" fieldDesc:"暂时不明建议先给固定值1" json:"vip_stage"`
|
||||
@@ -79,8 +78,18 @@ type OutInfo struct {
|
||||
// 暂时不明给0
|
||||
SpiritID uint32 `struc:"uint32" fieldDesc:"暂时不明给0" json:"spirit_id"`
|
||||
|
||||
// 跟随精灵的个体值
|
||||
PetDV uint32 `struc:"uint32" fieldDesc:"跟随精灵的个体值" json:"pet_dv"`
|
||||
// 宠物 ID 暂时无法测试,给 0
|
||||
PetDV uint32 `struc:"uint32" fieldDesc:"宠物ID暂时无法测试, 给0" json:"pet_dv"`
|
||||
// 宠物闪光暂时无法测试,给 0
|
||||
PetShiny uint32 `struc:"uint32" fieldDesc:"宠物闪光暂时无法测试, 给0" json:"pet_shiny"`
|
||||
// 宠物皮肤暂时无法测试,给 0
|
||||
PetSkin uint32 `struc:"uint32" fieldDesc:"宠物皮肤暂时无法测试, 给0" json:"pet_skin"`
|
||||
// 填充字符
|
||||
Reserved uint32 `struc:"uint32" fieldDesc:"填充字符" json:"reserved"`
|
||||
// 填充字符
|
||||
Reserved1 uint32 `struc:"uint32" fieldDesc:"填充字符" json:"reserved1"`
|
||||
// 填充字符
|
||||
Reserved2 uint32 `struc:"uint32" fieldDesc:"填充字符" json:"reserved2"`
|
||||
|
||||
// 暂时不明给0
|
||||
FightFlag uint32 `struc:"uint32" fieldDesc:"暂时不明给0" json:"fight_flag"`
|
||||
@@ -116,20 +125,32 @@ type OutInfo struct {
|
||||
TeamIsShow uint32 `struc:"uint32" fieldDesc:"暂时不明给0" json:"team_is_show"`
|
||||
|
||||
// 暂时不明给0
|
||||
LogoBg int16 `struc:"int16" fieldDesc:"暂时不明给0" json:"logo_bg"`
|
||||
LogoBg uint16 `json:"logo_bg"`
|
||||
|
||||
// 暂时不明给0
|
||||
LogoIcon int16 `struc:"int16" fieldDesc:"暂时不明给0" json:"logo_icon"`
|
||||
LogoIcon uint16 ` json:"logo_icon"`
|
||||
|
||||
// 暂时不明给0
|
||||
LogoColor int16 `struc:"int16" fieldDesc:"暂时不明给0" json:"logo_color"`
|
||||
LogoColor uint16 `json:"logo_color"`
|
||||
|
||||
// 暂时不明给0
|
||||
TxtColor int16 `struc:"int16" fieldDesc:"暂时不明给0" json:"txt_color"`
|
||||
TxtColor uint16 `json:"txt_color"`
|
||||
|
||||
// 4字节
|
||||
LogoWord [4]byte `struc:"[4]byte" fieldDesc:"4字节" json:"logo_word"`
|
||||
LogoWord [4]byte `struc:"[4]byte" fieldDesc:"4字节" json:"logo_word"`
|
||||
ClothesLen uint32 `struc:"sizeof=Clothes" fieldDesc:"穿戴装备的信息" json:"clothes_len"`
|
||||
|
||||
// 穿戴装备的信息
|
||||
Clothes []model.PeopleItemInfo `fieldDesc:"穿戴装备的信息" json:"clothes"`
|
||||
}
|
||||
|
||||
func NewOutInfo() *OutInfo {
|
||||
l := &OutInfo{}
|
||||
|
||||
// 自动填充 struct tag 里的 default 值
|
||||
if err := defaults.Set(l); err != nil {
|
||||
panic(err) // 方便发现 default 设置错误
|
||||
}
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
16
logic/service/maps/maplist.go
Normal file
16
logic/service/maps/maplist.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package maps
|
||||
|
||||
import (
|
||||
"blazing/common/socket/handler"
|
||||
)
|
||||
|
||||
type ListMapPlayerInboundInfo struct {
|
||||
Head handler.TomeeHeader `cmd:"2003" struc:"[0]pad"` //切换地图
|
||||
}
|
||||
|
||||
type ListMapPlayerOutboundInfo struct {
|
||||
PlayersLen uint32 `struc:"sizeof=Player" json:"player_len"`
|
||||
|
||||
// 穿戴装备的信息
|
||||
Player []OutInfo ` json:"player"`
|
||||
}
|
||||
@@ -18,8 +18,9 @@ type LeaveMapInboundInfo struct {
|
||||
func (t *LeaveMapInboundInfo) Broadcast(mapid uint32, o LeaveMapOutboundInfo) {
|
||||
|
||||
space.GetSpace(mapid).Range(func(playerID uint32, player *entity.Player) bool {
|
||||
t.Head.Result = 0
|
||||
|
||||
player.SendPack(t.Head.Pack(o))
|
||||
player.SendPack(t.Head.Pack(&o))
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
@@ -25,9 +25,10 @@ func GetMapHot() []MapHotInfo {
|
||||
}
|
||||
var result = make([]MapHotInfo, 0)
|
||||
for k, v := range tt {
|
||||
|
||||
result = append(result, MapHotInfo{
|
||||
MapID: uint32(k),
|
||||
Count: uint32(GetSpace(uint32(v)).Len()),
|
||||
Count: uint32(v),
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
@@ -20,9 +20,14 @@ type InInfo struct {
|
||||
|
||||
func (t *InInfo) Broadcast(mapid uint32, o OutInfo) {
|
||||
|
||||
GetSpace(mapid).Range(func(playerID uint32, player *entity.Player) bool {
|
||||
//tt := planetmap
|
||||
//g.Dump(GetSpace(mapid).Len())
|
||||
|
||||
player.SendPack(t.Head.Pack(o))
|
||||
GetSpace(mapid).Range(func(playerID uint32, player *entity.Player) bool {
|
||||
t.Head.Result = 0
|
||||
tt := t.Head.Pack(&o)
|
||||
err := player.SendPack(tt)
|
||||
player.Cheak(err)
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user