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