diff --git a/.gitignore b/.gitignore index ab91161e..3610bd09 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,6 @@ node/api/*.pb.go # Local game data config table file gdconf/game_data_config/csv/*.csv + +# Deploy +docker/docker-compose.yaml diff --git a/Makefile b/Makefile index 059d546b..a8be99e7 100644 --- a/Makefile +++ b/Makefile @@ -39,6 +39,7 @@ docker_clean: # 复制配置模板等文件 .PHONY: docker_config docker_config: + mkdir -p ./docker && cp -rf ./docker-compose.yaml ./docker/ mkdir -p ./docker/node/bin && cp -rf ./cmd/node/* ./docker/node/bin/ mkdir -p ./docker/dispatch/bin && cp -rf ./cmd/dispatch/* ./docker/dispatch/bin/ mkdir -p ./docker/gate/bin && cp -rf ./cmd/gate/* ./docker/gate/bin/ diff --git a/README.md b/README.md index 0e26dab3..b3f1f270 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ make docker_build # 构建镜像 ```shell make gen_csv # 生成配置表 +cd docker # 启动前请先确保各服务器的配置文件正确(如docker/node/bin/application.toml) docker-compose up -d # 启动服务器 ``` diff --git a/cmd/dispatch/application.toml b/cmd/dispatch/application.toml index 90073e6c..3f13c154 100644 --- a/cmd/dispatch/application.toml +++ b/cmd/dispatch/application.toml @@ -9,5 +9,9 @@ max_size = 10485760 [database] url = "mongodb://mongo:27017" +[redis] +addr = "redis:6379" +password = "" + [mq] nats_url = "nats://nats:4222" diff --git a/dispatch/dao/account_dao.go b/dispatch/dao/account_mongo.go similarity index 96% rename from dispatch/dao/account_dao.go rename to dispatch/dao/account_mongo.go index 62c64146..234a1ee5 100644 --- a/dispatch/dao/account_dao.go +++ b/dispatch/dao/account_mongo.go @@ -12,7 +12,7 @@ import ( "go.mongodb.org/mongo-driver/mongo" ) -func (d *Dao) GetNextAccountId() (uint64, error) { +func (d *Dao) _GetNextAccountId() (uint64, error) { db := d.db.Collection("account_id_counter") find := db.FindOne(context.TODO(), bson.D{{"_id", "default"}}) item := new(model.AccountIDCounter) @@ -50,7 +50,7 @@ func (d *Dao) GetNextAccountId() (uint64, error) { return item.AccountID, nil } -func (d *Dao) GetNextYuanShenUid() (uint64, error) { +func (d *Dao) _GetNextYuanShenUid() (uint64, error) { db := d.db.Collection("player_id_counter") find := db.FindOne(context.TODO(), bson.D{{"_id", "default"}}) item := new(model.PlayerIDCounter) diff --git a/dispatch/dao/account_redis.go b/dispatch/dao/account_redis.go new file mode 100644 index 00000000..00e780af --- /dev/null +++ b/dispatch/dao/account_redis.go @@ -0,0 +1,46 @@ +package dao + +import ( + "context" +) + +const RedisPlayerKeyPrefix = "HK4E" + +const ( + AccountIdRedisKey = "AccountId" + AccountIdBegin uint64 = 1 + YuanShenUidRedisKey = "YuanShenUid" + YuanShenUidBegin uint64 = 100000001 +) + +func (d *Dao) GetNextAccountId() (uint64, error) { + return d.redisInc(RedisPlayerKeyPrefix + ":" + AccountIdRedisKey) +} + +func (d *Dao) GetNextYuanShenUid() (uint64, error) { + return d.redisInc(RedisPlayerKeyPrefix + ":" + YuanShenUidRedisKey) +} + +func (d *Dao) redisInc(keyName string) (uint64, error) { + exist, err := d.redis.Exists(context.TODO(), keyName).Result() + if err != nil { + return 0, err + } + if exist == 0 { + var value uint64 = 0 + if keyName == AccountIdRedisKey { + value = AccountIdBegin + } else if keyName == YuanShenUidRedisKey { + value = YuanShenUidBegin + } + err := d.redis.Set(context.TODO(), keyName, value, 0).Err() + if err != nil { + return 0, err + } + } + id, err := d.redis.Incr(context.TODO(), keyName).Result() + if err != nil { + return 0, err + } + return uint64(id), nil +} diff --git a/dispatch/dao/dao.go b/dispatch/dao/dao.go index 5de09cd8..6e326256 100644 --- a/dispatch/dao/dao.go +++ b/dispatch/dao/dao.go @@ -6,6 +6,7 @@ import ( "hk4e/common/config" "hk4e/pkg/logger" + "github.com/go-redis/redis/v8" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readpref" @@ -14,6 +15,7 @@ import ( type Dao struct { mongo *mongo.Client db *mongo.Database + redis *redis.Client } func NewDao() (r *Dao) { @@ -31,6 +33,18 @@ func NewDao() (r *Dao) { } r.mongo = client r.db = client.Database("dispatch_hk4e") + r.redis = redis.NewClient(&redis.Options{ + Addr: config.CONF.Redis.Addr, + Password: config.CONF.Redis.Password, + DB: 0, + PoolSize: 10, + MinIdleConns: 1, + }) + err = r.redis.Ping(context.TODO()).Err() + if err != nil { + logger.Error("redis ping error: %v", err) + return nil + } return r } @@ -39,4 +53,8 @@ func (d *Dao) CloseDao() { if err != nil { logger.Error("mongo close error: %v", err) } + err = d.redis.Close() + if err != nil { + logger.Error("redis close error: %v", err) + } } diff --git a/docker-compose.yaml b/docker-compose.yaml index cce9cc66..b01ce59d 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -11,7 +11,7 @@ services: - /etc/localtime:/etc/localtime - /etc/timezone:/etc/timezone - /usr/share/zoneinfo:/usr/share/zoneinfo - - ./docker/node/bin/application.toml:/node/application.toml + - ./node/bin/application.toml:/node/application.toml deploy: resources: limits: @@ -30,9 +30,9 @@ services: - /etc/localtime:/etc/localtime - /etc/timezone:/etc/timezone - /usr/share/zoneinfo:/usr/share/zoneinfo - - ./docker/dispatch/bin/application.toml:/dispatch/application.toml - - ./docker/dispatch/bin/key:/dispatch/key - - ./docker/dispatch/bin/static:/dispatch/static + - ./dispatch/bin/application.toml:/dispatch/application.toml + - ./dispatch/bin/key:/dispatch/key + - ./dispatch/bin/static:/dispatch/static depends_on: - node_services deploy: @@ -54,8 +54,8 @@ services: - /etc/localtime:/etc/localtime - /etc/timezone:/etc/timezone - /usr/share/zoneinfo:/usr/share/zoneinfo - - ./docker/gate/bin/application.toml:/gate/application.toml - - ./docker/gate/bin/key:/gate/key + - ./gate/bin/application.toml:/gate/application.toml + - ./gate/bin/key:/gate/key depends_on: - dispatch_services deploy: @@ -74,7 +74,7 @@ services: - /etc/localtime:/etc/localtime - /etc/timezone:/etc/timezone - /usr/share/zoneinfo:/usr/share/zoneinfo - - ./docker/fight/bin/application.toml:/fight/application.toml + - ./fight/bin/application.toml:/fight/application.toml depends_on: - gate_services deploy: @@ -93,7 +93,7 @@ services: - /etc/localtime:/etc/localtime - /etc/timezone:/etc/timezone - /usr/share/zoneinfo:/usr/share/zoneinfo - - ./docker/pathfinding/bin/application.toml:/pathfinding/application.toml + - ./pathfinding/bin/application.toml:/pathfinding/application.toml depends_on: - fight_services deploy: @@ -112,8 +112,8 @@ services: - /etc/localtime:/etc/localtime - /etc/timezone:/etc/timezone - /usr/share/zoneinfo:/usr/share/zoneinfo - - ./docker/gs/bin/application.toml:/gs/application.toml - - ./gdconf/game_data_config:/gs/game_data_config + - ./gs/bin/application.toml:/gs/application.toml + - ../gdconf/game_data_config:/gs/game_data_config depends_on: - pathfinding_services deploy: @@ -134,7 +134,7 @@ services: - /etc/localtime:/etc/localtime - /etc/timezone:/etc/timezone - /usr/share/zoneinfo:/usr/share/zoneinfo - - ./docker/gm/bin/application.toml:/gm/application.toml + - ./gm/bin/application.toml:/gm/application.toml depends_on: - gs_services deploy: diff --git a/gdconf/game_data_config.go b/gdconf/game_data_config.go index 61a81156..53e65644 100644 --- a/gdconf/game_data_config.go +++ b/gdconf/game_data_config.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "strings" + "time" "hk4e/common/config" "hk4e/pkg/logger" @@ -15,6 +16,7 @@ import ( // 游戏数据配置表 var CONF *GameDataConfig = nil +var CONF_RELOAD *GameDataConfig = nil type GameDataConfig struct { // 配置表路径前缀 @@ -44,8 +46,22 @@ type GameDataConfig struct { func InitGameDataConfig() { CONF = new(GameDataConfig) + startTime := time.Now().Unix() CONF.loadAll() - logger.Info("load all game data config finish") + endTime := time.Now().Unix() + logger.Info("load all game data config finish, cost: %v(s)", endTime-startTime) +} + +func ReloadGameDataConfig() { + CONF_RELOAD = new(GameDataConfig) + startTime := time.Now().Unix() + CONF_RELOAD.loadAll() + endTime := time.Now().Unix() + logger.Info("reload all game data config finish, cost: %v(s)", endTime-startTime) +} + +func ReplaceGameDataConfig() { + CONF = CONF_RELOAD } func (g *GameDataConfig) loadAll() { diff --git a/gs/game/local_event_manager.go b/gs/game/local_event_manager.go index 09b21db5..05c5f0f3 100644 --- a/gs/game/local_event_manager.go +++ b/gs/game/local_event_manager.go @@ -4,6 +4,7 @@ import ( "time" "hk4e/common/mq" + "hk4e/gdconf" "hk4e/gs/model" "hk4e/pkg/logger" "hk4e/pkg/object" @@ -17,6 +18,8 @@ const ( RunUserCopyAndSave // 执行一次在线玩家内存数据复制到数据库写入协程 ExitRunUserCopyAndSave UserOfflineSaveToDbFinish + ReloadGameDataConfig + ReloadGameDataConfigFinish ) type LocalEvent struct { @@ -134,5 +137,19 @@ func (l *LocalEventManager) LocalEventHandle(localEvent *LocalEvent) { logger.Info("user change gs notify to gate, uid: %v, gate appid: %v, gs appid: %v, host uid: %v", playerOfflineInfo.Player.PlayerID, playerOfflineInfo.Player.GateAppId, gsAppId, playerOfflineInfo.ChangeGsInfo.JoinHostUserId) } + case ReloadGameDataConfig: + go func() { + defer func() { + if err := recover(); err != nil { + logger.Error("reload game data config error: %v", err) + } + }() + gdconf.ReloadGameDataConfig() + LOCAL_EVENT_MANAGER.localEventChan <- &LocalEvent{ + EventId: ReloadGameDataConfigFinish, + } + }() + case ReloadGameDataConfigFinish: + gdconf.ReplaceGameDataConfig() } }