Skip to content

Commit

Permalink
guild_member service created in /internal/service
Browse files Browse the repository at this point in the history
  • Loading branch information
stratic-dev committed Oct 17, 2024
1 parent 5baff56 commit 674ac9b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package channelserver
package service

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

Expand Down Expand Up @@ -46,15 +47,17 @@ func (gm *GuildMember) IsSubLeader() bool {
return gm.OrderIndex <= 3
}

func (gm *GuildMember) Save(s *Session) error {
func (gm *GuildMember) Save() error {
db, err := db.GetDB()
logger := logger.Get()

if err != nil {
s.Logger.Fatal(fmt.Sprintf("Failed to get database instance: %s", err))
logger.Fatal(fmt.Sprintf("Failed to get database instance: %s", err))
}
_, err = db.Exec("UPDATE guild_characters SET avoid_leadership=$1, order_index=$2 WHERE character_id=$3", gm.AvoidLeadership, gm.OrderIndex, gm.CharID)

if err != nil {
s.Logger.Error(
logger.Error(
"failed to update guild member data",
zap.Error(err),
zap.Uint32("charID", gm.CharID),
Expand Down Expand Up @@ -91,18 +94,20 @@ SELECT * FROM (
) AS subquery
`

func GetGuildMembers(s *Session, guildID uint32, applicants bool) ([]*GuildMember, error) {
func GetGuildMembers(guildID uint32, applicants bool) ([]*GuildMember, error) {
db, err := db.GetDB()
logger := logger.Get()

if err != nil {
s.Logger.Fatal(fmt.Sprintf("Failed to get database instance: %s", err))
logger.Fatal(fmt.Sprintf("Failed to get database instance: %s", err))
}
rows, err := db.Queryx(fmt.Sprintf(`
%s
WHERE guild_id = $1 AND is_applicant = $2
`, guildMembersSelectSQL), guildID, applicants)

if err != nil {
s.Logger.Error("failed to retrieve membership data for guild", zap.Error(err), zap.Uint32("guildID", guildID))
logger.Error("failed to retrieve membership data for guild", zap.Error(err), zap.Uint32("guildID", guildID))
return nil, err
}

Expand All @@ -111,7 +116,7 @@ func GetGuildMembers(s *Session, guildID uint32, applicants bool) ([]*GuildMembe
members := make([]*GuildMember, 0)

for rows.Next() {
member, err := buildGuildMemberObjectFromDBResult(rows, err, s)
member, err := buildGuildMemberObjectFromDBResult(rows, err)

if err != nil {
return nil, err
Expand All @@ -123,15 +128,17 @@ func GetGuildMembers(s *Session, guildID uint32, applicants bool) ([]*GuildMembe
return members, nil
}

func GetCharacterGuildData(s *Session, charID uint32) (*GuildMember, error) {
func GetCharacterGuildData(charID uint32) (*GuildMember, error) {
db, err := db.GetDB()
logger := logger.Get()

if err != nil {
s.Logger.Fatal(fmt.Sprintf("Failed to get database instance: %s", err))
logger.Fatal(fmt.Sprintf("Failed to get database instance: %s", err))
}
rows, err := db.Queryx(fmt.Sprintf("%s WHERE character_id=$1", guildMembersSelectSQL), charID)

if err != nil {
s.Logger.Error(fmt.Sprintf("failed to retrieve membership data for character '%d'", charID))
logger.Error(fmt.Sprintf("failed to retrieve membership data for character '%d'", charID))
return nil, err
}

Expand All @@ -143,16 +150,18 @@ func GetCharacterGuildData(s *Session, charID uint32) (*GuildMember, error) {
return nil, nil
}

return buildGuildMemberObjectFromDBResult(rows, err, s)
return buildGuildMemberObjectFromDBResult(rows, err)
}

func buildGuildMemberObjectFromDBResult(rows *sqlx.Rows, err error, s *Session) (*GuildMember, error) {
func buildGuildMemberObjectFromDBResult(rows *sqlx.Rows, err error) (*GuildMember, error) {
logger := logger.Get()

memberData := &GuildMember{}

err = rows.StructScan(&memberData)

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

Expand Down
4 changes: 2 additions & 2 deletions server/channelserver/handlers_festa.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,15 @@ func handleMsgMhfEnumerateFestaMember(s *Session, db *sqlx.DB, p mhfpacket.MHFPa
s.DoAckSimpleFail(pkt.AckHandle, make([]byte, 4))
return
}
members, err := GetGuildMembers(s, guild.ID, false)
members, err := service.GetGuildMembers(guild.ID, false)
if err != nil {
s.DoAckSimpleFail(pkt.AckHandle, make([]byte, 4))
return
}
sort.Slice(members, func(i, j int) bool {
return members[i].Souls > members[j].Souls
})
var validMembers []*GuildMember
var validMembers []*service.GuildMember
for _, member := range members {
if member.Souls > 0 {
validMembers = append(validMembers, member)
Expand Down
30 changes: 15 additions & 15 deletions server/channelserver/handlers_guild.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func HandleMsgMhfOperateGuild(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgMhfOperateGuild)

guild, err := service.GetGuildInfoByID(pkt.GuildID)
characterGuildInfo, err := GetCharacterGuildData(s, s.CharID)
characterGuildInfo, err := service.GetCharacterGuildData(s.CharID)
if err != nil {
s.DoAckSimpleFail(pkt.AckHandle, make([]byte, 4))
return
Expand All @@ -74,7 +74,7 @@ func HandleMsgMhfOperateGuild(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket) {
}
bf.WriteUint32(uint32(response))
case mhfpacket.OperateGuildResign:
guildMembers, err := GetGuildMembers(s, guild.ID, false)
guildMembers, err := service.GetGuildMembers(guild.ID, false)
if err == nil {
sort.Slice(guildMembers[:], func(i, j int) bool {
return guildMembers[i].OrderIndex < guildMembers[j].OrderIndex
Expand All @@ -84,8 +84,8 @@ func HandleMsgMhfOperateGuild(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket) {
guild.LeaderCharID = guildMembers[i].CharID
guildMembers[0].OrderIndex = guildMembers[i].OrderIndex
guildMembers[i].OrderIndex = 1
guildMembers[0].Save(s)
guildMembers[i].Save(s)
guildMembers[0].Save()
guildMembers[i].Save()
bf.WriteUint32(guildMembers[i].CharID)
break
}
Expand Down Expand Up @@ -250,7 +250,7 @@ func handleDonateRP(s *Session, amount uint16, guild *service.Guild, _type int)
}

func handleAvoidLeadershipUpdate(s *Session, pkt *mhfpacket.MsgMhfOperateGuild, avoidLeadership bool) {
characterGuildData, err := GetCharacterGuildData(s, s.CharID)
characterGuildData, err := service.GetCharacterGuildData(s.CharID)

if err != nil {
s.DoAckSimpleFail(pkt.AckHandle, make([]byte, 4))
Expand All @@ -259,7 +259,7 @@ func handleAvoidLeadershipUpdate(s *Session, pkt *mhfpacket.MsgMhfOperateGuild,

characterGuildData.AvoidLeadership = avoidLeadership

err = characterGuildData.Save(s)
err = characterGuildData.Save()

if err != nil {
s.DoAckSimpleFail(pkt.AckHandle, make([]byte, 4))
Expand All @@ -279,7 +279,7 @@ func HandleMsgMhfOperateGuildMember(s *Session, db *sqlx.DB, p mhfpacket.MHFPack
return
}

actorCharacter, err := GetCharacterGuildData(s, s.CharID)
actorCharacter, err := service.GetCharacterGuildData(s.CharID)

if err != nil || (!actorCharacter.IsSubLeader() && guild.LeaderCharID != s.CharID) {
s.DoAckSimpleFail(pkt.AckHandle, make([]byte, 4))
Expand Down Expand Up @@ -351,7 +351,7 @@ func HandleMsgMhfInfoGuild(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket) {
guildComment := stringsupport.UTF8ToSJIS(guild.Comment)
guildLeaderName := stringsupport.UTF8ToSJIS(guild.LeaderName)

characterGuildData, err := GetCharacterGuildData(s, s.CharID)
characterGuildData, err := service.GetCharacterGuildData(s.CharID)
characterJoinedAt := uint32(0xFFFFFFFF)

if characterGuildData != nil && characterGuildData.JoinedAt != nil {
Expand Down Expand Up @@ -503,7 +503,7 @@ func HandleMsgMhfInfoGuild(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket) {
bf.WriteUint32(0) // No alliance
}

applicants, err := GetGuildMembers(s, guild.ID, true)
applicants, err := service.GetGuildMembers(guild.ID, true)
if err != nil || (characterGuildData != nil && !characterGuildData.CanRecruit()) {
bf.WriteUint16(0)
} else {
Expand Down Expand Up @@ -834,7 +834,7 @@ func HandleMsgMhfEnumerateGuildMember(s *Session, db *sqlx.DB, p mhfpacket.MHFPa
return
}

guildMembers, err := GetGuildMembers(s, guild.ID, false)
guildMembers, err := service.GetGuildMembers(guild.ID, false)

if err != nil {
s.Logger.Error("failed to retrieve guild")
Expand Down Expand Up @@ -884,7 +884,7 @@ func HandleMsgMhfEnumerateGuildMember(s *Session, db *sqlx.DB, p mhfpacket.MHFPa
if guild.AllianceID > 0 {
bf.WriteUint16(alliance.TotalMembers - uint16(len(guildMembers)))
if guild.ID != alliance.ParentGuildID {
mems, err := GetGuildMembers(s, alliance.ParentGuildID, false)
mems, err := service.GetGuildMembers(alliance.ParentGuildID, false)
if err != nil {
panic(err)
}
Expand All @@ -893,7 +893,7 @@ func HandleMsgMhfEnumerateGuildMember(s *Session, db *sqlx.DB, p mhfpacket.MHFPa
}
}
if guild.ID != alliance.SubGuild1ID {
mems, err := GetGuildMembers(s, alliance.SubGuild1ID, false)
mems, err := service.GetGuildMembers(alliance.SubGuild1ID, false)
if err != nil {
panic(err)
}
Expand All @@ -902,7 +902,7 @@ func HandleMsgMhfEnumerateGuildMember(s *Session, db *sqlx.DB, p mhfpacket.MHFPa
}
}
if guild.ID != alliance.SubGuild2ID {
mems, err := GetGuildMembers(s, alliance.SubGuild2ID, false)
mems, err := service.GetGuildMembers(alliance.SubGuild2ID, false)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -937,7 +937,7 @@ func HandleMsgMhfGetGuildManageRight(s *Session, db *sqlx.DB, p mhfpacket.MHFPac

bf := byteframe.NewByteFrame()
bf.WriteUint32(uint32(guild.MemberCount))
members, _ := GetGuildMembers(s, guild.ID, false)
members, _ := service.GetGuildMembers(guild.ID, false)
for _, member := range members {
bf.WriteUint32(member.CharID)
bf.WriteBool(member.Recruiter)
Expand Down Expand Up @@ -1021,7 +1021,7 @@ func HandleMsgMhfUpdateGuildIcon(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket)
panic(err)
}

characterInfo, err := GetCharacterGuildData(s, s.CharID)
characterInfo, err := service.GetCharacterGuildData(s.CharID)

if err != nil {
panic(err)
Expand Down
4 changes: 2 additions & 2 deletions server/channelserver/handlers_guild_scout.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
func HandleMsgMhfPostGuildScout(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgMhfPostGuildScout)

actorCharGuildData, err := GetCharacterGuildData(s, s.CharID)
actorCharGuildData, err := service.GetCharacterGuildData(s.CharID)

if err != nil {
s.DoAckBufFail(pkt.AckHandle, make([]byte, 4))
Expand Down Expand Up @@ -95,7 +95,7 @@ func HandleMsgMhfPostGuildScout(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket)
func HandleMsgMhfCancelGuildScout(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgMhfCancelGuildScout)

guildCharData, err := GetCharacterGuildData(s, s.CharID)
guildCharData, err := service.GetCharacterGuildData(s.CharID)

if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion server/channelserver/handlers_house.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func handleMsgMhfEnumerateHouse(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket)
if err != nil || guild == nil {
break
}
guildMembers, err := GetGuildMembers(s, guild.ID, false)
guildMembers, err := service.GetGuildMembers(guild.ID, false)
if err != nil {
break
}
Expand Down
4 changes: 2 additions & 2 deletions server/channelserver/handlers_mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ func handleMsgMhfSendMail(s *Session, db *sqlx.DB, p mhfpacket.MHFPacket) {
`

if pkt.RecipientID == 0 { // Guild mail
g, err := GetGuildInfoByCharacterId(s, s.CharID)
g, err := service.GetGuildInfoByCharacterId(s.CharID)
if err != nil {
s.Logger.Error("Failed to get guild info for mail")
s.DoAckSimpleSucceed(pkt.AckHandle, make([]byte, 4))
return
}
gm, err := GetGuildMembers(s, g.ID, false)
gm, err := service.GetGuildMembers(g.ID, false)
if err != nil {
s.Logger.Error("Failed to get guild members for mail")
s.DoAckSimpleSucceed(pkt.AckHandle, make([]byte, 4))
Expand Down

0 comments on commit 674ac9b

Please sign in to comment.