Skip to content

Commit

Permalink
refactored guild and guild alliance into internal/service
Browse files Browse the repository at this point in the history
  • Loading branch information
stratic-dev committed Oct 17, 2024
1 parent fa35141 commit 5baff56
Show file tree
Hide file tree
Showing 11 changed files with 846 additions and 786 deletions.
641 changes: 641 additions & 0 deletions internal/service/guild.go

Large diffs are not rendered by default.

112 changes: 112 additions & 0 deletions internal/service/guild_alliance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package service

import (
"erupe-ce/utils/db"
"erupe-ce/utils/logger"
"fmt"
"time"

"github.com/jmoiron/sqlx"
"go.uber.org/zap"
)

type GuildAlliance struct {
ID uint32 `db:"id"`
Name string `db:"name"`
CreatedAt time.Time `db:"created_at"`
TotalMembers uint16

ParentGuildID uint32 `db:"parent_id"`
SubGuild1ID uint32 `db:"sub1_id"`
SubGuild2ID uint32 `db:"sub2_id"`

ParentGuild Guild
SubGuild1 Guild
SubGuild2 Guild
}

const AllianceInfoSelectQuery = `
SELECT
ga.id,
ga.name,
created_at,
parent_id,
CASE
WHEN sub1_id IS NULL THEN 0
ELSE sub1_id
END,
CASE
WHEN sub2_id IS NULL THEN 0
ELSE sub2_id
END
FROM guild_alliances ga
`

func GetAllianceData(AllianceID uint32) (*GuildAlliance, error) {
db, err := db.GetDB()
logger := logger.Get()

if err != nil {
logger.Fatal(fmt.Sprintf("Failed to get database instance: %s", err))
}
rows, err := db.Queryx(fmt.Sprintf(`
%s
WHERE ga.id = $1
`, AllianceInfoSelectQuery), AllianceID)
if err != nil {
logger.Error("Failed to retrieve alliance data from database", zap.Error(err))
return nil, err
}
defer rows.Close()
hasRow := rows.Next()
if !hasRow {
return nil, nil
}

return BuildAllianceObjectFromDbResult(rows, err)
}

func BuildAllianceObjectFromDbResult(result *sqlx.Rows, err error) (*GuildAlliance, error) {
alliance := &GuildAlliance{}
logger := logger.Get()

err = result.StructScan(alliance)

if err != nil {
logger.Error("failed to retrieve alliance from database", zap.Error(err))
return nil, err
}

parentGuild, err := GetGuildInfoByID(alliance.ParentGuildID)
if err != nil {
logger.Error("Failed to get parent guild info", zap.Error(err))
return nil, err
} else {
alliance.ParentGuild = *parentGuild
alliance.TotalMembers += parentGuild.MemberCount
}

if alliance.SubGuild1ID > 0 {
subGuild1, err := GetGuildInfoByID(alliance.SubGuild1ID)
if err != nil {
logger.Error("Failed to get sub guild 1 info", zap.Error(err))
return nil, err
} else {
alliance.SubGuild1 = *subGuild1
alliance.TotalMembers += subGuild1.MemberCount
}
}

if alliance.SubGuild2ID > 0 {
subGuild2, err := GetGuildInfoByID(alliance.SubGuild2ID)
if err != nil {
logger.Error("Failed to get sub guild 2 info", zap.Error(err))
return nil, err
} else {
alliance.SubGuild2 = *subGuild2
alliance.TotalMembers += subGuild2.MemberCount
}
}

return alliance, nil
}
13 changes: 7 additions & 6 deletions server/channelserver/handlers_festa.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"erupe-ce/config"
"erupe-ce/internal/constant"
"erupe-ce/internal/model"
"erupe-ce/internal/service"

"erupe-ce/network/mhfpacket"
"erupe-ce/utils/byteframe"
Expand Down Expand Up @@ -354,10 +355,10 @@ func handleMsgMhfInfoFesta(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket) {
func handleMsgMhfStateFestaU(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgMhfStateFestaU)

guild, err := GetGuildInfoByCharacterId(s, s.CharID)
guild, err := service.GetGuildInfoByCharacterId(s.CharID)
applicant := false
if guild != nil {
applicant, _ = guild.HasApplicationForCharID(s, s.CharID)
applicant, _ = guild.HasApplicationForCharID(s.CharID)
}
if err != nil || guild == nil || applicant {
s.DoAckSimpleFail(pkt.AckHandle, make([]byte, 4))
Expand All @@ -381,10 +382,10 @@ func handleMsgMhfStateFestaU(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket) {
// state festa (G)uild
func handleMsgMhfStateFestaG(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgMhfStateFestaG)
guild, err := GetGuildInfoByCharacterId(s, s.CharID)
guild, err := service.GetGuildInfoByCharacterId(s.CharID)
applicant := false
if guild != nil {
applicant, _ = guild.HasApplicationForCharID(s, s.CharID)
applicant, _ = guild.HasApplicationForCharID(s.CharID)
}
resp := byteframe.NewByteFrame()
if err != nil || guild == nil || applicant {
Expand All @@ -406,7 +407,7 @@ func handleMsgMhfStateFestaG(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket) {

func handleMsgMhfEnumerateFestaMember(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgMhfEnumerateFestaMember)
guild, err := GetGuildInfoByCharacterId(s, s.CharID)
guild, err := service.GetGuildInfoByCharacterId(s.CharID)
if err != nil || guild == nil {
s.DoAckSimpleFail(pkt.AckHandle, make([]byte, 4))
return
Expand Down Expand Up @@ -450,7 +451,7 @@ func handleMsgMhfVoteFesta(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket) {
func handleMsgMhfEntryFesta(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgMhfEntryFesta)

guild, err := GetGuildInfoByCharacterId(s, s.CharID)
guild, err := service.GetGuildInfoByCharacterId(s.CharID)
if err != nil || guild == nil {
s.DoAckSimpleFail(pkt.AckHandle, make([]byte, 4))
return
Expand Down
Loading

0 comments on commit 5baff56

Please sign in to comment.