diff --git a/cmd/console/cmd.go b/cmd/console/cmd.go index 53eeabd..8f6078d 100644 --- a/cmd/console/cmd.go +++ b/cmd/console/cmd.go @@ -56,6 +56,7 @@ func init() { RootCmd.AddCommand(commands.UpdateStripePriceId) RootCmd.AddCommand(commands.ClanRecalculateCommand) RootCmd.AddCommand(commands.RemoveUnrankedClanScores) + RootCmd.AddCommand(commands.ClanUnrankAllCommand) // Migrations RootCmd.AddCommand(migrations.MigrationPlaylistMapsetCmd) diff --git a/cmd/console/commands/clan_rank_map.go b/cmd/console/commands/clan_rank_map.go index 2369020..718724c 100644 --- a/cmd/console/commands/clan_rank_map.go +++ b/cmd/console/commands/clan_rank_map.go @@ -13,42 +13,38 @@ var ClanRankMapCmd = &cobra.Command{ Use: "clan:rank:map", Short: "Ranks a map for clans", Run: func(cmd *cobra.Command, args []string) { - const increment = 17 - for i := 1; i <= 2; i++ { - for j := 0; j < 3; j++ { - mapQua, err := getRandomMap(i, float64(j * increment), float64((j + 1) * increment)) + mapQua, err := getRandomMap(i, 0, 100) - if err != nil { - logrus.Error("Error retrieving random map", err) - } + if err != nil { + logrus.Error("Error retrieving random map", err) + } - if err := db.UpdateMapClanRanked(mapQua.Id, true); err != nil { - logrus.Error("Error updating clan ranked status: ", err) - return - } - - clanUsers, err := db.GetAllUsersInAClan() - - if err != nil { - logrus.Error("Error retrieving users a part of a clan", err) + if err := db.UpdateMapClanRanked(mapQua.Id, true); err != nil { + logrus.Error("Error updating clan ranked status: ", err) + return + } + + clanUsers, err := db.GetAllUsersInAClan() + + if err != nil { + logrus.Error("Error retrieving users a part of a clan", err) + return + } + + for _, user := range clanUsers { + if err := db.NewClanMapRankedNotification(mapQua, user.Id).Insert(); err != nil { + logrus.Error("Error inserting clan map ranked notification", err) return } - - for _, user := range clanUsers { - if err := db.NewClanMapRankedNotification(mapQua, user.Id).Insert(); err != nil { - logrus.Error("Error inserting clan map ranked notification", err) - return - } - } - - if err := sendClanMapToRedis(mapQua); err != nil { - logrus.Error("Error sending clan map to redis: ", err) - } - - logrus.Info("Ranked Clan Map: ", mapQua.Id, mapQua, mapQua.DifficultyRating) - _ = webhooks.SendClanRankedWebhook(mapQua) } + + if err := sendClanMapToRedis(mapQua); err != nil { + logrus.Error("Error sending clan map to redis: ", err) + } + + logrus.Info("Ranked Clan Map: ", mapQua.Id, mapQua, mapQua.DifficultyRating) + _ = webhooks.SendClanRankedWebhook(mapQua) } }, } @@ -58,7 +54,7 @@ func getRandomMap(mode int, minDiff float64, maxDiff float64) (*db.MapQua, error result := db.SQL.Raw("SELECT * FROM maps "+ "WHERE maps.clan_ranked = 0 AND maps.ranked_status = 2 AND maps.game_mode = ? AND "+ - "maps.difficulty_rating >= ? AND maps.difficulty_rating <= ? " + + "maps.difficulty_rating >= ? AND maps.difficulty_rating <= ? "+ "ORDER BY RAND() LIMIT 1;", mode, minDiff, maxDiff). Scan(&mapQua) diff --git a/cmd/console/commands/clan_unrank_all.go b/cmd/console/commands/clan_unrank_all.go new file mode 100644 index 0000000..cf53395 --- /dev/null +++ b/cmd/console/commands/clan_unrank_all.go @@ -0,0 +1,49 @@ +package commands + +import ( + "github.com/Quaver/api2/db" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +var ClanUnrankAllCommand = &cobra.Command{ + Use: "clan:unrank:all", + Short: "Unranks all clan maps", + Run: func(cmd *cobra.Command, args []string) { + maps := make([]*db.MapQua, 0) + + result := db.SQL. + Where("clan_ranked = 1"). + Find(&maps) + + if result.Error != nil { + logrus.Error(result.Error) + return + } + + for _, mapQua := range maps { + if err := db.UpdateMapClanRanked(mapQua.Id, false); err != nil { + logrus.Error("Error updating map clan ranked: ", err) + return + } + + result := db.SQL.Model(&db.Score{}). + Where("map_md5 = ?", mapQua.MD5). + Update("clan_id", nil) + + if result.Error != nil { + logrus.Error("Error resetting clan_id for scores: ", result.Error) + return + } + + result = db.SQL.Delete(&db.ClanScore{}, "map_md5 = ?", mapQua.MD5) + + if result.Error != nil { + logrus.Error("Error deleting clan scores: ", result.Error) + return + } + + logrus.Info("Successfully unranked clan map: ", mapQua) + } + }, +} diff --git a/db/maps.go b/db/maps.go index db7dc7c..5d6aed8 100644 --- a/db/maps.go +++ b/db/maps.go @@ -162,10 +162,16 @@ func UpdateMapDifficultyRating(id int, difficultyRating float64) error { // UpdateMapClanRanked Updates the clan ranked status of a map func UpdateMapClanRanked(id int, clanRanked bool) error { + var timeRanked int64 + + if clanRanked { + timeRanked = time.Now().UnixMilli() + } + result := SQL.Model(&MapQua{}). Where("id = ?", id). Update("clan_ranked", clanRanked). - Update("date_clan_ranked", time.Now().UnixMilli()) + Update("date_clan_ranked", timeRanked) return result.Error } diff --git a/db/scores.go b/db/scores.go index 8316bb6..e1101e4 100644 --- a/db/scores.go +++ b/db/scores.go @@ -595,7 +595,7 @@ func GetClanPlayerScoresOnMap(md5 string, clanId int, callAfterFind bool) ([]*Sc AND s.clan_id = ? AND s.failed = 0 ) - %v`, getSelectUserScoreboardQuery(10, false)), md5, clanId). + %v`, getSelectUserScoreboardQuery(5, false)), md5, clanId). Scan(&scores) if result.Error != nil { diff --git a/webhooks/webhooks.go b/webhooks/webhooks.go index 5a6d000..1908e42 100644 --- a/webhooks/webhooks.go +++ b/webhooks/webhooks.go @@ -362,8 +362,7 @@ func SendClanFirstPlaceWebhook(clan *db.Clan, mapQua *db.MapQua, newScore *db.Cl embed := discord.NewEmbedBuilder(). SetAuthor(fmt.Sprintf("[%v] %v", clan.Tag, clan.Name), fmt.Sprintf("https://two.quavergame.com/clans/%v", newScore.ClanId), clan.AvatarURL()). - SetDescription("🏆 Achieved a new first place clan score!\n\n"+ - "**Note: Clans is in private beta which is only available to [donators](https://two.quavergame.com/donate).**"). + SetDescription("🏆 Achieved a new first place clan score!"). AddField("Map", fmt.Sprintf("[%v](https://two.quavergame.com/mapset/%v/map/%v?type=clan)", mapQua, mapQua.MapsetId, mapQua.Id), false). AddField("Overall Rating", fmt.Sprintf("%.2f", newScore.OverallRating), true). AddField("Overall Accuracy", fmt.Sprintf("%.2f%%", newScore.OverallAccuracy), true). @@ -396,8 +395,7 @@ func SendClanRankedWebhook(mapQua *db.MapQua) error { embed := discord.NewEmbedBuilder(). SetTitle("✅ New Map Clan Ranked!"). - SetDescription("A new map has been clan ranked and is now available to get scores on.\n\n"+ - "**Note: Clans is in private beta which is only available to [donators](https://two.quavergame.com/donate).**"). + SetDescription("A new map has been clan ranked and is now available to get scores on."). AddField("Map", fmt.Sprintf("[%v](https://two.quavergame.com/mapset/%v/map/%v?type=clan)", mapQua, mapQua.MapsetId, mapQua.Id), false). AddField("Creator", fmt.Sprintf("[%v](https://quavergame.com/user/%v)", mapQua.CreatorUsername, mapQua.CreatorId), true). AddField("Game Mode", enums.GetShorthandGameModeString(mapQua.GameMode), true).