ecs/servers/game/server.go
2025-06-20 15:34:46 +08:00

310 lines
16 KiB
Go

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.LoggerServer
ServerConfig logic.ServerConfig
InnerServer server.HttpServer
ExterServer server.NetServer
MongoClient db.MongoClient
SdConfig sd.Config
RegisterClient sd.RegisterClient
httpRpcClient rpc.HttpRpcClient
SubscribeClient sd.SubscribeClient
tables *data.Tables
//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) Init() (err error) {
err = this.LoggerServer.Init()
if err != nil {
return err
}
err = this.ServerConfig.Init()
if err != nil {
return err
}
err = this.Load(this.ServerConfig.DataDir())
if err != nil {
return err
}
this.InnerServer.SetServer(this)
err = this.InnerServer.Init()
if err != nil {
return errors.Errorf("the 'InnerServer' init failed, %v", err)
}
this.ExterServer.SetServer(this)
this.ExterServer.SetCodec(message.NewProtobufCodec())
err = this.ExterServer.Init()
if err != nil {
return errors.Errorf("the 'ExterServer' init failed, %v", err)
}
err = this.MongoClient.Init()
if err != nil {
return errors.Errorf("the 'MongoClient' init failed, %v", err)
}
this.RegisterClient = zk.NewRegisterClient(&this.SdConfig)
this.RegisterClient.SetServer(this)
this.RegisterClient.SetListener(&this.InnerServer.Listener, &this.ExterServer.Listener)
err = this.RegisterClient.Init()
if err != nil {
return errors.Errorf("the 'RegisterClient' init failed, %v", err)
}
this.httpRpcClient.SetLogger(this.Logger())
err = this.httpRpcClient.Init()
if err != nil {
return errors.Errorf("the 'httpRpcClient' init failed, %v", err)
}
this.SubscribeClient = zk.NewSubscribeClient(&this.SdConfig)
this.SubscribeClient.SetServer(this)
//this.SubscribeClient.AddSubscribe("event", this.httpRpcClient.SubscribeCallback)
this.SubscribeClient.AddSubscribe("user", this.httpRpcClient.SubscribeCallback)
err = this.SubscribeClient.Init()
if err != nil {
return errors.Errorf("the 'SubscribeClient' init failed, %v", err)
}
//this.eventManager = logic.NewEventManager(this, &this.httpRpcClient)
this.serverManager = logic.NewServerManager(this, &this.ServerConfig, &this.MongoClient /*, this.eventManager*/)
this.playerManager = logic.NewPlayerManager(this, this.tables, &this.MongoClient /*, this.eventManager*/, this.serverManager)
err = server.InitManagers( /*this.eventManager, */ this.serverManager /*this.arenaManager, */, this.playerManager)
if err != nil {
return errors.Errorf("the manager init failed, %v", err)
}
this.innerHandler = handler.NewInnerHandler(this, this.tables /*this.eventManager, */, this.serverManager, this.playerManager)
this.messageHandler = handler.NewMessageHandler(this, this.tables /*this.eventManager*/, this.serverManager, this.playerManager /*this.arenaManager*/, &this.httpRpcClient)
this.playerHandler = handler.NewPlayerHandler(this, this.tables /*this.eventManager*/, this.playerManager /*this.arenaManager*/, &this.httpRpcClient)
//Internal rpc request
this.InnerServer.GetHandler("/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_ModMailReceive, this.playerHandler.MailReceive)
this.playerManager.Handler(pb.ModId_ModuleMail, pb.MsgId_ModMailReceiveAll, this.playerHandler.MailReceiveAll)
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 achievement module message handlers
//this.playerManager.Handler(proto.ModIdAchievement, proto.MsgIdAchievementGetReward, this.playerHandler.AchievementGetReward)
//
////Register player task module message handlers
//this.playerManager.Handler(proto.ModIdTask, proto.MsgIdTaskCommit, this.playerHandler.TaskCommit)
//this.playerManager.Handler(proto.ModIdTask, proto.MsgIdActiveGetReward, this.playerHandler.ActiveGetReward)
//
////Register player store module message handlers
//this.playerManager.Handler(proto.ModIdStore, proto.MsgIdStorePoolRefresh, this.playerHandler.StorePoolRefresh)
//this.playerManager.Handler(proto.ModIdStore, proto.MsgIdStoreBuy, this.playerHandler.StoreBuy)
//this.playerManager.Handler(proto.ModIdStore, proto.MsgIdGiftPackBuy, this.playerHandler.GiftPackBuy)
//this.playerManager.Handler(proto.ModIdStore, proto.MsgIdChargeCreateOrder, this.playerHandler.ChargeCreateOrder)
//this.playerManager.Handler(proto.ModIdStore, proto.MsgIdChargeWechatPay, this.playerHandler.ChargeWechatPay)
//this.playerManager.Handler(proto.ModIdStore, proto.MsgIdBattlePassRewardGet, this.playerHandler.BattlePassRewardGet)
//this.playerManager.Handler(proto.ModIdStore, proto.MsgIdBattlePassRewardGetAll, this.playerHandler.BattlePassRewardGetAll)
//this.playerManager.Handler(proto.ModIdStore, proto.MsgIdCdkeyExchange, this.playerHandler.CdkeyExchange)
//
////Register player activity module message handlers
//this.playerManager.Handler(proto.ModIdActivity, proto.MsgIdLoginRewardGet, this.playerHandler.LoginRewardGet)
//
////Register player ad module message handlers
//this.playerManager.Handler(proto.ModIdAd, proto.MsgIdAdGetReward, this.playerHandler.AdGetReward)
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.LoggerServer.Close()
return nil
}
func (this *gameServer) Load(dir string) error {
var dataTables = data.NewTables(dir)
var err = dataTables.Load()
if err != nil {
return err
}
this.tables = dataTables
//if this.arenaManager != nil {
// this.arenaManager.SetTables(this.tables)
//}
if this.playerManager != nil {
this.playerManager.SetTables(this.tables)
}
if this.innerHandler != nil {
this.innerHandler.SetTables(this.tables)
}
if this.messageHandler != nil {
this.messageHandler.SetTables(this.tables)
}
return nil
}