dcbot/internal/service/ennoblement_notification_builder.go
Dawid Wysokiński a8f309c299
All checks were successful
continuous-integration/drone/push Build is passing
refactor: introduce adapters (#114)
Reviewed-on: #114
2023-07-03 06:23:32 +00:00

114 lines
2.9 KiB
Go

package service
import (
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
)
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: e,
})
}
if b.canSendEnnoblementNotificationTypeLoss(g, e) {
notifications = append(notifications, domain.EnnoblementNotification{
Type: domain.EnnoblementNotificationTypeLoss,
ServerID: g.ServerID,
ChannelID: g.ChannelLosses,
LanguageTag: g.LanguageTag,
Ennoblement: e,
})
}
}
return notifications
}
func (b ennoblementNotificationBuilder) canSendEnnoblementNotificationTypeGain(g domain.GroupWithMonitors, e domain.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 domain.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 domain.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 domain.Ennoblement) bool {
return !e.Village.Player.Valid
}
func (b ennoblementNotificationBuilder) isGain(e domain.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 domain.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
}