From 5b408dfee04cb021196e37044b75dcd690f36e14 Mon Sep 17 00:00:00 2001 From: AlexPresso Date: Tue, 8 Mar 2022 22:07:50 +0100 Subject: [PATCH 1/4] Zunivers v1.0 changes --- config.default.json | 4 +++- services/webhookService.go | 8 +++++++- services/zuniversService.go | 5 +++++ structures/Challenge.go | 17 +++++++++++++++++ structures/Patchnote.go | 2 +- tasks/checkChallenges.go | 38 +++++++++++++++++++++++++++++++++++++ tasks/taskManager.go | 1 + 7 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 structures/Challenge.go create mode 100644 tasks/checkChallenges.go diff --git a/config.default.json b/config.default.json index c762a3d..94cf454 100644 --- a/config.default.json +++ b/config.default.json @@ -26,6 +26,8 @@ "new_achievement_category": "Une catégorie de succès a été ajoutée.", "achievement_category_changed": "Une catégorie de succès a été modifiée.", "new_achievement": "Un succès a été ajouté.", - "achievement_changed": "Un succès a été modifié." + "achievement_changed": "Un succès a été modifié.", + "challenge_changed": "Un challenge a été modifié.", + "new_challenge": "Un nouveau challenge a débuté" } } \ No newline at end of file diff --git a/services/webhookService.go b/services/webhookService.go index 006a35e..9eeeae4 100644 --- a/services/webhookService.go +++ b/services/webhookService.go @@ -152,7 +152,13 @@ func processDisplay(field *discord.EmbedField, oldValue, newValue interface{}, p } } - field.Value += fmt.Sprintf("__%s:__ %s`%s`\n", parts[1], oldValueText, fmt.Sprint(newValue)) + split := strings.Split(parts[1], "|") + format := "%s" + if len(split) > 1 { + format = split[1] + } + + field.Value += fmt.Sprintf("__%s:__ %s`%s`\n", split[0], fmt.Sprintf(format, oldValueText), fmt.Sprintf(format, newValue)) } func processImage(embed *discord.Embed, newValue interface{}, parts []string) { diff --git a/services/zuniversService.go b/services/zuniversService.go index eefd954..2c4bc2a 100644 --- a/services/zuniversService.go +++ b/services/zuniversService.go @@ -60,3 +60,8 @@ func FetchAchievements(categoryId string) (achProgress []structures.AchievementP err = utils.Request("/public/achievement/Alex'Presso%235480/"+categoryId, "GET", nil, &achProgress) return } + +func FetchChallenges() (chProgress []structures.ChallengeProgress, err error) { + err = utils.Request("/public/challenge", "GET", nil, &chProgress) + return +} diff --git a/structures/Challenge.go b/structures/Challenge.go new file mode 100644 index 0000000..e959951 --- /dev/null +++ b/structures/Challenge.go @@ -0,0 +1,17 @@ +package structures + +import "gorm.io/gorm" + +type ChallengeProgress struct { + Challenge *Challenge `json:"challenge"` +} + +type Challenge struct { + gorm.Model + + ChallengeID string `json:"id"` + Description string `json:"description" zu:"display=Description"` + RewardLoreDust string `json:"rewardLoreDust" zu:"display=Poussière de lore"` + Score string `json:"score" zu:"display=Score"` + Type string `json:"type" zu:"display=Type"` +} diff --git a/structures/Patchnote.go b/structures/Patchnote.go index a6439b5..b9595f6 100644 --- a/structures/Patchnote.go +++ b/structures/Patchnote.go @@ -9,7 +9,7 @@ type Patchnote struct { PatchnoteID string `json:"id"` Title string `json:"title" zu:"display=Titre"` - CreatedBy string `json:"createdBy" zu:"display=Auteur"` + CreatedBy string `json:"createdBy" zu:"display=Auteur|<@%s>"` Date *DateTime `json:"date" zu:"display=Date"` ImageUrl string `json:"imageUrl" zu:"imageUrl=%s"` Slug string `json:"slug" zu:"url=/post/%s"` diff --git a/tasks/checkChallenges.go b/tasks/checkChallenges.go new file mode 100644 index 0000000..92b341b --- /dev/null +++ b/tasks/checkChallenges.go @@ -0,0 +1,38 @@ +package tasks + +import ( + "github.com/alexpresso/zunivers-webhooks/services" + "github.com/alexpresso/zunivers-webhooks/structures" + "github.com/alexpresso/zunivers-webhooks/structures/discord" + "github.com/alexpresso/zunivers-webhooks/utils" + "gorm.io/gorm" +) + +func checkChallenges(db *gorm.DB, embeds *[]discord.Embed) { + chProgress, err := services.FetchChallenges() + if err != nil { + utils.Log("An error occurred while fetching challenges: " + err.Error()) + return + } + + dbChallengesMap := make(map[string]*structures.Challenge) + var challenges []*structures.Challenge + + for i := 0; i < len(chProgress); i++ { + challenge := &chProgress[i].Challenge + challenges = append(challenges, *challenge) + dbChallenge := dbChallengesMap[(*challenge).ChallengeID] + + if dbChallenge != nil { + (*challenge).ID = dbChallenge.ID + + if utils.AreDifferent(**challenge, *dbChallenge) { + *embeds = append(*embeds, *services.MakeEmbed("challenge_changed", *dbChallenge, **challenge)) + } + } else if len(dbChallengesMap) > 0 { + *embeds = append(*embeds, *services.MakeEmbed("new_challenge", nil, **challenge)) + } + } + + db.Save(&challenges) +} diff --git a/tasks/taskManager.go b/tasks/taskManager.go index 8e06187..6a69ccd 100644 --- a/tasks/taskManager.go +++ b/tasks/taskManager.go @@ -29,6 +29,7 @@ func checkInfos(db *gorm.DB) { checkEvents(db, embeds) checkAchievementCategories(db, embeds) checkSeason(db, embeds) + checkChallenges(db, embeds) utils.Log("Checked for infos.") From 507cda419d82eae87cb266cdcdf57ca44446aaba Mon Sep 17 00:00:00 2001 From: AlexPresso Date: Tue, 8 Mar 2022 22:11:46 +0100 Subject: [PATCH 2/4] Add challenge db migration --- database/database.go | 1 + 1 file changed, 1 insertion(+) diff --git a/database/database.go b/database/database.go index 848af63..819a152 100644 --- a/database/database.go +++ b/database/database.go @@ -27,6 +27,7 @@ func Init() (db *gorm.DB) { &structures.Event{}, &structures.AchievementCategory{}, &structures.Achievement{}, + &structures.Challenge{}, ) if err != nil { From 85440c82376152440dbb822faa787c8c596016ec Mon Sep 17 00:00:00 2001 From: AlexPresso Date: Tue, 22 Mar 2022 17:39:24 +0100 Subject: [PATCH 3/4] Fixes and improvements --- README.md | 19 ++++++++++--------- services/webhookService.go | 19 ++++++++++--------- structures/Challenge.go | 4 ++-- structures/Event.go | 2 +- structures/Item.go | 4 ++-- structures/Patchnote.go | 2 +- tasks/checkChallenges.go | 9 ++++++++- 7 files changed, 34 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index e54327e..ecbfabe 100644 --- a/README.md +++ b/README.md @@ -11,15 +11,16 @@ This project is not affiliated with the ZUnivers's project. It's a community pro ## Features - ✅ `!daily` reminder -- ✅ Notify for new patchnotes -- ✅ Notify for configuration changes -- ✅ Notify for new webapp versions -- ✅ Notify for new items/item changes -- ✅ Notify for new packs/packs changes -- ✅ Notify for new banners/banners changes -- ✅ Notify for new "ascension" season -- ✅ Notify for new event/events changes -- ✅ Notify for new achievements/achievements changes +- ✅ Notifies for new patchnotes +- ✅ Notifies for configuration changes +- ✅ Notifies for new webapp versions +- ✅ Notifies for new items/item changes +- ✅ Notifies for new packs/packs changes +- ✅ Notifies for new banners/banners changes +- ✅ Notifies for new "ascension" season +- ✅ Notifies for new event/events changes +- ✅ Notifies for new achievements/achievements changes +- ✅ Notifies for new challenges - ✅ Multiple webhooks dispatching ## Usage diff --git a/services/webhookService.go b/services/webhookService.go index 9eeeae4..1cbb996 100644 --- a/services/webhookService.go +++ b/services/webhookService.go @@ -141,24 +141,25 @@ func fillEmbed(embed *discord.Embed, oldObject, newObject interface{}) { } func processDisplay(field *discord.EmbedField, oldValue, newValue interface{}, parts []string) { + split := strings.Split(parts[1], "|") + format := "`%v`" + if len(split) > 1 { + format = split[1] + } + oldValueText := "" if oldValue != nil { if utils.IsTime(oldValue) { if utils.TimeDifference(oldValue, newValue) { - oldValueText = fmt.Sprintf("`%s` → ", fmt.Sprint(oldValue)) + oldValueText = fmt.Sprintf("`%s` → ", fmt.Sprintf(format, oldValue)) } } else if oldValue != newValue { - oldValueText = fmt.Sprintf("`%s` → ", fmt.Sprint(oldValue)) + oldValueText = fmt.Sprintf("`%s` → ", fmt.Sprintf(format, oldValue)) } } - split := strings.Split(parts[1], "|") - format := "%s" - if len(split) > 1 { - format = split[1] - } - - field.Value += fmt.Sprintf("__%s:__ %s`%s`\n", split[0], fmt.Sprintf(format, oldValueText), fmt.Sprintf(format, newValue)) + value := fmt.Sprintf("__%s:__ %s%s\n", split[0], oldValueText, fmt.Sprintf(format, newValue)) + field.Value += value } func processImage(embed *discord.Embed, newValue interface{}, parts []string) { diff --git a/structures/Challenge.go b/structures/Challenge.go index e959951..9ea35ff 100644 --- a/structures/Challenge.go +++ b/structures/Challenge.go @@ -11,7 +11,7 @@ type Challenge struct { ChallengeID string `json:"id"` Description string `json:"description" zu:"display=Description"` - RewardLoreDust string `json:"rewardLoreDust" zu:"display=Poussière de lore"` - Score string `json:"score" zu:"display=Score"` + RewardLoreDust uint32 `json:"rewardLoreDust" zu:"display=Poussière de lore"` + Score uint32 `json:"score" zu:"display=Score"` Type string `json:"type" zu:"display=Type"` } diff --git a/structures/Event.go b/structures/Event.go index e180c90..b6bfc9e 100644 --- a/structures/Event.go +++ b/structures/Event.go @@ -10,5 +10,5 @@ type Event struct { EndDate *DateTime `json:"endDate" zu:"display=Fin"` ImageURL string `json:"imageUrl" zu:"imageUrl=%s"` Name string `json:"name" zu:"display=Nom"` - BalanceCost int `json:"balanceCost" zu:"Coût im"` + BalanceCost int `json:"balanceCost" zu:"display=Coût d'invocation"` } diff --git a/structures/Item.go b/structures/Item.go index afe632b..7fcf947 100644 --- a/structures/Item.go +++ b/structures/Item.go @@ -9,12 +9,12 @@ type Item struct { Genre string `json:"genre" zu:"display=Genre"` Name string `json:"name" zu:"display=Nom"` Rarity uint32 `json:"rarity" zu:"display=Rareté"` - Slug string `json:"slug" zu:"url=/item/%s"` + Slug string `json:"slug" zu:"url=/carte/%s"` } type Pack struct { gorm.Model PackID string `json:"id"` - Name string `json:"name" zu:"display=Nom"` + Name string `json:"name" zu:"display=Nom;url=/catalogue/%s"` } diff --git a/structures/Patchnote.go b/structures/Patchnote.go index b9595f6..fff270a 100644 --- a/structures/Patchnote.go +++ b/structures/Patchnote.go @@ -12,5 +12,5 @@ type Patchnote struct { CreatedBy string `json:"createdBy" zu:"display=Auteur|<@%s>"` Date *DateTime `json:"date" zu:"display=Date"` ImageUrl string `json:"imageUrl" zu:"imageUrl=%s"` - Slug string `json:"slug" zu:"url=/post/%s"` + Slug string `json:"slug" zu:"url=/actu/%s"` } diff --git a/tasks/checkChallenges.go b/tasks/checkChallenges.go index 92b341b..4a1bd4a 100644 --- a/tasks/checkChallenges.go +++ b/tasks/checkChallenges.go @@ -15,8 +15,15 @@ func checkChallenges(db *gorm.DB, embeds *[]discord.Embed) { return } - dbChallengesMap := make(map[string]*structures.Challenge) var challenges []*structures.Challenge + var dbChallenges []structures.Challenge + dbChallengesMap := make(map[string]*structures.Challenge) + + db.Find(&dbChallenges) + for _, chall := range dbChallenges { + chall := chall + dbChallengesMap[chall.ChallengeID] = &chall + } for i := 0; i < len(chProgress); i++ { challenge := &chProgress[i].Challenge From 437ede74beca7d991d18f6b62cba77aaaa476ebd Mon Sep 17 00:00:00 2001 From: AlexPresso Date: Tue, 22 Mar 2022 17:40:48 +0100 Subject: [PATCH 4/4] Typo --- config.default.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.default.json b/config.default.json index 94cf454..52d927a 100644 --- a/config.default.json +++ b/config.default.json @@ -21,13 +21,13 @@ "new_banner": "Une nouvelle bannière a été ajoutée !", "banner_changed": "Une bannière a été modifiée !", "new_event": "Un nouvel event a débuté !", - "event_changed": "Un event a été modifié", + "event_changed": "Un event a été modifié3", "event_removed": "Un event s'est terminé.", "new_achievement_category": "Une catégorie de succès a été ajoutée.", "achievement_category_changed": "Une catégorie de succès a été modifiée.", "new_achievement": "Un succès a été ajouté.", "achievement_changed": "Un succès a été modifié.", "challenge_changed": "Un challenge a été modifié.", - "new_challenge": "Un nouveau challenge a débuté" + "new_challenge": "Un nouveau challenge a débuté3" } } \ No newline at end of file