character.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package world
  2. import "fmt"
  3. import "os"
  4. // import "strconv"
  5. import "github.com/beoran/woe/monolog"
  6. import "github.com/beoran/woe/sitef"
  7. /* Characters may exist "outside" of any world, because, at login of a client,
  8. * the Character must be loaded or created before it enters a World. */
  9. type Character struct {
  10. Being
  11. Account * Account
  12. }
  13. func NewCharacterFromBeing(being Being, account * Account) (*Character) {
  14. return &Character{being, account}
  15. }
  16. func (me * Character) Init(account * Account, name string,
  17. kin Entitylike, gender Entitylike, job Entitylike) (* Character) {
  18. me.Account = account
  19. me.Being.Init("character", name, account.Privilege, kin, gender, job)
  20. return me
  21. }
  22. func NewCharacter(account * Account, name string,
  23. kin Entitylike, gender Entitylike, job Entitylike) (* Character) {
  24. me := &Character{};
  25. return me.Init(account, name, kin, gender, job)
  26. }
  27. // Save a character into a a sitef record.
  28. func (me * Character) SaveSirec(rec * sitef.Record) (err error) {
  29. rec.Put("accountname", me.Account.Name)
  30. me.Being.SaveSitef(rec)
  31. return nil
  32. }
  33. // Load a character from a sitef record.
  34. func (me * Character) LoadSirec(rec sitef.Record) (err error) {
  35. aname := rec.Get("accountname")
  36. account, err := DefaultWorld.LoadAccount(aname)
  37. if err != nil {
  38. return err
  39. }
  40. me.Account = account
  41. me.Being.LoadSitef(rec)
  42. return nil
  43. }
  44. // Save a character as a sitef file.
  45. func (me * Character) Save(dirname string) (err error) {
  46. path := SavePathFor(dirname, "character", me.ID)
  47. rec := sitef.NewRecord()
  48. me.SaveSirec(rec)
  49. monolog.Debug("Saving Character record: %s %v", path, rec)
  50. return sitef.SaveRecord(path, *rec)
  51. }
  52. // Load a character from a sitef file. Does no account checking, but returns the account name.
  53. func LoadCharacter(dirname string, id string) (character *Character, aname string, err error) {
  54. path := SavePathFor(dirname, "character", id)
  55. records, err := sitef.ParseFilename(path)
  56. if err != nil {
  57. return nil, "", err
  58. }
  59. if len(records) < 1 {
  60. return nil, "", fmt.Errorf("No sitef record found for %s!", id)
  61. }
  62. record := records[0]
  63. monolog.Info("Loading Character record: %s %v", path, record)
  64. character = new(Character)
  65. aname = record.Get("accountname")
  66. character.Being.LoadSitef(*record);
  67. return character, aname, nil
  68. }
  69. // Load a character WITH A GIVEN NAME from a sitef file. Does no account checking, but returns the account name.
  70. func LoadCharacterByName(dirname string, name string) (character *Character, aname string, err error) {
  71. id := EntityNameToID("character", name)
  72. return LoadCharacter(dirname, id)
  73. }
  74. // Load an character from a sitef file for the given account.
  75. func (account * Account) LoadCharacter(dirname string, id string) (character *Character, err error) {
  76. character, aname, err := LoadCharacter(dirname, id)
  77. if character == nil {
  78. return character, err
  79. }
  80. if aname != account.Name {
  81. err := fmt.Errorf("Account doesn't match! %s %s", aname, account.Name)
  82. monolog.Error("%s", err.Error())
  83. return nil, err
  84. }
  85. character.Account = account
  86. return character, nil
  87. }
  88. // Deletes the character itself from disk
  89. func (me * Character) Delete(dirname string) bool {
  90. path := SavePathFor(dirname, "character", me.ID)
  91. if err := os.Remove(path) ; err != nil {
  92. monolog.Warning("Could not delete character: %v %s: %s",
  93. me, path, err.Error())
  94. return false
  95. }
  96. me.Account = nil
  97. monolog.Info("Character deleted: %s", me.ID)
  98. return true
  99. }
  100. func (me * Character) AskLong() string {
  101. return me.Long + "\n" + me.ToStatus()
  102. }