package base

import (
	"time"

	"mongo.games.com/goserver/core"
	"mongo.games.com/goserver/core/broker/rabbitmq"

	"mongo.games.com/game/common"
	"mongo.games.com/game/model"
	"mongo.games.com/game/mq"
)

var RabbitMQPublisher *mq.RabbitMQPublisher

func init() {
	model.InitGameParam()
	model.InitFishingParam()
	model.InitNormalParam()
	model.InitGMAC()

	core.RegisteHook(core.HOOK_BEFORE_START, func() error {
		model.StartupRPClient(common.CustomConfig.GetString("MgoRpcCliNet"), common.CustomConfig.GetString("MgoRpcCliAddr"), time.Duration(common.CustomConfig.GetInt("MgoRpcCliReconnInterV"))*time.Second)
		model.InitGameKVData()

		//rabbitmq打开链接
		RabbitMQPublisher = mq.NewRabbitMQPublisher(common.CustomConfig.GetString("RabbitMQURL"), rabbitmq.Exchange{Name: common.CustomConfig.GetString("RMQExchange"), Durable: true}, common.CustomConfig.GetInt("RMQPublishBacklog"))
		if RabbitMQPublisher != nil {
			err := RabbitMQPublisher.Start()
			if err != nil {
				panic(err)
			}
		}

		return nil
	})

	core.RegisteHook(core.HOOK_AFTER_STOP, func() error {
		//关闭rabbitmq连接
		if RabbitMQPublisher != nil {
			RabbitMQPublisher.Stop()
		}
		return nil
	})
}
