Skip to content

Commit

Permalink
Fix stats issue with invalid match ids (#425)
Browse files Browse the repository at this point in the history
* fix: fix stats issue with invalid match ids

* fix: fix argument in test
  • Loading branch information
kurokobo committed May 5, 2021
1 parent 6fecf8d commit b0ae8d9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
5 changes: 3 additions & 2 deletions discord/command_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/denverquane/amongusdiscord/metrics"
"log"
"regexp"
"strconv"
"strings"

"github.com/denverquane/amongusdiscord/metrics"

"github.com/bwmarrin/discordgo"
"github.com/denverquane/amongusdiscord/discord/command"
"github.com/denverquane/amongusdiscord/storage"
Expand Down Expand Up @@ -397,7 +398,7 @@ func (bot *Bot) HandleCommand(isAdmin, isPermissioned bool, sett *storage.GuildS
if len(strs) < 2 {
log.Println("Something very wrong with the regex for match/conn codes...")
} else {
s.ChannelMessageSendEmbed(m.ChannelID, bot.GameStatsEmbed(strs[1], strs[0], sett, premStatus))
s.ChannelMessageSendEmbed(m.ChannelID, bot.GameStatsEmbed(m.GuildID, strs[1], strs[0], sett, premStatus))
}
} else {
s.ChannelMessageSend(m.ChannelID, "I didn't recognize that user, you mistyped 'guild', or didn't provide a valid Match ID")
Expand Down
20 changes: 12 additions & 8 deletions discord/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"bytes"
"context"
"fmt"
"log"
"strconv"
"strings"

"github.com/automuteus/utils/pkg/game"
"github.com/automuteus/utils/pkg/premium"
"github.com/automuteus/utils/pkg/rediskey"
"github.com/bwmarrin/discordgo"
"github.com/denverquane/amongusdiscord/storage"
"github.com/nicksnyder/go-i18n/v2/i18n"
"log"
"strconv"
"strings"
)

func (bot *Bot) UserStatsEmbed(userID, guildID string, sett *storage.GuildSettings, prem premium.Tier) *discordgo.MessageEmbed {
Expand Down Expand Up @@ -893,15 +894,18 @@ func (bot *Bot) GuildStatsEmbed(guildID string, sett *storage.GuildSettings, pre
return &embed
}

func (bot *Bot) GameStatsEmbed(matchID, connectCode string, sett *storage.GuildSettings, prem premium.Tier) *discordgo.MessageEmbed {
gameData, err := bot.PostgresInterface.GetGame(connectCode, matchID)
func (bot *Bot) GameStatsEmbed(guildID, matchID, connectCode string, sett *storage.GuildSettings, prem premium.Tier) *discordgo.MessageEmbed {
gameData, err := bot.PostgresInterface.GetGame(guildID, connectCode, matchID)
if err != nil {
log.Fatal(err)
}

events, err := bot.PostgresInterface.GetGameEvents(matchID)
if err != nil {
log.Fatal(err)
events := []*storage.PostgresGameEvent{}
if gameData != nil {
events, err = bot.PostgresInterface.GetGameEvents(matchID)
if err != nil {
log.Fatal(err)
}
}

stats := storage.StatsFromGameAndEvents(gameData, events)
Expand Down
14 changes: 10 additions & 4 deletions storage/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"bytes"
"encoding/json"
"fmt"
"log"
"time"

"github.com/automuteus/galactus/broker"
"github.com/automuteus/utils/pkg/game"
"github.com/bwmarrin/discordgo"
"github.com/nicksnyder/go-i18n/v2/i18n"
"log"
"time"
)

var DiscussCode = fmt.Sprintf("%d", game.DISCUSS)
Expand Down Expand Up @@ -43,13 +44,18 @@ type GameStatistics struct {

func StatsFromGameAndEvents(pgame *PostgresGame, events []*PostgresGameEvent) GameStatistics {
stats := GameStatistics{
GameDuration: time.Second * time.Duration(pgame.EndTime-pgame.StartTime),
WinType: game.GameResult(pgame.WinType),
GameDuration: 0,
WinType: game.Unknown,
NumMeetings: 0,
NumDeaths: 0,
Events: []SimpleEvent{},
}

if pgame != nil {
stats.GameDuration = time.Second * time.Duration(pgame.EndTime-pgame.StartTime)
stats.WinType = game.GameResult(pgame.WinType)
}

if len(events) < 2 {
return stats
}
Expand Down
4 changes: 2 additions & 2 deletions storage/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ func (psqlInterface *PsqlInterface) GetUser(userID uint64) (*PostgresUser, error
return nil, nil
}

func (psqlInterface *PsqlInterface) GetGame(connectCode, matchID string) (*PostgresGame, error) {
func (psqlInterface *PsqlInterface) GetGame(guildID, connectCode, matchID string) (*PostgresGame, error) {
games := []*PostgresGame{}
err := pgxscan.Select(context.Background(), psqlInterface.Pool, &games, "SELECT * FROM games WHERE game_id = $1 AND connect_code = $2;", matchID, connectCode)
err := pgxscan.Select(context.Background(), psqlInterface.Pool, &games, "SELECT * FROM games WHERE guild_id = $1 AND game_id = $2 AND connect_code = $3;", guildID, matchID, connectCode)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion storage/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestPsqlInterface_Init(t *testing.T) {

psql.GetGuildPremiumStatus("141082723635691521")

game, err := psql.GetGame("B7B80986", "78467")
game, err := psql.GetGame("141082723635691521", "B7B80986", "78467")
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit b0ae8d9

Please sign in to comment.