package main

import (
	"log"
	"net"
	"net/http"
	"net/rpc"

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

	_ "games.yol.com/win88"
	_ "mongo.games.com/game/dbproxy/mq"

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

var rabbitMqConsumer *mq.RabbitMQConsumer

func init() {
	core.RegisteHook(core.HOOK_BEFORE_START, func() error {
		model.InitGameParam()
		rabbitMqConsumer = mq.NewRabbitMQConsumer(common.CustomConfig.GetString("RabbitMQURL"), rabbitmq.Exchange{Name: common.CustomConfig.GetString("RMQExchange"), Durable: true})
		if rabbitMqConsumer != nil {
			rabbitMqConsumer.Start()
		}

		//尝试初始化
		svc.GetOnePlayerIdFromBucket()
		return nil
	})

	core.RegisteHook(core.HOOK_AFTER_STOP, func() error {
		if rabbitMqConsumer != nil {
			rabbitMqConsumer.Stop()
		}
		model.ShutdownRPClient()
		return nil
	})
}

func main() {
	defer core.ClosePackages()
	core.LoadPackages("config.json")

	rpc.HandleHTTP() // 采用http协议作为rpc载体
	lis, err := net.Listen(common.CustomConfig.GetString("MgoRpcCliNet"), common.CustomConfig.GetString("MgoRpcCliAddr"))
	if err != nil {
		log.Fatalln("fatal error: ", err)
	}
	go http.Serve(lis, nil)

	waitor := module.Start()
	waitor.Wait("main()")
}
