aboutsummaryrefslogtreecommitdiff
path: root/internal/game/core_login_char.go
blob: 0f57d3cc8d53f6133a6c84f371c2ad71dab22356 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package game

import (
	"fmt"
	"strings"

	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
)

func (g *Game) handleNewCharName(sess *net.Session, input string) {
	name := strings.TrimSpace(input)

	if sess.PendingChar == "connect" && len(sess.Account.Characters) > 0 {
		if idx, err := parseChoiceIndex(name); err == nil && idx > 0 && idx <= len(sess.Account.Characters) {
			sess.PendingChar = ""
			g.connectCharacter(sess, sess.Account.Characters[idx-1])
			return
		}
		sess.WriteLine("Invalid choice.")
		sess.Write("\n> ")
		return
	}

	if name == "" {
		sess.Write("Character name: ")
		return
	}

	if verr := player.ValidName(name); verr != nil {
		sess.WriteLine(verr.Error())
		sess.Write("Character name: ")
		return
	}

	// Everything below runs on the owner goroutine (session input): the
	// existence check, file creation, and account publication are one
	// transaction with no interleaved session input, so a concurrent
	// creation of the same name cannot slip through the check.
	if g.AccountStore.CharacterExists(name) {
		sess.WriteLine("A character with that name already exists.")
		sess.Write("Character name: ")
		return
	}

	p, err := g.createCharacterTxn(sess.Account.Name, name, g.StartingRoom)
	if err != nil {
		sess.WriteLine(fmt.Sprintf("Error creating character: %v", err))
		sess.SetState(net.StateMenu)
		g.showMenu(sess)
		return
	}
	_ = p
	sess.Account.Characters = append(sess.Account.Characters, name)
	g.publishAccountUpdate(sess.Account.Name, sess.Account.Name)

	g.connectCharacter(sess, name)

	if sess.Player != nil {
		g.runEnterSteps(sess, sess.Player.RoomID)
	}
}

func (g *Game) connectCharacter(sess *net.Session, name string) {
	// This whole function runs as one owner step (invoked from session
	// input): membership validation, the online check, registry
	// publication, and room entry are serialized against every other
	// lifecycle operation, closing the check-then-register gap.

	// Authorize by ownership, not just existence: the character file's
	// owner must match this account, so a stale cached name cannot connect
	// a character reassigned to another account.
	actualName, err := g.canonicalChar(name)
	if err != nil {
		sess.WriteLine(fmt.Sprintf("Error loading character: %v", err))
		sess.SetState(net.StateMenu)
		g.showMenu(sess)
		return
	}
	if !g.isMemberOf(sess.Account.Name, actualName) {
		sess.WriteLine("That character does not belong to this account.")
		sess.SetState(net.StateMenu)
		g.showMenu(sess)
		return
	}
	name = actualName

	g.charsMu.Lock()
	if existing := g.loggedInChars[name]; existing != nil {
		g.charsMu.Unlock()
		sess.WriteLine("This character is logged in elsewhere.")
		sess.SetState(net.StateMenu)
		g.showMenu(sess)
		return
	}

	p, err := g.AccountStore.LoadCharacter(name)
	if err != nil {
		g.charsMu.Unlock()
		sess.WriteLine(fmt.Sprintf("Error loading character: %v", err))
		sess.SetState(net.StateMenu)
		g.showMenu(sess)
		return
	}
	g.adoptCharacterIdentity(p, sess.Account.Name)

	if g.Hub != nil {
		g.Hub.SetSessionPlayer(sess, p)
	} else {
		sess.Player = p
	}
	sess.SetState(net.StateGame)
	g.loggedInChars[name] = sess
	g.charsMu.Unlock()

	if sess.Account != nil && sess.Account.Options != nil {
		p.Options = make(map[string]any)
		for k, v := range sess.Account.Options {
			p.Options[k] = v
		}
	}

	p.Stats.RecordRoomVisit(p.RoomID)
	g.World.SeedGroundItems(p.RoomID)
	g.seedRoomMobs(p.RoomID)
	g.seedRoomObjects(p.RoomID)

	if g.Hub != nil {
		g.Hub.EnterRoom(sess, p.RoomID)
	}

	g.doLook(sess)
	g.checkAggro(sess)

	// Resume an interrupted LOCKED trigger sequence saved across disconnect.
	g.resumePendingSequence(sess, p)

	sess.WritePrompt("> ")
}

func (g *Game) handleRenameChar(sess *net.Session, input string) {
	name := strings.TrimSpace(input)
	if idx, err := parseChoiceIndex(name); err == nil && idx > 0 && idx <= len(sess.Account.Characters) {
		sess.PendingChar = sess.Account.Characters[idx-1]
	} else {
		sess.PendingChar = name
	}

	found := false
	for _, c := range sess.Account.Characters {
		if c == sess.PendingChar {
			found = true
			break
		}
	}
	if !found {
		sess.WriteLine(fmt.Sprintf("No character named %s on this account.", sess.PendingChar))
		g.showMenu(sess)
		return
	}

	sess.SetState(net.StateRenameCharName)
	sess.WriteLine(fmt.Sprintf("Renaming %s.", sess.PendingChar))
	sess.Write("New name: ")
}

// characterOnline reports whether the named character is currently connected
// through a live session. Menu operations that rename or delete character
// files must refuse while the character is online, otherwise the in-memory
// player later overwrites or resurrects the moved/removed file.
func (g *Game) characterOnline(name string) bool {
	g.charsMu.Lock()
	defer g.charsMu.Unlock()
	return g.loggedInChars[name] != nil
}

func (g *Game) handleRenameCharName(sess *net.Session, input string) {
	newName := strings.TrimSpace(input)
	oldName := sess.PendingChar
	if newName == "" {
		sess.Write("New name: ")
		return
	}
	if verr := player.ValidName(newName); verr != nil {
		sess.WriteLine(verr.Error())
		sess.Write("New name: ")
		return
	}
	if newName == oldName {
		sess.WriteLine("That's already the character's name.")
		g.showMenu(sess)
		return
	}

	// Checks and mutation run as one owner step: no session input can
	// interleave between the membership/online checks and the file
	// rename, and the rename is a single journal transaction.
	actualOld, err := g.canonicalChar(oldName)
	if err != nil {
		sess.WriteLine(fmt.Sprintf("Error renaming: %v", err))
		g.showMenu(sess)
		return
	}
	if !g.isMemberOf(sess.Account.Name, actualOld) {
		sess.WriteLine(fmt.Sprintf("No character named %s on this account.", oldName))
		sess.PendingChar = ""
		g.showMenu(sess)
		return
	}
	if newName != actualOld && g.AccountStore.CharacterExists(newName) {
		sess.WriteLine("A character with that name already exists.")
		sess.Write("New name: ")
		return
	}
	if g.characterOnline(actualOld) {
		sess.WriteLine("That character is logged in elsewhere. Log them out first.")
		sess.PendingChar = ""
		g.showMenu(sess)
		return
	}

	if err := g.renameCharacterTxn(sess.Account.Name, actualOld, newName); err != nil {
		sess.WriteLine(fmt.Sprintf("Error renaming: %v", err))
		g.showMenu(sess)
		return
	}

	sess.PendingChar = ""
	sess.WriteLine(fmt.Sprintf("%s renamed to %s.", actualOld, newName))
	// Refresh every session of this account so no stale character list or
	// pending selection survives the rename.
	g.publishAccountUpdate(sess.Account.Name, sess.Account.Name)
	g.showMenu(sess)
}

func (g *Game) showDeleteConfirm(sess *net.Session) {
	sess.SetState(net.StateDeleteChar)
	sess.WriteLine(fmt.Sprintf("\nDelete %s? Type DELETE %s to confirm, or anything else to cancel.", sess.PendingChar, sess.PendingChar))
}

func (g *Game) handleDeleteChar(sess *net.Session, input string) {
	if sess.PendingChar == "" {
		name := strings.TrimSpace(input)
		if idx, err := parseChoiceIndex(name); err == nil && idx > 0 && idx <= len(sess.Account.Characters) {
			sess.PendingChar = sess.Account.Characters[idx-1]
		} else {
			sess.PendingChar = name
		}
		found := false
		for _, c := range sess.Account.Characters {
			if c == sess.PendingChar {
				found = true
				break
			}
		}
		if !found {
			sess.WriteLine(fmt.Sprintf("No character named %s on this account.", sess.PendingChar))
			sess.PendingChar = ""
			g.showMenu(sess)
			return
		}
		g.showDeleteConfirm(sess)
		return
	}

	input = strings.TrimSpace(input)
	expected := "DELETE " + sess.PendingChar
	if !strings.EqualFold(input, expected) {
		sess.WriteLine("Delete cancelled.")
		sess.PendingChar = ""
		g.showMenu(sess)
		return
	}

	// Checks and mutation run as one owner step; the delete is a single
	// journal transaction (character file + account list).
	actualName, err := g.canonicalChar(sess.PendingChar)
	if err != nil {
		sess.WriteLine(fmt.Sprintf("Error deleting: %v", err))
		sess.PendingChar = ""
		g.showMenu(sess)
		return
	}
	if !g.isMemberOf(sess.Account.Name, actualName) {
		sess.WriteLine(fmt.Sprintf("No character named %s on this account.", sess.PendingChar))
		sess.PendingChar = ""
		g.showMenu(sess)
		return
	}
	if g.characterOnline(actualName) {
		sess.WriteLine("That character is logged in elsewhere. Log them out first.")
		sess.PendingChar = ""
		g.showMenu(sess)
		return
	}

	if err := g.deleteCharacterTxn(sess.Account.Name, actualName); err != nil {
		sess.WriteLine(fmt.Sprintf("Error deleting: %v", err))
		sess.PendingChar = ""
		g.showMenu(sess)
		return
	}

	sess.WriteLine(fmt.Sprintf("%s has been deleted.", actualName))
	sess.PendingChar = ""
	g.publishAccountUpdate(sess.Account.Name, sess.Account.Name)
	g.showMenu(sess)
}