Skip to content

Commit 4771a55

Browse files
committed
feat: view table splitter
1 parent 29d4ae7 commit 4771a55

File tree

16 files changed

+431
-365
lines changed

16 files changed

+431
-365
lines changed

app/settings/types/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ type DiscordOwnerID string
1919
type PingMessage string
2020

2121
type Tag string
22+
23+
type ViewID string
24+
type ViewEnumeratedID string
25+
26+
type ViewBeginning string
27+
type ViewRecord string
28+
type ViewEnd string

app/viewer/viewer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ func (v Viewer) Update() {
4343
for _, channelID := range channelIDs {
4444
v.api.SetChannelID(channelID)
4545
channel := NewChannelView(v.api, channelID)
46+
channel.Render()
4647
err := channel.Discover()
4748
if logus.CheckWarn(err, "unable to grab Discord msgs", logus.ChannelID(channelID)) {
4849
continue
4950
}
50-
channel.Render()
5151
channel.Send()
5252
channel.DeleteOld()
5353
time.Sleep(time.Duration(v.delays.betweenChannels) * time.Second)

app/viewer/views/alert.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
_ "embed"
99
"fmt"
1010
"text/template"
11-
"time"
1211
)
1312

1413
//go:embed alert_template.md
@@ -20,13 +19,11 @@ func init() {
2019
}
2120

2221
type TemplateAlertInput struct {
23-
Header string
24-
LastUpdated string
2522
PingMessage types.PingMessage
2623
Msg string
2724
}
2825

29-
func RenderAlertTemplate(Header string, ChannelID types.DiscordChannelID, Msg string, api *apis.API) string {
26+
func RenderAlertTemplate(ChannelID types.DiscordChannelID, Msg string, api *apis.API) types.ViewRecord {
3027

3128
pingMessage, err := api.Alerts.PingMessage.Status(ChannelID)
3229
logus.Debug("RenderAlertTemplate.PingMessage.Status", logus.OptError(err), logus.PingMessage(pingMessage))
@@ -39,10 +36,8 @@ func RenderAlertTemplate(Header string, ChannelID types.DiscordChannelID, Msg st
3936
}
4037

4138
input := TemplateAlertInput{
42-
Header: Header,
43-
LastUpdated: time.Now().String(),
4439
PingMessage: pingMessage,
4540
Msg: Msg,
4641
}
47-
return utils.TmpRender(alertTemplate, input)
42+
return types.ViewRecord(utils.TmpRender(alertTemplate, input))
4843
}

app/viewer/views/alert_template.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
{{.Header}} (last updated: {{.LastUpdated}})
21
{{.PingMessage}} **Alert: {{.Msg}}**

app/viewer/views/baseview/base.go

Lines changed: 58 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2928
type 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

3737
func 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-
6465
func 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

8788
const 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-
}
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
{{.Header}} (last updated: {{.LastUpdated}})
2-
**Bases:**
3-
{{range $val := .Bases -}}
41
```scss
5-
BaseName({{$val.Name | printf "%q"}});
6-
Health({{$val.Health | printf "\"%.1f\""}});
7-
HealthChangeInLast15m({{$val.HealthChange | printf "%q"}});
8-
Affiliation({{$val.Affiliation | printf "%q"}});
9-
{{- if $val.IsHealthDecreasing -}}{{ $val.HealthDecreasePhrase }}{{- end }}
10-
{{- if $val.IsUnderAttack -}}{{ $val.UnderAttackPhrase }}{{- end }}
2+
BaseName({{.Name | printf "%q"}});
3+
Health({{.Health | printf "\"%.1f\""}});
4+
HealthChangeInLast15m({{.HealthChange | printf "%q"}});
5+
Affiliation({{.Affiliation | printf "%q"}});
6+
{{- if .IsHealthDecreasing -}}{{ .HealthDecreasePhrase }}{{- end }}
7+
{{- if .IsUnderAttack -}}{{ .UnderAttackPhrase }}{{- end }}
118
```
12-
{{ end }}

0 commit comments

Comments
 (0)