Code Monkey home page Code Monkey logo

connect4-multiplayer-client's Introduction

Connect 4 Multiplayer: Client

Build npm:latest npm:beta semantic-release

A websocket-based client that connects users to connect 4 multiplayer game sessions.

Installation

Using npm:

$ npm i --save @brajkowski/connect4-multiplayer-client

Usage

This client is designed to connect to an instance of the connect4-multiplayer-server. To use the client, simply create an instance and open a connection to the server:

const client: Connect4Client = new Connect4Client();
client.open('wss://my-multiplayer-server-instance.myhost.com');

From there you can either create a new multiplayer session or join an existing session:

client.createSession(username);
// or
client.joinSession(sessionName, username);

The Connect4Client object provides methods to perform actions such as placing a chip or leaving the session:

client.makeMove(0); // Sends action to server to place chip in first column.
client.quit(); // Gracefully leave the multiplayer session and alert the server.

Finally there are several methods to write custom callbacks in order to handle actions that are coming from the server:

onOpen(callback: () => any)
onSessionCreated(callback: (sessionName: string) => any)
onSessionJoined(callback: (opponentUsername: string) => any)
onSessionNotFound(callback: () => any)
onOpponentJoin(callback: (username: string) => any)
onOpponentMove(callback: (column: number) => any)
onOpponentQuit(callback: () => any)
onGameRestart(callback: (thisClientStartsFirst: boolean) => any)
onSessionEnded(callback: () => any)
onClose(callback: () => any)

Example: Tying it all together

const client: Connect4Client = new Connect4Client();
client.onOpen(() => client.createSession(username));
client.onSessionCreated((sessionName) => shareSessionName(sessionName));
client.onOpponentJoin((username) => {
  setOpponentUsername(username);
  promptUserForMove(); // The session creator always goes first.
});
client.onOpponentMove((column) => {
  trackOpponentChipPlacement(column);
  promptUserForMove();
});
client.onGameRestart((thisClientStartsFirst) => {
  resetGameState();
  if (thisClientStartsFirst) promptUserForMove(); // Server decides who goes first.
});
client.open('wss://my-multiplayer-server-instance.myhost.com');

Building from Source

Using npm:

$ npm run build

will produce the compiled library under /dist.

connect4-multiplayer-client's People

Contributors

brajkowski avatar dependabot[bot] avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.