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

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
1 changed files with 14 additions and 10 deletions

View File

@ -36,7 +36,7 @@ func (e *executeMonitorsJob) Run() {
var wg sync.WaitGroup
for ch, embeds := range ennoblementNotificationsToEmbeds(notifications) {
for ch, embeds := range (ennoblementNotificationToMessageEmbedConverter{notifications}).convert() {
wg.Add(1)
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)
timestamp := formatTimestamp(time.Now())
for _, n := range ns {
for _, n := range c.ns {
if _, ok := m[n.ChannelID]; !ok {
m[n.ChannelID] = make(map[domain.EnnoblementNotificationType][]*discordgo.MessageEmbed)
}
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 {
embeds = append(embeds, &discordgo.MessageEmbed{
Color: ennoblementNotificationTypeToColor(n.Type),
Color: c.mapNotificationTypeToColor(n.Type),
Type: discordgo.EmbedTypeRich,
Timestamp: timestamp,
})
@ -98,16 +102,16 @@ func ennoblementNotificationsToEmbeds(ns []domain.EnnoblementNotification) map[s
return res
}
func buildEnnoblementNotificationDescription(n domain.EnnoblementNotification) string {
func (c ennoblementNotificationToMessageEmbedConverter) buildDescription(n domain.EnnoblementNotification) string {
return fmt.Sprintf(
"%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),
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 {
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 {
case domain.EnnoblementNotificationTypeGain:
return colorGreen