Skip to content

Commit

Permalink
Add command to recalc all clans
Browse files Browse the repository at this point in the history
  • Loading branch information
Swan committed Nov 24, 2024
1 parent 2ba82e4 commit 2c9c7e7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/console/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func init() {
RootCmd.AddCommand(commands.DeleteScoreCmd)
RootCmd.AddCommand(commands.FixStatsCmd)
RootCmd.AddCommand(commands.UpdateStripePriceId)
RootCmd.AddCommand(commands.ClanRecalculateCommand)

// Migrations
RootCmd.AddCommand(migrations.MigrationPlaylistMapsetCmd)
Expand Down
29 changes: 29 additions & 0 deletions cmd/console/commands/clan_recalc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package commands

import (
"github.com/Quaver/api2/db"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var ClanRecalculateCommand = &cobra.Command{
Use: "clan:recalc",
Short: "Recalculates all clans",
Run: func(cmd *cobra.Command, args []string) {
clans, err := db.GetClans()

if err != nil {
logrus.Error(err)
return
}

for _, clan := range clans {
if err := db.PerformFullClanRecalculation(clan); err != nil {
logrus.Error(err)
return
}

logrus.Info("Recalculated clan: ", clan.Id)
}
},
}
3 changes: 3 additions & 0 deletions db/clan_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ func PerformFullClanRecalculation(clan *Clan) error {
return err
}

if err := UpdateClanLeaderboard(clan, mode); err != nil {
return err
}
}

return nil
Expand Down
15 changes: 15 additions & 0 deletions db/clans.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ func (clan *Clan) Insert() error {
return nil
}

// GetClans Retrieves all clans from the db
func GetClans() ([]*Clan, error) {
clans := make([]*Clan, 0)

result := SQL.
Preload("Stats").
Find(&clans)

if result.Error != nil {
return nil, result.Error
}

return clans, nil
}

// GetClanById Gets a clan from the database by its id
func GetClanById(id int) (*Clan, error) {
var clan *Clan
Expand Down

0 comments on commit 2c9c7e7

Please sign in to comment.