package base

import (
	"time"

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

	server_proto "mongo.games.com/game/protocol/server"
	"mongo.games.com/game/srvdata"
)

var SceneMgrSingleton = &SceneMgr{
	Scenes:          make(map[int32]IScene),
	sceneDBGameFree: make(map[int32]*server_proto.DB_GameFree),
}

type SceneMgr struct {
	Scenes          map[int32]IScene
	sceneDBGameFree map[int32]*server_proto.DB_GameFree
}

func (sm *SceneMgr) AddScene(s IScene) {
	sm.Scenes[s.GetRoomId()] = s
}

func (sm *SceneMgr) DelScene(sceneId int32) {
	delete(sm.Scenes, sceneId)
	delete(sm.sceneDBGameFree, sceneId)
}

func (sm *SceneMgr) GetScene(sceneId int32) IScene {
	if s, exist := sm.Scenes[sceneId]; exist {
		return s
	}
	return nil
}

func (sm *SceneMgr) UpdateSceneDBGameFree(sceneId int32, dbGameFree *server_proto.DB_GameFree) {
	sm.sceneDBGameFree[sceneId] = dbGameFree
}

func (sm *SceneMgr) GetSceneDBGameFree(sceneId, gamefreeId int32) *server_proto.DB_GameFree {
	if data, exist := sm.sceneDBGameFree[sceneId]; exist {
		return data
	}

	return srvdata.PBDB_GameFreeMgr.GetData(gamefreeId)
}

// //////////////////////////////////////////////////////////////////
// / Module Implement [beg]
// //////////////////////////////////////////////////////////////////
func (sm *SceneMgr) Update() {
	for _, s := range sm.Scenes {
		s.Update(time.Now().UnixMilli()) // 毫秒时间戳
	}
	return
}

func (sm *SceneMgr) Init() {
}

func (sm *SceneMgr) ModuleName() string {
	return "SceneMgr-module"
}

func (sm *SceneMgr) Shutdown() {
	module.UnregisteModule(sm)
}

func init() {
	module.RegisteModule(SceneMgrSingleton, time.Millisecond*100, 0)
}
