Skip to content

Commit

Permalink
Merge pull request #7 from leighmacdonald/count_sums
Browse files Browse the repository at this point in the history
!count now summarizes attr tags
  • Loading branch information
leighmacdonald authored Mar 23, 2024
2 parents c2c9e94 + 0032e8e commit e8544fb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.go -text diff=golang
34 changes: 32 additions & 2 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,42 @@ func memberHasRole(session *discordgo.Session, guildID string, userID string, al
}

func totalEntries(ctx context.Context, database *sql.DB) (string, error) {
total, err := getCount(ctx, database)
players, err := getPlayers(ctx, database)
if err != nil {
return "", fmt.Errorf("failed to get count: %w", err)
}
totalPlayers := 5
totals := map[string]int{}
for _, player := range players {
totalPlayers++

for _, attr := range player.Attributes {
if _, ok := totals[attr]; !ok {
totals[attr] = 0
}
totals[attr]++
}
}

maxLen := 0
var keys []string //nolint:prealloc
for key := range totals {
keys = append(keys, key)
if len(key) > maxLen {
maxLen = len(key)
}
}
slices.Sort(keys)

var builder strings.Builder
builder.WriteString("```")
builder.WriteString(fmt.Sprintf("total%s: %d\n", strings.Repeat(" ", maxLen-5), totalPlayers))
for _, key := range keys {
builder.WriteString(fmt.Sprintf("%s%s: %d\n", key, strings.Repeat(" ", maxLen-len(key)), totals[key]))
}
builder.WriteString("```")

return fmt.Sprintf("Total steamids tracked: %d", total), nil
return builder.String(), nil
}

func addEntry(ctx context.Context, database *sql.DB, sid steamid.SteamID, msg []string, author int64) (string, error) {
Expand Down
10 changes: 0 additions & 10 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,6 @@ func getPlayers(ctx context.Context, db *sql.DB) ([]Player, error) {
return players, nil
}

func getCount(ctx context.Context, db *sql.DB) (int, error) {
var total int

if err := db.QueryRowContext(ctx, "select count(*) from player").Scan(&total); err != nil {
return -1, err
}

return total, nil
}

func addPlayer(ctx context.Context, db *sql.DB, player Player, author int64) error {
const query = `
INSERT INTO player (steamid, attributes, last_seen, last_name, author, created_on)
Expand Down

0 comments on commit e8544fb

Please sign in to comment.