Home Manual Reference Source Test Repository
import Game from 'p2pboardgameapi/src/Game.js'
public class | source

Game

A game session

Test:

Member Summary

Public Members
public

on: Object<string, Signal>

Event container

public

The player number (starting at 0 for the host)

public

The players’ unique user IDs

Method Summary

Public Methods
public

createDeck(cardFaces: CardFace[]): Deck

Creates and shuffles a new deck for the game

public

createDie(id: string, dieFaces: number | DieFace[]): Die

Creates a new die for the game

public

Creates a movable piece for the game

public

Gets the number of currently connected players

public

Get the game URL which other users can use to connect

public

start()

Starts the game for all players

public

Starts the P2P connection

Public Members

public on: Object<string, Signal> source

Event container

Properties:

NameTypeAttributeDescription
playerJoin Signal

Event signal when a player joins the game (Parameter: userID)

playerLeave Signal

Event signal when a player leaves the game (Parameter: userID)

start Signal

Event signal when the game starts (No parameters)

public playerNumber: boolean source

The player number (starting at 0 for the host)

public players: Array<number> source

The players’ unique user IDs

Public Methods

public createDeck(cardFaces: CardFace[]): Deck source

Creates and shuffles a new deck for the game

Params:

NameTypeAttributeDescription
cardFaces CardFace[]

The deck’s card faces (max. 52)

Return:

Deck

The newly created deck

Throw:

RangeError

when cardFaces has under 2 or over 52 elements

Test:

public createDie(id: string, dieFaces: number | DieFace[]): Die source

Creates a new die for the game

Params:

NameTypeAttributeDescription
id string

The die’s id

dieFaces number | DieFace[]
  • optional
  • default: 6

The die’s faces, numbered from 1 to n or custom faces

Return:

Die

The newly created die

Throw:

RangeError

when dieFaces has under 2 or over 52 elements

public createPiece(id: string): Piece source

Creates a movable piece for the game

Params:

NameTypeAttributeDescription
id string

The piece’s id

Return:

Piece

The newly created piece

public getURL(): string source

Get the game URL which other users can use to connect

Return:

string

The URL

Throw:

ConnectionNotStartedError

when startConnection() was not called before

Test:

public start() source

Starts the game for all players

Emit:

start

the game starts

Throw:

ConnectionNotStartedError

when startConnection() was not called before

Test: