models.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package main
  2. import (
  3. "time"
  4. "golang.org/x/crypto/bcrypt"
  5. )
  6. type Model struct {
  7. Id uint64 `gorm:"primaryKey"`
  8. CreatedAt time.Time
  9. UpdatedAt time.Time
  10. }
  11. type User struct {
  12. Model
  13. Name string `gorm:"uniqueIndex,collate:no_case"`
  14. Password string // bcrypted
  15. Class uint8 // TODO: Convert this to an enum
  16. Level uint32
  17. Exp uint32 // TODO: Decide if this needs 64-bit
  18. Loc *Vec2 `gorm:"embedded"` // Location within the world, Embedded so it's here in the User table
  19. WorldId uint64 // TODO: Ensure this is in fact the World.Id
  20. Gold uint64 // On hand
  21. GoldBank uint64 // In bank
  22. RedGem uint32 // TODO: Decide if this needs 64-bit
  23. GreenGem uint32 // TODO: Decide if this needs 64-bit
  24. BlueGem uint32 // TODO: Decide if this needs 64-bit
  25. Health uint32 // Current Health, TODO: Decide if this needs 64-bit
  26. MaxHealth uint32 // Maximum Health, TODO: Decide if this needs 64-bit
  27. Magic uint32 // Current Magic Points, TODO: Decide if this needs 64-bit
  28. MaxMagic uint32 // Maximum Magic Points, TODO: Decide if this needs 64-bit
  29. Weapon uint8 // TODO: Convert this to an enum
  30. Armor uint8 // TODO: Convert this to an enum
  31. Ring uint8 // TODO: Convert this to an enum
  32. Amulet uint8 // TODO: Convert this to an enum
  33. }
  34. func (u *User) SetPassword(password string) error {
  35. hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
  36. if err != nil {
  37. return err
  38. }
  39. u.Password = string(hash)
  40. return nil
  41. }
  42. func (u *User) VerifyPassword(password string) error {
  43. err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
  44. return err
  45. }
  46. func (u *User) PasswordCost() int {
  47. cost, err := bcrypt.Cost([]byte(u.Password))
  48. if err != nil {
  49. return -1
  50. }
  51. return cost
  52. }