Code Monkey home page Code Monkey logo

chesspgn's Introduction

#PGN exporter and importer

##Usage

###You can only export one game at a time. Writing multiple games to a PGN is simply a matter of writing each of these one after another to a file.

const exporter = require("exporter");

const tags = {
    Event: "An event",
    White: "Guy, White",
    Black: "Guy, Black"
};

const movelist = [
    {variations: [0]},
    {move: "e4", prev: 0, variations[1]},
    {move: "e5", prev: 1}
];
const config = {
    // If this exists, it will be written as a comment just before the final result
    textresult: "White Resigns",
    // If this exists, it will export no more than this many variations.
    // zero means it will only export the main line.
    maxvariations: 0,
    // If this exists, it will make the max line length of the movelist this.
    // The default is 255, per the pgn specification.
    linelength: 80,
    // If this exists, and isn't truthy, it will write only the movelist with no tags
    // at all. Usually used for things like getting only a movelist in unit tests.
    writetags: true,
    // If this exists, it won't write tags, any result, extra new lines, etc. It will
    // JUST return the moves. It's an extension of above really because it was still
    // writing other stuff. This will return JUST the move string. You can also use it
    // in combination with linelength and maxvariations.
    movesonly: false
};
// config itself can be left out (undefined)
const pgnstring = exporter.exportToPgn(tags, movelist, config);

###You can import a string or a buffer. In general, the number of games in a string or a buffer will be small, and as such importer.gamelist will be managable.

importer.import('[Event "someevent"] *');
console.log(importer.gamelist);

###You can also import a Readable stream. In this case, you probably do not want to risk gamelist being hundreds of thousands of games. Using the event listener will inform you when games are ready, delete those from the internal array after processing, and inform you when the stream has ended:

importer.import(stream)
    .events.on('gamesready', (gamelist) => {
        // There will be one or more game objects here:
        console.log("tags=" + gamelist[0].tags + ", movelist=" + gamelist[0].movelist);
    })
    .on('end', () => {
        console.log("There are no more games in stream");
    });
// Because "gamesready" was listened to, this variable is unreliable. Do not use it.
console.log(importer.gamelist);

###The movelist The movelist is the most important part of the importer and exporter. It is an array of node objects, and each node has a specific set of keys:

const node = {
  move: "e4", // The move being made in this node of the tree
  prev: 23,   // The index of this nodes parent. movelist[23] is this nodes parent
  variations: [40, 52, 63] // Our children. movelist[40], movelist[52], and movelist[63] are all variations of moves made in response to e4, with movelist[40], or the first variation in the list, being main line.
  comment: "This comment will show up in exported PGNs as the text in the {curly braces}. Imported {curly braces} will be here",
  nag: "$3", // This will be imported if valid nag syntax is found. On export though, it will export anything in this field, making PGNs invalid if it's not a true NAG.
  nonstandardcomment: "This comment appeared BEFORE the move", // If we have non-standard PGN comments, they will be saved here
  nonstandardnag: ["$4", "$5", "$6"] // If we have nonstandard NAGs (i.e. multiples, particularly non-standardized like 6. 'Nf6?? +- = !' they will be saved in this array.
};

movelist[0] NEVER has a move, nor does it have a prev. movelist[0] is the root of the tree, which would be position at the starting FEN. It's variations are all of the first players various responses to the initial position.

chesspgn's People

Watchers

 avatar

Forkers

arbitre125

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.