chesstournament

A JavaScript library to manage Chess Tournaments

Usage no npm install needed!

<script type="module">
  import chesstournament from 'https://cdn.skypack.dev/chesstournament';
</script>

README

chesstournament.js

chesstournament.js is a JavaScript library to manage chess tournaments.

Note: As this library is under development, it has not been published on npm yet.

Installation

You can use this library in node.js as well as natively in your favorite browser. For node.js simply install it using npm:

npm install chesstournament

For browser usage:

<script src="chesstournament.js"></script>

Quick Start

var Tournament = require('chesstournament');
var Player     = Tournament.Player;
var Team       = Tournament.Team;

var type       = 'team';                // either 'individual' or 'team'
var tournament = new Tournament(type);

var team = new Team('Chess Club One');  // create a new Team
tournament.teams.add(team);             //   and register it
team.players.add(new Player('Falco'));  // add some
team.players.add(new Player('Jacob'));  //   nice team mates

// or simply import a SWT file by the chesstournament-SWT-support plugin
Tournament.from.use(require('chesstournament-SWT-support'));
Tournament.from('my/tournament.SWT', function(err, importedTournament) {
  console.log(importedTournament.rounds.length);
});

Import & Export

chesstournament.js provides an easy way to bind import and export plugins. You can simply register such a plugin to your Tournament module:

// import plugin
Tournament.from.use(require('my-import-handler'));

// export plugin
Tournament.to.use(require('my-export-handler'));

The so registered plugins can be used via Tournament.from() and tournament.to():

// input can be everything the import plugin supports, for example
//   a filename or the content itself
Tournament.from('your input here', handleImportedTournament);

function handleImportedTournament(err, tournament) {
  // do what you want
  
  // for example export it to another format
  tournament.to.myRegisteredExport(options, handleExportedTournament);
  
  // alternatively use
  Tournament.to.myRegisteredExport(tournament, options, handleExportedTournament);
}

function handleExportedTournament(err, export) {
  console.log(export);
}

Existing Import/Export Plugins

There is currently only one userland plugin to import existing tournaments from Swiss-Chess Tournament (SWT) files. This list will get updated once there are more plugins:

Write your own plugin

A plugin which provides both import and export functionalities is specified by an object of the following form:

{
  // Name of this format, so a direct call Tournament.from[name] is
  //   possible, e.g. Tournament.from.CTX('your CTX data', handleTournament);
  name: 'CTX',

  // Function to determine if this importer should be used, depending
  //   on the content, which might also be a filename.
  determine: function determineFunction(content) { return true; }

  // Function to import this format. Keep in mind, that content might
  //   also be a filename.
  import: function importFunction(content, callback) { callback(err, tournament); }

  // Function to export into this format.
  export: function exportFunction(tournament, options, callback) { callback(err, exported); }
}

If only the import attribute is specified, it is an import plugin. Same applies to export.

Import as well as export plugins can also be registered directly by assigning it to the Tournament.from and Tournament.to objects:

// register import plugin
Tournament.from.myFormat = function importFunction(content, callback) { callback(err, tournament); };
// necessary if you also want to use the shortcut Tournament.from()
Tournament.from.myFormat.determine = function determineFunction(content) { return true; };

// register export plugin
Tournament.to.myFormat = function exportFunction(tournament, options, callback) { callback(err, exported); };