refactor: introduce new converter: EnnoblementNotification -> *discordgo.MessageEmbed
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Dawid Wysokiński 2023-06-20 06:14:16 +02:00
parent e110807619
commit 5de9348f67
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892

View File

@ -36,7 +36,7 @@ func (e *executeMonitorsJob) Run() {
var wg sync.WaitGroup var wg sync.WaitGroup
for ch, embeds := range ennoblementNotificationsToEmbeds(notifications) { for ch, embeds := range (ennoblementNotificationToMessageEmbedConverter{notifications}).convert() {
wg.Add(1) wg.Add(1)
go func(ch string, embeds []*discordgo.MessageEmbed) { go func(ch string, embeds []*discordgo.MessageEmbed) {
@ -56,20 +56,24 @@ func (e *executeMonitorsJob) Run() {
) )
} }
func ennoblementNotificationsToEmbeds(ns []domain.EnnoblementNotification) map[string][]*discordgo.MessageEmbed { type ennoblementNotificationToMessageEmbedConverter struct {
ns []domain.EnnoblementNotification
}
func (c ennoblementNotificationToMessageEmbedConverter) convert() map[string][]*discordgo.MessageEmbed {
m := make(map[string]map[domain.EnnoblementNotificationType][]*discordgo.MessageEmbed) m := make(map[string]map[domain.EnnoblementNotificationType][]*discordgo.MessageEmbed)
timestamp := formatTimestamp(time.Now()) timestamp := formatTimestamp(time.Now())
for _, n := range ns { for _, n := range c.ns {
if _, ok := m[n.ChannelID]; !ok { if _, ok := m[n.ChannelID]; !ok {
m[n.ChannelID] = make(map[domain.EnnoblementNotificationType][]*discordgo.MessageEmbed) m[n.ChannelID] = make(map[domain.EnnoblementNotificationType][]*discordgo.MessageEmbed)
} }
embeds := m[n.ChannelID][n.Type] embeds := m[n.ChannelID][n.Type]
str := buildEnnoblementNotificationDescription(n) str := c.buildDescription(n)
if l := len(embeds); l == 0 || len(embeds[l-1].Description)+len(str) > embedDescriptionCharLimit { if l := len(embeds); l == 0 || len(embeds[l-1].Description)+len(str) > embedDescriptionCharLimit {
embeds = append(embeds, &discordgo.MessageEmbed{ embeds = append(embeds, &discordgo.MessageEmbed{
Color: ennoblementNotificationTypeToColor(n.Type), Color: c.mapNotificationTypeToColor(n.Type),
Type: discordgo.EmbedTypeRich, Type: discordgo.EmbedTypeRich,
Timestamp: timestamp, Timestamp: timestamp,
}) })
@ -98,16 +102,16 @@ func ennoblementNotificationsToEmbeds(ns []domain.EnnoblementNotification) map[s
return res return res
} }
func buildEnnoblementNotificationDescription(n domain.EnnoblementNotification) string { func (c ennoblementNotificationToMessageEmbedConverter) buildDescription(n domain.EnnoblementNotification) string {
return fmt.Sprintf( return fmt.Sprintf(
"%s has taken %s (Old owner: %s)", "%s has taken %s (Old owner: %s)",
nullPlayerMetaToMarkdown(n.Ennoblement.NewOwner), c.buildPlayerMarkdown(n.Ennoblement.NewOwner),
buildLink(n.Ennoblement.Village.FullName, n.Ennoblement.Village.ProfileURL), buildLink(n.Ennoblement.Village.FullName, n.Ennoblement.Village.ProfileURL),
nullPlayerMetaToMarkdown(n.Ennoblement.Village.Player), c.buildPlayerMarkdown(n.Ennoblement.Village.Player),
) )
} }
func nullPlayerMetaToMarkdown(p domain.NullPlayerMeta) string { func (c ennoblementNotificationToMessageEmbedConverter) buildPlayerMarkdown(p domain.NullPlayerMeta) string {
if !p.Valid { if !p.Valid {
return "Barbarians" return "Barbarians"
} }
@ -123,7 +127,7 @@ func nullPlayerMetaToMarkdown(p domain.NullPlayerMeta) string {
"]" "]"
} }
func ennoblementNotificationTypeToColor(t domain.EnnoblementNotificationType) int { func (c ennoblementNotificationToMessageEmbedConverter) mapNotificationTypeToColor(t domain.EnnoblementNotificationType) int {
switch t { switch t {
case domain.EnnoblementNotificationTypeGain: case domain.EnnoblementNotificationTypeGain:
return colorGreen return colorGreen