Compare commits

...

2 Commits

Author SHA1 Message Date
sk 5c38610b55 mongodb库升级包 2024-10-11 14:01:15 +08:00
sk 9a376b06c0 客户端升级统计 2024-10-11 10:57:29 +08:00
5 changed files with 256 additions and 25 deletions

View File

@ -133,10 +133,12 @@ type PlayerLoginEvent struct {
IsNew int32 //是否是新用户1是 0否 IsNew int32 //是否是新用户1是 0否
DeviceId string //设备id DeviceId string //设备id
ChannelId string //渠道id ChannelId string //渠道id
AppChannel string //包渠道
ClientVer int32 // 客户端版本
} }
func CreatePlayerLoginEvent(snid int32, channel, promoter, platform, city, os, ip string, createTime, func CreatePlayerLoginEvent(snid int32, channel, promoter, platform, city, os, ip string, createTime,
upgradeTime time.Time, isBindPhone int32, telephonePromoter int32, deviceId, channelId string) *PlayerLoginEvent { upgradeTime time.Time, isBindPhone int32, telephonePromoter int32, deviceId, channelId, appChannel string, clientVer int32) *PlayerLoginEvent {
isNew := int32(0) isNew := int32(0)
if createTime.Local().YearDay() == time.Now().Local().YearDay() && createTime.Local().Year() == time.Now().Local().Year() { if createTime.Local().YearDay() == time.Now().Local().YearDay() && createTime.Local().Year() == time.Now().Local().Year() {
isNew = 1 isNew = 1
@ -158,25 +160,11 @@ func CreatePlayerLoginEvent(snid int32, channel, promoter, platform, city, os, i
IsNew: isNew, IsNew: isNew,
DeviceId: deviceId, DeviceId: deviceId,
ChannelId: channelId, ChannelId: channelId,
AppChannel: appChannel,
ClientVer: clientVer,
} }
} }
func MarshalPlayerLoginEvent(source, snid int32, channel, promoter, platform, city, os, ip string,
createTime, upgradeTime time.Time, isBindPhone int32, telephonePromoter int32, deviceId, channelId string) (data string, err error) {
raw := &RabbitMQDataRaw{
Source: source,
Data: CreatePlayerLoginEvent(snid, channel, promoter, platform, city, os, ip, createTime,
upgradeTime, isBindPhone, telephonePromoter, deviceId, channelId),
}
d, e := json.Marshal(raw)
if e == nil {
data = string(d[:])
}
err = e
return
}
// 用户升级账号 // 用户升级账号
type PlayerBindPhoneEvent struct { type PlayerBindPhoneEvent struct {
SnId int32 //用户ID SnId int32 //用户ID

58
mongo/export.go Normal file
View File

@ -0,0 +1,58 @@
package mongo
import (
"errors"
"mongo.games.com/game/mongo/internal"
"mongo.games.com/goserver/core/logger"
)
type Config = internal.Config
type DatabaseConfig = internal.DatabaseConfig
type Collection = internal.Collection
var _manager *internal.Manager
var _conf *internal.Config
// GetConfig 获取配置
func GetConfig() *Config {
return _conf
}
// Init 初始化
func Init(conf *Config) error {
_conf = conf
_manager = internal.NewManager(_conf)
return nil
}
// Restart 重启
func Restart() {
if _manager == nil {
logger.Logger.Errorf("mongo manager is nil, please call Init() first")
return
}
_manager.Restart(_conf)
}
// GetGlobalCollection 获取全局库
// database: 数据库名称
// collection: 集合名称
func GetGlobalCollection(database, collection string) (*Collection, error) {
if _manager == nil {
return nil, errors.New("mongo manager is nil, please call Init() first")
}
return _manager.GetCollection("global", database, collection)
}
// GetCollection 获取平台库
// platform: 平台id
// database: 数据库名称
// collection: 集合名称
func GetCollection(platform, database, collection string) (*Collection, error) {
if _manager == nil {
return nil, errors.New("mongo manager is nil, please call Init() first")
}
return _manager.GetCollection(platform, database, collection)
}

185
mongo/internal/mongo.go Normal file
View File

@ -0,0 +1,185 @@
package internal
import (
"context"
"fmt"
"sync"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"mongo.games.com/goserver/core/logger"
)
type Config struct {
Global map[string]*DatabaseConfig
Platforms map[string]map[string]*DatabaseConfig
}
type DatabaseConfig struct {
HostName string // 主机地址
HostPort int32 // 端口
Database string // 数据库名
Username string // 用户名
Password string // 密码
Options string // 配置
}
type Collection struct {
Database *Database
*mongo.Collection
}
type Database struct {
*DatabaseConfig
Client *mongo.Client
Database *mongo.Database
Collection sync.Map
}
func (d *Database) Connect() error {
if d.DatabaseConfig == nil {
err := fmt.Errorf("mongo Connect error, DatabaseConifg not found")
logger.Logger.Error(err)
return err
}
login := ""
if d.DatabaseConfig.Username != "" {
login = d.DatabaseConfig.Username + ":" + d.DatabaseConfig.Password + "@"
}
host := d.DatabaseConfig.HostName
if d.DatabaseConfig.HostName == "" {
host = "127.0.0.1"
}
port := d.DatabaseConfig.HostPort
if d.DatabaseConfig.HostPort == 0 {
port = 27017
}
myOptions := d.DatabaseConfig.Options
if myOptions != "" {
myOptions = "?" + myOptions
}
s := fmt.Sprintf("mongodb://%s%s:%d/admin%s", login, host, port, myOptions)
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(s))
if err != nil {
logger.Logger.Errorf("mongo Connect %v error: %v config:%+v", s, err, *d.DatabaseConfig)
return err
}
logger.Logger.Tracef("mongo connect success %+v", *d.DatabaseConfig)
d.Client = client
d.Database = client.Database(d.DatabaseConfig.Database)
return nil
}
func (d *Database) GetCollection(name string) (*Collection, error) {
if d.Database == nil {
err := fmt.Errorf("mongo GetCollection error, collection:%v, database is nil", name)
logger.Logger.Error(err)
return nil, err
}
v, ok := d.Collection.Load(name)
if !ok {
v = &Collection{
Database: d,
Collection: d.Database.Collection(name),
}
d.Collection.Store(name, v)
}
c, _ := v.(*Collection)
return c, nil
}
type Manager struct {
conf *Config
global *sync.Map // 内部库名称:Database
platforms *sync.Map // 平台id:内部库名称:Database
}
func (m *Manager) GetCollection(key, database, collection string) (*Collection, error) {
switch key {
case "global":
v, ok := m.global.Load(database)
if !ok {
db := &Database{
DatabaseConfig: m.conf.Global[database],
Collection: sync.Map{},
}
if err := db.Connect(); err != nil {
return nil, err
}
v = db
m.global.Store(database, v)
}
d, _ := v.(*Database)
return d.GetCollection(collection)
default:
var mp *sync.Map
v, ok := m.platforms.Load(key) // 平台id
if !ok {
mp = new(sync.Map)
m.platforms.Store(key, mp)
} else {
mp = v.(*sync.Map)
}
v, ok = mp.Load(database)
if !ok {
db := &Database{
DatabaseConfig: m.conf.Platforms[key][database],
Collection: sync.Map{},
}
if err := db.Connect(); err != nil {
return nil, err
}
v = db
mp.Store(database, v)
}
d, _ := v.(*Database)
return d.GetCollection(collection)
}
}
func (m *Manager) Restart(conf *Config) {
logger.Logger.Infof("mongo manager restart...")
global := m.global
platforms := m.platforms
time.AfterFunc(time.Minute, func() {
logger.Logger.Infof("mongo manager restart close...")
global.Range(func(key, value any) bool {
if v, ok := value.(*Database); ok {
v.Client.Disconnect(nil)
}
return true
})
platforms.Range(func(key, value any) bool {
if v, ok := value.(*sync.Map); ok {
v.Range(func(key, value any) bool {
if v, ok := value.(*Database); ok {
v.Client.Disconnect(nil)
}
return true
})
}
return true
})
})
m.conf = conf
m.global = &sync.Map{}
m.platforms = &sync.Map{}
}
func (m *Manager) GetConfig() *Config {
return m.conf
}
func NewManager(conf *Config) *Manager {
return &Manager{
conf: conf,
global: &sync.Map{},
platforms: &sync.Map{},
}
}

View File

@ -1983,18 +1983,18 @@ func CSPlayerData(s *netlib.Session, packetid int, data interface{}, sid int64)
} }
} }
// 老版本升级 // 客户端升级奖励
clientUpgradeConfig := PlatformMgrSingleton.GetConfig(p.Platform).ClientUpgrade clientUpgradeConfig := PlatformMgrSingleton.GetConfig(p.Platform).ClientUpgrade
if clientUpgradeConfig != nil && clientUpgradeConfig.GetOn() == common.On && if clientUpgradeConfig != nil && clientUpgradeConfig.GetOn() == common.On &&
(len(model.GameParamData.ClientVersionChannel) == 0 || slices.Contains(model.GameParamData.ClientVersionChannel, p.LastChannel)) { (len(model.GameParamData.ClientVersionChannel) == 0 || slices.Contains(model.GameParamData.ClientVersionChannel, p.LastChannel)) {
if p.ClientVer < model.GameParamData.ClientVersion { if p.ClientVer < model.GameParamData.ClientVersion && cspl.GetClientVer() == model.GameParamData.ClientVersion {
if cspl.GetClientVer() == model.GameParamData.ClientVersion { AddMailClientUpgrade(p.SnId, clientUpgradeConfig.GetReward())
// 升级了
p.ClientVer = model.GameParamData.ClientVersion
AddMailClientUpgrade(p.SnId, clientUpgradeConfig.GetReward())
}
} }
} }
// 客户端升级
if p.ClientVer < cspl.GetClientVer() && cspl.GetClientVer() <= model.GameParamData.ClientVersion {
p.ClientVer = cspl.GetClientVer()
}
} }
// 有缓存数据 // 有缓存数据

View File

@ -2940,7 +2940,7 @@ func (this *Player) ReportLoginEvent() {
} }
mq.Write(model.GenerateLogin(model.CreatePlayerLoginEvent(this.SnId, mq.Write(model.GenerateLogin(model.CreatePlayerLoginEvent(this.SnId,
this.Channel, this.BeUnderAgentCode, this.Platform, this.City, this.DeviceOS, this.Ip, this.Channel, this.BeUnderAgentCode, this.Platform, this.City, this.DeviceOS, this.Ip,
this.CreateTime, this.UpgradeTime, isBindPhone, this.TelephonePromoter, this.DeviceId, this.ChannelId))) this.CreateTime, this.UpgradeTime, isBindPhone, this.TelephonePromoter, this.DeviceId, this.ChannelId, this.AppChannel, this.ClientVer)))
//登录通知 //登录通知
//ActMonitorMgrSington.SendActMonitorEvent(ActState_Login, this.SnId, this.Name, this.Platform, //ActMonitorMgrSington.SendActMonitorEvent(ActState_Login, this.SnId, this.Name, this.Platform,
// 0, 0, "", 0) // 0, 0, "", 0)