233 lines
6.1 KiB
Go
233 lines
6.1 KiB
Go
package base
|
||
|
||
import (
|
||
"time"
|
||
|
||
"mongo.games.com/goserver/core/logger"
|
||
"mongo.games.com/goserver/core/module"
|
||
"mongo.games.com/goserver/core/netlib"
|
||
"mongo.games.com/goserver/srvlib/action"
|
||
srvlibproto "mongo.games.com/goserver/srvlib/protocol"
|
||
|
||
"mongo.games.com/game/common"
|
||
"mongo.games.com/game/proto"
|
||
"mongo.games.com/game/protocol/gamehall"
|
||
"mongo.games.com/game/protocol/server"
|
||
)
|
||
|
||
var SceneMgrSington = &SceneMgr{
|
||
scenes: make(map[int]*Scene),
|
||
scenesByGame: make(map[int]map[int]*Scene),
|
||
scenesByGameFree: make(map[int32]map[int]*Scene),
|
||
PlatformScene: make(map[string]bool),
|
||
}
|
||
|
||
type SceneMgr struct {
|
||
scenes map[int]*Scene // 房间id
|
||
scenesByGame map[int]map[int]*Scene // 游戏id:房间id
|
||
scenesByGameFree map[int32]map[int]*Scene // 场次id:房间id
|
||
lastSendJackPot time.Time //
|
||
PlatformScene map[string]bool //
|
||
}
|
||
|
||
type CreateSceneParam struct {
|
||
Session *netlib.Session
|
||
*server.WGCreateScene
|
||
}
|
||
|
||
func (this *SceneMgr) CreateScene(args *CreateSceneParam) *Scene {
|
||
scene := NewScene(args)
|
||
if scene == nil {
|
||
logger.Logger.Error("(this *SceneMgr) CreateScene, scene == nil")
|
||
return nil
|
||
}
|
||
|
||
platform := args.GetPlatform()
|
||
gameId := args.GetGameId()
|
||
gameFreeId := args.GetDBGameFree().GetId()
|
||
// 平台标记
|
||
this.scenes[int(scene.SceneId)] = scene
|
||
if _, ok := this.PlatformScene[platform]; !ok {
|
||
this.PlatformScene[platform] = true
|
||
}
|
||
// 游戏id索引
|
||
if ss, exist := this.scenesByGame[int(gameId)]; exist {
|
||
ss[int(scene.SceneId)] = scene
|
||
} else {
|
||
ss = make(map[int]*Scene)
|
||
ss[int(scene.SceneId)] = scene
|
||
this.scenesByGame[int(gameId)] = ss
|
||
}
|
||
// 场次id索引
|
||
if ss, exist := this.scenesByGameFree[gameFreeId]; exist {
|
||
ss[int(scene.SceneId)] = scene
|
||
} else {
|
||
ss = make(map[int]*Scene)
|
||
ss[int(scene.SceneId)] = scene
|
||
this.scenesByGameFree[gameFreeId] = ss
|
||
}
|
||
scene.OnStart()
|
||
logger.Logger.Infof("(this *SceneMgr) CreateScene,New scene,id:[%d] replaycode:[%v]", scene.SceneId, args.GetReplayCode())
|
||
|
||
return scene
|
||
}
|
||
|
||
func (this *SceneMgr) DestroyScene(sceneId int) {
|
||
if scene, exist := this.scenes[sceneId]; exist {
|
||
scene.OnStop()
|
||
// 游戏id
|
||
if ss, exist := this.scenesByGame[scene.GetGameId()]; exist {
|
||
delete(ss, int(scene.SceneId))
|
||
}
|
||
// 场次id
|
||
if ss, exist := this.scenesByGameFree[scene.GetGameFreeId()]; exist {
|
||
delete(ss, int(scene.SceneId))
|
||
}
|
||
// 房间id
|
||
delete(this.scenes, sceneId)
|
||
logger.Logger.Infof("(this *SceneMgr) DestroyScene, sceneid = %v", sceneId)
|
||
}
|
||
}
|
||
|
||
func (this *SceneMgr) GetScene(sceneId int) *Scene {
|
||
if s, exist := this.scenes[sceneId]; exist {
|
||
return s
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (this *SceneMgr) GetSceneByGameId(platform string, gameId int32) []*Scene {
|
||
var ss []*Scene
|
||
if data, ok := this.scenesByGame[int(gameId)]; ok {
|
||
for _, scene := range data {
|
||
if scene.Platform == platform {
|
||
ss = append(ss, scene)
|
||
}
|
||
}
|
||
}
|
||
return ss
|
||
}
|
||
|
||
func (this *SceneMgr) JackPotSync(platform string, gameIds ...int32) {
|
||
for _, gameId := range gameIds {
|
||
ss := this.GetSceneByGameId(platform, gameId)
|
||
if ss != nil {
|
||
mgs := make(map[*netlib.Session][]*srvlibproto.MCSessionUnion)
|
||
pack := &gamehall.SCHundredSceneGetGameJackpot{}
|
||
for _, s := range ss {
|
||
val := s.sp.GetJackPotVal(s)
|
||
if val > 0 {
|
||
jpfi := &gamehall.GameJackpotFundInfo{
|
||
GameFreeId: proto.Int32(s.GetGameFreeId()),
|
||
JackPotFund: proto.Int64(val),
|
||
}
|
||
pack.GameJackpotFund = append(pack.GameJackpotFund, jpfi)
|
||
}
|
||
for _, p := range s.Players {
|
||
if p != nil && p.gateSess != nil && p.IsOnLine() {
|
||
mgs[p.gateSess] = append(mgs[p.gateSess], &srvlibproto.MCSessionUnion{
|
||
Mccs: &srvlibproto.MCClientSession{
|
||
SId: proto.Int64(p.sid),
|
||
},
|
||
})
|
||
}
|
||
}
|
||
}
|
||
if len(pack.GameJackpotFund) == 0 || len(mgs) == 0 {
|
||
continue
|
||
}
|
||
proto.SetDefaults(pack)
|
||
//以平台为标识向该平台内所有玩家广播奖池变动消息,游戏内外的玩家可监听该消息,减少由gamesrv向worldsrv转发这一步
|
||
//tags := []string{platform}
|
||
//logger.Logger.Trace("jackpot avengers", pack)
|
||
//PlayerMgrSington.BroadcastMessageToGroup(int(gamehall.HundredScenePacketID_PACKET_SC_GAMEJACKPOT), pack, tags)
|
||
|
||
for gateSess, v := range mgs {
|
||
if gateSess != nil && len(v) != 0 {
|
||
action.MulticastMessageToServer(gateSess, int(gamehall.HundredScenePacketID_PACKET_SC_GAMEJACKPOT), pack, v...)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
func (this *SceneMgr) OnMiniTimer() {
|
||
|
||
}
|
||
|
||
func (this *SceneMgr) OnHourTimer() {
|
||
|
||
}
|
||
|
||
func (this *SceneMgr) OnDayTimer() {
|
||
logger.Logger.Info("(this *SceneMgr) OnDayTimer")
|
||
// for _, scene := range this.scenes {
|
||
// scene.OnDayTimer()
|
||
// }
|
||
PlayerMgrSington.OnDayTimer()
|
||
}
|
||
|
||
func (this *SceneMgr) OnWeekTimer() {
|
||
logger.Logger.Info("(this *SceneMgr) OnWeekTimer")
|
||
// for _, scene := range this.scenes {
|
||
// scene.OnWeekTimer()
|
||
// }
|
||
}
|
||
|
||
func (this *SceneMgr) OnMonthTimer() {
|
||
logger.Logger.Info("(this *SceneMgr) OnMonthTimer")
|
||
// for _, scene := range this.scenes {
|
||
// scene.OnMonthTimer()
|
||
// }
|
||
}
|
||
|
||
func (this *SceneMgr) DestoryAllScene() {
|
||
for _, s := range this.scenes {
|
||
s.Destroy(true)
|
||
}
|
||
}
|
||
|
||
// //////////////////////////////////////////////////////////////////
|
||
// / Module Implement [beg]
|
||
// //////////////////////////////////////////////////////////////////
|
||
func (this *SceneMgr) ModuleName() string {
|
||
return "SceneMgr"
|
||
}
|
||
|
||
func (this *SceneMgr) Init() {
|
||
//RestoreMDump(dumpFileName)
|
||
this.lastSendJackPot = time.Now()
|
||
}
|
||
|
||
func (this *SceneMgr) Update() {
|
||
for _, s := range this.scenes {
|
||
s.OnTick()
|
||
}
|
||
if time.Now().Sub(this.lastSendJackPot) > 0 {
|
||
this.lastSendJackPot = time.Now().Add(time.Second * 3)
|
||
for platform, _ := range this.PlatformScene {
|
||
if platform != common.Platform_Sys {
|
||
this.JackPotSync(platform,
|
||
common.GameId_Avengers,
|
||
common.GameId_IceAge,
|
||
common.GameId_EasterIsland,
|
||
common.GameId_CaiShen,
|
||
common.GameId_TamQuoc)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
func (this *SceneMgr) Shutdown() {
|
||
//WriteMDump(dumpFileName)
|
||
for _, s := range this.scenes {
|
||
s.Destroy(true)
|
||
}
|
||
module.UnregisteModule(this)
|
||
}
|
||
|
||
func init() {
|
||
module.RegisteModule(SceneMgrSington, time.Millisecond*50, 0)
|
||
//RegisteDayTimeChangeListener(SceneMgrSington)
|
||
}
|