players now created with a name
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,5 @@
|
|||||||
.env
|
.env
|
||||||
|
.test.env
|
||||||
*.db*
|
*.db*
|
||||||
.logs/
|
.logs/
|
||||||
server.log
|
server.log
|
||||||
|
|||||||
@@ -28,14 +28,15 @@ func (p *Player) DisplayName() string {
|
|||||||
|
|
||||||
// NewPlayer creates a new player in the database. If there is an existing user with the same
|
// NewPlayer creates a new player in the database. If there is an existing user with the same
|
||||||
// discordID, it will automatically link that user to the player
|
// discordID, it will automatically link that user to the player
|
||||||
func NewPlayer(ctx context.Context, tx bun.Tx, discordID string, audit *AuditMeta) (*Player, error) {
|
func NewPlayer(ctx context.Context, tx bun.Tx, name, discordID string, audit *AuditMeta) (*Player, error) {
|
||||||
player := &Player{DiscordID: discordID}
|
player := &Player{DiscordID: discordID, Name: name}
|
||||||
user, err := GetUserByDiscordID(ctx, tx, discordID)
|
user, err := GetUserByDiscordID(ctx, tx, discordID)
|
||||||
if err != nil && !IsBadRequest(err) {
|
if err != nil && !IsBadRequest(err) {
|
||||||
return nil, errors.Wrap(err, "GetUserByDiscordID")
|
return nil, errors.Wrap(err, "GetUserByDiscordID")
|
||||||
}
|
}
|
||||||
if user != nil {
|
if user != nil {
|
||||||
player.UserID = &user.ID
|
player.UserID = &user.ID
|
||||||
|
player.Name = user.Username
|
||||||
}
|
}
|
||||||
err = Insert(tx, player).
|
err = Insert(tx, player).
|
||||||
WithAudit(audit, nil).Exec(ctx)
|
WithAudit(audit, nil).Exec(ctx)
|
||||||
@@ -45,6 +46,16 @@ func NewPlayer(ctx context.Context, tx bun.Tx, discordID string, audit *AuditMet
|
|||||||
return player, nil
|
return player, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewPlayerFromLog(ctx context.Context, tx bun.Tx, name string, slapID uint32, audit *AuditMeta) (*Player, error) {
|
||||||
|
player := &Player{Name: name, SlapID: &slapID}
|
||||||
|
err := Insert(tx, player).
|
||||||
|
WithAudit(audit, nil).Exec(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "Insert")
|
||||||
|
}
|
||||||
|
return player, nil
|
||||||
|
}
|
||||||
|
|
||||||
// ConnectPlayer links the user to an existing player, or creates a new player to link if not found
|
// ConnectPlayer links the user to an existing player, or creates a new player to link if not found
|
||||||
// Populates User.Player on success
|
// Populates User.Player on success
|
||||||
func (u *User) ConnectPlayer(ctx context.Context, tx bun.Tx, audit *AuditMeta) error {
|
func (u *User) ConnectPlayer(ctx context.Context, tx bun.Tx, audit *AuditMeta) error {
|
||||||
@@ -56,7 +67,7 @@ func (u *User) ConnectPlayer(ctx context.Context, tx bun.Tx, audit *AuditMeta) e
|
|||||||
return errors.Wrap(err, "GetByField")
|
return errors.Wrap(err, "GetByField")
|
||||||
}
|
}
|
||||||
// Player doesn't exist, create a new one
|
// Player doesn't exist, create a new one
|
||||||
player, err = NewPlayer(ctx, tx, u.DiscordID, audit)
|
player, err = NewPlayer(ctx, tx, u.Username, u.DiscordID, audit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "NewPlayer")
|
return errors.Wrap(err, "NewPlayer")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user