init control, fix to snake case
This commit is contained in:
parent
75a1927d3a
commit
ee19412965
6
.gitignore
vendored
6
.gitignore
vendored
@ -1,6 +1,6 @@
|
||||
# Dragon Knight test/build files
|
||||
/dk
|
||||
_sessions.json
|
||||
users.json
|
||||
sessions.json
|
||||
/tmp
|
||||
wal.log
|
||||
*.db-shm
|
||||
*.db-wal
|
||||
|
9
control.json
Normal file
9
control.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"id": 1,
|
||||
"world_size": 200,
|
||||
"open": 1,
|
||||
"admin_email": "",
|
||||
"class_1_name": "Mage",
|
||||
"class_2_name": "Warrior",
|
||||
"class_3_name": "Paladin"
|
||||
}
|
@ -359,13 +359,21 @@ func toSnakeCase(s string) string {
|
||||
runes := []rune(s)
|
||||
|
||||
for i, r := range runes {
|
||||
if i > 0 && unicode.IsUpper(r) {
|
||||
// Don't add underscore if previous char was also uppercase
|
||||
// unless next char is lowercase (end of acronym)
|
||||
if !unicode.IsUpper(runes[i-1]) ||
|
||||
(i+1 < len(runes) && unicode.IsLower(runes[i+1])) {
|
||||
if i > 0 {
|
||||
prev := runes[i-1]
|
||||
|
||||
// Add underscore before digit if previous char was letter
|
||||
if unicode.IsDigit(r) && unicode.IsLetter(prev) {
|
||||
result.WriteByte('_')
|
||||
}
|
||||
// Add underscore before uppercase letter
|
||||
if unicode.IsUpper(r) {
|
||||
// Don't add if previous was also uppercase (unless end of acronym)
|
||||
if !unicode.IsUpper(prev) ||
|
||||
(i+1 < len(runes) && unicode.IsLower(runes[i+1])) {
|
||||
result.WriteByte('_')
|
||||
}
|
||||
}
|
||||
}
|
||||
result.WriteRune(unicode.ToLower(r))
|
||||
}
|
||||
|
@ -95,35 +95,46 @@ func showRegister(ctx sushi.Ctx) {
|
||||
|
||||
// processRegister handles registration form submission
|
||||
func processRegister(ctx sushi.Ctx) {
|
||||
fmt.Println("DEBUG: Starting registration process")
|
||||
|
||||
username := strings.TrimSpace(ctx.Form("username").String())
|
||||
email := strings.TrimSpace(ctx.Form("email").String())
|
||||
userPassword := ctx.Form("password").String()
|
||||
confirmPassword := ctx.Form("confirm_password").String()
|
||||
|
||||
fmt.Printf("DEBUG: Form data - username: '%s', email: '%s', password length: %d\n",
|
||||
username, email, len(userPassword))
|
||||
|
||||
formData := map[string]string{
|
||||
"username": username,
|
||||
"email": email,
|
||||
}
|
||||
|
||||
if err := validateRegistration(username, email, userPassword, confirmPassword); err != nil {
|
||||
fmt.Printf("DEBUG: Validation failed: %v\n", err)
|
||||
setFlashAndFormData(ctx, err.Error(), formData)
|
||||
ctx.Redirect("/register")
|
||||
return
|
||||
}
|
||||
fmt.Println("DEBUG: Validation passed")
|
||||
|
||||
// Check if username already exists
|
||||
if _, err := users.ByUsername(username); err == nil {
|
||||
fmt.Printf("DEBUG: Username '%s' already exists\n", username)
|
||||
setFlashAndFormData(ctx, "Username already exists", formData)
|
||||
ctx.Redirect("/register")
|
||||
return
|
||||
}
|
||||
fmt.Printf("DEBUG: Username '%s' is available\n", username)
|
||||
|
||||
// Check if email already exists
|
||||
if _, err := users.ByEmail(email); err == nil {
|
||||
fmt.Printf("DEBUG: Email '%s' already exists\n", email)
|
||||
setFlashAndFormData(ctx, "Email already registered", formData)
|
||||
ctx.Redirect("/register")
|
||||
return
|
||||
}
|
||||
fmt.Printf("DEBUG: Email '%s' is available\n", email)
|
||||
|
||||
// Create new user
|
||||
user := users.New()
|
||||
@ -133,26 +144,34 @@ func processRegister(ctx sushi.Ctx) {
|
||||
user.ClassID = 1
|
||||
user.Auth = 1
|
||||
|
||||
fmt.Printf("DEBUG: Created user struct with ID: %d\n", user.ID)
|
||||
|
||||
// Validate before inserting
|
||||
if err := user.Validate(); err != nil {
|
||||
fmt.Printf("DEBUG: User validation failed: %v\n", err)
|
||||
setFlashAndFormData(ctx, fmt.Sprintf("Invalid user data: %s", err.Error()), formData)
|
||||
ctx.Redirect("/register")
|
||||
return
|
||||
}
|
||||
fmt.Println("DEBUG: User validation passed")
|
||||
|
||||
if err := user.Insert(); err != nil {
|
||||
fmt.Printf("DEBUG: User insert failed: %v\n", err)
|
||||
setFlashAndFormData(ctx, "Failed to create account", formData)
|
||||
ctx.Redirect("/register")
|
||||
return
|
||||
}
|
||||
fmt.Printf("DEBUG: User inserted successfully with ID: %d\n", user.ID)
|
||||
|
||||
// Auto-login after registration
|
||||
ctx.Login(user.ID, user)
|
||||
fmt.Printf("DEBUG: User logged in with ID: %d\n", user.ID)
|
||||
|
||||
// Set success message
|
||||
sess := ctx.GetCurrentSession()
|
||||
sess.SetFlash("success", fmt.Sprintf("Greetings, %s!", user.Username))
|
||||
|
||||
fmt.Println("DEBUG: Registration completed successfully")
|
||||
ctx.Redirect("/")
|
||||
}
|
||||
|
||||
|
6
main.go
6
main.go
@ -9,6 +9,7 @@ import (
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
|
||||
"dk/internal/control"
|
||||
"dk/internal/database"
|
||||
"dk/internal/models/users"
|
||||
"dk/internal/routes"
|
||||
@ -62,10 +63,13 @@ func start(port string) error {
|
||||
}
|
||||
defer database.DB().Close()
|
||||
|
||||
control.Init(filepath.Join(cwd, "control.json"))
|
||||
defer control.Save()
|
||||
|
||||
template.InitializeCache(cwd)
|
||||
|
||||
app := sushi.New()
|
||||
sushi.InitSessions(filepath.Join(cwd, "data/_sessions.json"))
|
||||
sushi.InitSessions(filepath.Join(cwd, "data/sessions.json"))
|
||||
|
||||
app.Use(session.Middleware())
|
||||
app.Use(auth.Middleware(getUserByID))
|
||||
|
Loading…
x
Reference in New Issue
Block a user