Skip to content

Commit

Permalink
More work on the UserReputationModel
Browse files Browse the repository at this point in the history
  • Loading branch information
wneessen committed Dec 3, 2022
1 parent a059350 commit 6835a90
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
14 changes: 11 additions & 3 deletions bot/sc_handler_sot_reputation.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func (b *Bot) SlashCmdSoTReputation(s *discordgo.Session, i *discordgo.Interacti
return fmt.Errorf("provided option value is not a string")
}

re, err := regexp.Compile(`^(?i:athena|hoarder|merchant|order|reaper|hunter|servants|guardians)$`)
re, err := regexp.Compile(`^(?i:factiong|hunterscall|merchantalliance|bilgerats|talltales|athenasfortune|` +
`goldhoarders|orderofsouls|reapersbones)$`)
if err != nil {
return err
}
Expand All @@ -69,11 +70,18 @@ func (b *Bot) SlashCmdSoTReputation(s *discordgo.Session, i *discordgo.Interacti
return err
}

rp, err := b.SoTGetReputation(r)
/*
rp, err := b.SoTGetReputation(r)
if err != nil {
return err
}
*/
b.Log.Debug().Msgf("UID: %d, Emi: %s", r.User.ID, fa)
ur, err := b.Model.UserReputation.GetByUserIDAtTime(r.User.ID, fa, time.Now().Add(time.Minute*-40))
if err != nil {
return err
}
b.Log.Debug().Msgf("FACTIONS: %+v\n", rp)
b.Log.Debug().Msgf("FACTIONS: %+v\n", ur)

/*
var ef []*discordgo.MessageEmbedField
Expand Down
18 changes: 9 additions & 9 deletions bot/slashcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,22 +185,22 @@ func (b *Bot) getSlashCommands() []*discordgo.ApplicationCommand {
// reputation provides the current emissary reputation value in the different factions
{
Name: "reputation",
Description: "Returns your current reputation value in the different emissary/allegiance factions",
Description: "Returns your current reputation value in the different emissary/allegiance faction",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "emissary-or-allegiance-faction",
Description: "Name of the emissary/allegiance faction",
Required: true,
Choices: []*discordgo.ApplicationCommandOptionChoice{
{Name: "Athena's Fortune", Value: "athena"},
{Name: "Gold Hoarder", Value: "hoarder"},
{Name: "Merchant Alliance", Value: "merchant"},
{Name: "Order of Souls", Value: "order"},
{Name: "Reaper's Bone", Value: "reaper"},
{Name: "Hunter's Call", Value: "hunter"},
{Name: "Servants of the Flame", Value: "servants"},
{Name: "Guardians of Fortune", Value: "guardians"},
{Name: "Athena's Fortune", Value: "athenasfortune"},
{Name: "Gold Hoarder", Value: "goldhoarders"},
{Name: "Merchant Alliance", Value: "merchantalliance"},
{Name: "Order of Souls", Value: "orderofsouls"},
{Name: "Reaper's Bone", Value: "reapersbones"},
{Name: "Hunter's Call", Value: "hunterscall"},
{Name: "Servants of the Flame", Value: "factionb"},
{Name: "Guardians of Fortune", Value: "factiong"},
},
},
},
Expand Down
3 changes: 3 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ var (

// ErrUserNil should be returned if the check for the *User returns nil
ErrUserNil = errors.New("user pointer must not be nil:w")

// ErrUserRepNotExistent should be used in case a requested user reputation was not found in the database
ErrUserRepNotExistent = errors.New("requested user reputation not existent in database")
)

// Model is a collection of all available models
Expand Down
32 changes: 17 additions & 15 deletions model/user_reputation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model
import (
"context"
"database/sql"
"errors"
"time"
)

Expand Down Expand Up @@ -58,38 +59,39 @@ func (m UserStatModel) GetByUserID(i int64) (*UserStat, error) {
}
return &us, nil
}
*/

// GetByUserIDAtTime retrieves the User details from the database based on the given User ID at a specific
// point of time
func (m UserStatModel) GetByUserIDAtTime(i int64, t time.Time) (*UserStat, error) {
q := `SELECT id, user_id, title, gold, doubloons, ancient_coins, kraken, megalodon, chests, ships, vomit, distance, ctime
FROM user_stats s
WHERE s.user_id = $1
AND s.ctime >= $2
func (m UserReputationModel) GetByUserIDAtTime(i int64, e string, t time.Time) (*UserReputation, error) {
q := `SELECT id, user_id, emissary, motto, rank, lvl, xp, next_lvl, xp_next_lvl, titlestotal, titlesunlocked,
emblemstotal, emblemsunlocked, itemstotal, itemsunlocked, ctime
FROM user_reputation r
WHERE r.user_id = $1
AND r.ctime >= $3
AND LOWER(r.emissary) = LOWER($2)
ORDER BY id
LIMIT 1`

var us UserStat
var ur UserReputation
ctx, cancel := context.WithTimeout(context.Background(), SQLTimeout)
defer cancel()

row := m.DB.QueryRowContext(ctx, q, i, t)
err := row.Scan(&us.ID, &us.UserID, &us.Title, &us.Gold, &us.Doubloons, &us.AncientCoins, &us.KrakenDefeated,
&us.MegalodonEnounter, &us.ChestsHandedIn, &us.ShipsSunk, &us.VomittedTimes, &us.DistanceSailed,
&us.CreateTime)
row := m.DB.QueryRowContext(ctx, q, i, e, t)
err := row.Scan(&ur.ID, &ur.UserID, &ur.Emissary, &ur.Motto, &ur.Rank, &ur.Level, &ur.Experience,
&ur.NextLevel, &ur.ExperienceNextLevel, &ur.TitlesTotal, &ur.TitlesUnlocked, &ur.EmblemsTotal,
&ur.EmblemsUnlocked, &ur.ItemsTotal, &ur.ItemsUnlocked, &ur.CreateTime)
if err != nil {
switch {
case errors.Is(err, sql.ErrNoRows):
return &us, ErrUserStatNotExistent
return &ur, ErrUserRepNotExistent
default:
return &us, err
return &ur, err
}
}
return &us, nil
return &ur, nil
}

*/

// Insert adds a new User into the database
func (m UserReputationModel) Insert(ur *UserReputation) error {
q := `INSERT INTO user_reputation (user_id, emissary, motto, rank, lvl, xp, next_lvl, xp_next_lvl, titlestotal,
Expand Down

0 comments on commit 6835a90

Please sign in to comment.