| 439 | } |
| 440 | |
| 441 | func LoadUser(username string, skipValidation ...bool) (*UserRecord, error) { |
| 442 | |
| 443 | idx := GetUserIndex() |
| 444 | userId, found := idx.FindByUsername(username) |
| 445 | |
| 446 | if !found { |
| 447 | return nil, errors.New("user doesn't exist") |
| 448 | } |
| 449 | |
| 450 | userFilePath := util.FilePath(string(configs.GetFilePathsConfig().DataFiles), `/`, `users`, `/`, strconv.Itoa(userId)+`.yaml`) |
| 451 | |
| 452 | userFileTxt, err := util.ReadFile(userFilePath) |
| 453 | if err != nil { |
| 454 | return nil, err |
| 455 | } |
| 456 | |
| 457 | loadedUser := &UserRecord{} |
| 458 | if err := yaml.Unmarshal([]byte(userFileTxt), loadedUser); err != nil { |
| 459 | mudlog.Error("LoadUser", "error", err.Error()) |
| 460 | } |
| 461 | |
| 462 | if len(skipValidation) == 0 || !skipValidation[0] { |
| 463 | if err := loadedUser.Character.Validate(true); err == nil { |
| 464 | SaveUser(*loadedUser) |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | if loadedUser.Joined.IsZero() { |
| 469 | loadedUser.Joined = time.Now() |
| 470 | } |
| 471 | |
| 472 | // Set their connection time to now |
| 473 | loadedUser.connectionTime = time.Now() |
| 474 | |
| 475 | return loadedUser, nil |
| 476 | } |
| 477 | |
| 478 | // Loads all user recvords and runs against a function. |
| 479 | // Stops searching if false is returned. |