/
turnips.go
367 lines (306 loc) · 11 KB
/
turnips.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package turnips
import (
"encoding/json"
"fmt"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/Plloi/Junior/router"
"github.com/bwmarrin/discordgo"
"github.com/sdomino/scribble"
)
func notImplemented(s *discordgo.Session, m *discordgo.MessageCreate) {
s.ChannelMessageSend(m.ChannelID, "Command reserved, but not implemented")
}
type TurnipCommands struct {
db *scribble.Driver
prices []turnipPrice
debug bool
}
func Setup(r *router.CommandRouter) {
dir := "./turnipdb/"
db, err := scribble.New(dir, nil)
if err != nil {
fmt.Println("Error", err)
}
//Load Pricedb
records, err := db.ReadAll("users")
if err != nil {
fmt.Println("Error", err)
}
prices := []turnipPrice{}
for _, f := range records {
priceRec := turnipPrice{}
if err := json.Unmarshal([]byte(f), &priceRec); err != nil {
fmt.Println("Error", err)
}
prices = append(prices, priceRec)
}
tc := &TurnipCommands{db: db, prices: prices, debug: false}
r.RegisterCommand("addturnips", "Adds your turnip price to the current list", tc.addturnips)
r.RegisterCommand("top5", "Show the top 5 turnip prices", tc.top5)
r.RegisterCommand("settime", "Lets me know what time it is in your game for expiring turnip prices, please send as Wednesday 12 AM", tc.settime)
r.RegisterCommand("checktime", "I'll tell you what time i think it is in your game", tc.checktime)
r.RegisterCommand("buyalert", "Set the threshold buy price for the bot to alert you, set to -1 to receive all", tc.buyAlert)
r.RegisterCommand("sellalert", "Set the threshold sell price for the bot to alert you, set to -1 to receive all", tc.sellAlert)
// r.RegisterCommand("alertme", "Add me to the alert group", tc.alertme)
}
func (tc *TurnipCommands) top5(s *discordgo.Session, m *discordgo.MessageCreate) {
// Loop the list looking for best buy and sell prices
memberList := make(map[string]*discordgo.Member)
Server, _ := s.Guild(m.GuildID)
for _, Member := range Server.Members {
if Member.Nick == "" {
Member.Nick = Member.User.Username
}
memberList[Member.User.ID] = Member
}
buying := []turnipPrice{}
selling := []turnipPrice{}
sort.SliceStable(tc.prices, func(i, j int) bool {
return tc.prices[i].Price > tc.prices[j].Price
})
for _, price := range tc.prices {
_, ok := memberList[price.AuthorID]
if !ok || getOffsetTime(time.Now(), price.TimeOffset).After(price.Time) {
continue
}
if price.Time.Weekday() == 0 {
selling = append([]turnipPrice{price}, selling...)
} else {
buying = append(buying, price)
}
}
msg := ""
if len(buying) > 0 {
msg = "Buying:\n"
for i := 0; i < len(buying) && i < 5; i++ {
msg += fmt.Sprintf("* %s is buying @ %d\n", memberList[buying[i].AuthorID].Nick, buying[i].Price)
}
}
if len(selling) > 0 {
msg += "Selling:\n"
for i := 0; i < len(selling) && i < 5; i++ {
msg += fmt.Sprintf("* %s is selling @ %d\n", memberList[selling[i].AuthorID].Nick, selling[i].Price)
}
}
if len(msg) == 0 {
msg = "No prices active at this time"
}
s.ChannelMessageSend(m.ChannelID, msg)
}
func (tc *TurnipCommands) addturnips(s *discordgo.Session, m *discordgo.MessageCreate) {
i, err := strconv.Atoi(m.Content)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "please format message as addturnips 123")
return
}
if i <= 0 {
s.ChannelMessageSend(m.ChannelID, "Price needs to be greater than 0")
return
}
offset := 0
userIndex := tc.findUserListingIndex(m.Author.ID)
if userIndex != -1 {
offset = tc.prices[userIndex].TimeOffset
}
playertime := getOffsetTime(time.Now(), offset)
invalid := false
//Sunday
if playertime.Weekday() == 0 && playertime.Hour() >= 5 && playertime.Hour() <= 12 {
playertime = time.Date(playertime.Year(), playertime.Month(), playertime.Day(), 12, 0, 0, 0, playertime.Location())
tc.debugLog(fmt.Sprintf("Weekday: %d, Hour: %d, Price: %d, Setting Hour: %d", playertime.Weekday(), playertime.Hour(), i, 12))
// Weekday morning
} else if playertime.Weekday() != 0 && playertime.Hour() >= 8 && playertime.Hour() < 12 {
playertime = time.Date(playertime.Year(), playertime.Month(), playertime.Day(), 12, 0, 0, 0, playertime.Location())
tc.debugLog(fmt.Sprintf("Weekday: %d, Hour: %d, Price: %d, Setting Hour: %d", playertime.Weekday(), playertime.Hour(), i, 12))
// Weekday Afternoon
} else if playertime.Weekday() != 0 && playertime.Hour() >= 12 && playertime.Hour() < 22 {
playertime = time.Date(playertime.Year(), playertime.Month(), playertime.Day(), 22, 0, 0, 0, playertime.Location())
tc.debugLog(fmt.Sprintf("Weekday: %d, Hour: %d, Price: %d, Setting Hour: %d", playertime.Weekday(), playertime.Hour(), i, 20))
} else {
invalid = true
}
if invalid == true {
s.ChannelMessageSend(m.ChannelID, "Aren't you outside of a valid time?")
return
}
var record turnipPrice
if userIndex == -1 {
record = turnipPrice{AuthorID: m.Author.ID, Price: i, Time: playertime}
tc.prices = append(tc.prices, record)
} else {
tc.prices[userIndex].Time = playertime
tc.prices[userIndex].Price = i
record = tc.prices[userIndex]
}
if err := tc.db.Write("users", m.Author.ID, record); err != nil {
fmt.Println("Error", err)
}
s.ChannelMessageSend(m.ChannelID, "Thank you, price added to current market.")
tc.sendAlert(s, m, record)
}
func (tc *TurnipCommands) settime(s *discordgo.Session, m *discordgo.MessageCreate) {
now := time.Now()
regex := regexp.MustCompile(`^((?:sun|mon|tues|wednes|thurs|fri|satur)day) (\d{1,2})\ ?([ap][m])$`)
matches := regex.FindStringSubmatch(strings.ToLower(m.Content))
if matches == nil {
s.ChannelMessageSend(m.ChannelID, "Unable to parse time. please send as 'settime Wednesday 12 PM'")
return
}
hour, _ := strconv.Atoi(matches[2])
if len(matches) == 4 && matches[3] == "pm" && hour != 12 {
hour += 12
} else if len(matches) == 4 && matches[3] == "am" && hour == 12 {
hour = 0
}
offset := hour - now.Hour()
testtime := time.Date(now.Year(), now.Month(), now.Day(), now.Hour()+offset, now.Minute(), now.Second(), now.Nanosecond(), now.Location())
day := 0
switch matches[1] {
case "monday":
day = 1
case "tuesday":
day = 2
case "wednesday":
day = 3
case "thursday":
day = 4
case "friday":
day = 5
case "saturday":
day = 6
}
if testtime.Weekday() != time.Weekday(day) {
days := day - int(now.Weekday())
fmt.Printf("Adjusting Day by %d Days.\n", days)
offset = days*24 + offset
}
//Find and update, or add new user record
index := tc.findUserListingIndex(m.Author.ID)
var record turnipPrice
if index == -1 {
record = turnipPrice{AuthorID: m.Author.ID, Price: 0, Time: time.Unix(0, 0), TimeOffset: offset}
tc.prices = append(tc.prices, record)
} else {
tc.prices[index].TimeOffset = offset
record = tc.prices[index]
}
if err := tc.db.Write("users", m.Author.ID, record); err != nil {
fmt.Println("Error", err)
}
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s You Game Time Has Been Updated", m.Author.Mention()))
}
func (tc *TurnipCommands) checktime(s *discordgo.Session, m *discordgo.MessageCreate) {
offset := 0
userIndex := tc.findUserListingIndex(m.Author.ID)
if userIndex != -1 {
offset = tc.prices[userIndex].TimeOffset
}
playertime := getOffsetTime(time.Now(), offset)
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s I think your game time is %s", m.Author.Mention(), playertime.Format("Monday 3PM")))
}
func (tc *TurnipCommands) alertme(s *discordgo.Session, m *discordgo.MessageCreate) {
testtime := time.Now()
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Time now: %s", testtime))
user := tc.prices[tc.findUserListingIndex(m.Author.ID)]
testtime = time.Date(testtime.Year(), testtime.Month(), testtime.Day(), testtime.Hour()+user.TimeOffset, testtime.Minute(), testtime.Second(), testtime.Nanosecond(), testtime.Location())
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Time Offset: %s", testtime))
notImplemented(s, m)
}
func (tc *TurnipCommands) findUserListingIndex(id string) int {
for i := 0; i < len(tc.prices); i++ {
if tc.prices[i].AuthorID == id {
return i
}
}
return -1
}
func getOffsetTime(intime time.Time, offset int) time.Time {
return time.Date(intime.Year(), intime.Month(), intime.Day(), intime.Hour()+offset, intime.Minute(), intime.Second(), intime.Nanosecond(), intime.Location())
}
func (tc *TurnipCommands) buyAlert(s *discordgo.Session, m *discordgo.MessageCreate) {
Threshold, err := strconv.Atoi(m.Content)
if err != nil {
tc.debugLog(fmt.Sprintf("BuyTreshold encountered an Error:\n Message Content: %s\n Error: %s", m.Content, err))
s.ChannelMessageSend(m.ChannelID, "Unable to process Alert request")
return
}
userIndex := tc.findUserListingIndex(m.Author.ID)
var User turnipPrice
//Existing User
if userIndex != -1 {
tc.prices[userIndex].BuyThreshold = Threshold
User = tc.prices[userIndex]
} else {
User.BuyThreshold = Threshold
User.AuthorID = m.Author.ID
}
if err := tc.db.Write("users", m.Author.ID, User); err != nil {
fmt.Println("Error", err)
}
s.ChannelMessageSend(m.ChannelID, "Threshold noted")
}
func (tc *TurnipCommands) sellAlert(s *discordgo.Session, m *discordgo.MessageCreate) {
Threshold, err := strconv.Atoi(m.Content)
if err != nil {
tc.debugLog(fmt.Sprintf("SellTreshold encountered an Error:\n Message Content: %s\n Error: %s", m.Content, err))
s.ChannelMessageSend(m.ChannelID, "Unable to process Alert request")
return
}
userIndex := tc.findUserListingIndex(m.Author.ID)
var User turnipPrice
//Existing User
if userIndex != -1 {
tc.prices[userIndex].SellThreshold = Threshold
User = tc.prices[userIndex]
} else {
User.SellThreshold = Threshold
User.AuthorID = m.Author.ID
}
if err := tc.db.Write("users", m.Author.ID, User); err != nil {
fmt.Println("Error", err)
}
s.ChannelMessageSend(m.ChannelID, "Threshold noted")
}
func (tc *TurnipCommands) sendAlert(s *discordgo.Session, m *discordgo.MessageCreate, SourceUser turnipPrice) {
// Loop the list looking for best buy and sell prices
memberList := make(map[string]*discordgo.Member)
Server, _ := s.Guild(m.GuildID)
for _, Member := range Server.Members {
if Member.Nick == "" {
Member.Nick = Member.User.Username
}
memberList[Member.User.ID] = Member
}
msg := ""
for _, User := range tc.prices {
_, ok := memberList[User.AuthorID]
if !ok || SourceUser.AuthorID == User.AuthorID {
continue
}
//Check Buying
if (SourceUser.Time.Weekday() != 0) && User.BuyThreshold <= SourceUser.Price && User.BuyThreshold != 0 {
msg += fmt.Sprintf("%s, ", memberList[User.AuthorID].Mention())
}
//Check Selling
if (SourceUser.Time.Weekday() == 0) && (User.SellThreshold >= SourceUser.Price || User.SellThreshold == -1) {
msg += fmt.Sprintf("%s, ", memberList[User.AuthorID].Mention())
}
}
if len(msg) > 0 {
buysell := "buying"
if SourceUser.Time.Weekday() == 0 {
buysell = "selling"
}
msg = fmt.Sprintf("Hey %s! %s is %s at %d!", msg[0:len(msg)-2], memberList[SourceUser.AuthorID].Nick, buysell, SourceUser.Price)
}
s.ChannelMessageSend(m.ChannelID, msg)
}
func (tc *TurnipCommands) debugLog(logentry string) {
if tc.debug == true {
fmt.Printf("%s: %s", time.Now(), logentry)
}
}