Skip to content

Commit 4a92cae

Browse files
committed
1 parent d1385dc commit 4a92cae

4 files changed

Lines changed: 58 additions & 5 deletions

File tree

docs/database/bsa_accounts.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ A player can have one account per provider.
1111
CREATE TABLE `bsa_accounts` (
1212
`account_id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
1313
`provider_id` BIGINT UNSIGNED NOT NULL,
14-
`server_id` BIGINT UNSIGNED NULL,
1514
`player_id` BIGINT UNSIGNED NOT NULL,
1615
`identifier` VARCHAR(255) NOT NULL,
1716
`username` VARCHAR(255) NULL,
@@ -41,12 +40,11 @@ CREATE TABLE `bsa_accounts` (
4140

4241
- account_id - unique id of the account.
4342
- provider_id - provider that owns this account identity.
44-
- server_id - server currently reporting this account as active; null means no server affinity.
4543
- player_id - player that owns the account.
4644
- identifier - unique provider identifier (`steamid64`, `snowflake`, etc) within a provider.
4745
- username - provider username at time of record.
4846
- time - generic time accumulator for the account.
49-
- heartbeat_at - account heartbeat used to detect stale active sessions.
47+
- heartbeat_at - account heartbeat used for recent activity.
5048
- created_at - timestamp for when the account was created.
5149

5250
## Restrictions
@@ -66,6 +64,7 @@ Triggers prevent update or deletion of these rows.
6664

6765
## Interlink Behavior
6866
- Account presence and session ownership are coordinated with periodic `heartbeat_at` updates and `server_id` attach/detach writes.
67+
- You must apply a `session_id` from `bsa_sessions` to the account data being networked.
6968
- `database.players:connected(account, secondaries)`
7069
- `database.players:disconnected(account, secondaries)`
7170
- `database.players:timeset(account, time_seconds)`

docs/database/bsa_sessions.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# bsa_sessions
2+
Sessions represent live or recently-live account presence on servers.\
3+
An account may have multiple session rows at once.
4+
5+
!!! warning
6+
This table may incur high reads to the database due to online-state lookups.\
7+
Please ensure your platform isn't requesting information so often.
8+
9+
## Structure
10+
```sql
11+
CREATE TABLE `bsa_sessions` (
12+
`session_id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
13+
`account_id` BIGINT UNSIGNED NOT NULL,
14+
`server_id` BIGINT UNSIGNED NULL,
15+
`heartbeat_at` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
16+
`created_at` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
17+
18+
CONSTRAINT fk_conn_server
19+
FOREIGN KEY (`server_id`) REFERENCES `bsa_servers`(`server_id`)
20+
ON DELETE SET NULL
21+
ON UPDATE CASCADE,
22+
23+
CONSTRAINT fk_conn_account
24+
FOREIGN KEY (`account_id`) REFERENCES `bsa_accounts`(`account_id`)
25+
ON DELETE CASCADE
26+
ON UPDATE CASCADE
27+
);
28+
```
29+
30+
- session_id - unique id of the session.
31+
- account_id - account that owns this session.
32+
- server_id - server currently attached to the session; null means detached or orphaned from a server record.
33+
- heartbeat_at - timestamp for the last session heartbeat.
34+
- created_at - timestamp for when the session row was created.
35+
36+
## Restrictions
37+
- There are no trigger-protected rows in this table.
38+
- Multiple sessions per account are allowed by design.
39+
- Deleting an account cascades and deletes its sessions.
40+
- Deleting a server does not delete sessions; it sets `server_id` to `NULL`.
41+
42+
## Design Requirements
43+
- Treat a session as alive only when `heartbeat_at` is within 60 seconds, and the linked server heartbeat in `bsa_servers` is also within 60 seconds.
44+
- Create a new row when a player/account attaches to a server and no reusable `session_id` is already held locally.
45+
- Refresh `heartbeat_at` regularly for active sessions, the current platform also refreshes `server_id` on heartbeat.
46+
- Delete the row on clean disconnect/shutdown rather than keeping indefinite session history.
47+
- Always propagate `session_id` with replicated account presence so remote caches can update the correct live entry.
48+
49+
## Interlink Behavior
50+
- `database.players:connected(account, secondaries)` payloads must include `session_id`.
51+
- `database.players:disconnected(account, secondaries)` payloads must include `session_id`.
52+
- On interlink reconnect/startup, platforms should clear any stale sessions owned by their current `server_id` and broadcast matching disconnect events.

docs/platforms/garrysmod/database/variables/players.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Replicated per-player state and player-facing helper API exposed through `BSA.Pl
66

77
## Runtime Events
88
- `#!ts players.initialized(entity: Player)`
9-
- `#!ts players.connected(steamid64: string, player?: Player)`
10-
- `#!ts players.disconnected(steamid64: string, player?: Player)`
9+
- `#!ts players.connected(steamid64: string, session_id: number, player?: Player)`
10+
- `#!ts players.disconnected(steamid64: string, session_id: number, player?: Player)`
1111
- `#!ts players.faked(entity: Player, ent_index: number, name?: string, color?: Color)`
1212
- `#!ts players.disguised(entity: Player, ent_index: number, disguise_name?: string)`
1313
- `#!ts players.primaried(entity: Player, old_group, new_group)`
@@ -47,6 +47,7 @@ Time + AFK:
4747

4848
- `#!ts player:GetPlayTime(absolute?: boolean): number`
4949
- `#!ts player:GetSessionTime(): number`
50+
- `#!ts player:GetSessionID(): number`
5051
- `#!ts player:SetAFK(state: boolean): void`
5152
- `#!ts player:GetAFK(): boolean`
5253
- `#!ts player:GetAFKTime(absolute?: boolean): number`

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ nav:
2222
- Interlink: database/bsa_interlink.md
2323
- Groups: database/bsa_groups.md
2424
- Players: database/bsa_players.md
25+
- Sessions: database/bsa_sessions.md
2526
- Player Groups: database/bsa_player_groups.md
2627
- Permissions: database/bsa_permissions.md
2728
- Group Permissions: database/bsa_group_permissions.md

0 commit comments

Comments
 (0)