package main

import (
	"mongo.games.com/game/model"
	webapi_proto "mongo.games.com/game/protocol/webapi"
	"mongo.games.com/goserver/core/module"
	"sort"
	"time"
)

var ChessRankMgrSington = &ChessRankMgr{
	ChessRankcfg: make(map[string]map[int32]*webapi_proto.ChessRankcfgData),
	ChessRankArr: make(map[string]map[int32][]int32),
}

type ChessRankMgr struct {
	ChessRankcfg map[string]map[int32]*webapi_proto.ChessRankcfgData
	ChessRankArr map[string]map[int32][]int32
}

func (this *ChessRankMgr) ModuleName() string {
	return "ChessRankMgr"
}

func (this *ChessRankMgr) Init() {
	if !model.GameParamData.UseEtcd {
		// 后台说现在没有不走ETCD情况~
	} else {
		EtcdMgrSington.InitUpdateChessRankcfg()
	}
}

func (this *ChessRankMgr) UpdateChessRankConfig(cfg *webapi_proto.ChessRankcfgData) {
	if this.ChessRankcfg[cfg.GetPlatform()] == nil {
		this.ChessRankcfg[cfg.GetPlatform()] = make(map[int32]*webapi_proto.ChessRankcfgData)
	}
	this.ChessRankcfg[cfg.GetPlatform()][cfg.GetGameId()] = cfg

	sort.Slice(cfg.Datas, func(i, j int) bool {
		return cfg.GetDatas()[i].Score < cfg.GetDatas()[j].Score
	})

	if this.ChessRankArr[cfg.GetPlatform()] == nil {
		this.ChessRankArr[cfg.GetPlatform()] = make(map[int32][]int32)
	}
	var arr []int32
	for _, v := range cfg.GetDatas() {
		arr = append(arr, v.GetScore())
	}
	this.ChessRankArr[cfg.GetPlatform()][cfg.GetGameId()] = arr
}

func (this *ChessRankMgr) GetChessRankConfig(platform string, gameId int32) *webapi_proto.ChessRankcfgData {
	return this.ChessRankcfg[platform][gameId]
}

func (this *ChessRankMgr) GetChessRankArr(platform string, gameId int32) []int32 {
	return this.ChessRankArr[platform][gameId]
}

func (this *ChessRankMgr) Update() {

}

func (this *ChessRankMgr) Shutdown() {
	module.UnregisteModule(this)
}

func init() {
	module.RegisteModule(ChessRankMgrSington, time.Second, 0)
}
