Class: CypherPoker

CypherPoker(settingsObject)

Main CypherPoker.JS lobby, account manager, table maker, and game launcher.

Constructor

new CypherPoker(settingsObject)

Creates a new CypherPoker.JS instance.
Parameters:
Name Type Description
settingsObject Object An external settings object specifying startup and initialization options for the instance. This reference is set to the settingas property.
Source:
See:
Example
var settingsObj = {
   "p2p":{
     "connectInfo":{
         "create":"return (new P2PRouter())",
         "type":"wss",
         "url":"ws://127.0.0.1:8090"
     },
     "transports": {
        "preferred":["webrtc","wss","ortc"],
        "quickConnect":true
      }
  },
  "api":{
     "connectInfo":{
        "create":"return (new APIRouter())",
        "type":"wss",
        "url":"ws://127.0.0.1:8090"
     }
  },
  "crypto":{
     "create":"return (new SRACrypto(4))",
     "bitLength": 1024,
     "radix": 16
  },
  "debug":false
}
var cypherpoker = new CypherPoker(settingsObj);

Extends

Members

accounts

Properties:
Name Type Description
accounts Array Indexed array of CypherPokerAccount instances managed by this instance.
Source:

(readonly) announcedTables

Properties:
Name Type Description
announcedTables Array A current copy of the list of tables announced by other owners.
Source:

api

Properties:
Name Type Default Description
api Object null Reference to a networking interface over which RPC API functions are invoked. This is a direct reference to ConnectivityManager.api unless ConnectivityManager hasn't been instantiated.
Source:

(readonly) apiConnected

Properties:
Name Type Default Description
apiConnected Boolean false Returns the ConnectivityManager.apiConnected value unless ConnectivityManager hasn't been instantiated yet.
Source:

beaconInterval

Properties:
Name Type Default Description
beaconInterval Number 5000 The interval, in milliseconds, to activate the internal table announcement beacon per table (owned tables only!)
Source:

captureNewTables

Properties:
Name Type Default Description
captureNewTables Boolean false If set to true, the instance begins to immediately capture new table announcements made over the peer-to-peer network. The network does not need to be connected for this setting to be changed.
Source:

connectivityManager

Properties:
Name Type Description
connectivityManager ConnectivityManager A reference to the connectivity manager instance used to control the p2p and api instances as well as to provide utility functions for CypherPokerUI.
Source:

(readonly) crypto

Properties:
Name Type Description
crypto SRACrypto An interface for asynchronous cryptographic operations.
Source:

(readonly) games

Properties:
Name Type Description
games Array A list of references to CypherPokerGame instances managed by this instance.
Source:

(readonly) joinedTables

Properties:
Name Type Description
joinedTables Array A current copy of the list of the tables we've joined (owned and others').
Source:

maxCapturedTables

Properties:
Name Type Default Description
maxCapturedTables Number 99 The maximum number of tables that should be captured to the announcedTables array. Once this limit is reached, items are shuffled so that new items always have the smallest index.
Source:

maxCapturesPerPeer

Properties:
Name Type Default Description
maxCapturesPerPeer Number 5 The maximum number of tables that should be captured to the announcedTables array per peer. If this many tables currently exist in announcedTables array, new and/or unique announcements by the same peer will be ignored.
Source:

(readonly) openTables

Properties:
Name Type Description
openTables Boolean Indicates whether the instance has any owned and open tables (true), or if all owned and open tables are filled (false).
Source:

p2p

Properties:
Name Type Default Description
p2p Object null Reference to a peer-to-peer networking interface. This is a direct reference to ConnectivityManager.p2p unless ConnectivityManager hasn't been instantiated.
Source:

(readonly) p2pConnected

Properties:
Name Type Default Description
p2pConnected Boolean false Returns the ConnectivityManager.p2pConnected value unless ConnectivityManager hasn't been instantiated yet.
Source:

(readonly) settings

Properties:
Name Type Description
settings Object The main settings object for the instance as provided during instatiation time.
Source:

Methods

addEventListener(type, listener, contextopt)

Registers a new event listener with the extending class instance.
Parameters:
Name Type Attributes Default Description
type String The event type to register.
listener function The listening function to invoke on the event.
context Object <optional>
null Unlike the traditional addEventListener parameter, this is the context or scope in which to invoke the listening function (since we can't use capture phases). If null, the listener is invoked in the context of the EventDispatcher or extending instance.
Inherited From:
Source:

announceTable(tableObj, contextopt)

Announces a table on the currently connected peer-to-peer network. If an associated beacon timer is found, it is automatically stopped when the requiredPID list of the table is empty.
Parameters:
Name Type Attributes Default Description
tableObj CypherPoker#TableObject The table to announce. If the table's requiredPID array is empty, the request is rejected.
context CypherPoker <optional>
null The CypherPoker instance to execute the function in (typically specified as part of a timer). If null, the current this context is assumed.
Source:

clearAccounts()

Clears the accounts array and any non-domain (inactive) accounts currently in memory. This function does not clear any user interface elements that contain account information.
Source:

copyTable(sourceTable, targetObject)

Copies the core properties of a source table object to another object. The target object will be identifiable as a TableObject after the copy.
Parameters:
Name Type Description
sourceTable TableObject The table from which to copy from.
targetObject Object The target object to copy the core properties of sourceTable to.
Source:

(async) createAccount(type, password, networkopt) → {Promise}

Creates a new cryptocurrency account for use with games.
Parameters:
Name Type Attributes Default Description
type String The cryptocurrency type of the new account. Valid values include: "bitcoin"
password String The password to associate with the account.
network String <optional>
null The network sub-type, if applicable, of the cryptocurrency type. For example, if type is "bitcoin" then network may be "main" or "test3".
Source:
Returns:
The promise resolves with a new CypherPokerAccount instance or rejects with an Error if a problem occurs.
Type
Promise

createGame(tableObj, account, playerInfoopt) → {CypherPokerGame}

Attempts to create a new CypherPokerGame instance from a table object. All required private IDs must already have joined the table prior to calling this function.
Parameters:
Name Type Attributes Default Description
tableObj CypherPoker#TableObject The table from which to create a new game.
account CypherPokerAccount The player account to use with this game.
playerInfo Object <optional>
null Additional information about us to send to other players at the table when they signal that their game is ready.
Source:
Fires:
Returns:
A new game instance associated with the table or null if one couldn't be created.
Type
CypherPokerGame

createTable(tableName, players, tableIDopt, activateBeaconopt) → {CypherPoker#TableObject}

Creates a new CypherPoker.JS table and optionally begins to advertise it on the available peer-to-peer network. The table is automatically joined and added to the joinedTables array.
Parameters:
Name Type Attributes Default Description
tableName String The name of the table to create.
players Number | Array If this is a number it specifies that ANY other players up to this numeric limit may join the table. When this parameter is an array it's assumed to be an indexed list of private IDs to allow to the table (may also be a mix of wildcards / any PIDs: ["*", "*", "a4ec890...]). This value does not include self (i.e. only other players).
tableID String <optional>
null The unique (per peer), table ID to generate the table with. Omitting this parameter or setting it to null causes an ID to be automatically generated.
activateBeacon Boolean <optional>
true If true, an internal beacon is automatically started at a beaconInterval interval to advertise the table on the peer-to-peer network. If false, use the announceTable function to manually announce the returned table.
Source:
Returns:
A newly created CypherPoker.JS table as specified by the parameters.
Type
CypherPoker#TableObject

getJoinedTables(tableNameopt, tableIDopt, ownerPIDopt) → {Array}

Retrieves a list of tables we've joined using at least one of three search criteria.
Parameters:
Name Type Attributes Default Description
tableName String <optional>
null The name of the table(s) to search for. If null, this parameter is ignored.
tableID String <optional>
null The ID of the table(s) to search for. If null, this parameter is ignored.
ownerPID String <optional>
null The private ID of the table(s)' owner to search for. If null, this parameter is ignored.
Source:
Returns:
A list of tables currently joined that matches one or more of the search criteria specified in the parameters. If all parameters are null, the whole list of joined tables is returned (same as joinedTables).
Type
Array

getListeners(type) → {Array}

Returns all registered listeners for a specific event type.
Parameters:
Name Type Description
type String The event type to return registered listeners for.
Inherited From:
Source:
Returns:
A list of registered event listener objects for the specific event. Each object contains a listener function reference and an execution context reference.
Type
Array

isTableReady(tableObj) → {Boolean}

Evaluates whether a table is ready or not. A table is considered ready if it is a valid TableObject, has one or more joined private IDs and no required private IDs.
Parameters:
Name Type Description
tableObj TableObject The table to evaluate.
Source:
Returns:
True if the table is ready.
Type
Boolean

isTableValid(tableObjopt) → {Boolean}

Checks whether the supplied argument is a valid CypherPoker.JS table object.
Parameters:
Name Type Attributes Default Description
tableObj TableObject <optional>
null The object to examine.
Source:
Returns:
True if the supplied object appears to be a valid TableObject suitable for use with CypherPoker.JS
Type
Boolean

joinTable() → {Promise}

Requests to join another owner's table.
Properties:
Name Type Attributes Default Description
A CypherPoker#TableObject CypherPoker.JS table (object) to request to join.
replyTimeout Number <optional>
20000 A time, in milliseconds, to wait for the reply before considering the request as having timed out.
Source:
Throws:
A standard Error is thrown if peer to peer networking hasn't been successfully negotiated.
Type
Error
Returns:
The promise will be resolved if the table was successfully joined otherwise it will be rejected.
Type
Promise

leaveJoinedTable(tableObj) → {Boolean}

Leaves a table that was joined. This table must be tracked internally by this instance as having been joined.
Parameters:
Name Type Description
tableObj CypherPoker#TableObject The table to leave.
Source:
Returns:
True if the leave notification was delievered to the peer-to-peer networking interface, false if there was a problem verifying the parameter.
Type
Boolean

localTableStorage(tableObj, useLSopt, clearopt) → {Object}

Returns or clears an arbitrary local-only data storage object associated with a specific table. The returned storage object can be used to store data related to the table that shouldn't, or can't, be included in the actual table object, within peer-to-peer communications, or in API calls (unless explicitly copied).
Parameters:
Name Type Attributes Default Description
tableObj TableObject The table object for which to retrieve the data object.
useLS Boolean <optional>
false If true, the localStorage object will be used to provide more permanent storage that is maintained between sessions.
clear Boolean <optional>
false If true, the data associated with the table object is cleared.
Source:
Returns:
A local data storage object associated with the specified table. If one doesn't exist, an empty one is created. If clear=true, null is returned.
Type
Object

(async) onAPIDisconnect(eventObj)

Invoked by the ConnectivityManager.api instance when it dispatches a close event when the API connection closes unexpectedly (the server closes the connection).
Parameters:
Name Type Description
eventObj Object A "close" event object
Source:

(async) processURLParams(urlParams)

Parameters:
Name Type Description
urlParams URLSearchParams Options supplied as parsed URL parameters to apply to the settings object.
Source:

removeAllGames(destroyopt)

Removes and optionally destroys all game instances in the internal games array.
Parameters:
Name Type Attributes Default Description
destroy Boolean <optional>
true If true, each instance's destroy function is invoked prior to removal.
Source:

removeAllTables(joinedopt, announcedopt)

Removes all tables from the joinedTables and announcedTables arrays. Any table announcements or join requests currently in progress are cancelled.
Parameters:
Name Type Attributes Default Description
joined Boolean <optional>
true If true, all tables in the joinedTables array should be removed.
announced Boolean <optional>
true If true, all tables in the announcedTables array should be removed.
Source:

removeEventListener(type, listener, contextopt)

Removes an event listener from the extending instance.
Parameters:
Name Type Attributes Default Description
type String The event type to remove the function from.
listener function The listening function to remove.
context Object <optional>
null The context or scope in which the listener exists.
Inherited From:
Source:

removeGame(gameRef, destroyopt)

Removes and optionally destroys a game instance from the internal games array.
Parameters:
Name Type Attributes Default Description
gameRef CypherPokerGame A reference the tracked game instance to remove.
destroy Boolean <optional>
true If true, the instance's destroy function is invoked prior to removal.
Source:

removeTable(tableObj, announcedopt) → {Boolean}

Removes a table from the joinedTables or announcedTables array and stops any announcement beacon associated with it if applicable.
Parameters:
Name Type Attributes Default Description
tableObj CypherPoker#TableObject The table to remove and, if applicable, stop announcing.
announced Boolean <optional>
false If true, the referenced table is removed from the CypherPoker#announcedTables array otherwise it's removed from the joinedTables array.
Source:
Returns:
True if the table was successfully removed, false if no such table could be found.
Type
Boolean

restoreAccounts(domain)

Restores saved accounts from the browser's localStorage for a specific domain or API service and stores them to the accounts array. Any accounts present in the array are removed.
Parameters:
Name Type Description
domain String | Array The domain(s), URL(s), or unique server identifier(s) associated with the accounts to be restored. If a string is supplied only accounts matching the single identitier will be restored. If this parameter is an array, accounts matching any element will be restored. If null is supplied, all accounts will be restored.
Source:

saveAccounts()

Saves the internal accounts array and any non-domain accounts to the browser's localStorage.
Source:

sendToTable(tableObj, message) → {Boolean}

Sends a message to the joined peers of a table.
Parameters:
Name Type Description
tableObj CypherPoker#TableObject The table to send the message to.
message * The message to send. Cannot be null or undefined.
Source:
Returns:
True if the message was delivered to the peer-to-peer networking interface, false if there was a problem processing the parameters.
Type
Boolean

(async) start(optionsopt)

Starts the instance once all internal and external initialization has been completed. Usually this function can be invoked directly after a new instance is created unless otherwise required.
Parameters:
Name Type Attributes Default Description
options Object <optional>
null Optional startup options that can be used to override default settings, behaviours, and functionality.
Properties
Name Type Attributes Description
urlParams URLSearchParams <optional>
Any options that may have been supplied to the application via the URL as parameters (name-value pairs).
Source:
See:
Fires:

Type Definitions

TableObject

An object containing properties and references required by CypherPoker.JS that refer to a table or group of peers.
Type:
  • Object
Properties:
Name Type Description
ownerPID String The private ID of the owner / creator of the table.
tableID String The pseudo-randomly generated, unique table ID of the table.
tableName String The name given to the table by the owner.
requiredPID Array Indexed array of private IDs of peers required to join this room before it's considered full or ready. The wildcard asterisk ("*") can be used to signify any PID.
joinedPID Array Indexed array of private IDs that have been accepted by the owner, usually in a tablejoin CypherPoker peer-to-peer message. This array should ONLY contain valid private IDs (no wildcards).
restorePID Array Copy of the original private IDs in the requiredPID array used to restore it if members of the joinePID array leave the table.
tableInfo Object Additional information to be included with the table. Use this object rather than a TableObject at the root level since it is dynamic (may cause unexpected behaviour).
Source:

Events

newgame

A new CypherPokerGame instance has been created.
Type:
  • Object
Properties:
Name Type Description
game CypherPokerGame The newly created game instance.
Source:

start

The instance has successfully started.
Type:
  • Event
Source:

tablejoin

A notification that a new peer (possibly us), is joining another owner's table.
Type:
  • Event
Properties:
Name Type Description
data Object The JSON-RPC 2.0 object containing the table update.
Properties
Name Type Description
result Object The standard JSON-RPC 2.0 notification result object.
Properties
Name Type Description
from String The private ID of the notification sender.
data CypherPoker#TableObject The table associated with the notification.
table CypherPoker#TableObject The table object being tracked by us, updated after the request has been processed.
Source:
Listeners of This Event:

tablejoinrequest

An external peer has made a successful request to join one of our tables.
Type:
  • Event
Properties:
Name Type Description
data Object The JSON-RPC 2.0 object containing the request.
joined String The private ID of the peer that has just joined.
data.result Object The standard JSON-RPC 2.0 notification result object.
Properties
Name Type Description
from String The private ID of the request sender.
data CypherPoker#TableObject The table associated with the notification.
table CypherPoker#TableObject The table object being tracked by us, updated after the request has been processed.
Source:
Listeners of This Event:

tablejointimeout

A join request to another owner's table has timed out without a response.
Type:
  • Object
Properties:
Name Type Description
table CypherPoker#TableObject The table that has timed out.
Source:

tableleave

An external peer is leaving a table.
Type:
  • Event
Properties:
Name Type Description
data Object The JSON-RPC 2.0 object containing the table being left.
Properties
Name Type Description
result Object The standard JSON-RPC 2.0 notification result object.
Properties
Name Type Description
from String The private ID of the notification sender.
data CypherPoker#TableObject The table associated with the notification.
table CypherPoker#TableObject The table object being tracked by us, updated after the request has been processed. If the leaving peer was the owner, the table is destroyed and this reference is null.
Source:

tablemsg

A table member is sending a message to other table members.
Type:
  • Event
Properties:
Name Type Description
data Object The JSON-RPC 2.0 object containing the message information.
Properties
Name Type Description
result Object The standard JSON-RPC 2.0 notification result object.
Properties
Name Type Description
from String The private ID of the message sender.
data CypherPoker#TableObject The table associated with the message.
Properties
Name Type Description
message * The message being sent.
Source:

tablenew

An external peer is announcing a unique new table (within allowable limits).
Type:
  • Event
Properties:
Name Type Description
data Object The JSON-RPC 2.0 object containing the announcement.
Properties
Name Type Description
result Object The standard JSON-RPC 2.0 notification result object.
Properties
Name Type Description
from String The private ID of the notification sender.
data CypherPoker#TableObject The table associated with the notification.
Source:

tableready

The associated table's required private IDs have all joined and the table is ready (e.g. to start a game)
Type:
  • Event
Properties:
Name Type Description
table CypherPoker#TableObject The table associated with the notification.
Source: