Options
All
  • Public
  • Public/Protected
  • All
Menu

Peer Party

React hooks for connecting guests to a host via WebRTC.

@compendium/peer-party

Peer Party uses WebRTC and a common set of game rules to host and share a multiplayer game with freinds, all without the use of a server. Guest can emit valid game moves to the host and the host will broadcast what moves have been made by whom.

NPM JavaScript Style Guide

Install

npm install --save @compendium/peer-party

Usage

import React from 'react'
import { usePartyGuest, usePartyHost } from '@compendium/peer-party'

const MyGamesRules = {
  makeGuestMove: ({ state }) => ({ ...state, title: "Guests are better" })
  makeHostMove: ({ state }) => ({ ...state, title: "Host is best" })
}

const GuestComponent = ({ roomId }) => {
  const { state, moves } = usePartyGuest({ roomId, game: MyGamesRules });
  return (
    <div>
      <h1>{state.title}</h1>
      <button onClick={() => moves.makeGuestMove()}>
        Click Me
      </button>
    </div>
  );
}

const HostComponent = ({ roomId }) => {
  const { state, moves } = usePartyHost({ roomId, game: MyGameRules });
  return (
    <div>
      <h1>{state.title}</h1>
      <button onClick={() => moves.makeHostMove()}>
        Click Me
      </button>
    </div>
  );
}

Architecture and Design

All guests using the list of events generate their own local state by applying the deterministic move functions to a state. This prevents the host having to send large amounts of data to guests over the network, whilst maintaining a reactive experience and allowing guests to preempt the game state after making their move locally.

Peer party networking diagram

Randomness is handled using seeded random number generators.

Secret states are handled on the host, which sends a patch (diffs) to each guest, which they apply to their local state.

License

UNLICENSED © James O'Toole

Index

Type aliases

Args

Args: any

ConnectionStatus

ConnectionStatus: boolean

Game

Game: Moves

Move

Moves

Moves: {}

Type declaration

  • [key: string]: Move

PeerId

PeerId: string

RandomMove

RandomMove: { _isRandom: true; _isSecret?: false }

Type declaration

RandomNumberGenerator

RandomNumberGenerator: () => number

Type declaration

    • (): number
    • Returns number

RandomSecretMove

RandomSecretMove: { _isRandom: true; _isSecret: true }

Type declaration

RevealSecretFunction

RevealSecretFunction: (contextId: PeerId, extractionFunction: (state: State) => State) => any

Type declaration

Secret

Secret: string

SecretMove

SecretMove: { _isRandom?: false; _isSecret: true }

Type declaration

SimpleMove

SimpleMove: { _isRandom?: false; _isSecret?: false }

Type declaration

State

State: any

Functions

Const shuffle

usePartyGuest

usePartyHost

withRandom

  • A higher order function to provide random, a random number generator, to a move defintion.

    withRandom(({ random, state }) => {
      const isHeads = random() > 0.5;
      return ({ ...state, isHeads })
    })
    

    Parameters

    • moveFn: Move

      A game move definition, which will require randomness

    Returns RandomMove | RandomSecretMove

    A random move definition

withSecret

  • A higher order function to provide contextId and revealASecret, enabling the move to return a different state for different peers.

    withSecret(({ contextId, hostId, revealSecret, state }) => {
      const isHostContext = hostId === contextId
      if (isHostContext) {
        return ({ ...state, deck: state.deck.slice(1) })
      } else {
        const topCardOfDeck = revealSecret(hostId, s => s.deck[0])
        return ({ ...state, revealedCard: topCardOfDeck })
      }
    })
    

    Parameters

    • moveFn: Move

      A move defition that needs to be secretive

    Returns SecretMove | RandomSecretMove

    A move defintion with context awareness and access to secret state

Legend

  • Property

Generated using TypeDoc