package main import ( "ecs/proto/pb" "ecs/servers/game/data" "ecs/servers/game/handler" "ecs/servers/game/logic" "github.com/oylshe1314/framework/client/db" "github.com/oylshe1314/framework/client/rpc" "github.com/oylshe1314/framework/client/sd" "github.com/oylshe1314/framework/client/sd/zk" "github.com/oylshe1314/framework/errors" "github.com/oylshe1314/framework/message" "github.com/oylshe1314/framework/server" "github.com/oylshe1314/framework/util" "time" ) type gameServer struct { server.NamedServer serverConfig *logic.ServerConfig tables *data.Tables InnerServer server.HttpServer ExterServer server.NetServer dbConfig *db.Config MongoClient db.MongoClient sdConfig *sd.Config RegisterClient sd.RegisterClient httpRpcClient rpc.HttpRpcClient SubscribeClient sd.SubscribeClient eventManager *logic.EventManager serverManager *logic.ServerManager //arenaManager *logic.ArenaManager playerManager *logic.PlayerManager messageHandler *handler.MessageHandler innerHandler *handler.InnerHandler playerHandler *handler.PlayerHandler } func NewGameServer() server.Server { return &gameServer{} } func (this *gameServer) WithServerConfig(serverConfig *logic.ServerConfig) { this.serverConfig = serverConfig } func (this *gameServer) WithDbConfig(config *db.Config) { this.dbConfig = config } func (this *gameServer) WithSdConfig(config *sd.Config) { this.sdConfig = config } func (this *gameServer) Init() (err error) { err = this.NamedServer.Init() if err != nil { return err } this.InnerServer.SetServer(this) err = this.InnerServer.Init() if err != nil { return errors.Error("the 'InnerServer' init failed, ", err) } this.ExterServer.SetServer(this) this.ExterServer.SetCodec(message.NewProtobufCodec()) err = this.ExterServer.Init() if err != nil { return errors.Error("the 'ExterServer' init failed, ", err) } this.MongoClient = db.NewMongoClient(this.dbConfig) err = this.MongoClient.Init() if err != nil { return errors.Error("the 'MongoClient' init failed, ", err) } this.RegisterClient = zk.NewRegisterClient(this, this.sdConfig, &this.InnerServer.Listener, &this.ExterServer.Listener) err = this.RegisterClient.Init() if err != nil { return errors.Error("the 'RegisterClient' init failed, ", err) } this.httpRpcClient = rpc.NewHttpRpcClient(this.Logger()) err = this.httpRpcClient.Init() if err != nil { return errors.Error("the 'httpRpcClient' init failed, ", err) } this.SubscribeClient = zk.NewSubscribeClient(this, this.sdConfig, map[string]sd.SubscribeCallback{ "event": this.httpRpcClient.SubscribeCallback, "user": this.httpRpcClient.SubscribeCallback, }) err = this.SubscribeClient.Init() if err != nil { return errors.Error("the 'SubscribeClient' init failed, ", err) } this.eventManager = logic.NewEventManager(this, this.httpRpcClient) this.serverManager = logic.NewServerManager(this, this.MongoClient, this.serverConfig, this.eventManager) this.playerManager = logic.NewPlayerManager(this, this.MongoClient, this.eventManager, this.serverManager) err = server.InitManagers(this.eventManager, this.serverManager, this.playerManager) if err != nil { return errors.Error("the manager init failed, ", err) } this.innerHandler = handler.NewInnerHandler(this, this.httpRpcClient, this.tables, this.eventManager, this.serverManager, this.playerManager) this.messageHandler = handler.NewMessageHandler(this, this.httpRpcClient, this.tables, this.eventManager, this.serverManager, this.playerManager) this.playerHandler = handler.NewPlayerHandler(this, this.httpRpcClient, this.tables, this.eventManager, this.serverManager, this.playerManager) err = this.LoadTables(this.serverConfig.DataDir) if err != nil { return errors.Error("load tables failed, ", err) } //Internal rpc request this.InnerServer.PostHandler("/server/info", this.innerHandler.ServerInfo) this.InnerServer.PostHandler("/server/close", this.innerHandler.ServerClose) this.InnerServer.PostHandler("/server/data/reload", this.innerHandler.ServerDataReload) this.InnerServer.GetHandler("/server/player/query", this.innerHandler.ServerPlayerQuery) this.InnerServer.PostHandler("/server/player/operate", this.innerHandler.ServerPlayerOperate) //this.InnerServer.GetHandler("/server/mail/list", this.innerHandler.ServerMailList) //this.InnerServer.PostHandler("/server/mail/send", this.innerHandler.ServerMailSend) //this.InnerServer.PostHandler("/server/mail/delete", this.innerHandler.ServerMailDelete) //Register Internal player http request handlers this.InnerServer.PostHandler("/player/", this.innerHandler.PlayerRequest) this.playerManager.InnerHandler("/player/mail/send", true, this.innerHandler.PlayerMailSend) //this.playerManager.InnerHandler("/player/charge/callback", true, this.innerHandler.PlayerChargeCallback) //Register net connect handler this.ExterServer.ConnectHandler(this.messageHandler.HandleConnect) //Register net disconnect handler this.ExterServer.DisconnectHandler(this.messageHandler.HandleDisconnect) //Register Common module message handlers this.ExterServer.MessageHandler(uint16(pb.ModId_ModuleCommon), uint16(pb.MsgId_ModCommonHeartbeat), this.messageHandler.Heartbeat) //Register Login module message handlers this.ExterServer.MessageHandler(uint16(pb.ModId_ModuleLogin), uint16(pb.MsgId_ModLoginUserAuth), this.messageHandler.UserAuth) this.ExterServer.MessageHandler(uint16(pb.ModId_ModuleLogin), uint16(pb.MsgId_ModLoginRoleCreate), this.messageHandler.RoleCreate) this.ExterServer.MessageHandler(uint16(pb.ModId_ModuleLogin), uint16(pb.MsgId_ModLoginRoleLogin), this.messageHandler.RoleLogin) this.ExterServer.MessageHandler(uint16(pb.ModId_ModuleLogin), uint16(pb.MsgId_ModLoginRoleLogout), this.messageHandler.RoleLogout) this.ExterServer.MessageHandler(uint16(pb.ModId_ModuleLogin), uint16(pb.MsgId_ModLoginReconnect), this.messageHandler.Reconnect) this.ExterServer.DefaultHandler(this.messageHandler.Message) //Register player common module message handlers this.playerManager.Handler(pb.ModId_ModuleCommon, pb.MsgId_ModCommonGmCommand, this.playerHandler.GmCommand) // ////Register player role model message handlers this.playerManager.Handler(pb.ModId_ModuleRole, pb.MsgId_ModRoleChangeLanguage, this.playerHandler.RoleChangeLanguage) this.playerManager.Handler(pb.ModId_ModuleRole, pb.MsgId_ModRoleChangeRoleName, this.playerHandler.RoleChangeName) this.playerManager.Handler(pb.ModId_ModuleRole, pb.MsgId_ModRolePropertyChange, this.playerHandler.RolePropertyChange) this.playerManager.Handler(pb.ModId_ModuleRole, pb.MsgId_ModRoleLineupHeroChange, this.playerHandler.LineupHeroChange) this.playerManager.Handler(pb.ModId_ModuleRole, pb.MsgId_ModRoleLineupHeroPosition, this.playerHandler.LineupHeroPosition) this.playerManager.Handler(pb.ModId_ModuleRole, pb.MsgId_ModRoleLineupHeroEquip, this.playerHandler.LineupHeroEquip) this.playerManager.Handler(pb.ModId_ModuleRole, pb.MsgId_ModRoleLineupHeroTreasure, this.playerHandler.LineupHeroTreasure) this.playerManager.Handler(pb.ModId_ModuleRole, pb.MsgId_ModRoleLineupHeroArtifact, this.playerHandler.LineupHeroArtifact) this.playerManager.Handler(pb.ModId_ModuleRole, pb.MsgId_ModRoleLineupHeroMounts, this.playerHandler.LineupHeroMounts) this.playerManager.Handler(pb.ModId_ModuleRole, pb.MsgId_ModRoleLineupHeroGeneral, this.playerHandler.LineupHeroGeneral) this.playerManager.Handler(pb.ModId_ModuleRole, pb.MsgId_ModRoleLineupHeroOrnament, this.playerHandler.LineupHeroOrnament) this.playerManager.Handler(pb.ModId_ModuleRole, pb.MsgId_ModRoleLineupHelperChange, this.playerHandler.LineupHelperChange) this.playerManager.Handler(pb.ModId_ModuleRole, pb.MsgId_ModRoleLineupEquipInlay, this.playerHandler.LineupEquipInlay) this.playerManager.Handler(pb.ModId_ModuleRole, pb.MsgId_ModRoleLineupTreasureInlay, this.playerHandler.LineupTreasureInlay) this.playerManager.Handler(pb.ModId_ModuleRole, pb.MsgId_ModRoleLineupCreate, this.playerHandler.LineupCreate) this.playerManager.Handler(pb.ModId_ModuleRole, pb.MsgId_ModRoleLineupChangeName, this.playerHandler.LineupChangeName) this.playerManager.Handler(pb.ModId_ModuleRole, pb.MsgId_ModRoleLineupActivate, this.playerHandler.LineupActivate) this.playerManager.Handler(pb.ModId_ModuleRole, pb.MsgId_ModRoleLineupDelete, this.playerHandler.LineupDelete) //this.playerManager.Handler(proto.ModIdRole, proto.MsgIdRoleTalentUpgrade, this.playerHandler.RoleTalentUpgrade) //this.playerManager.Handler(proto.ModIdRole, proto.MsgIdRoleTalentReset, this.playerHandler.RoleTalentReset) // ////Register player level module message handlers this.playerManager.Handler(pb.ModId_ModuleLevel, pb.MsgId_ModLevelEnter, this.playerHandler.LevelEnter) this.playerManager.Handler(pb.ModId_ModuleLevel, pb.MsgId_ModLevelSweep, this.playerHandler.LevelSweep) //this.playerManager.Handler(proto.ModIdLevel, proto.MsgIdCopySpeedGetReward, this.playerHandler.CopySpeedGetReward) //this.playerManager.Handler(proto.ModIdLevel, proto.MsgIdCopySpeedRankList, this.playerHandler.CopySpeedRankList) //this.playerManager.Handler(proto.ModIdLevel, proto.MsgIdArenaPlayerDetail, this.playerHandler.ArenaPlayerDetail) //this.playerManager.Handler(proto.ModIdLevel, proto.MsgIdMaterialCopySweep, this.playerHandler.MaterialCopySweep) //this.playerManager.Handler(proto.ModIdLevel, proto.MsgIdPlanetMine, this.playerHandler.PlanetMine) //this.playerManager.Handler(proto.ModIdLevel, proto.MsgIdPlanetCivilizationChoose, this.playerHandler.PlanetCivilizationChoose) //this.playerManager.Handler(proto.ModIdLevel, proto.MsgIdPlanetCivilizationBuild, this.playerHandler.PlanetCivilizationBuild) //this.playerManager.Handler(proto.ModIdLevel, proto.MsgIdPlanetCivilizationUpgrade, this.playerHandler.PlanetCivilizationUpgrade) //this.playerManager.Handler(proto.ModIdLevel, proto.MsgIdPlanetCivilizationDestroy, this.playerHandler.PlanetCivilizationDestroy) //this.playerManager.Handler(proto.ModIdLevel, proto.MsgIdPlanetRawStoneMine, this.playerHandler.PlanetRawStoneMine) //this.playerManager.Handler(proto.ModIdLevel, proto.MsgIdPlanetRawStoneAccrueReward, this.playerHandler.PlanetRawStoneAccrueReward) //this.playerManager.Handler(proto.ModIdLevel, proto.MsgIdCopyArenaMatches, this.playerHandler.CopyArenaMatches) //this.playerManager.Handler(proto.ModIdLevel, proto.MsgIdCopyArenaSelectMatch, this.playerHandler.CopyArenaSelectMatch) //this.playerManager.Handler(proto.ModIdLevel, proto.MsgIdCopyArenaRankList, this.playerHandler.CopyArenaRankList) //this.playerManager.Handler(proto.ModIdLevel, proto.MsgIdCopyArenaBattleRecordList, this.playerHandler.CopyArenaBattleRecordList) //this.playerManager.Handler(proto.ModIdLevel, proto.MsgIdCopyMainlineRankList, this.playerHandler.CopyMainlineRankList) //Register player battle module message handlers this.playerManager.Handler(pb.ModId_ModuleBattle, pb.MsgId_ModBattleEnd, this.playerHandler.BattleEnd) //this.playerManager.Handler(proto.ModIdBattle, proto.MsgIdBattleResult, this.playerHandler.BattleResult) //this.playerManager.Handler(proto.ModIdBattle, proto.MsgIdIdleBattleGetReward, this.playerHandler.IdleBattleGetReward) //this.playerManager.Handler(proto.ModIdBattle, proto.MsgIdIdleQuickBattle, this.playerHandler.IdleQuickBattle) //this.playerManager.Handler(proto.ModIdBattle, proto.MsgIdArenaBattleResult, this.playerHandler.ArenaBattleResult) //this.playerManager.Handler(proto.ModIdBattle, proto.MsgIdBattleRadarUse, this.playerHandler.BattleRadarUse) //Register player hero module message handlers this.playerManager.Handler(pb.ModId_ModuleHero, pb.MsgId_ModHeroUpgrade, this.playerHandler.HeroUpgrade) this.playerManager.Handler(pb.ModId_ModuleHero, pb.MsgId_ModHeroBreak, this.playerHandler.HeroBreak) this.playerManager.Handler(pb.ModId_ModuleHero, pb.MsgId_ModHeroAwaken, this.playerHandler.HeroAwaken) this.playerManager.Handler(pb.ModId_ModuleHero, pb.MsgId_ModHeroSoul, this.playerHandler.HeroSoul) this.playerManager.Handler(pb.ModId_ModuleHero, pb.MsgId_ModHeroFateBreak, this.playerHandler.HeroFateBreak) this.playerManager.Handler(pb.ModId_ModuleHero, pb.MsgId_ModHeroQuality, this.playerHandler.HeroQuality) this.playerManager.Handler(pb.ModId_ModuleHero, pb.MsgId_ModHeroBookActivate, this.playerHandler.HeroBookActivate) //Register player item module message handlers this.playerManager.Handler(pb.ModId_ModuleItem, pb.MsgId_ModItemUse, this.playerHandler.ItemUse) this.playerManager.Handler(pb.ModId_ModuleItem, pb.MsgId_ModItemSale, this.playerHandler.ItemSale) this.playerManager.Handler(pb.ModId_ModuleItem, pb.MsgId_ModItemEquipUpgrade, this.playerHandler.EquipUpgrade) this.playerManager.Handler(pb.ModId_ModuleItem, pb.MsgId_ModItemEquipRefine, this.playerHandler.EquipRefine) this.playerManager.Handler(pb.ModId_ModuleItem, pb.MsgId_ModItemTreasureUpgrade, this.playerHandler.TreasureUpgrade) this.playerManager.Handler(pb.ModId_ModuleItem, pb.MsgId_ModItemTreasureRefine, this.playerHandler.TreasureRefine) //Register player mail module message handlers this.playerManager.Handler(pb.ModId_ModuleMail, pb.MsgId_ModMailSend, this.playerHandler.MailSend) this.playerManager.Handler(pb.ModId_ModuleMail, pb.MsgId_ModMailRead, this.playerHandler.MailRead) this.playerManager.Handler(pb.ModId_ModuleMail, pb.MsgId_ModMailItemGet, this.playerHandler.MailItemGet) this.playerManager.Handler(pb.ModId_ModuleMail, pb.MsgId_ModMailItemGetAll, this.playerHandler.MailItemGetAll) this.playerManager.Handler(pb.ModId_ModuleMail, pb.MsgId_ModMailDeleteAll, this.playerHandler.MailDelete) this.playerManager.Handler(pb.ModId_ModuleMail, pb.MsgId_ModMailDeleteAll, this.playerHandler.MailDeleteAll) //Register player task module message handlers this.playerManager.Handler(pb.ModId_ModuleTask, pb.MsgId_ModTaskCommit, this.playerHandler.TaskCommit) ////Register player achievement module message handlers this.playerManager.Handler(pb.ModId_ModuleAchievement, pb.MsgId_ModAchievementRewardGet, this.playerHandler.AchievementRewardGet) ////Register player store module message handlers //this.playerManager.Handler(pb.ModIdStore, pb.MsgIdStorePoolRefresh, this.playerHandler.StorePoolRefresh) //this.playerManager.Handler(pb.ModIdStore, pb.MsgIdStoreBuy, this.playerHandler.StoreBuy) //this.playerManager.Handler(pb.ModIdStore, pb.MsgIdGiftPackBuy, this.playerHandler.GiftPackBuy) //this.playerManager.Handler(pb.ModIdStore, pb.MsgIdChargeCreateOrder, this.playerHandler.ChargeCreateOrder) //this.playerManager.Handler(pb.ModIdStore, pb.MsgIdChargeWechatPay, this.playerHandler.ChargeWechatPay) //this.playerManager.Handler(pb.ModIdStore, pb.MsgIdBattlePassRewardGet, this.playerHandler.BattlePassRewardGet) //this.playerManager.Handler(pb.ModIdStore, pb.MsgIdBattlePassRewardGetAll, this.playerHandler.BattlePassRewardGetAll) //this.playerManager.Handler(pb.ModIdStore, pb.MsgIdCdkeyExchange, this.playerHandler.CdkeyExchange) //Register player reward module message handlers this.playerManager.Handler(pb.ModId_ModuleReward, pb.MsgId_ModRewardActiveGet, this.playerHandler.RewardActiveGet) this.playerManager.Handler(pb.ModId_ModuleReward, pb.MsgId_ModRewardLoginGet, this.playerHandler.RewardLoginGet) ////Register player ad module message handlers //this.playerManager.Handler(pb.ModIdAd, pb.MsgIdAdGetReward, this.playerHandler.AdGetReward) // Register player lottery module message handlers this.playerManager.Handler(pb.ModId_ModuleLottery, pb.MsgId_ModLotteryDraw, this.playerHandler.LotteryDraw) return } func (this *gameServer) Serve() (err error) { return util.WaitAny(this.InnerServer.Serve, this.ExterServer.Serve, this.RegisterClient.Work, this.SubscribeClient.Work) } func (this *gameServer) Close() error { this.Logger().Info("The managers is closing") _ = server.CloseManagers(this.playerManager /*this.arenaManager, this.serverManager, this.eventManager*/) this.Logger().Info("The 'MongoClient' is closing") _ = this.MongoClient.Close() this.Logger().Info("The 'RegisterClient' is closing") _ = this.RegisterClient.Close() this.Logger().Info("The 'SubscribeClient' is closing") _ = this.SubscribeClient.Close() this.Logger().Info("The 'httpRpcClient' is closing") _ = this.httpRpcClient.Close() this.Logger().Info("The 'ExterServer' is closing") _ = this.ExterServer.Close() this.Logger().Info("The 'InnerServer' is closing") _ = this.InnerServer.Close() this.Logger().Info("Server closed") time.Sleep(time.Second) _ = this.NamedServer.Close() return nil } func (this *gameServer) LoadTables(dir string) error { tables, err := data.NewTables(dir) if err != nil { return err } this.tables = tables if this.innerHandler != nil { this.innerHandler.SetTables(this.tables) } if this.messageHandler != nil { this.messageHandler.SetTables(this.tables) } if this.playerHandler != nil { this.playerHandler.SetTables(this.tables) } //if this.arenaManager != nil { // this.arenaManager.SetTables(this.tables) //} if this.playerManager != nil { this.playerManager.SetTables(this.tables) } return nil }