Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clans Reset #139

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/console/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
58 changes: 27 additions & 31 deletions cmd/console/commands/clan_rank_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
}
Expand All @@ -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)

Expand Down
49 changes: 49 additions & 0 deletions cmd/console/commands/clan_unrank_all.go
Original file line number Diff line number Diff line change
@@ -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)
}
},
}
8 changes: 7 additions & 1 deletion db/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion db/scores.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions webhooks/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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).
Expand Down
Loading