ecs/servers/game/server.go

328 lines
17 KiB
Go
Raw Permalink Normal View History

2025-06-04 18:17:39 +08:00
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 {
2025-07-16 10:05:22 +08:00
server.NamedServer
2025-06-04 18:17:39 +08:00
2025-07-16 10:05:22 +08:00
serverConfig *logic.ServerConfig
tables *data.Tables
2025-06-04 18:17:39 +08:00
InnerServer server.HttpServer
ExterServer server.NetServer
2025-07-16 10:05:22 +08:00
dbConfig *db.Config
2025-06-04 18:17:39 +08:00
MongoClient db.MongoClient
2025-07-16 10:05:22 +08:00
sdConfig *sd.Config
2025-06-04 18:17:39 +08:00
RegisterClient sd.RegisterClient
httpRpcClient rpc.HttpRpcClient
SubscribeClient sd.SubscribeClient
2025-07-16 10:05:22 +08:00
eventManager *logic.EventManager
2025-06-04 18:17:39 +08:00
serverManager *logic.ServerManager
//arenaManager *logic.ArenaManager
playerManager *logic.PlayerManager
messageHandler *handler.MessageHandler
innerHandler *handler.InnerHandler
playerHandler *handler.PlayerHandler
}
func NewGameServer() server.Server {
return &gameServer{}
}
2025-07-16 10:05:22 +08:00
func (this *gameServer) WithServerConfig(serverConfig *logic.ServerConfig) {
this.serverConfig = serverConfig
}
2025-06-04 18:17:39 +08:00
2025-07-16 10:05:22 +08:00
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()
2025-06-04 18:17:39 +08:00
if err != nil {
return err
}
this.InnerServer.SetServer(this)
err = this.InnerServer.Init()
if err != nil {
2025-07-16 10:05:22 +08:00
return errors.Error("the 'InnerServer' init failed, ", err)
2025-06-04 18:17:39 +08:00
}
this.ExterServer.SetServer(this)
this.ExterServer.SetCodec(message.NewProtobufCodec())
err = this.ExterServer.Init()
if err != nil {
2025-07-16 10:05:22 +08:00
return errors.Error("the 'ExterServer' init failed, ", err)
2025-06-04 18:17:39 +08:00
}
2025-07-16 10:05:22 +08:00
this.MongoClient = db.NewMongoClient(this.dbConfig)
2025-06-04 18:17:39 +08:00
err = this.MongoClient.Init()
if err != nil {
2025-07-16 10:05:22 +08:00
return errors.Error("the 'MongoClient' init failed, ", err)
2025-06-04 18:17:39 +08:00
}
2025-07-16 10:05:22 +08:00
this.RegisterClient = zk.NewRegisterClient(this, this.sdConfig, &this.InnerServer.Listener, &this.ExterServer.Listener)
2025-06-04 18:17:39 +08:00
err = this.RegisterClient.Init()
if err != nil {
2025-07-16 10:05:22 +08:00
return errors.Error("the 'RegisterClient' init failed, ", err)
2025-06-04 18:17:39 +08:00
}
2025-07-16 10:05:22 +08:00
this.httpRpcClient = rpc.NewHttpRpcClient(this.Logger())
2025-06-04 18:17:39 +08:00
err = this.httpRpcClient.Init()
if err != nil {
2025-07-16 10:05:22 +08:00
return errors.Error("the 'httpRpcClient' init failed, ", err)
2025-06-04 18:17:39 +08:00
}
2025-07-16 10:05:22 +08:00
this.SubscribeClient = zk.NewSubscribeClient(this, this.sdConfig, map[string]sd.SubscribeCallback{
"event": this.httpRpcClient.SubscribeCallback,
"user": this.httpRpcClient.SubscribeCallback,
})
2025-06-04 18:17:39 +08:00
err = this.SubscribeClient.Init()
if err != nil {
2025-07-16 10:05:22 +08:00
return errors.Error("the 'SubscribeClient' init failed, ", err)
2025-06-04 18:17:39 +08:00
}
2025-07-16 10:05:22 +08:00
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)
2025-06-04 18:17:39 +08:00
2025-07-16 10:05:22 +08:00
err = server.InitManagers(this.eventManager, this.serverManager, this.playerManager)
2025-06-04 18:17:39 +08:00
if err != nil {
2025-07-16 10:05:22 +08:00
return errors.Error("the manager init failed, ", err)
2025-06-04 18:17:39 +08:00
}
2025-07-16 10:05:22 +08:00
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)
}
2025-06-04 18:17:39 +08:00
//Internal rpc request
2025-07-17 16:21:08 +08:00
this.InnerServer.PostHandler("/server/info", this.innerHandler.ServerInfo)
this.InnerServer.PostHandler("/server/close", this.innerHandler.ServerClose)
2025-06-04 18:17:39 +08:00
this.InnerServer.PostHandler("/server/data/reload", this.innerHandler.ServerDataReload)
2025-07-16 10:05:22 +08:00
this.InnerServer.GetHandler("/server/player/query", this.innerHandler.ServerPlayerQuery)
this.InnerServer.PostHandler("/server/player/operate", this.innerHandler.ServerPlayerOperate)
2025-06-04 18:17:39 +08:00
//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
2025-07-16 10:05:22 +08:00
this.InnerServer.PostHandler("/player/", this.innerHandler.PlayerRequest)
this.playerManager.InnerHandler("/player/mail/send", true, this.innerHandler.PlayerMailSend)
2025-06-04 18:17:39 +08:00
//this.playerManager.InnerHandler("/player/charge/callback", true, this.innerHandler.PlayerChargeCallback)
2025-06-05 17:47:59 +08:00
//Register net connect handler
2025-06-04 18:17:39 +08:00
this.ExterServer.ConnectHandler(this.messageHandler.HandleConnect)
2025-06-05 17:47:59 +08:00
//Register net disconnect handler
2025-06-04 18:17:39 +08:00
this.ExterServer.DisconnectHandler(this.messageHandler.HandleDisconnect)
2025-06-05 17:47:59 +08:00
//Register Common module message handlers
2025-06-04 18:17:39 +08:00
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
2025-06-20 15:34:46 +08:00
this.playerManager.Handler(pb.ModId_ModuleCommon, pb.MsgId_ModCommonGmCommand, this.playerHandler.GmCommand)
2025-06-04 18:17:39 +08:00
//
////Register player role model message handlers
2025-06-20 15:34:46 +08:00
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)
2025-06-04 18:17:39 +08:00
//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
2025-06-20 15:34:46 +08:00
this.playerManager.Handler(pb.ModId_ModuleLevel, pb.MsgId_ModLevelEnter, this.playerHandler.LevelEnter)
this.playerManager.Handler(pb.ModId_ModuleLevel, pb.MsgId_ModLevelSweep, this.playerHandler.LevelSweep)
2025-06-04 18:17:39 +08:00
//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)
2025-06-05 17:47:59 +08:00
//Register player battle module message handlers
2025-06-20 15:34:46 +08:00
this.playerManager.Handler(pb.ModId_ModuleBattle, pb.MsgId_ModBattleEnd, this.playerHandler.BattleEnd)
2025-06-04 18:17:39 +08:00
//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
2025-06-20 15:34:46 +08:00
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)
2025-06-04 18:17:39 +08:00
//Register player item module message handlers
2025-06-20 15:34:46 +08:00
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)
2025-07-16 10:05:22 +08:00
this.playerManager.Handler(pb.ModId_ModuleMail, pb.MsgId_ModMailItemGet, this.playerHandler.MailItemGet)
this.playerManager.Handler(pb.ModId_ModuleMail, pb.MsgId_ModMailItemGetAll, this.playerHandler.MailItemGetAll)
2025-06-20 15:34:46 +08:00
this.playerManager.Handler(pb.ModId_ModuleMail, pb.MsgId_ModMailDeleteAll, this.playerHandler.MailDelete)
this.playerManager.Handler(pb.ModId_ModuleMail, pb.MsgId_ModMailDeleteAll, this.playerHandler.MailDeleteAll)
2025-07-16 10:05:22 +08:00
//Register player task module message handlers
this.playerManager.Handler(pb.ModId_ModuleTask, pb.MsgId_ModTaskCommit, this.playerHandler.TaskCommit)
2025-06-04 18:17:39 +08:00
////Register player achievement module message handlers
2025-07-16 10:05:22 +08:00
this.playerManager.Handler(pb.ModId_ModuleAchievement, pb.MsgId_ModAchievementRewardGet, this.playerHandler.AchievementRewardGet)
2025-06-04 18:17:39 +08:00
////Register player store module message handlers
2025-07-16 10:05:22 +08:00
//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)
2025-06-04 18:17:39 +08:00
////Register player ad module message handlers
2025-07-16 10:05:22 +08:00
//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)
2025-06-04 18:17:39 +08:00
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)
2025-07-16 10:05:22 +08:00
_ = this.NamedServer.Close()
2025-06-04 18:17:39 +08:00
return nil
}
2025-07-16 10:05:22 +08:00
func (this *gameServer) LoadTables(dir string) error {
tables, err := data.NewTables(dir)
2025-06-04 18:17:39 +08:00
if err != nil {
return err
}
2025-07-16 10:05:22 +08:00
this.tables = tables
2025-06-04 18:17:39 +08:00
if this.innerHandler != nil {
this.innerHandler.SetTables(this.tables)
}
2025-07-16 10:05:22 +08:00
2025-06-04 18:17:39 +08:00
if this.messageHandler != nil {
this.messageHandler.SetTables(this.tables)
}
2025-07-16 10:05:22 +08:00
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)
}
2025-06-04 18:17:39 +08:00
return nil
}