diff --git a/common/cool/global.go b/common/cool/global.go index b31318e3c..f2987cc34 100644 --- a/common/cool/global.go +++ b/common/cool/global.go @@ -1,7 +1,6 @@ package cool import ( - "blazing/common/data/entity" "blazing/common/utils" "context" "reflect" @@ -14,8 +13,7 @@ import ( var ctx = context.TODO() var ( - Mainplayer = &utils.SyncMap[uint32, *entity.Player]{} //玩家数据 - CmdCache = &utils.SyncMap[uint32, reflect.Value]{} //命令缓存 + CmdCache = &utils.SyncMap[uint32, reflect.Value]{} //命令缓存 Loger = glog.New() ) @@ -37,12 +35,3 @@ func init() { glog.Debug(context.Background(), "初始化雪花算法", newId) } -func ConutPlayer() int { - - count := 0 - Mainplayer.Range(func(uint32, *entity.Player) bool { - count++ - return true // 继续遍历 - }) - return count -} diff --git a/common/data/entity/conn.go b/common/data/entity/conn.go deleted file mode 100644 index 25e6f68ee..000000000 --- a/common/data/entity/conn.go +++ /dev/null @@ -1,37 +0,0 @@ -package entity - -import ( - "github.com/gobwas/ws" - "github.com/gobwas/ws/wsutil" - "github.com/panjf2000/gnet/v2" - "github.com/panjf2000/gnet/v2/pkg/logging" -) - -type Conn struct { - MainConn gnet.Conn `struc:"[0]pad"` //TODO 不序列化,,序列化下面的作为blob存数据库 -} - -func NewConn(c gnet.Conn) *Conn { - return &Conn{MainConn: c} -} -func (c *Conn) SendPack(bytes []byte) error { - if t, ok := c.MainConn.Context().(*ClientData); ok { - if t.Getwsmsg().Upgraded { - // This is the echo server - err := wsutil.WriteServerMessage(c.MainConn, ws.OpBinary, bytes) - if err != nil { - logging.Infof("conn[%v] [err=%v]", c.MainConn.RemoteAddr().String(), err.Error()) - return err - } - } else { - - _, err := c.MainConn.Write(bytes) - if err != nil { - logging.Error(err) - - } - } - } - - return nil -} diff --git a/common/data/entity/task.go b/common/data/entity/task.go deleted file mode 100644 index 9356433d1..000000000 --- a/common/data/entity/task.go +++ /dev/null @@ -1 +0,0 @@ -package entity diff --git a/common/data/entity/Client.go b/common/data/socket/Client.go similarity index 78% rename from common/data/entity/Client.go rename to common/data/socket/Client.go index c4876a895..08a77cf79 100644 --- a/common/data/entity/Client.go +++ b/common/data/socket/Client.go @@ -1,6 +1,18 @@ -package entity +package socket -import "sync" +import ( + "sync" +) + +func ConutPlayer() int { + + count := 0 + Mainplayer.Range(func(uint32, *Player) bool { + count++ + return true // 继续遍历 + }) + return count +} type ClientData struct { isCrossDomain bool //是否跨域过 diff --git a/common/data/entity/player.go b/common/data/socket/player.go similarity index 72% rename from common/data/entity/player.go rename to common/data/socket/player.go index 79801aae2..979d2760f 100644 --- a/common/data/entity/player.go +++ b/common/data/socket/player.go @@ -1,24 +1,52 @@ -package entity +package socket import ( + "blazing/common/utils" + "blazing/modules/blazing/model" "context" "fmt" "sync" "time" + "github.com/gobwas/ws" + "github.com/gobwas/ws/wsutil" "github.com/gogf/gf/v2/frame/g" + "github.com/panjf2000/gnet/pkg/logging" "github.com/tnnmigga/enum" ) +var Mainplayer = &utils.SyncMap[uint32, *Player]{} //玩家数据 + +func (c *Conn) SendPack(bytes []byte) error { + if t, ok := c.MainConn.Context().(*ClientData); ok { + if t.Getwsmsg().Upgraded { + // This is the echo server + err := wsutil.WriteServerMessage(c.MainConn, ws.OpBinary, bytes) + if err != nil { + logging.Infof("conn[%v] [err=%v]", c.MainConn.RemoteAddr().String(), err.Error()) + return err + } + } else { + + _, err := c.MainConn.Write(bytes) + if err != nil { + logging.Error(err) + + } + } + } + + return nil +} + type Player struct { MainConn Conn - UserID uint32 //用户ID - IsLogin bool //是否登录 - mu sync.Mutex - MapId uint32 //当前所在的地图ID + + IsLogin bool //是否登录 + mu sync.Mutex loginChan chan struct{} // 登录完成通知通道 - Nick string //昵称 + Info *model.PlayerInfo StopChan chan struct{} //停止刷怪协程 IsFighting bool context.Context @@ -28,12 +56,6 @@ type Player struct { // PlayerOption 定义配置 Player 的函数类型 type PlayerOption func(*Player) -// WithUserID 设置用户ID的选项函数 -func WithUserID(userID uint32) PlayerOption { - return func(p *Player) { - p.UserID = userID - } -} func WithConn(c Conn) PlayerOption { return func(p *Player) { p.MainConn = c @@ -51,14 +73,6 @@ func NewPlayer(opts ...PlayerOption) *Player { return p } -func (p *Player) GetUserID() uint32 { - return p.UserID -} - -func (p *Player) SetData(key any) uint32 { - - return p.UserID -} func (p *Player) SendPack(b []byte) error { fmt.Println("发送数据包", len(b)) err := p.MainConn.SendPack(b) @@ -66,7 +80,7 @@ func (p *Player) SendPack(b []byte) error { } func (p *Player) Cheak(b error) { if b != nil { - g.Log().Error(context.Background(), "出现错误", p.UserID, b.Error()) + g.Log().Error(context.Background(), "出现错误", p.Info.UserID, b.Error()) } } @@ -150,10 +164,10 @@ type Playerinvite struct { //挂载到[]Playerinvite上? 被邀请者->邀请 func (lw *Player) InvitePlayerToBattle(target int64, mode EnumBattleMode) { t, ok := Playerinvitemap[uint32(target)] //被邀请者是否被邀请过 if ok { //说明存在被邀请 - t = append(t, Playerinvite{uint32(lw.UserID), mode}) + t = append(t, Playerinvite{uint32(lw.Info.UserID), mode}) Playerinvitemap[uint32(target)] = t } else { - Playerinvitemap[uint32(target)] = []Playerinvite{{uint32(lw.UserID), mode}} + Playerinvitemap[uint32(target)] = []Playerinvite{{uint32(lw.Info.UserID), mode}} } lw.Playerinvite = uint32(target) diff --git a/common/data/entity/wscodec.go b/common/data/socket/wscodec.go similarity index 91% rename from common/data/entity/wscodec.go rename to common/data/socket/wscodec.go index 6a0b130bc..61c4aff48 100644 --- a/common/data/entity/wscodec.go +++ b/common/data/socket/wscodec.go @@ -1,4 +1,4 @@ -package entity +package socket import ( "bytes" @@ -11,6 +11,14 @@ import ( "github.com/panjf2000/gnet/v2/pkg/logging" ) +type Conn struct { + MainConn gnet.Conn `struc:"[0]pad"` //TODO 不序列化,,序列化下面的作为blob存数据库 +} + +func NewConn(c gnet.Conn) *Conn { + return &Conn{MainConn: c} +} + type WsCodec struct { Upgraded bool // 链接是否升级 Buf bytes.Buffer // 从实际socket中读取到的数据缓存 diff --git a/common/socket/Server.go b/common/socket/Server.go deleted file mode 100644 index 0b72ddb70..000000000 --- a/common/socket/Server.go +++ /dev/null @@ -1 +0,0 @@ -package socket diff --git a/common/socket/ServerEvent.go b/common/socket/ServerEvent.go index 8cef8a359..879f9c4a6 100644 --- a/common/socket/ServerEvent.go +++ b/common/socket/ServerEvent.go @@ -6,9 +6,8 @@ import ( "sync/atomic" "time" - "blazing/common/data/entity" "blazing/common/data/share" - "blazing/cool" + "blazing/common/data/socket" "github.com/gogf/gf/v2/os/glog" "github.com/panjf2000/gnet/v2" @@ -42,17 +41,17 @@ func (s *Server) Stop() error { func (s *Server) OnClose(c gnet.Conn, _ error) (action gnet.Action) { atomic.AddInt64(&s.connected, -1) //logging.Infof("conn[%v] disconnected", c.RemoteAddr().String()) - v, ok := c.Context().(*entity.ClientData) + v, ok := c.Context().(*socket.ClientData) if !ok { return } t := v.GetPlayer() - if t != nil { - glog.Debug(context.Background(), t.UserID, "断开连接") + if v != nil { + glog.Debug(context.Background(), t, "断开连接") t.IsLogin = false - cool.Mainplayer.Delete(t.UserID) - share.ShareManager.DeleteUserOnline(t.UserID) //设置用户登录服务器 + socket.Mainplayer.Delete(t.Info.UserID) + share.ShareManager.DeleteUserOnline(t.Info.UserID) //设置用户登录服务器 } //} @@ -73,7 +72,7 @@ func (s *Server) OnBoot(eng gnet.Engine) gnet.Action { func (s *Server) OnOpen(conn gnet.Conn) (out []byte, action gnet.Action) { if conn.Context() == nil { - conn.SetContext(entity.NewClientData()) //注入data + conn.SetContext(socket.NewClientData()) //注入data } atomic.AddInt64(&s.connected, 1) @@ -86,7 +85,7 @@ func (s *Server) OnTraffic(c gnet.Conn) (action gnet.Action) { return gnet.Close } - ws := c.Context().(*entity.ClientData).Getwsmsg() + ws := c.Context().(*socket.ClientData).Getwsmsg() tt, len1 := ws.ReadBufferBytes(c) if tt == gnet.Close { @@ -165,7 +164,7 @@ const CROSS_DOMAIN = "