v0.8.2

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

Constructors

constructor

• new Player(socket, eventBus, id?): Player

Parameters

Name Type
socket WebSocket
eventBus EventBus
id? string

Returns

Player

Properties

id

• Readonly id: string

Methods

disconnect

▸ disconnect(): void

Disconnect the player from the server.

Returns

void


getAttribute

▸ getAttribute(key): any

Get a single attribute from the player.

Parameters

Name Type Description
key string The key of the attribute to get

Returns

any

The value of the attribute, or undefined if it doesn’t exist


getAttributes

▸ getAttributes(): Record<string, any>

Get all attributes from the player.

Returns

Record<string, any>

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 string The key of the attribute to check

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 () => void The function to call when the player disconnects

Returns

void


sendMessage

▸ sendMessage(message): void

Parameters

Name Type
message any

Returns

void


setAttribute

▸ setAttribute(key, value, notify?): void

Set a single attribute on the player and emit an event for the change.

Parameters

Name Type Default value Description
key string undefined The attribute name
value any undefined The attribute value
notify boolean true Whether to emit an event (defaults to true)

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 Record<string, any> Object containing attribute key-value pairs

Returns

void


setTable

▸ setTable(table): void

Parameters

Name Type
table null | Table

Returns

void