Skip to content

Commit

Permalink
Merge pull request #138 from Cufee/release
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
Cufee authored Jan 19, 2025
2 parents cf0dd16 + d1aa15b commit 12036fa
Show file tree
Hide file tree
Showing 71 changed files with 2,211 additions and 635 deletions.
2 changes: 1 addition & 1 deletion cmd/core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/cufee/aftermath/internal/database"
"github.com/cufee/aftermath/internal/external/wargaming"
"github.com/cufee/aftermath/internal/realtime"
stats "github.com/cufee/aftermath/internal/stats/client/v1"
stats "github.com/cufee/aftermath/internal/stats/client/v2"
"github.com/cufee/aftermath/internal/stats/fetch/v1"
"golang.org/x/text/language"
)
Expand Down
8 changes: 7 additions & 1 deletion cmd/discord/commands/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ var UserOption = builder.NewOption("user", discordgo.ApplicationCommandOptionUse
builder.SetDescKey("common_option_stats_user_description"),
)

var DefaultStatsOptions = []builder.Option{
var SessionStatsOptions = []builder.Option{
DaysOption,
NicknameOption,
VehicleOption,
UserOption,
}

var CareerStatsOptions = []builder.Option{
NicknameOption,
VehicleOption,
UserOption,
}

type StatsOptions struct {
PeriodStart time.Time
Days int
Expand Down
12 changes: 6 additions & 6 deletions cmd/discord/commands/public/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func init() {
ComponentType(func(s string) bool {
var keys []string
keys = append(keys, "autocomplete_links_favorite_selected", "autocomplete_links_remove_selected") // links
keys = append(keys, "autocomplete_my_session_account", "autocomplete_my_stats_account") // my
keys = append(keys, "autocomplete_my_session_account", "autocomplete_my_career_account") // my
keys = append(keys, "autocomplete_widget_account") // widget
return slices.Contains(keys, s)
}).
Expand Down Expand Up @@ -63,8 +63,8 @@ func init() {
builder.NewCommand("autocomplete_tank_search").
ComponentType(func(s string) bool {
var keys []string
keys = append(keys, "autocomplete_stats_tank", "autocomplete_session_tank") // stats/session
keys = append(keys, "autocomplete_my_session_tank", "autocomplete_my_stats_tank") // my
keys = append(keys, "autocomplete_career_tank", "autocomplete_stats_tank", "autocomplete_session_tank") // stats/session
keys = append(keys, "autocomplete_my_session_tank", "autocomplete_my_career_tank") // my
return slices.Contains(keys, s)
}).
Handler(func(ctx common.Context) error {
Expand Down Expand Up @@ -100,9 +100,9 @@ func init() {
builder.NewCommand("autocomplete_account_search").
ComponentType(func(s string) bool {
var keys []string
keys = append(keys, "autocomplete_manage_accounts_search_nickname") // manage
keys = append(keys, "autocomplete_stats_nickname", "autocomplete_session_nickname") // stats/session
keys = append(keys, "autocomplete_links_add_nickname") // links
keys = append(keys, "autocomplete_manage_accounts_search_nickname") // manage
keys = append(keys, "autocomplete_career_nickname", "autocomplete_stats_nickname", "autocomplete_session_nickname") // stats/session
keys = append(keys, "autocomplete_links_add_nickname") // links
return slices.Contains(keys, s)
}).
Handler(func(ctx common.Context) error {
Expand Down
158 changes: 158 additions & 0 deletions cmd/discord/commands/public/career.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package public

import (
"bytes"
"context"
"errors"
"fmt"

"github.com/bwmarrin/discordgo"
"github.com/cufee/aftermath/cmd/discord/commands"
"github.com/cufee/aftermath/cmd/discord/commands/builder"
"github.com/cufee/aftermath/cmd/discord/common"
"github.com/cufee/aftermath/cmd/discord/middleware"
"github.com/cufee/aftermath/internal/database"
"github.com/cufee/aftermath/internal/database/models"
"github.com/cufee/aftermath/internal/external/blitzstars"
"github.com/cufee/aftermath/internal/log"
"github.com/cufee/aftermath/internal/logic"
"github.com/cufee/aftermath/internal/permissions"
stats "github.com/cufee/aftermath/internal/stats/client/common"
"github.com/cufee/aftermath/internal/utils"
)

var (
careerCommandMiddleware = []middleware.MiddlewareFunc{middleware.RequirePermissions(permissions.UseTextCommands, permissions.UseImageCommands)}
careerCommandOptions = commands.CareerStatsOptions
)

func careerCommandHandler(ctx common.Context) error {
options := commands.GetDefaultStatsOptions(ctx.Options())
message, valid := options.Validate(ctx)
if !valid {
return ctx.Reply().Send(message)
}

var accountID string
var opts = []stats.RequestOption{stats.WithWN8(), stats.WithVehicleID(options.TankID)}

ioptions := statsOptions{StatsOptions: options}

switch {
case options.UserID != "":
// mentioned another user, check if the user has an account linked
mentionedUser, _ := ctx.Core().Database().GetUserByID(ctx.Ctx(), options.UserID, database.WithConnections(), database.WithSubscriptions(), database.WithContent())
defaultAccount, hasDefaultAccount := mentionedUser.Connection(models.ConnectionTypeWargaming, nil, utils.Pointer(true))
if !hasDefaultAccount {
return ctx.Reply().Send("stats_error_connection_not_found_vague")
}
accountID = defaultAccount.ReferenceID

if img, content, err := logic.GetAccountBackgroundImage(ctx.Ctx(), ctx.Core().Database(), accountID); err == nil {
opts = append(opts, stats.WithBackground(img, true))
ioptions.BackgroundID = content.ID
}

case options.AccountID != "":
// account selected from autocomplete
accountID = options.AccountID

if img, content, err := logic.GetAccountBackgroundImage(ctx.Ctx(), ctx.Core().Database(), accountID); err == nil {
opts = append(opts, stats.WithBackground(img, true))
ioptions.BackgroundID = content.ID
}

case options.NicknameSearch != "" && options.AccountID == "":
// nickname provided, but user did not select an option from autocomplete
accounts, err := accountsFromBadInput(ctx.Ctx(), ctx.Core().Fetch(), options.NicknameSearch)
if err != nil {
return ctx.Err(err)
}
if len(accounts) == 0 {
return ctx.Reply().Send("stats_account_not_found")
}

realms := make(map[string]struct{})
for _, a := range accounts {
realms[a.Realm.String()] = struct{}{}
}
if len(realms) > 1 {
reply, err := realmSelectButtons(ctx, ctx.ID(), accounts)
if err != nil {
return ctx.Err(err)
}
return reply.Send()
}

// one or more options on the same server - just pick the first one
message = "stats_bad_nickname_input_hint"
accountID = fmt.Sprint(accounts[0].ID)

default:
defaultAccount, hasDefaultAccount := ctx.User().Connection(models.ConnectionTypeWargaming, nil, utils.Pointer(true))
if !hasDefaultAccount {
return ctx.Reply().Send("command_career_help_message")
}
// command used without options, but user has a default connection
accountID = defaultAccount.ReferenceID

if img, content, err := logic.GetAccountBackgroundImage(ctx.Ctx(), ctx.Core().Database(), accountID); err == nil {
opts = append(opts, stats.WithBackground(img, true))
ioptions.BackgroundID = content.ID
} else {
background, _ := ctx.User().Content(models.UserContentTypePersonalBackground)
if img, err := logic.UserContentToImage(background); err == nil {
opts = append(opts, stats.WithBackground(img, true))
ioptions.BackgroundID = background.ID
}
}
}

image, meta, err := ctx.Core().Stats(ctx.Locale()).PeriodImage(context.Background(), accountID, options.PeriodStart, opts...)
if errors.Is(err, blitzstars.ErrServiceUnavailable) {
return ctx.Reply().
Hint(ctx.InteractionID()).
Component(discordgo.ActionsRow{Components: []discordgo.MessageComponent{common.ButtonJoinPrimaryGuild(ctx.Localize("buttons_have_a_question_question"))}}).
Send("blitz_stars_error_service_down")
}
if err != nil {
return ctx.Err(err)
}

ioptions.AccountID = accountID
button, saveErr := ioptions.refreshButton(ctx, ctx.ID())
if saveErr != nil {
// nil button will not cause an error and will be ignored
log.Err(err).Str("interactionId", ctx.ID()).Str("command", "session").Msg("failed to save discord interaction")
}

var buf bytes.Buffer
err = image.PNG(&buf)
if err != nil {
return ctx.Err(err)
}

var timings []string
if ctx.User().HasPermission(permissions.UseDebugFeatures) {
timings = append(timings, "```")
for name, duration := range meta.Timings {
timings = append(timings, fmt.Sprintf("%s: %v", name, duration.Milliseconds()))
}
timings = append(timings, "```")
}

return ctx.Reply().WithAds().Hint(message).File(buf.Bytes(), "stats_command_by_aftermath.png").Component(button).Text(timings...).Send()
}

func init() {
commands.LoadedPublic.Add(builder.NewCommand("stats").
Middleware(careerCommandMiddleware...).
Options(careerCommandOptions...).
Params(builder.SetDescKey("command_career_desc")).
Handler(careerCommandHandler))
commands.LoadedPublic.Add(builder.NewCommand("career").
Middleware(careerCommandMiddleware...).
Options(careerCommandOptions...).
Params(builder.SetNameKey("command_career_name"), builder.SetDescKey("command_career_desc")).
Handler(careerCommandHandler))
}
4 changes: 2 additions & 2 deletions cmd/discord/commands/public/interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"golang.org/x/sync/errgroup"
"golang.org/x/text/language"

stats "github.com/cufee/aftermath/internal/stats/client/v1"
stats "github.com/cufee/aftermath/internal/stats/client/common"

"github.com/cufee/aftermath/internal/log"
"github.com/pkg/errors"
Expand Down Expand Up @@ -315,7 +315,7 @@ func init() {
var image stats.Image
var meta stats.Metadata
switch interaction.EventID {
case "stats":
case "career", "stats":
img, mt, err := ctx.Core().Stats(ctx.Locale()).PeriodImage(context.Background(), ioptions.AccountID, ioptions.PeriodStart, opts...)
if errors.Is(err, blitzstars.ErrServiceUnavailable) {
return ctx.Reply().
Expand Down
10 changes: 5 additions & 5 deletions cmd/discord/commands/public/my.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/cufee/aftermath/internal/log"
"github.com/cufee/aftermath/internal/logic"
"github.com/cufee/aftermath/internal/permissions"
stats "github.com/cufee/aftermath/internal/stats/client/v1"
stats "github.com/cufee/aftermath/internal/stats/client/common"
"github.com/cufee/aftermath/internal/stats/fetch/v1"
"github.com/cufee/aftermath/internal/utils"
"github.com/pkg/errors"
Expand All @@ -27,8 +27,8 @@ func init() {
Middleware(middleware.RequirePermissions(permissions.UseTextCommands, permissions.UseImageCommands)).
Params(builder.SetNameKey("command_my_name"), builder.SetDescKey("command_my_description")).
Options(
builder.NewOption("stats", discordgo.ApplicationCommandOptionSubCommand).
Params(builder.SetNameKey("command_my_stats_name"), builder.SetDescKey("command_my_stats_description")).
builder.NewOption("career", discordgo.ApplicationCommandOptionSubCommand).
Params(builder.SetNameKey("command_my_career_name"), builder.SetDescKey("command_my_career_description")).
Options(
commands.DaysOption,
commands.VehicleOption,
Expand Down Expand Up @@ -85,7 +85,7 @@ func init() {
var err error
var image stats.Image
switch subcommand {
case "stats":
case "career":
image, _, err = ctx.Core().Stats(ctx.Locale()).PeriodImage(context.Background(), accountID, options.PeriodStart, opts...)
case "session":
image, _, err = ctx.Core().Stats(ctx.Locale()).SessionImage(context.Background(), accountID, options.PeriodStart, opts...)
Expand Down Expand Up @@ -120,7 +120,7 @@ func init() {
if err != nil {
return ctx.Err(err)
}
return ctx.Reply().File(buf.Bytes(), "session_command_by_aftermath.png").Component(button).Send()
return ctx.Reply().WithAds().File(buf.Bytes(), "session_command_by_aftermath.png").Component(button).Send()
}),
)
}
2 changes: 1 addition & 1 deletion cmd/discord/commands/public/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/cufee/aftermath/cmd/discord/common"
"github.com/cufee/aftermath/cmd/discord/middleware"
"github.com/cufee/aftermath/internal/permissions"
stats "github.com/cufee/aftermath/internal/stats/client/v1"
stats "github.com/cufee/aftermath/internal/stats/client/common"
"github.com/cufee/aftermath/internal/stats/fetch/v1/replay"
"github.com/pkg/errors"
)
Expand Down
4 changes: 2 additions & 2 deletions cmd/discord/commands/public/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/cufee/aftermath/internal/log"
"github.com/cufee/aftermath/internal/logic"
"github.com/cufee/aftermath/internal/permissions"
stats "github.com/cufee/aftermath/internal/stats/client/v1"
stats "github.com/cufee/aftermath/internal/stats/client/common"
"github.com/cufee/aftermath/internal/stats/fetch/v1"
"github.com/cufee/aftermath/internal/utils"
"github.com/pkg/errors"
Expand All @@ -24,7 +24,7 @@ func init() {
commands.LoadedPublic.Add(
builder.NewCommand("session").
Middleware(middleware.RequirePermissions(permissions.UseTextCommands, permissions.UseImageCommands)).
Options(commands.DefaultStatsOptions...).
Options(commands.SessionStatsOptions...).
Handler(func(ctx common.Context) error {
options := commands.GetDefaultStatsOptions(ctx.Options())
message, valid := options.Validate(ctx)
Expand Down
Loading

0 comments on commit 12036fa

Please sign in to comment.