mirror of
https://github.com/Mezeporta/Erupe.git
synced 2025-12-13 23:44:52 +01:00
automatically create new character when none exist
This commit is contained in:
@@ -8,15 +8,9 @@ import (
|
|||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Server) newUserChara(username string) error {
|
func (s *Server) newUserChara(uid int) error {
|
||||||
var id int
|
|
||||||
err := s.db.QueryRow("SELECT id FROM users WHERE username = $1", username).Scan(&id)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
var numNewChars int
|
var numNewChars int
|
||||||
err = s.db.QueryRow("SELECT COUNT(*) FROM characters WHERE user_id = $1 AND is_new_character = true", id).Scan(&numNewChars)
|
err := s.db.QueryRow("SELECT COUNT(*) FROM characters WHERE user_id = $1 AND is_new_character = true", uid).Scan(&numNewChars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -31,7 +25,7 @@ func (s *Server) newUserChara(username string) error {
|
|||||||
user_id, is_female, is_new_character, name, unk_desc_string,
|
user_id, is_female, is_new_character, name, unk_desc_string,
|
||||||
hrp, gr, weapon_type, last_login)
|
hrp, gr, weapon_type, last_login)
|
||||||
VALUES($1, False, True, '', '', 0, 0, 0, $2)`,
|
VALUES($1, False, True, '', '', 0, 0, 0, $2)`,
|
||||||
id,
|
uid,
|
||||||
uint32(time.Now().Unix()),
|
uint32(time.Now().Unix()),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -41,38 +35,20 @@ func (s *Server) newUserChara(username string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) registerDBAccount(username string, password string) error {
|
func (s *Server) registerDBAccount(username string, password string) (int, error) {
|
||||||
// Create salted hash of user password
|
// Create salted hash of user password
|
||||||
passwordHash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
passwordHash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return 0, err
|
||||||
}
|
|
||||||
|
|
||||||
_, err = s.db.Exec("INSERT INTO users (username, password, return_expires) VALUES ($1, $2, $3)", username, string(passwordHash), time.Now().Add(time.Hour*24*30))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var id int
|
var id int
|
||||||
err = s.db.QueryRow("SELECT id FROM users WHERE username = $1", username).Scan(&id)
|
err = s.db.QueryRow("INSERT INTO users (username, password, return_expires) VALUES ($1, $2, $3) RETURNING id", username, string(passwordHash), time.Now().Add(time.Hour*24*30)).Scan(&id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a base new character.
|
return id, nil
|
||||||
_, err = s.db.Exec(`
|
|
||||||
INSERT INTO characters (
|
|
||||||
user_id, is_female, is_new_character, name, unk_desc_string,
|
|
||||||
hrp, gr, weapon_type, last_login)
|
|
||||||
VALUES($1, False, True, '', '', 0, 0, 0, $2)`,
|
|
||||||
id,
|
|
||||||
uint32(time.Now().Unix()),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type character struct {
|
type character struct {
|
||||||
@@ -89,7 +65,7 @@ type character struct {
|
|||||||
|
|
||||||
func (s *Server) getCharactersForUser(uid int) ([]character, error) {
|
func (s *Server) getCharactersForUser(uid int) ([]character, error) {
|
||||||
characters := make([]character, 0)
|
characters := make([]character, 0)
|
||||||
err := s.db.Select(&characters, "SELECT id, is_female, is_new_character, name, unk_desc_string, hrp, gr, weapon_type, last_login FROM characters WHERE user_id = $1 AND deleted = false ORDER BY id ASC", uid)
|
err := s.db.Select(&characters, "SELECT id, is_female, is_new_character, name, unk_desc_string, hrp, gr, weapon_type, last_login FROM characters WHERE user_id = $1 AND deleted = false ORDER BY id", uid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,12 @@ import (
|
|||||||
func (s *Session) makeSignResponse(uid int) []byte {
|
func (s *Session) makeSignResponse(uid int) []byte {
|
||||||
// Get the characters from the DB.
|
// Get the characters from the DB.
|
||||||
chars, err := s.server.getCharactersForUser(uid)
|
chars, err := s.server.getCharactersForUser(uid)
|
||||||
|
if len(chars) == 0 {
|
||||||
|
err = s.server.newUserChara(uid)
|
||||||
|
if err == nil {
|
||||||
|
chars, err = s.server.getCharactersForUser(uid)
|
||||||
|
}
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.Warn("Error getting characters from DB", zap.Error(err))
|
s.logger.Warn("Error getting characters from DB", zap.Error(err))
|
||||||
}
|
}
|
||||||
@@ -23,7 +29,7 @@ func (s *Session) makeSignResponse(uid int) []byte {
|
|||||||
|
|
||||||
bf := byteframe.NewByteFrame()
|
bf := byteframe.NewByteFrame()
|
||||||
|
|
||||||
bf.WriteUint8(1) // resp_code
|
bf.WriteUint8(uint8(SIGN_SUCCESS)) // resp_code
|
||||||
if (s.server.erupeConfig.PatchServerManifest != "" && s.server.erupeConfig.PatchServerFile != "") || s.client == PS3 {
|
if (s.server.erupeConfig.PatchServerManifest != "" && s.server.erupeConfig.PatchServerFile != "") || s.client == PS3 {
|
||||||
bf.WriteUint8(2)
|
bf.WriteUint8(2)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ func (s *Session) authenticate(username string, password string) {
|
|||||||
s.logger.Info("User not found", zap.String("Username", username))
|
s.logger.Info("User not found", zap.String("Username", username))
|
||||||
if s.server.erupeConfig.DevMode && s.server.erupeConfig.DevModeOptions.AutoCreateAccount {
|
if s.server.erupeConfig.DevMode && s.server.erupeConfig.DevModeOptions.AutoCreateAccount {
|
||||||
s.logger.Info("Creating user", zap.String("Username", username))
|
s.logger.Info("Creating user", zap.String("Username", username))
|
||||||
err = s.server.registerDBAccount(username, password)
|
id, err = s.server.registerDBAccount(username, password)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
bf.WriteBytes(s.makeSignResponse(id))
|
bf.WriteBytes(s.makeSignResponse(id))
|
||||||
}
|
}
|
||||||
@@ -113,7 +113,7 @@ func (s *Session) authenticate(username string, password string) {
|
|||||||
if bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil || s.client == VITA || s.client == PS3 || s.client == WIIU {
|
if bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil || s.client == VITA || s.client == PS3 || s.client == WIIU {
|
||||||
s.logger.Debug("Passwords match!")
|
s.logger.Debug("Passwords match!")
|
||||||
if newCharaReq {
|
if newCharaReq {
|
||||||
err = s.server.newUserChara(username)
|
err = s.server.newUserChara(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.Error("Error adding new character to user", zap.Error(err))
|
s.logger.Error("Error adding new character to user", zap.Error(err))
|
||||||
bf.WriteUint8(uint8(SIGN_EABORT))
|
bf.WriteUint8(uint8(SIGN_EABORT))
|
||||||
|
|||||||
Reference in New Issue
Block a user