package main import ( "log" "time" "golang.org/x/crypto/bcrypt" "gorm.io/driver/sqlite" "gorm.io/gorm" ) type UserDB struct { DB *gorm.DB } type Model struct { Id uint64 `gorm:"primaryKey"` CreatedAt time.Time UpdatedAt time.Time } func (u *UserDB) Open(filename string) error { var err error u.DB, err = gorm.Open(sqlite.Open(filename), &gorm.Config{}) if err != nil { return err } err = u.DB.AutoMigrate(&User{}, &Faction{}) return err } type User struct { Model Name string `gorm:"uniqueIndex,collate:no_case"` Password string // bcrypt Faction *Faction `gorm:"-"` // Hot loaded in memory FactionId uint64 FactionSO bool // Faction.Senior_Officer FactionO bool // Faction.Officer Pos *Vec2 `gorm:"embedded"` World uint32 Health uint32 MaxHealth uint32 Magic uint32 MaxMagic uint32 Class Class // K T C W R Level uint32 Xp uint64 Weapon Weapon WeaponEnchant WeaponEnchant Armor Armor ArmorEnchant ArmorEnchant RangedWeapon RangedWeapon RangedWeaponEnchant RangedWeaponEnchant Shield Shield Ring Ring Amulet Amulet Gold uint64 GoldBanked uint64 GemRed uint32 GemGreen uint32 GemBlue uint32 } func (u *UserDB) FindUserByName(name string) *User { var usr *User result := u.DB.First(&usr, &User{Name: name}) if result.Error != nil { log.Printf("UserDB.FindUser(name='%s') > %v", name, result.Error) return nil } // Hot load the users faction if they are in one if usr != nil && usr.FactionId != 0 { fac := u.FindFactionById(usr.FactionId) if fac != nil { usr.Faction = fac } } return usr } func (u *UserDB) FindUserById(id uint64) *User { var usr *User result := u.DB.First(&usr, id) if result.Error != nil { log.Printf("UserDB.FindUser(id=%d) > %v", id, result.Error) return nil } // Hot load the users faction if they are in one if usr != nil && usr.FactionId != 0 { fac := u.FindFactionById(usr.FactionId) if fac != nil { usr.Faction = fac } } return usr } 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 { return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) } func (u *User) PasswordCost() int { cost, err := bcrypt.Cost([]byte(u.Password)) if err != nil { log.Printf("User.PasswordCost() > %v", err) return -1 } return cost } func (u *UserDB) SaveUser(usr *User) error { result := u.DB.Save(usr) return result.Error } type Faction struct { Model Name string `gorm:"unique,collate:no_case"` Description string LeaderId uint64 `gorm:"unique"` Gold uint64 GemRed uint32 GemGreen uint32 GemBlue uint32 } func (u *UserDB) FindFactionByName(name string) *Faction { var fac *Faction result := u.DB.First(&fac, &Faction{Name: name}) if result.Error != nil { log.Printf("UserDB.FindFactionByName(name='%s') > %v", name, result.Error) return nil } return fac } func (u *UserDB) FindFactionById(id uint64) *Faction { var fac *Faction result := u.DB.First(&fac, id) if result.Error != nil { log.Printf("UserDB.FindFactionById(id=%d) > %v", id, result.Error) return nil } return fac } func (u *UserDB) FindFactionByLeaderId(leader_id uint64) *Faction { var fac *Faction result := u.DB.First(&fac, &Faction{LeaderId: leader_id}) if result.Error != nil { log.Printf("UserDB.FindFactionByLeaderId(leader_id=%d) > %v", leader_id, result.Error) return nil } return fac } func (u *UserDB) SaveFaction(fac *Faction) error { result := u.DB.Save(fac) return result.Error }