修改bug
This commit is contained in:
parent
ee957ecdad
commit
41c32e5fa7
@ -119,7 +119,7 @@ func LoadCommonConfig[T any](rd *redis.Client, GitCommit, GitBranch, BuildDate s
|
||||
if s == "" {
|
||||
log.DebugF("load config:empty mysql key")
|
||||
comm.Mysql = Mysql{Host: mysqlAddress, Port: mysqlPort, Password: mysqlPasswd, Username: mysqlUser, DbName: mysqlDBName}
|
||||
if bs, err := json.Marshal(&comm.Redis); err == nil {
|
||||
if bs, err := json.Marshal(&comm.Mysql); err == nil {
|
||||
err = rd.Set(context.Background(), mysqlKey, string(bs), 0).Err()
|
||||
}
|
||||
} else {
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"game/common/proto/pb"
|
||||
"game/common/utils"
|
||||
"github.com/fox/fox/etcd"
|
||||
"github.com/fox/fox/log"
|
||||
"github.com/fox/fox/xrand"
|
||||
@ -71,12 +72,22 @@ func (m *UserBindService) serviceIsValid(serviceName string) bool {
|
||||
return valid
|
||||
}
|
||||
|
||||
func (m *UserBindService) stringAllServiceNode() string {
|
||||
var nodes []any
|
||||
m.etcdRegistry.GetNodes().Range(func(_, value any) bool {
|
||||
nodes = append(nodes, value)
|
||||
return true
|
||||
})
|
||||
return utils.JsonMarshal(nodes)
|
||||
}
|
||||
|
||||
// 从etcd中找可用服务节点随机选择一个
|
||||
func (m *UserBindService) RandServiceNode(typeId pb.ServiceTypeId) (*etcd.ServiceNode, error) {
|
||||
var nodes []etcd.ServiceNode
|
||||
var version string
|
||||
m.etcdRegistry.GetNodes().Range(func(_, value any) bool {
|
||||
if node, ok := value.(etcd.ServiceNode); ok && node.TypeId == int(typeId) {
|
||||
node, ok := value.(etcd.ServiceNode)
|
||||
if ok && node.TypeId == int(typeId) {
|
||||
if version < node.Version {
|
||||
version = node.Version
|
||||
}
|
||||
@ -92,7 +103,7 @@ func (m *UserBindService) RandServiceNode(typeId pb.ServiceTypeId) (*etcd.Servic
|
||||
return true
|
||||
})
|
||||
if len(nodes) == 0 {
|
||||
return nil, fmt.Errorf("not found service node.type id: %v", typeId)
|
||||
return nil, fmt.Errorf("not found service node.type id:%v. all node:%v", typeId, m.stringAllServiceNode())
|
||||
}
|
||||
n := xrand.IntN(len(nodes))
|
||||
return &nodes[n], nil
|
||||
@ -107,8 +118,10 @@ func (m *UserBindService) FindServiceName(userId int64, typeId pb.ServiceTypeId)
|
||||
// redis也没有玩家的服务节点信息,从etcd中找可用服务节点随机选择一个
|
||||
node, err := m.RandServiceNode(typeId)
|
||||
if err != nil {
|
||||
log.ErrorF("etcd中随机一个服务节点时,错误:%v", err)
|
||||
return "", err
|
||||
}
|
||||
//log.DebugF("etcd中随机一个服务节点:%s", node.Name)
|
||||
m.rdb.Set(context.Background(), m.makeRedisKey(userId, typeId), node.Name, 2*24*time.Hour)
|
||||
return node.Name, nil
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ const (
|
||||
timeFormat = "2006-01-02 15:04:05"
|
||||
)
|
||||
|
||||
func Marshal(a any) string {
|
||||
func JsonMarshal(a any) string {
|
||||
s, _ := json.Marshal(a)
|
||||
return string(s)
|
||||
}
|
||||
|
@ -59,6 +59,7 @@ RemotePath="/home/ubuntu/game"
|
||||
RemoteFile="$RemotePath/$FILE"
|
||||
LocalFile="bin/$FILE"
|
||||
|
||||
rm -rf '$LocalFile'
|
||||
plink -ssh -batch -pw "$Password" "$Username@$Host" "rm -f '$RemoteFile'"
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "删除文件成功"
|
||||
|
@ -94,8 +94,10 @@ func (s *ChatService) OnMessage(data []byte) error {
|
||||
}
|
||||
if req, err := s.processor.Unmarshal(iMsg.MsgId, iMsg.Msg); err == nil {
|
||||
err = s.processor.Dispatch(iMsg.MsgId, iMsg.UserId, req)
|
||||
} else {
|
||||
log.Error(err.Error())
|
||||
}
|
||||
//log.Debug(s.Log("on message:%v", string(msg)))
|
||||
//log.Debug(s.Log("received message:%v", iMsg.MsgId))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,6 @@ import (
|
||||
"game/common/serviceName"
|
||||
"game/common/topicName"
|
||||
"game/common/userBindService"
|
||||
"game/common/utils"
|
||||
"game/server/gate/config"
|
||||
"game/server/gate/model"
|
||||
"github.com/fox/fox/ipb"
|
||||
@ -132,7 +131,7 @@ func (s *GateService) connMessage(conn ws.IConn, iMsg *ipb.InternalMsg) {
|
||||
} else {
|
||||
s.SendClientData(conn, iMsg.ServiceName, iMsg.MsgId, iMsg.Msg)
|
||||
}
|
||||
//log.Debug(s.Log("on message:%v", string(msg)))
|
||||
//log.Debug(s.Log("received service message:%v", iMsg.MsgId))
|
||||
}
|
||||
|
||||
// 处理其它服发送过来的消息。大部分是将消息转发给玩家
|
||||
@ -157,13 +156,11 @@ func (s *GateService) OnMessage(data []byte) error {
|
||||
查找topic,根据serviceTypeId以及玩家id查找玩家过往访问该服务的节点,优先使用原节点
|
||||
*/
|
||||
func (s *GateService) findTopic(userId int64, serviceTypeId pb.ServiceTypeId) (topic, sName string) {
|
||||
if userId != 0 {
|
||||
var err error
|
||||
if sName, err = s.bindService.FindServiceName(userId, serviceTypeId); err == nil {
|
||||
return service.TopicEx(sName), sName
|
||||
} else {
|
||||
log.Error(err.Error())
|
||||
}
|
||||
var err error
|
||||
if sName, err = s.bindService.FindServiceName(userId, serviceTypeId); err == nil {
|
||||
return service.TopicEx(sName), sName
|
||||
} else {
|
||||
log.Error(err.Error())
|
||||
}
|
||||
return "", sName
|
||||
}
|
||||
@ -192,7 +189,7 @@ func (s *GateService) WsOnMessage(conn ws.IConn, data []byte) {
|
||||
} else {
|
||||
log.Error(s.Log("topic:%v not exist.user:%v", topic, conn.UserId()))
|
||||
}
|
||||
log.Debug(s.Log("client to gate:%v", utils.Marshal(msg)))
|
||||
log.Debug(s.Log("received client message:%v", msg.MsgId))
|
||||
}
|
||||
|
||||
// 向内部服务发送消息
|
||||
|
@ -15,6 +15,7 @@ var (
|
||||
)
|
||||
|
||||
func InitRedis() {
|
||||
log.Debug("init redis")
|
||||
var err error
|
||||
cfg := &config.Cfg.Redis
|
||||
UserRedis, err = db.InitRedis(cfg.Password, cfg.Host, cfg.Port, 0)
|
||||
@ -25,6 +26,7 @@ func InitRedis() {
|
||||
}
|
||||
|
||||
func InitDb() {
|
||||
log.Debug("init db")
|
||||
var err error
|
||||
cfg := &config.Cfg.Mysql
|
||||
UserDB, err = db.InitMysql(cfg.Username, cfg.Password, cfg.Host, cfg.Port, cfg.DbName)
|
||||
|
@ -24,18 +24,18 @@ type UserAccount struct {
|
||||
DeviceID string `gorm:"type:varchar(64);index"` // 设备ID
|
||||
LastLoginIP string `gorm:"type:varchar(45)"` // 最后登录IP(支持IPv6)
|
||||
LastLoginTime time.Time // 最后登录时间
|
||||
Status int `gorm:"type:tinyint;default:1"` // 账号状态 1-正常 2-冻结 3-封禁
|
||||
RegisterIP string `gorm:"type:varchar(45)"` // 注册IP
|
||||
RegisterTime time.Time `gorm:"default:CURRENT_TIMESTAMP"` // 注册时间
|
||||
Status int `gorm:"type:tinyint;default:1"` // 账号状态 1-正常 2-冻结 3-封禁
|
||||
RegisterIP string `gorm:"type:varchar(45)"` // 注册IP
|
||||
RegisterTime time.Time `gorm:"type:TIMESTAMP;default:CURRENT_TIMESTAMP"` // 注册时间
|
||||
}
|
||||
|
||||
// 玩家登录记录表
|
||||
type UserLoginLog struct {
|
||||
gorm.Model
|
||||
PlayerID uint `gorm:"index"` // 关联玩家ID
|
||||
LoginIP string `gorm:"type:varchar(45);not null"` // 登录IP
|
||||
LoginTime time.Time `gorm:"default:CURRENT_TIMESTAMP"` // 登录时间
|
||||
DeviceInfo string `gorm:"type:varchar(255)"` // 设备信息(JSON格式)
|
||||
PlayerID uint `gorm:"index"` // 关联玩家ID
|
||||
LoginIP string `gorm:"type:varchar(45);not null"` // 登录IP
|
||||
LoginTime time.Time `gorm:"type:TIMESTAMP;default:CURRENT_TIMESTAMP"` // 登录时间
|
||||
DeviceInfo string `gorm:"type:varchar(255)"` // 设备信息(JSON格式)
|
||||
LoginResult bool // 登录结果 true-成功 false-失败
|
||||
FailReason string `gorm:"type:varchar(100)"` // 失败原因
|
||||
}
|
||||
@ -101,14 +101,14 @@ func (s *UserLoginOp) RegisterNewUser(username, password, ip, deviceID string) (
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
user := UserAccount{
|
||||
Username: username,
|
||||
Password: string(hashedPassword),
|
||||
DeviceID: deviceID,
|
||||
RegisterIP: ip,
|
||||
Status: 1,
|
||||
LastLoginIP: ip,
|
||||
Username: username,
|
||||
Password: string(hashedPassword),
|
||||
DeviceID: deviceID,
|
||||
RegisterIP: ip,
|
||||
Status: 1,
|
||||
LastLoginIP: ip,
|
||||
LastLoginTime: time.Now(),
|
||||
}
|
||||
|
||||
if err := s.db.Create(&user).Error; err != nil {
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
|
||||
func (s *LoginService) initProcessor() {
|
||||
s.processor.RegisterMessages(processor.RegisterMetas{
|
||||
pb.MsgId_C2SUserLogoutId: {pb.C2SUserLogin{}, s.onLoginOrRegister},
|
||||
pb.MsgId_C2SUserLoginId: {pb.C2SUserLogin{}, s.onLoginOrRegister},
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,7 @@ type LoginService struct {
|
||||
}
|
||||
|
||||
func Init() {
|
||||
log.DebugF("init service begin id:%v, num:%v", config.Command.ServiceId, config.Command.ServiceNum)
|
||||
for i := 0; i < config.Command.ServiceNum; i++ {
|
||||
sid := config.Command.ServiceId + i
|
||||
if srv := newLoginService(sid); srv != nil {
|
||||
@ -94,8 +95,10 @@ func (s *LoginService) OnMessage(data []byte) error {
|
||||
}
|
||||
if req, err := s.processor.Unmarshal(iMsg.MsgId, iMsg.Msg); err == nil {
|
||||
err = s.processor.Dispatch(iMsg.MsgId, iMsg, req)
|
||||
} else {
|
||||
log.Error(err.Error())
|
||||
}
|
||||
//log.Debug(s.Log("on message:%v", string(msg)))
|
||||
//log.Debug(s.Log("received message:%v", iMsg.MsgId))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user