aboutsummaryrefslogtreecommitdiff
path: root/internal/game/session_registry.go
blob: cf4e8df265904a43e9e7863f0f0f8cc33e42d774 (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
package game

import "thehouseoficarus/internal/net"

// attachSession registers a fully-logged-in session in the game's
// character registry. Menu flows call it through connectCharacter; the test
// harness uses it to skip the menu.
func (g *Game) attachSession(sess *net.Session) {
	g.charsMu.Lock()
	g.loggedInChars[sess.Player.Name] = sess
	g.charsMu.Unlock()
}

// detachSession removes a character's registry entry and drops every command
// queued under its name. Leftover queue entries retain the departed session
// (and its transport) and would otherwise replay against the next session
// that logs in as the same character.
func (g *Game) detachSession(name string) {
	g.charsMu.Lock()
	delete(g.loggedInChars, name)
	g.charsMu.Unlock()
	if g.queue != nil {
		g.queue.ClearPlayer(name)
	}
}

// sessionRegistered reports whether sess is still the live session for name.
// Tick subscribers that can outlive a disconnect/re-login must consult this
// before acting on a session that is no longer in the world (a detached
// combat or logout subscriber would otherwise keep saving a ghost).
func (g *Game) sessionRegistered(sess *net.Session, name string) bool {
	if sess == nil || sess.Player == nil {
		return false
	}
	g.charsMu.Lock()
	current := g.loggedInChars[name]
	g.charsMu.Unlock()
	return current == sess
}