From 989de7b4ae453a0287b9045fc34e188b9704d6cb Mon Sep 17 00:00:00 2001 From: Bernardo Heynemann Date: Wed, 3 Aug 2016 11:32:20 -0300 Subject: [PATCH] Previous owner in our model --- api/clan.go | 20 +++++++++++------- models/clan.go | 51 ++++++++++++++++++++++++++------------------- models/clan_test.go | 24 ++++++++++++++------- 3 files changed, 58 insertions(+), 37 deletions(-) diff --git a/api/clan.go b/api/clan.go index cd947333..1ab63898 100644 --- a/api/clan.go +++ b/api/clan.go @@ -183,7 +183,7 @@ func UpdateClanHandler(app *App) func(c *iris.Context) { } } -func dispatchClanOwnershipChangeHook(app *App, db models.DB, hookType int, gameID, publicID string) error { +func dispatchClanOwnershipChangeHook(app *App, db models.DB, hookType int, gameID, publicID string, previousOwner *models.Player) error { l := app.Logger.With( zap.String("source", "clanHandler"), zap.String("operation", "dispatchClanOwnershipChangeHook"), @@ -206,13 +206,17 @@ func dispatchClanOwnershipChangeHook(app *App, db models.DB, hookType int, gameI newOwnerJSON := newOwner.Serialize() delete(newOwnerJSON, "gameID") + previousOwnerJSON := previousOwner.Serialize() + delete(previousOwnerJSON, "gameID") + clanJSON := clan.Serialize() delete(clanJSON, "gameID") result := map[string]interface{}{ - "gameID": gameID, - "clan": clanJSON, - "newOwner": newOwnerJSON, + "gameID": gameID, + "clan": clanJSON, + "previousOwner": previousOwnerJSON, + "newOwner": newOwnerJSON, } l.Debug("Dispatching hook...") app.DispatchHooks(gameID, hookType, result) @@ -244,7 +248,7 @@ func LeaveClanHandler(app *App) func(c *iris.Context) { l.Debug("DB Connection successful.") l.Debug("Leaving clan...") - err = models.LeaveClan( + previousOwner, err := models.LeaveClan( db, gameID, publicID, @@ -262,7 +266,7 @@ func LeaveClanHandler(app *App) func(c *iris.Context) { } l.Debug("Clan left successfully.") - err = dispatchClanOwnershipChangeHook(app, db, models.ClanLeftHook, gameID, publicID) + err = dispatchClanOwnershipChangeHook(app, db, models.ClanLeftHook, gameID, publicID, previousOwner) if err != nil { FailWith(500, err.Error(), c) return @@ -312,7 +316,7 @@ func TransferOwnershipHandler(app *App) func(c *iris.Context) { l.Debug("DB Connection successful.") l.Debug("Transferring clan ownership...") - err = models.TransferClanOwnership( + previousOwner, err := models.TransferClanOwnership( db, gameID, publicID, @@ -327,7 +331,7 @@ func TransferOwnershipHandler(app *App) func(c *iris.Context) { return } - err = dispatchClanOwnershipChangeHook(app, db, models.ClanOwnershipTransferredHook, gameID, publicID) + err = dispatchClanOwnershipChangeHook(app, db, models.ClanOwnershipTransferredHook, gameID, publicID, previousOwner) if err != nil { l.Error("Clan ownership transfer hook dispatch failed.", zap.Error(err)) FailWith(500, err.Error(), c) diff --git a/models/clan.go b/models/clan.go index d120e6de..7a8d59d4 100644 --- a/models/clan.go +++ b/models/clan.go @@ -229,17 +229,21 @@ func CreateClan(db DB, gameID, publicID, name, ownerPublicID string, metadata ma } // LeaveClan allows the clan owner to leave the clan and transfer the clan ownership to the next player in line -func LeaveClan(db DB, gameID, publicID string) error { +func LeaveClan(db DB, gameID, publicID string) (*Player, error) { clan, err := GetClanByPublicID(db, gameID, publicID) if err != nil { - return err + return nil, err } oldOwnerID := clan.OwnerID err = IncrementPlayerOwnershipCount(db, oldOwnerID, -1) if err != nil { - return err + return nil, err + } + oldOwner, err := GetPlayerByID(db, oldOwnerID) + if err != nil { + return nil, err } newOwnerMembership, err := GetOldestMemberWithHighestLevel(db, gameID, publicID) @@ -249,55 +253,60 @@ func LeaveClan(db DB, gameID, publicID string) error { // Clan has no members, delete it _, err = db.Delete(clan) if err != nil { - return err + return nil, err } - return nil + return oldOwner, nil } - return err + return nil, err } clan.OwnerID = newOwnerMembership.PlayerID _, err = db.Update(clan) if err != nil { - return err + return nil, err } err = deleteMembershipHelper(db, newOwnerMembership, newOwnerMembership.PlayerID) if err != nil { - return err + return nil, err } err = IncrementPlayerOwnershipCount(db, newOwnerMembership.PlayerID, 1) if err != nil { - return err + return nil, err } - return nil + return oldOwner, nil } // TransferClanOwnership allows the clan owner to transfer the clan ownership to the a clan member -func TransferClanOwnership(db DB, gameID, clanPublicID, playerPublicID string, levels map[string]interface{}, maxLevel int) error { +func TransferClanOwnership(db DB, gameID, clanPublicID, playerPublicID string, levels map[string]interface{}, maxLevel int) (*Player, error) { clan, err := GetClanByPublicID(db, gameID, clanPublicID) if err != nil { - return err + return nil, err } newOwnerMembership, err := GetValidMembershipByClanAndPlayerPublicID(db, gameID, clanPublicID, playerPublicID) if err != nil { - return err + return nil, err } oldOwnerID := clan.OwnerID clan.OwnerID = newOwnerMembership.PlayerID _, err = db.Update(clan) if err != nil { - return err + return nil, err + } + + oldOwner, err := GetPlayerByID(db, oldOwnerID) + if err != nil { + return nil, err } level := GetLevelByLevelInt(maxLevel, levels) if level == "" { - return &InvalidLevelForGameError{gameID, level} + return nil, &InvalidLevelForGameError{gameID, level} } oldOwnerMembership, err := GetDeletedMembershipByPlayerID(db, gameID, oldOwnerID) if err != nil { @@ -314,7 +323,7 @@ func TransferClanOwnership(db DB, gameID, clanPublicID, playerPublicID string, l UpdatedAt: util.NowMilli(), }) if err != nil { - return err + return nil, err } } else { oldOwnerMembership.Approved = true @@ -330,23 +339,23 @@ func TransferClanOwnership(db DB, gameID, clanPublicID, playerPublicID string, l err = deleteMembershipHelper(db, newOwnerMembership, newOwnerMembership.PlayerID) if err != nil { - return err + return nil, err } err = IncrementPlayerOwnershipCount(db, newOwnerMembership.PlayerID, 1) if err != nil { - return err + return nil, err } err = IncrementPlayerOwnershipCount(db, oldOwnerID, -1) if err != nil { - return err + return nil, err } err = IncrementPlayerMembershipCount(db, oldOwnerID, 1) if err != nil { - return err + return nil, err } - return nil + return oldOwner, nil } // UpdateClan updates an existing clan diff --git a/models/clan_test.go b/models/clan_test.go index e98ce71c..91275161 100644 --- a/models/clan_test.go +++ b/models/clan_test.go @@ -440,9 +440,11 @@ var _ = Describe("Clan Model", func() { _, clan, owner, _, memberships, err := GetClanWithMemberships(testDb, 1, 0, 0, 0, "", "") Expect(err).NotTo(HaveOccurred()) - err = LeaveClan(testDb, clan.GameID, clan.PublicID) + previousOwner, err := LeaveClan(testDb, clan.GameID, clan.PublicID) Expect(err).NotTo(HaveOccurred()) + Expect(previousOwner.ID).To(Equal(owner.ID)) + dbClan, err := GetClanByPublicID(testDb, clan.GameID, clan.PublicID) Expect(err).NotTo(HaveOccurred()) Expect(dbClan.OwnerID).To(Equal(memberships[0].PlayerID)) @@ -469,8 +471,10 @@ var _ = Describe("Clan Model", func() { _, clan, owner, _, _, err := GetClanWithMemberships(testDb, 0, 0, 0, 0, "", "") Expect(err).NotTo(HaveOccurred()) - err = LeaveClan(testDb, clan.GameID, clan.PublicID) + previousOwner, err := LeaveClan(testDb, clan.GameID, clan.PublicID) Expect(err).NotTo(HaveOccurred()) + Expect(previousOwner.ID).To(Equal(owner.ID)) + _, err = GetClanByPublicID(testDb, clan.GameID, clan.PublicID) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(Equal(fmt.Sprintf("Clan was not found with id: %s", clan.PublicID))) @@ -486,7 +490,7 @@ var _ = Describe("Clan Model", func() { _, clan, _, _, _, err := GetClanWithMemberships(testDb, 1, 0, 0, 0, "", "") Expect(err).NotTo(HaveOccurred()) - err = LeaveClan(testDb, clan.GameID, "-1") + _, err = LeaveClan(testDb, clan.GameID, "-1") Expect(err).To(HaveOccurred()) Expect(err.Error()).To(Equal("Clan was not found with id: -1")) }) @@ -498,7 +502,7 @@ var _ = Describe("Clan Model", func() { It("And first clan owner and next owner memberhip exists", func() { game, clan, owner, players, memberships, err := GetClanWithMemberships(testDb, 1, 0, 0, 0, "", "") Expect(err).NotTo(HaveOccurred()) - err = TransferClanOwnership( + previousOwner, err := TransferClanOwnership( testDb, clan.GameID, clan.PublicID, @@ -508,6 +512,8 @@ var _ = Describe("Clan Model", func() { ) Expect(err).NotTo(HaveOccurred()) + Expect(previousOwner.ID).To(Equal(owner.ID)) + dbClan, err := GetClanByPublicID(testDb, clan.GameID, clan.PublicID) Expect(err).NotTo(HaveOccurred()) Expect(dbClan.OwnerID).To(Equal(players[0].ID)) @@ -538,7 +544,7 @@ var _ = Describe("Clan Model", func() { game, clan, owner, players, memberships, err := GetClanWithMemberships(testDb, 2, 0, 0, 0, "", "") Expect(err).NotTo(HaveOccurred()) - err = TransferClanOwnership( + previousOwner, err := TransferClanOwnership( testDb, clan.GameID, clan.PublicID, @@ -547,8 +553,9 @@ var _ = Describe("Clan Model", func() { game.MaxMembershipLevel, ) Expect(err).NotTo(HaveOccurred()) + Expect(previousOwner.ID).To(Equal(owner.ID)) - err = TransferClanOwnership( + previousOwner, err = TransferClanOwnership( testDb, clan.GameID, clan.PublicID, @@ -557,6 +564,7 @@ var _ = Describe("Clan Model", func() { game.MaxMembershipLevel, ) Expect(err).NotTo(HaveOccurred()) + Expect(previousOwner.ID).To(Equal(players[0].ID)) dbClan, err := GetClanByPublicID(testDb, clan.GameID, clan.PublicID) Expect(err).NotTo(HaveOccurred()) @@ -600,7 +608,7 @@ var _ = Describe("Clan Model", func() { game, clan, _, players, _, err := GetClanWithMemberships(testDb, 1, 0, 0, 0, "", "") Expect(err).NotTo(HaveOccurred()) - err = TransferClanOwnership( + _, err = TransferClanOwnership( testDb, clan.GameID, "-1", @@ -616,7 +624,7 @@ var _ = Describe("Clan Model", func() { game, clan, _, _, _, err := GetClanWithMemberships(testDb, 1, 0, 0, 0, "", "") Expect(err).NotTo(HaveOccurred()) - err = TransferClanOwnership( + _, err = TransferClanOwnership( testDb, clan.GameID, clan.PublicID,