refactor(channelserver): remove Channels fallbacks, use Registry as sole cross-channel API

main.go always sets both Channels and Registry together, making the
Channels fallback paths dead code. This removes:

- Server.Channels field from the Server struct
- 3 if/else fallback blocks in handlers_session.go (replaced with
  Registry.FindChannelForStage, SearchSessions, SearchStages)
- 1 if/else fallback block in handlers_guild_ops.go (replaced with
  Registry.NotifyMailToCharID)
- 3 method fallbacks in sys_channel_server.go (WorldcastMHF,
  FindSessionByCharID, DisconnectUser now delegate directly)

Updates anti-patterns.md #6 to "accepted design" — Session struct is
appropriate for this game server's handler pattern, and cross-channel
coupling is now fully routed through the ChannelRegistry interface.
This commit is contained in:
Houmgaor
2026-02-22 16:16:44 +01:00
parent cd630a7a58
commit 53b5bb3b96
11 changed files with 113 additions and 252 deletions

View File

@@ -43,7 +43,6 @@ type Config struct {
// own locks internally and may be acquired at any point.
type Server struct {
sync.Mutex
Channels []*Server
Registry ChannelRegistry
ID uint16
GlobalID string
@@ -332,16 +331,7 @@ func (s *Server) BroadcastMHF(pkt mhfpacket.MHFPacket, ignoredSession *Session)
// WorldcastMHF broadcasts a packet to all sessions across all channel servers.
func (s *Server) WorldcastMHF(pkt mhfpacket.MHFPacket, ignoredSession *Session, ignoredChannel *Server) {
if s.Registry != nil {
s.Registry.Worldcast(pkt, ignoredSession, ignoredChannel)
return
}
for _, c := range s.Channels {
if c == ignoredChannel {
continue
}
c.BroadcastMHF(pkt, ignoredSession)
}
s.Registry.Worldcast(pkt, ignoredSession, ignoredChannel)
}
// BroadcastChatMessage broadcasts a simple chat message to all the sessions.
@@ -382,20 +372,7 @@ func (s *Server) DiscordScreenShotSend(charName string, title string, descriptio
// FindSessionByCharID looks up a session by character ID across all channels.
func (s *Server) FindSessionByCharID(charID uint32) *Session {
if s.Registry != nil {
return s.Registry.FindSessionByCharID(charID)
}
for _, c := range s.Channels {
c.Lock()
for _, session := range c.sessions {
if session.charID == charID {
c.Unlock()
return session
}
}
c.Unlock()
}
return nil
return s.Registry.FindSessionByCharID(charID)
}
// DisconnectUser disconnects all sessions belonging to the given user ID.
@@ -404,22 +381,7 @@ func (s *Server) DisconnectUser(uid uint32) {
if err != nil {
s.logger.Error("Failed to query characters for disconnect", zap.Error(err))
}
if s.Registry != nil {
s.Registry.DisconnectUser(cids)
return
}
for _, c := range s.Channels {
c.Lock()
for _, session := range c.sessions {
for _, cid := range cids {
if session.charID == cid {
_ = session.rawConn.Close()
break
}
}
}
c.Unlock()
}
s.Registry.DisconnectUser(cids)
}
// FindObjectByChar finds a stage object owned by the given character ID.