refactor(channelserver): extract CharacterRepository for characters table access

Centralizes all characters table SQL behind a CharacterRepository struct
in repo_character.go. The 4 existing helpers (loadCharacterData,
saveCharacterData, readCharacterInt, adjustCharacterInt) now delegate to
the repository, keeping identical signatures so all ~70 callsites remain
unchanged. Direct queries in handlers_session.go, sys_channel_server.go
(DisconnectUser), and handlers_mail.go are also migrated.

Pure refactor with zero behavior change — first step toward eliminating
the ~130 scattered character queries identified in anti-patterns #9.
This commit is contained in:
Houmgaor
2026-02-20 21:38:21 +01:00
parent a369a855bf
commit 197e44d04c
7 changed files with 320 additions and 31 deletions

View File

@@ -45,6 +45,7 @@ type Server struct {
Port uint16
logger *zap.Logger
db *sqlx.DB
charRepo *CharacterRepository
erupeConfig *_config.Config
acceptConns chan net.Conn
deleteConns chan net.Conn
@@ -115,6 +116,8 @@ func NewServer(config *Config) *Server {
handlerTable: buildHandlerTable(),
}
s.charRepo = NewCharacterRepository(config.DB)
// Mezeporta
s.stages["sl1Ns200p0a0u0"] = NewStage("sl1Ns200p0a0u0")
@@ -361,17 +364,9 @@ func (s *Server) FindSessionByCharID(charID uint32) *Session {
// DisconnectUser disconnects all sessions belonging to the given user ID.
func (s *Server) DisconnectUser(uid uint32) {
var cid uint32
var cids []uint32
rows, err := s.db.Query(`SELECT id FROM characters WHERE user_id=$1`, uid)
cids, err := s.charRepo.GetCharIDsByUserID(uid)
if err != nil {
s.logger.Error("Failed to query characters for disconnect", zap.Error(err))
} else {
defer func() { _ = rows.Close() }()
for rows.Next() {
_ = rows.Scan(&cid)
cids = append(cids, cid)
}
}
if s.Registry != nil {
s.Registry.DisconnectUser(cids)