@@ -13,7 +13,6 @@ import (
1313 "strconv"
1414 "strings"
1515 "text/template"
16- "time"
1716)
1817
1918//go:embed base_template.md
@@ -27,20 +26,30 @@ func init() {
2726// Base
2827
2928type TemplateBase struct {
30- main views.TemplateShared
31- alertHealthLowerThan views.TemplateShared
32- alertHealthIsDecreasing views.TemplateShared
33- alertBaseUnderAttack views.TemplateShared
29+ main views.ViewTable
30+ alertHealthLowerThan views.ViewTable
31+ alertHealthIsDecreasing views.ViewTable
32+ alertBaseUnderAttack views.ViewTable
3433 api * apis.API
34+ * views.SharedViewTableSplitter
3535}
3636
3737func NewTemplateBase (api * apis.API ) * TemplateBase {
3838 base := TemplateBase {}
3939 base .api = api
40- base .main .Header = "#darkbot-base-view"
41- base .alertHealthLowerThan .Header = "#darkbot-base-alert-health-lower-than"
42- base .alertHealthIsDecreasing .Header = "#darkbot-base-health-is-decreasing"
43- base .alertBaseUnderAttack .Header = "#darkbot-base-base-under-attack"
40+ base .main .ViewID = "#darkbot-base-view"
41+ base .alertHealthLowerThan .ViewID = "#darkbot-base-alert-health-lower-than"
42+ base .alertHealthIsDecreasing .ViewID = "#darkbot-base-health-is-decreasing"
43+ base .alertBaseUnderAttack .ViewID = "#darkbot-base-base-under-attack"
44+
45+ base .SharedViewTableSplitter = views .NewSharedViewSplitter (
46+ api ,
47+ & base ,
48+ & base .main ,
49+ & base .alertHealthLowerThan ,
50+ & base .alertHealthIsDecreasing ,
51+ & base .alertBaseUnderAttack ,
52+ )
4453 return & base
4554}
4655
@@ -53,14 +62,6 @@ type TemplateAugmentedBase struct {
5362 UnderAttackPhrase string
5463}
5564
56- type TemplateRendererBaseInput struct {
57- Header string
58- LastUpdated string
59- Bases []TemplateAugmentedBase
60- HealthDecreasePhrase string
61- UnderAttackPhrase string
62- }
63-
6465func BaseContainsTag (bas base.Base , tags []types.Tag ) bool {
6566 for _ , tag := range tags {
6667 if strings .Contains (bas .Name , string (tag )) {
@@ -86,22 +87,23 @@ func MatchBases(bases []base.Base, tags []types.Tag) []base.Base {
8687
8788const HealthRateDecreasingThreshold = - 0.01
8889
89- func (b * TemplateBase ) Render () error {
90- input := TemplateRendererBaseInput {
91- Header : b .main .Header ,
92- LastUpdated : time .Now ().String (),
93- HealthDecreasePhrase : "\n @healthDecreasing;" ,
94- UnderAttackPhrase : "\n @underAttack;" ,
95- }
96-
90+ func (b * TemplateBase ) GenerateRecords () error {
9791 record , err := b .api .Scrappy .GetBaseStorage ().GetLatestRecord ()
98- if logus .CheckWarn (err , "unable to render TemplateBase " ) {
92+ if logus .CheckWarn (err , "unable to query bases from storage in Template base Generate records " ) {
9993 return err
10094 }
10195 sort .Slice (record .List , func (i , j int ) bool {
10296 return record .List [i ].Name < record .List [j ].Name
10397 })
10498
99+ var beginning strings.Builder
100+ beginning .WriteString ("**Bases:**\n " )
101+ b .main .ViewBeginning = types .ViewBeginning (beginning .String ())
102+
103+ HealthDecreasePhrase := "\n @healthDecreasing;"
104+ UnderAttackPhrase := "\n @underAttack;"
105+ bases := []TemplateAugmentedBase {}
106+
105107 tags , _ := b .api .Bases .TagsList (b .api .ChannelID )
106108
107109 matchedBases := MatchBases (record .List , tags )
@@ -121,98 +123,65 @@ func (b *TemplateBase) Render() error {
121123 UnderAttack = healthDeritiveNumber < HealthRateDecreasingThreshold || strings .Contains (string (b .api .Scrappy .GetBaseAttackStorage ().GetData ()), base .Name )
122124 }
123125
124- input . Bases = append ( input . Bases , TemplateAugmentedBase {
126+ baseVars := TemplateAugmentedBase {
125127 Base : base ,
126128 HealthChange : healthDeritive ,
127129 IsHealthDecreasing : HealthDecreasing ,
128130 IsUnderAttack : UnderAttack ,
129- HealthDecreasePhrase : input . HealthDecreasePhrase ,
130- UnderAttackPhrase : input . UnderAttackPhrase ,
131- })
132- }
131+ HealthDecreasePhrase : HealthDecreasePhrase ,
132+ UnderAttackPhrase : UnderAttackPhrase ,
133+ }
134+ bases = append ( bases , baseVars )
133135
134- if len (input .Bases ) != 0 {
135- b .main .Content = utils .TmpRender (baseTemplate , input )
136136 }
137137
138- // Alerts
139- if DerivativesInitializing {
140- // Don't update alerts until bases are properly initalized. To avoid extra pings to players
141- return nil
138+ for _ , base := range bases {
139+ b .main .AppendRecord (types .ViewRecord (utils .TmpRender (baseTemplate , base )))
142140 }
143141
144- b .alertHealthLowerThan .Content = ""
145142 if healthThreshold , err := b .api .Alerts .BaseHealthLowerThan .Status (b .api .ChannelID ); err == nil {
146- for _ , base := range input . Bases {
143+ for _ , base := range bases {
147144 if int (base .Health ) < healthThreshold {
148- b .alertHealthLowerThan .Content = views .RenderAlertTemplate (b .alertHealthLowerThan .Header , b .api .ChannelID , fmt .Sprintf ("Base %s has health %d lower than threshold %d" , base .Name , int (base .Health ), healthThreshold ), b .api )
145+ b .alertHealthLowerThan .AppendRecord (views .RenderAlertTemplate (
146+ b .api .ChannelID ,
147+ fmt .Sprintf ("Base %s has health %d lower than threshold %d" , base .Name , int (base .Health ), healthThreshold ),
148+ b .api ,
149+ ))
149150 break
150151 }
151152 }
152153 }
153154
154- b .alertHealthIsDecreasing .Content = ""
155155 if isAlertEnabled , err := b .api .Alerts .BaseHealthIsDecreasing .Status (b .api .ChannelID ); err == nil && isAlertEnabled {
156- for _ , base := range input . Bases {
156+ for _ , base := range bases {
157157 if base .IsHealthDecreasing {
158- b .alertHealthIsDecreasing .Content = views .RenderAlertTemplate (b .alertHealthIsDecreasing .Header , b .api .ChannelID , fmt .Sprintf ("Base %s health %d is decreasing with value %s" , base .Name , int (base .Health ), base .HealthChange ), b .api )
158+ b .alertHealthIsDecreasing .AppendRecord (views .RenderAlertTemplate (
159+ b .api .ChannelID ,
160+ fmt .Sprintf ("Base %s health %d is decreasing with value %s" , base .Name , int (base .Health ), base .HealthChange ),
161+ b .api ,
162+ ))
159163 break
160164 }
161165 }
162166 }
163167
164- b .alertBaseUnderAttack .Content = ""
165168 if isAlertEnabled , _ := b .api .Alerts .BaseIsUnderAttack .Status (b .api .ChannelID ); isAlertEnabled {
166- for _ , base := range input . Bases {
169+ for _ , base := range bases {
167170 if base .IsUnderAttack {
168- b .alertBaseUnderAttack .Content = views .RenderAlertTemplate (b .alertBaseUnderAttack .Header , b .api .ChannelID , fmt .Sprintf ("Base %s health %d is probably under attack because health change %s is dropping faster than %f. Or it was detected at forum attack declaration thread." , base .Name , int (base .Health ), base .HealthChange , HealthRateDecreasingThreshold ), b .api )
171+ b .alertBaseUnderAttack .AppendRecord (views .RenderAlertTemplate (
172+ b .api .ChannelID ,
173+ fmt .Sprintf ("Base %s health %d is probably under attack because health change %s is dropping faster than %f. Or it was detected at forum attack declaration thread." ,
174+ base .Name ,
175+ int (base .Health ),
176+ base .HealthChange ,
177+ HealthRateDecreasingThreshold ,
178+ ),
179+ b .api ,
180+ ))
169181 break
170182 }
171183 }
172184 }
173185
174186 return nil
175187}
176-
177- func (t * TemplateBase ) Send () {
178- t .main .Send (t .api )
179- t .alertHealthLowerThan .Send (t .api )
180- t .alertHealthIsDecreasing .Send (t .api )
181- t .alertBaseUnderAttack .Send (t .api )
182- }
183-
184- func (t * TemplateBase ) MatchMessageID (messageID types.DiscordMessageID ) bool {
185-
186- if messageID == t .main .MessageID {
187- return true
188- }
189- if messageID == t .alertHealthLowerThan .MessageID {
190- return true
191- }
192- if messageID == t .alertHealthIsDecreasing .MessageID {
193- return true
194- }
195- if messageID == t .alertBaseUnderAttack .MessageID {
196- return true
197- }
198- return false
199- }
200-
201- func (t * TemplateBase ) DiscoverMessageID (content string , msgID types.DiscordMessageID ) {
202- if strings .Contains (content , t .main .Header ) {
203- t .main .MessageID = msgID
204- t .main .Content = content
205- }
206- if strings .Contains (content , t .alertHealthLowerThan .Header ) {
207- t .alertHealthLowerThan .MessageID = msgID
208- t .alertHealthLowerThan .Content = content
209- }
210- if strings .Contains (content , t .alertHealthIsDecreasing .Header ) {
211- t .alertHealthIsDecreasing .MessageID = msgID
212- t .alertHealthIsDecreasing .Content = content
213- }
214- if strings .Contains (content , t .alertBaseUnderAttack .Header ) {
215- t .alertBaseUnderAttack .MessageID = msgID
216- t .alertBaseUnderAttack .Content = content
217- }
218- }
0 commit comments