package main

import (
	"strings"

	"go.etcd.io/etcd/client/v3"

	"mongo.games.com/goserver/core/logger"

	"mongo.games.com/game/dbproxy/mongo"
	"mongo.games.com/game/etcd"
	"mongo.games.com/game/protocol/webapi"
)

func init() {
	etcd.Register(etcd.ETCDKEY_SYS_PLT_DBCFG_PREFIX, webapi.PlatformDbConfig{}, func(completeKey string, isInit bool, event *clientv3.Event, data interface{}) {
		if event.Type == clientv3.EventTypeDelete {
			return
		}

		config, ok := data.(*webapi.PlatformDbConfig)
		if !ok {
			logger.Logger.Errorf("etcd completeKey:%s, data type error", completeKey)
			return
		}

		s := strings.TrimPrefix(completeKey, etcd.ETCDKEY_SYS_PLT_DBCFG_PREFIX)
		arr := strings.Split(s, "/")
		if len(arr) >= 1 {
			pltId := arr[0]
			//用户库
			mongo.MgoSessionMgrSington.UptCfgWithEtcd(pltId, "user", config.MongoDb)
			//日志库
			mongo.MgoSessionMgrSington.UptCfgWithEtcd(pltId, "log", config.MongoDbLog)
		}
	})
}
