12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package main
- import (
- "time"
- "golang.org/x/crypto/bcrypt"
- )
- type Model struct {
- Id uint64 `gorm:"primaryKey"`
- CreatedAt time.Time
- UpdatedAt time.Time
- }
- type User struct {
- Model
- Name string `gorm:"uniqueIndex,collate:no_case"`
- Password string // bcrypted
- Class uint8 // TODO: Convert this to an enum
- Level uint32
- Exp uint32 // TODO: Decide if this needs 64-bit
- Loc *Vec2 `gorm:"embedded"` // Location within the world, Embedded so it's here in the User table
- WorldId uint64 // TODO: Ensure this is in fact the World.Id
- Gold uint64 // On hand
- GoldBank uint64 // In bank
- RedGem uint32 // TODO: Decide if this needs 64-bit
- GreenGem uint32 // TODO: Decide if this needs 64-bit
- BlueGem uint32 // TODO: Decide if this needs 64-bit
- Health uint32 // Current Health, TODO: Decide if this needs 64-bit
- MaxHealth uint32 // Maximum Health, TODO: Decide if this needs 64-bit
- Magic uint32 // Current Magic Points, TODO: Decide if this needs 64-bit
- MaxMagic uint32 // Maximum Magic Points, TODO: Decide if this needs 64-bit
- Weapon uint8 // TODO: Convert this to an enum
- Armor uint8 // TODO: Convert this to an enum
- Ring uint8 // TODO: Convert this to an enum
- Amulet uint8 // TODO: Convert this to an enum
- }
- func (u *User) SetPassword(password string) error {
- hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
- if err != nil {
- return err
- }
- u.Password = string(hash)
- return nil
- }
- func (u *User) VerifyPassword(password string) error {
- err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
- return err
- }
- func (u *User) PasswordCost() int {
- cost, err := bcrypt.Cost([]byte(u.Password))
- if err != nil {
- return -1
- }
- return cost
- }
|