package base

import (
	"time"

	"mongo.games.com/goserver/core/logger"
	"mongo.games.com/goserver/core/module"
	"mongo.games.com/goserver/core/netlib"
)

const (
	// RobotSessionStartId 机器人session开始id
	RobotSessionStartId = 100000000
)

var (
	BenchMarkModule     = &BenchMark{}
	WaitConnectSessions []*netlib.SessionConfig
)

func NewSessionConfig() *netlib.SessionConfig {
	BenchMarkModule.idx++
	cfg := Config.Connects
	cfg.Id = BenchMarkModule.idx
	cfg.Init()
	return &cfg
}

type BenchMark struct {
	idx int
}

func (m *BenchMark) ModuleName() string {
	return "benchmark-module"
}

func (m *BenchMark) Init() {
	m.idx = RobotSessionStartId
	for i := 0; i < Config.Count; i++ {
		cfg := NewSessionConfig()
		logger.Logger.Info("waite connect session id=", cfg.Id)
		WaitConnectSessions = append(WaitConnectSessions, cfg)
	}
}

// Update 机器开始连接游戏服务器
func (m *BenchMark) Update() {
	n := len(WaitConnectSessions)
	if n > 0 {
		config := WaitConnectSessions[n-1]
		WaitConnectSessions = WaitConnectSessions[:n-1]
		if err := netlib.Connect(config); err != nil {
			logger.Logger.Error("netlib.Connect error", err)
		}
	}
}

func (m *BenchMark) Shutdown() {
	module.UnregisteModule(m)
}

func init() {
	module.RegisteModule(BenchMarkModule, time.Millisecond, 1)
}
