v0.10.0
Class: Player
Represents a connected client in the game.
✅ Attribute Support
The Player class handles communication with the client and keeps track of the player’s current table and custom attributes.
Table of contents
Constructors
Properties
Methods
- disconnect
- getAttribute
- getAttributes
- getTable
- hasAttribute
- onDisconnect
- sendMessage
- setAttribute
- setAttributes
- setSocket
- setTable
Constructors
constructor
• new Player(socket, eventBus, id?): Player
Parameters
| Name | Type |
|---|---|
socket | WebSocket |
eventBus | EventBus |
id? | string |
Returns
Properties
id
• Readonly id: string
Methods
disconnect
▸ disconnect(): void
Disconnect the player from the server.
Returns
void
getAttribute
▸ getAttribute(key): unknown
Get a single attribute from the player.
Parameters
| Name | Type | Description |
|---|---|---|
key | Object | The key of the attribute to get |
key.key | string | - |
Returns
unknown
The value of the attribute, or undefined if it doesn’t exist
getAttributes
▸ getAttributes(): Record<string, unknown>
Get all attributes from the player.
Returns
Record<string, unknown>
An object containing all player attributes
getTable
▸ getTable(): null | Table
Returns
null | Table
hasAttribute
▸ hasAttribute(key): boolean
Check if the player has an attribute.
Parameters
| Name | Type | Description |
|---|---|---|
key | Object | The key of the attribute to check |
key.key | string | - |
Returns
boolean
True if the attribute exists, false otherwise
onDisconnect
▸ onDisconnect(callback): void
Register a callback to be called when the player disconnects
Parameters
| Name | Type | Description |
|---|---|---|
callback | Object | The function to call when the player disconnects |
callback.callback | () => void | - |
Returns
void
sendMessage
▸ sendMessage(«destructured»): void
Parameters
| Name | Type |
|---|---|
«destructured» | Object |
› message | unknown |
Returns
void
setAttribute
▸ setAttribute(«destructured»): void
Set a single attribute on the player and emit an event for the change.
Parameters
| Name | Type | Default value |
|---|---|---|
«destructured» | Object | undefined |
› key | string | undefined |
› notify? | boolean | true |
› value | unknown | undefined |
Returns
void
setAttributes
▸ setAttributes(attributes): void
Set multiple attributes at once and emit a single event. This is more efficient than calling setAttribute multiple times.
Parameters
| Name | Type | Description |
|---|---|---|
attributes | Object | Object containing attribute key-value pairs |
attributes.attributes | Record<string, unknown> | - |
Returns
void
setSocket
▸ setSocket(socket): void
Updates the socket connection for this player. Useful for handling reconnections without losing player state.
Parameters
| Name | Type | Description |
|---|---|---|
socket | Object | The new WebSocket connection |
socket.socket | WebSocket | - |
Returns
void
setTable
▸ setTable(«destructured»): void
Parameters
| Name | Type |
|---|---|
«destructured» | Object |
› table | null | Table |
Returns
void