npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jacksondc/crane

v0.2.0

Published

AI game toolkit

Downloads

1

Readme

Overview

Crane is a toolkit for writing games for AI players. It provides an API to communicate between a game written in JavaScript and players written in other languages, as well as utilities for running tournaments.

Version -1. All APIs unstable.

Quick start

Player

Put players in the /players directory. Each player must define the method respond, which takes a string command and returns a string response:

function respond(command) {
  return (command === move) ? "rock" : "err";
}

Crane currently supports players written in Python 3, JavaScript, and Java. For other languages, you'll need to write a client.

Game

Install with npm:

npm install --save @jacksondc/crane

Require:

var crane = require('@jacksondc/crane');

Call readPlayers to get a list of all players:

var players = crane.readPlayers();

Communicate with players using send:

var playerMove = players[0].send('move'); //rock

For a full example, see the rock paper scissors game.

Game API

readPlayers([players])

Returns an array of Player objects corresponding to files in the /players directory. Players not written in supported languages will be ignored. To receive players only for specific objects, pass in their filenames as an array.

crane.readPlayers(['python-player.py', 'js-player.js']); //only pick these two players

setTimoutLength(length)

Sets the amount of time, in milliseconds, that Crane will wait for player responses before throwing an error. Defaults to 1000.

crane.setTimeoutLength(500); //wait half a second for responses

Player.send(message)

Accepts a string message, which will be passed into the player's respond method as its only argument. Returns a string message returned by the respond method.

var response = player.send("move")

Messages are passed through stdin/stdout and aren't sanitized at all. Special characters or newlines will likely mess everything up. Whitespace is preserved as long as it doesn't start or end the message.

Player API

A player only needs to implement one method, respond, which accepts a single string argument, a message from the game, and returns a string to the game. Full examples of players written in all supported languages can be found in the rock paper scissors game.

function respond(message) {
  return "response";
}

Language Notes

Crane currently has clients for Python 3, JavaScript, and Java (it will recognize files with the extensions .py, .js and .class.) It assumes python is executable as python3, node as node, and Java as java.

Automating Gameplay

playTournament(players, eachMatch, [options])

NOTE: playTournament currently only plays two-player games.

Plays some number of matches between each player in the tournament and tallies up the results, which are passed to the callback and (optionally) printed in a table. Matches between the same combinations of players will be played back-to-back.

Arguments

  1. players: An array of player objects (from readPlayers) to consider for games in the tournament
  2. eachRound: A function to execute each round
  3. options.callback: A callback to execute when the tournament is finished
  4. options.rounds: the number of times to play each combination of opponents against each other (default 100). Can also be set to 'UNTIL_SIGNIFICANT', which plays until the first-place player has a statistically-significant lead over the second-place player
  5. options.numRandomizations: the number of times to re-randomize score datasets in testing statistical significance (default 1000). Ignored unless options.rounds is set to 'UNTIL_SIGNIFICANT'
  6. options.pValueThreshold: the probability of a random outcome of the a gap between the two leading players greater than or equal to the actual gap below which no more rounds will be played (default 0.05). Ignored unless options.rounds is set to 'UNTIL_SIGNIFICANT'
  7. options.printTable: set to false to suppress table printing (default true)

The eachMatch function accepts two arguments, a match object and a callback. The match object consists of a players array at match.players with all the participating players and a match number at match.index (an integer starting at one). The callback accepts two arguments: an error and a score array, where each score is for the player at the corresponding index of in the players array.

options.callback will be passed two arguments: an error and an array of results. Results will be sorted with best performers (lowest ranks) at the beginning. Each entry in results has a player object at result.player, a cumulative score (across all matches played) at result.score, and an average score at result.avgScore.

An example call to playTournament and possible output:

game.playTournament(game.readPlayers(), function(match, cb) {
  // ...
  cb(null, [1, -1]); //win for player 0
}, {
  callback: function(err, results) {
    console.log('%s wins with %d points', results[0].player.getName(), results[0].score);
  },
  numRounds: 5
});
┌──────────┬────────────────────┬────────────┬────────────┐
│ Rank     │ Player             │ Score      │ Avg Score  │
├──────────┼────────────────────┼────────────┼────────────┤
│ 1        │ python-player      │          5 │       1.00 │
├──────────┼────────────────────┼────────────┼────────────┤
│ 2        │ js-player          │          1 │       0.20 │
├──────────┼────────────────────┼────────────┼────────────┤
│ 3        │ JavaPlayer         │         -6 │      -1.20 │
└──────────┴────────────────────┴────────────┴────────────┘

python-player wins with 5 points

Writing a Client

If you want to use a language that doesn't already have a client, you'll have to write your own. Here's the general framework:

  1. A new client is initialized for each player, so the same client might be running multiple times at once.
  2. The client is executed through the command line. All messages are sent through standard output and received through standard input.
  3. The client will receive one argument, the filename (without file extension) of its player. The client must then import this file and find its respond method.
  4. Each line is considered a message. If multiple lines are received at once, split them before processing.
  5. The first word (up to a space) of each message from the server is a unique message ID. The second word is a command. Anything after that is data.
  6. The first word of every response from the client is the same message ID. The second word is a status code: 0 for okay, 1 for error. Anything after that is data, which will be sent back to the game.
  7. There is (currently) only one valid command, player, which may be followed by data. If there is data, it should be passed as an argument to the player's respond() method. Before sending data to the player, remove whitespace (especially newlines) from either side. Whatever respond() returns should be data in the client's response to the game.
  8. Clients can also send messages that are not responses to server messages, by replacing the first token (normally the id) with 'err' or 'log' (depending on the type of message). This is useful for debugging or for errors finding the player file before the first message arrives.

Look at the existing clients for more implementation specifics.