-
Notifications
You must be signed in to change notification settings - Fork 4
/
notifier.go
256 lines (208 loc) · 5.98 KB
/
notifier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package notifier
import (
"bufio"
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
"github.com/hpcloud/tail"
log "github.com/sirupsen/logrus"
"github.com/tommyblue/ED-AFK-Notifier/bots"
)
type Notifier struct {
bot bots.Bot
journalFile string
journalChanged chan struct{}
cfg *Cfg
totalPiratesReward int
killedPirates int
activeMissions int
loggedMissions map[int]bool
totalMissionsReward int
}
type Cfg struct {
Token string
ChannelId int64
JournalPath string
FighterNotifs bool
ShieldsNotifs bool // notify about shields state
KillsNotifs bool // notify about killed pirates
KillsSilentNotifs bool // reduce number of notifications for killed pirates, sending a notification every 10 kills
}
// New returns a Notifier with provided configuration
func New(cfg *Cfg) (*Notifier, error) {
bot, err := bots.NewTelegram(cfg.Token, cfg.ChannelId)
if err != nil {
return nil, fmt.Errorf("cannot setup the Telegram bot: %v", err)
}
j, err := journalFile(cfg.JournalPath)
if err != nil {
return nil, err
}
log.Infoln("Found most recent journal file:", j)
e := &Notifier{
bot: bot,
journalFile: filepath.Join(cfg.JournalPath, j),
journalChanged: make(chan struct{}),
cfg: cfg,
}
e.initNotifier()
e.watchJournal()
return e, nil
}
func (e *Notifier) watchJournal() {
go func() {
for range time.Tick(30 * time.Second) {
oldJournal := e.journalFile
j, err := journalFile(e.cfg.JournalPath)
if err != nil {
continue
}
if oldJournal != filepath.Join(e.cfg.JournalPath, j) {
log.Infoln("Found new journal file:", j)
e.journalFile = filepath.Join(e.cfg.JournalPath, j)
e.initNotifier()
e.journalChanged <- struct{}{}
}
}
}()
}
type eventType string
var (
bountyEventType eventType = "Bounty"
missionsEventType eventType = "Missions"
missionAcceptedEventType eventType = "MissionAccepted"
missionRedirectedEventType eventType = "MissionRedirected"
missionCompletedEventType eventType = "MissionCompleted"
missionAbandonedEventType eventType = "MissionAbandoned"
hullDamageEventType eventType = "HullDamage"
diedEventType eventType = "Died"
shieldStateEventType eventType = "ShieldState"
)
func (e *Notifier) initCounters() {
e.totalPiratesReward = 0
e.killedPirates = 0
e.activeMissions = 0
e.loggedMissions = make(map[int]bool)
e.totalMissionsReward = 0
}
func (e *Notifier) initNotifier() {
e.initCounters()
file, err := os.Open(e.journalFile)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var lastMissionsTs time.Time
for scanner.Scan() {
var j journalEvent
line := scanner.Text()
if err := json.Unmarshal([]byte(line), &j); err != nil {
log.Infof("Cannot unmarshal %s", line)
continue
}
switch j.Event {
case bountyEventType:
e.totalPiratesReward += j.TotalPiratesReward
e.killedPirates++
log.Debugf("Total reward: %d\n", e.totalPiratesReward)
log.Debugf("Killed pirates: %d\n", e.killedPirates)
case missionsEventType:
lastMissionsTs = j.Timestamp
e.activeMissions = 0
for _, m := range j.Active {
if m.Expires != 0 {
e.activeMissions++
}
}
log.Debugf("Active missions: %d\n", e.activeMissions)
// The following actions must be accepted only if their timestamp is newer the last
// "Missions" event or the missions count will be wrong.
case missionAcceptedEventType:
if j.Timestamp.After(lastMissionsTs) {
continue
}
e.activeMissions++
log.Debugf("Active missions: %d\n", e.activeMissions)
case missionRedirectedEventType:
if j.Timestamp.After(lastMissionsTs) {
continue
}
if e.loggedMissions[j.MissionID] {
continue
}
e.activeMissions--
e.loggedMissions[j.MissionID] = true
log.Debugf("Active missions: %d\n", e.activeMissions)
case missionCompletedEventType:
if j.Timestamp.After(lastMissionsTs) {
continue
}
if e.loggedMissions[j.MissionID] {
continue
}
e.activeMissions--
delete(e.loggedMissions, j.MissionID)
e.totalMissionsReward += j.MissionReward
log.Debugf("Active missions: %d\n", e.activeMissions)
log.Debugf("Total missions reward: %d\n", e.totalMissionsReward)
case missionAbandonedEventType:
if j.Timestamp.After(lastMissionsTs) {
continue
}
e.activeMissions--
delete(e.loggedMissions, j.MissionID)
log.Debugf("Active missions: %d\n", e.activeMissions)
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
// Start the Notifier engine, thus reading the Journal and sending notifications through the bot
func (e *Notifier) Start() {
e.bot.Start()
for {
log.Infoln("Reading journal...")
t, err := tail.TailFile(e.journalFile, tail.Config{Follow: true, Poll: true})
if err != nil {
log.Fatalf("cannot tail the log file: %v\n", err)
}
go func() {
<-e.journalChanged
log.Infoln("Journal changed, reloading...")
t.Stop()
}()
startTime := time.Now()
events := map[eventType]eventFn{
hullDamageEventType: hullDamageEvent,
diedEventType: diedEvent,
shieldStateEventType: shieldStateEvent,
bountyEventType: bountyEvent,
missionAcceptedEventType: missionAcceptedEvent,
missionCompletedEventType: missionCompletedEvent,
missionRedirectedEventType: missionRedirectedEvent,
missionAbandonedEventType: missionAbandonedEvent,
missionsEventType: missionsInitEvent,
}
for line := range t.Lines {
var j journalEvent
if err := json.Unmarshal([]byte(line.Text), &j); err != nil {
log.Infof("Cannot unmarshal %s", line.Text)
}
// Skip logs already in the journal befor this app has started
var skipNotify bool
if j.Timestamp.Before(startTime) {
skipNotify = true
}
// log.Debugln(line.Text)
if fn, ok := events[j.Event]; ok {
if err := fn(e, j, skipNotify); err != nil {
log.Infoln("[ERROR]", err)
}
}
}
}
}