game/server/gate/server/userService.go

55 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package server
import (
"fmt"
"game/common/proto/pb"
"strings"
"sync"
)
var userServiceMgr userServiceManager
/*
记录玩家访问过的节点信息,玩家下线后清除相关信息,比如玩家在某个玩法服里掉线,重连后要回到该房间,应由对应的服务节点主动将玩家拉回房间
*/
type userServiceManager struct {
inService sync.Map // key:userId+service_type_id value:service_name
}
func (m *userServiceManager) makeKey(userId int64, typeId pb.ServiceTypeId) string {
return fmt.Sprintf("%s_%d", userId, typeId)
}
func (m *userServiceManager) Add(userId int64, typeId pb.ServiceTypeId, serviceName string) {
k := m.makeKey(userId, typeId)
m.inService.Store(k, serviceName)
}
// 玩家下线,清除他呆过的所有服务节点信息
func (m *userServiceManager) CleanUser(userId int64) {
var del []string
m.inService.Range(func(k, v interface{}) bool {
userServiceType := k.(string)
if strings.Contains(userServiceType, fmt.Sprintf("%d", userId)) {
del = append(del, userServiceType)
}
return true
})
for _, k := range del {
m.inService.Delete(k)
}
}
// todo:要考虑到旧服务可能已关闭这里要访问etcd中是否有该服务节点最好采用订阅机制让etcd变动时清除对应服务节点
func (m *userServiceManager) FindServiceName(userId int64, typeId pb.ServiceTypeId) (serviceName string, ok bool) {
var v any
if v, ok = m.inService.Load(m.makeKey(userId, typeId)); ok {
serviceName = v.(string)
}
return
}
func (m *userServiceManager) Del(userId int64, typeId pb.ServiceTypeId) {
m.inService.Delete(m.makeKey(userId, typeId))
}