dcbot/internal/service/ennoblement_notification_bu...

151 lines
4.1 KiB
Go

package service
import (
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
"gitea.dwysokinski.me/twhelp/dcbot/internal/twhelp"
)
type ennoblementNotificationBuilder struct {
groups []domain.GroupWithMonitors
ennoblements listEnnoblementsResult
}
func (b ennoblementNotificationBuilder) build() []domain.EnnoblementNotification {
var notifications []domain.EnnoblementNotification
for _, g := range b.groups {
notifications = append(notifications, b.buildGroup(g)...)
}
return notifications
}
func (b ennoblementNotificationBuilder) buildGroup(g domain.GroupWithMonitors) []domain.EnnoblementNotification {
var notifications []domain.EnnoblementNotification
for _, e := range b.ennoblements.find(g.VersionCode, g.ServerKey) {
if b.canSendEnnoblementNotificationTypeGain(g, e) {
notifications = append(notifications, domain.EnnoblementNotification{
Type: domain.EnnoblementNotificationTypeGain,
ServerID: g.ServerID,
ChannelID: g.ChannelGains,
LanguageTag: g.LanguageTag,
Ennoblement: b.ennoblementToDomainModel(e),
})
}
if b.canSendEnnoblementNotificationTypeLoss(g, e) {
notifications = append(notifications, domain.EnnoblementNotification{
Type: domain.EnnoblementNotificationTypeLoss,
ServerID: g.ServerID,
ChannelID: g.ChannelLosses,
LanguageTag: g.LanguageTag,
Ennoblement: b.ennoblementToDomainModel(e),
})
}
}
return notifications
}
func (b ennoblementNotificationBuilder) canSendEnnoblementNotificationTypeGain(g domain.GroupWithMonitors, e twhelp.Ennoblement) bool {
if g.ChannelGains == "" {
return false
}
if !g.Barbarians && b.isBarbarian(e) {
return false
}
if !g.Internals && b.isInternal(e, g.Monitors) {
return false
}
return b.isGain(e, g.Monitors)
}
func (b ennoblementNotificationBuilder) canSendEnnoblementNotificationTypeLoss(g domain.GroupWithMonitors, e twhelp.Ennoblement) bool {
if g.ChannelLosses == "" {
return false
}
if b.isInternal(e, g.Monitors) {
return false
}
return b.isLoss(e, g.Monitors)
}
func (b ennoblementNotificationBuilder) isInternal(e twhelp.Ennoblement, monitors []domain.Monitor) bool {
var n, o bool
for _, m := range monitors {
if m.TribeID == e.NewOwner.Player.Tribe.Tribe.ID {
n = true
}
if m.TribeID == e.Village.Player.Player.Tribe.Tribe.ID {
o = true
}
}
return n && o
}
func (b ennoblementNotificationBuilder) isBarbarian(e twhelp.Ennoblement) bool {
return !e.Village.Player.Valid
}
func (b ennoblementNotificationBuilder) isGain(e twhelp.Ennoblement, monitors []domain.Monitor) bool {
var n bool
for _, m := range monitors {
if m.TribeID == e.NewOwner.Player.Tribe.Tribe.ID {
n = true
break
}
}
return n && e.NewOwner.Player.ID != e.Village.Player.Player.ID
}
func (b ennoblementNotificationBuilder) isLoss(e twhelp.Ennoblement, monitors []domain.Monitor) bool {
var o bool
for _, m := range monitors {
if m.TribeID == e.Village.Player.Player.Tribe.Tribe.ID {
o = true
break
}
}
return o && e.NewOwner.Player.ID != e.Village.Player.Player.ID
}
func (b ennoblementNotificationBuilder) ennoblementToDomainModel(e twhelp.Ennoblement) domain.Ennoblement {
return domain.Ennoblement{
ID: e.ID,
Village: domain.VillageMeta{
ID: e.Village.ID,
FullName: e.Village.FullName,
ProfileURL: e.Village.ProfileURL,
Player: domain.NullPlayerMeta{
Player: domain.PlayerMeta{
ID: e.Village.Player.Player.ID,
Name: e.Village.Player.Player.Name,
ProfileURL: e.Village.Player.Player.ProfileURL,
Tribe: domain.NullTribeMeta{
Tribe: domain.TribeMeta(e.Village.Player.Player.Tribe.Tribe),
Valid: e.Village.Player.Player.Tribe.Valid,
},
},
Valid: e.Village.Player.Valid,
},
},
NewOwner: domain.NullPlayerMeta{
Player: domain.PlayerMeta{
ID: e.NewOwner.Player.ID,
Name: e.NewOwner.Player.Name,
ProfileURL: e.NewOwner.Player.ProfileURL,
Tribe: domain.NullTribeMeta{
Tribe: domain.TribeMeta(e.NewOwner.Player.Tribe.Tribe),
Valid: e.NewOwner.Player.Tribe.Valid,
},
},
Valid: e.NewOwner.Valid,
},
CreatedAt: e.CreatedAt,
}
}