-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer_save.go
166 lines (139 loc) · 3.71 KB
/
player_save.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"bytes"
"encoding/json"
"strings"
"sync"
"time"
)
// Autosave all players
func saveAllCharacters(force bool) {
for _, target := range charList {
if target.dirty || force {
target.saveCharacter()
}
}
}
func characterNameAvailable(name string) bool {
var accs, chars int
//defer func() { mudLog("characterNameAvailable: searched %v accounts and %v characters.", accs, chars) }()
for _, item := range accountIndex {
accs++
desc := descData{}
desc.loadAccount(item.UUID)
for _, item := range desc.account.Characters {
chars++
if strings.EqualFold(item.Login, name) {
return false
}
}
}
return true
}
var charSaveLock sync.Mutex
// Returns true on save
func (player *characterData) saveCharacter() bool {
charSaveLock.Lock()
defer charSaveLock.Unlock()
if player.desc == nil {
critLog("savePlayer: Nil desc: %v", player.Name)
return false
} else if player.desc.account == nil {
critLog("savePlayer: Nil account: %v", player.Name)
return false
} else if player == nil {
critLog("savePlayer: Nil player.")
return false
} else if !player.UUID.hasUUID() {
critLog("savePlayer: Player '%v' doesn't have a UUID.", player.Name)
return false
}
player.Version = CHARACTER_VERSION
player.SaveTime = time.Now().UTC()
target := *player
player.dirty = false
go func(target characterData) {
fileName := DATA_DIR + ACCOUNT_DIR + target.desc.account.UUID.toString() + "/" + target.UUID.toString() + ".json"
outbuf := new(bytes.Buffer)
enc := json.NewEncoder(outbuf)
enc.SetIndent("", "\t")
err := enc.Encode(&target)
if err != nil {
critLog("savePlayer: enc.Encode: %v", err.Error())
return
}
err = saveFile(fileName, outbuf.Bytes())
if err != nil {
critLog("savePlayer: saveFile failed %v", err.Error())
return
}
}(target)
return true
}
// Load character into game world.
func (desc *descData) loadCharacter(plrStr string) *characterData {
if desc == nil || desc.account == nil {
return nil
}
uuid := uuidData{}
for _, target := range desc.account.Characters {
if target.Login == plrStr {
uuid = target.UUID
break
}
}
if !uuid.hasUUID() {
critLog("loadPlayer: Player not found in account.")
return nil
}
target := checkPlayingUUID(plrStr, uuid)
if target != nil {
target.send(fairwellBuf)
target.send("Another connection from your account has forcefully taken over control of this character.")
target.desc.kill()
desc.character = target
desc.character.desc = desc
desc.state = CON_PLAYING
return target
} else {
data, err := readFile(DATA_DIR + ACCOUNT_DIR + desc.account.UUID.toString() + "/" + uuid.toString() + ".json")
if err != nil {
return nil
}
player := &characterData{ConfigVals: map[int]*ConfigValue{}, NoteRead: map[string]time.Time{}}
err = json.Unmarshal(data, player)
if err != nil {
critLog("loadPlayer: Unable to unmarshal the data.")
return nil
}
if desc.telnet.Options != nil && player.Config.hasFlag(CONFIG_NOCOLOR) {
desc.telnet.Options.MTTS.clearFlag(1 << MTTS_ANSI)
}
return player
}
}
// Load a player, but do not load into game world.
func (desc *descData) pLoad(plrStr string) *characterData {
for _, acc := range accountIndex {
desc := descData{}
desc.loadAccount(acc.UUID)
for _, char := range desc.account.Characters {
if !strings.EqualFold(char.Login, plrStr) {
continue
}
data, err := readFile(DATA_DIR + ACCOUNT_DIR + acc.UUID.toString() + "/" + char.UUID.toString() + ".json")
if err != nil {
return nil
}
target := &characterData{}
err = json.Unmarshal(data, target)
if err != nil {
critLog("loadPlayer: Unable to unmarshal the data.")
return nil
}
target.desc = &desc
return target
}
}
return nil
}