72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
package config
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"game/common/config"
|
|
"game/common/constant"
|
|
"github.com/fox/fox/db"
|
|
"github.com/fox/fox/log"
|
|
"github.com/go-redis/redis/v8"
|
|
)
|
|
|
|
var Command *config.Command
|
|
var Cfg *config.Common[MatchConfig]
|
|
|
|
type MatchConfig struct {
|
|
//Color *game.ColorConfig
|
|
}
|
|
|
|
func InitLog() {
|
|
log.Open("./log/match.log", log.DebugL)
|
|
log.Info("")
|
|
log.Info("")
|
|
log.Info("")
|
|
log.Info("-----init log success-----")
|
|
}
|
|
|
|
func LoadConfig(GitCommit, GitBranch, BuildDate string) {
|
|
Command = config.ParseCommand()
|
|
rdb, err := db.InitRedis(Command.RedisPassword, Command.RedisHost, Command.RedisPort, constant.Redis0Config)
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
return
|
|
}
|
|
defer func() { _ = rdb.Close() }()
|
|
Cfg, err = config.LoadCommonConfig[MatchConfig](rdb, GitCommit, GitBranch, BuildDate)
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
return
|
|
}
|
|
log.DebugF("load common config success")
|
|
|
|
//Cfg.Special.Color, err = LoadGameConfig[game.ColorConfig](rdb, game.ColorKey)
|
|
}
|
|
|
|
type Value[T any] struct {
|
|
Val T
|
|
}
|
|
|
|
func LoadGameConfig[T any](rd *redis.Client, gameKey string) (*T, error) {
|
|
s, err := rd.Get(context.Background(), gameKey).Result()
|
|
if err != nil && !errors.Is(err, redis.Nil) {
|
|
log.ErrorF("init config:%v", err)
|
|
return nil, err
|
|
}
|
|
if s == "" {
|
|
return nil, fmt.Errorf("config:%s not found from redis", gameKey)
|
|
}
|
|
var val Value[T]
|
|
if err = json.Unmarshal([]byte(s), &val.Val); err != nil {
|
|
log.ErrorF("init %v config:%v", gameKey, err)
|
|
return nil, err
|
|
}
|
|
return &val.Val, nil
|
|
}
|
|
|
|
//func GetColorConfig() *game.ColorConfig {
|
|
// return Cfg.Special.Color
|
|
//}
|