53 lines
1.0 KiB
Go
53 lines
1.0 KiB
Go
package ws
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
//var userMgr = newUserManager()
|
|
|
|
type userManager struct {
|
|
users sync.Map // map[int64]uint32
|
|
connMgr *connManager
|
|
}
|
|
|
|
func newUserManager(connMgr *connManager) *userManager {
|
|
return &userManager{
|
|
connMgr: connMgr,
|
|
}
|
|
}
|
|
|
|
func (m *userManager) Add(connId uint32, userId int64) bool {
|
|
if conn, ok := m.connMgr.Get(connId); ok {
|
|
if conn.UserId() == 0 {
|
|
//log.DebugF("添加玩家:%v 连接:%v", userId, connId)
|
|
} else {
|
|
//log.DebugF("将连接:%v里的玩家id:%v设置为:%v", connId, conn.UserId(), userId)
|
|
}
|
|
conn.setUserId(userId)
|
|
m.users.Store(userId, connId)
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (m *userManager) GetConnId(userId int64) uint32 {
|
|
if connId, ok := m.users.Load(userId); ok {
|
|
return connId.(uint32)
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// k:userid v:conn
|
|
func (m *userManager) Rang(cb func(k, v any) bool) {
|
|
m.users.Range(cb)
|
|
}
|
|
|
|
func (m *userManager) Remove(userId int64) {
|
|
if userId < 1 {
|
|
return
|
|
}
|
|
//log.DebugF("删除玩家:%v", userId)
|
|
m.users.Delete(userId)
|
|
}
|