Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
v1.4.7-DEV: #42 add daily summary feature
Browse files Browse the repository at this point in the history
  • Loading branch information
wneessen committed Jun 18, 2021
1 parent 745c07d commit f02f322
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 84 deletions.
31 changes: 1 addition & 30 deletions bot/playsot_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (
"github.com/wneessen/sotbot/database"
"github.com/wneessen/sotbot/response"
"github.com/wneessen/sotbot/user"
"golang.org/x/text/language"
"golang.org/x/text/message"
"sort"
"time"
)

Expand Down Expand Up @@ -170,34 +167,8 @@ func (b *Bot) UserPlaysSot(s *discordgo.Session, m *discordgo.PresenceUpdate) {
voyageStats["7_Ship"] = int64(userStats.ShipsSunk) - int64(oldStats.ShipsSunk)
voyageStats["8_Vomit"] = int64(userStats.VomitedTotal) - int64(oldStats.VomitedTotal)

// Prepare the output
p := message.NewPrinter(language.German)
var emFields []*discordgo.MessageEmbedField
var keyNames []string
for k := range voyageStats {
keyNames = append(keyNames, k)
}
sort.Strings(keyNames)
for _, k := range keyNames {
v := voyageStats[k]
if v != 0 {
emFields = append(emFields, &discordgo.MessageEmbedField{
Name: fmt.Sprintf("%v %v", response.Icon(k), response.IconKey(k)),
Value: fmt.Sprintf("%v**%v** %v", response.BalanceIcon(k, v),
p.Sprintf("%d", v), response.IconValue(k)),
Inline: true,
})
}
}

emFields := response.FormatEmFields(voyageStats)
if len(emFields) > 0 {
for len(emFields)%3 != 0 {
emFields = append(emFields, &discordgo.MessageEmbedField{
Value: "\U0000FEFF",
Name: "\U0000FEFF",
Inline: true,
})
}
// Response with the Embed
responseEmbed := &discordgo.MessageEmbed{
Type: discordgo.EmbedTypeRich,
Expand Down
20 changes: 20 additions & 0 deletions bot/slashcmd_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,26 @@ func (b *Bot) SlashCmdHandler(s *discordgo.Session, i *discordgo.InteractionCrea
response.SlashCmdEmbedDeferred(s, i.Interaction, em)
return

// SoT: Provide daily summary to the user
case cmdName == "summary":
response.SlashCmdResponseDeferred(s, i.Interaction)
if !userObj.IsRegistered() {
response.SlashCmdDel(s, i.Interaction)
return
}
if !userObj.HasRatCookie() {
response.SlashCmdDel(s, i.Interaction)
return
}
em, err := handler.GetSotSummary(b.HttpClient, userObj, b.Db)
if err != nil {
re := fmt.Sprintf("An error occurred checking your SoT daily summary: %v", err)
response.SlashCmdResponseEdit(s, i.Interaction, userObj, re, true)
return
}
response.SlashCmdEmbedDeferred(s, i.Interaction, em)
return

// SoT: Retrieve user ledger position with a specific faction/company
case cmdName == "code":
em, err := handler.GetSotRandomCode()
Expand Down
6 changes: 6 additions & 0 deletions bot/slashcmd_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ func (b *Bot) SlashCmdList() []*discordgo.ApplicationCommand {
Name: "traderoutes",
Description: "Let SoTBot tell you the currently active trade routes in SoT",
},

// SoT: Daily summary
{
Name: "summary",
Description: "Let SoTBot tell you your daily summary",
},
}

return slashCmds
Expand Down
28 changes: 1 addition & 27 deletions handler/sot_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import (
"github.com/wneessen/sotbot/api"
"github.com/wneessen/sotbot/response"
"github.com/wneessen/sotbot/user"
"golang.org/x/text/language"
"golang.org/x/text/message"
"net/http"
"sort"
)

// Get current SoT balance
Expand All @@ -29,30 +26,7 @@ func GetSotBalance(h *http.Client, u *user.User) (*discordgo.MessageEmbed, error
balanceData["2_Doubloon"] = int64(userBalance.Doubloons)
balanceData["3_AncientCoin"] = int64(userBalance.AncientCoins)

p := message.NewPrinter(language.German)
var emFields []*discordgo.MessageEmbedField
var keyNames []string
for k := range balanceData {
keyNames = append(keyNames, k)
}
sort.Strings(keyNames)
for _, k := range keyNames {
v := balanceData[k]
if v != 0 {
emFields = append(emFields, &discordgo.MessageEmbedField{
Name: fmt.Sprintf("%v %v", response.Icon(k), response.IconKey(k)),
Value: fmt.Sprintf("**%v** %v", p.Sprintf("%d", v), response.IconValue(k)),
Inline: true,
})
}
}
for len(emFields)%3 != 0 {
emFields = append(emFields, &discordgo.MessageEmbedField{
Value: "\U0000FEFF",
Name: "\U0000FEFF",
Inline: true,
})
}
emFields := response.FormatEmFields(balanceData)
responseEmbed := &discordgo.MessageEmbed{
Type: discordgo.EmbedTypeRich,
Title: fmt.Sprintf("Current Sea of Thieves balance of user @%v", u.AuthorName),
Expand Down
28 changes: 1 addition & 27 deletions handler/sot_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import (
"github.com/wneessen/sotbot/api"
"github.com/wneessen/sotbot/response"
"github.com/wneessen/sotbot/user"
"golang.org/x/text/language"
"golang.org/x/text/message"
"net/http"
"sort"
)

// Just a test handler
Expand All @@ -30,30 +27,7 @@ func GetSotStats(h *http.Client, u *user.User) (*discordgo.MessageEmbed, error)
statsData["4_Ship"] = int64(userStats.ShipsSunk)
statsData["5_Vomit"] = int64(userStats.VomitedTotal)

p := message.NewPrinter(language.German)
var emFields []*discordgo.MessageEmbedField
var keyNames []string
for k := range statsData {
keyNames = append(keyNames, k)
}
sort.Strings(keyNames)
for _, k := range keyNames {
v := statsData[k]
if v != 0 {
emFields = append(emFields, &discordgo.MessageEmbedField{
Name: fmt.Sprintf("%v %v", response.Icon(k), response.IconKey(k)),
Value: fmt.Sprintf("**%v** %v", p.Sprintf("%d", v), response.IconValue(k)),
Inline: true,
})
}
}
for len(emFields)%3 != 0 {
emFields = append(emFields, &discordgo.MessageEmbedField{
Value: "\U0000FEFF",
Name: "\U0000FEFF",
Inline: true,
})
}
emFields := response.FormatEmFields(statsData)
responseEmbed := &discordgo.MessageEmbed{
Type: discordgo.EmbedTypeRich,
Title: fmt.Sprintf("Current Sea of Thieves stats of user @%v", u.AuthorName),
Expand Down
65 changes: 65 additions & 0 deletions handler/sot_summary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package handler

import (
"fmt"
"github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus"
"github.com/wneessen/sotbot/api"
"github.com/wneessen/sotbot/cache"
"github.com/wneessen/sotbot/response"
"github.com/wneessen/sotbot/user"
"gorm.io/gorm"
"net/http"
)

// Provide a daily summary
func GetSotSummary(h *http.Client, u *user.User, d *gorm.DB) (*discordgo.MessageEmbed, error) {
l := log.WithFields(log.Fields{
"action": "handler.GetSotSummary",
})

var oldBalance api.UserBalance
keyName := fmt.Sprintf("sot_balance_%v", u.UserInfo.UserId)
if err := cache.Read(keyName, &oldBalance, d); err != nil {
return &discordgo.MessageEmbed{}, err
}
userBalance, err := api.GetBalance(h, u.RatCookie)
if err != nil {
l.Errorf("Failed to fetch user balance from API: %v", err)
return &discordgo.MessageEmbed{}, err
}
summaryData := make(map[string]int64)
summaryData["1_Gold"] = int64(userBalance.Gold) - int64(oldBalance.Gold)
summaryData["2_Doubloon"] = int64(userBalance.Doubloons) - int64(oldBalance.Doubloons)
summaryData["3_AncientCoin"] = int64(userBalance.AncientCoins) - int64(oldBalance.AncientCoins)

var oldStats api.UserStats
keyName = fmt.Sprintf("sot_stats_%v", u.UserInfo.UserId)
if err := cache.Read(keyName, &oldStats, d); err != nil {
return &discordgo.MessageEmbed{}, err
}
userStats, err := api.GetStats(h, u.RatCookie)
if err != nil {
l.Errorf("An error occurred fetching user stats: %v", err)
return &discordgo.MessageEmbed{}, err
}
summaryData["4_Kraken"] = int64(userStats.KrakenDefeated) - int64(oldStats.KrakenDefeated)
summaryData["5_Megalodon"] = int64(userStats.MegalodonEncounters) - int64(oldStats.MegalodonEncounters)
summaryData["6_Chest"] = int64(userStats.ChestsHandedIn) - int64(oldStats.ChestsHandedIn)
summaryData["7_Ship"] = int64(userStats.ShipsSunk) - int64(oldStats.ShipsSunk)
summaryData["8_Vomit"] = int64(userStats.VomitedTotal) - int64(oldStats.VomitedTotal)

emFields := response.FormatEmFields(summaryData)
responseEmbed := &discordgo.MessageEmbed{
Type: discordgo.EmbedTypeRich,
Title: fmt.Sprintf("Daily Sea of Thieves summary for @%v", u.AuthorName),
Description: "No changes happend since yesterday",
}

if len(emFields) > 0 {
responseEmbed.Fields = emFields
responseEmbed.Description = ""
}

return responseEmbed, nil
}
39 changes: 39 additions & 0 deletions response/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package response

import (
"fmt"
"github.com/bwmarrin/discordgo"
"golang.org/x/text/language"
"golang.org/x/text/message"
"sort"
)

func FormatEmFields(vo map[string]int64) []*discordgo.MessageEmbedField {
// Prepare the output
p := message.NewPrinter(language.German)
var emFields []*discordgo.MessageEmbedField
var keyNames []string
for k := range vo {
keyNames = append(keyNames, k)
}
sort.Strings(keyNames)
for _, k := range keyNames {
v := vo[k]
if v != 0 {
emFields = append(emFields, &discordgo.MessageEmbedField{
Name: fmt.Sprintf("%v %v", Icon(k), IconKey(k)),
Value: fmt.Sprintf("%v**%v** %v", BalanceIcon(k, v), p.Sprintf("%d", v), IconValue(k)),
Inline: true,
})
}
}
for len(emFields)%3 != 0 {
emFields = append(emFields, &discordgo.MessageEmbedField{
Value: "\U0000FEFF",
Name: "\U0000FEFF",
Inline: true,
})
}

return emFields
}

0 comments on commit f02f322

Please sign in to comment.