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

@rocambille/elo

v2.1.7

Published

Enrich your objects with Elo rating.

Downloads

27

Readme

Elo

npm (scoped) npm bundle size (scoped)

Enrich your objects with Elo rating.

Installation

npm i @rocambille/elo

Basic usage

import elo from "@rocambille/elo";
// or const elo = require("@rocambille/elo");

const player = elo();

let foo = { a: 42 };
let bar = { z: 43 };

const odds = player(foo).oddsAgainst(bar);

console.log(`foo has a ${odds * 100}% chance of winning against bar`);

[foo, bar] = player(foo).wins(bar);
// or [foo, bar] = player(foo).loses(bar);
// or [foo, bar] = player(foo).ties(bar);

console.log(foo, bar);

Should print something like this:

foo has a 50% chance of winning against bar
{
  a: 42,
  elo: {
    rating: 1516,
    matchCount: 1,
    lastDelta: 16,
    lastPlayedAt: 1629554837908
  }
} {
  z: 43,
  elo: {
    rating: 1484,
    matchCount: 1,
    lastDelta: -16,
    lastPlayedAt: 1629554837908
  }
}

Note that the elo module keeps foo.a and bar.z untouched. It enriches the objects with Elo stuff (rating, match count, last delta and last play timestamp). This is useful if you want to add elo rating on top of existing data, e.g. coming from an API.

The Pool object

You can manage a whole collection using the Pool object as follows:

import { Pool } from "@rocambille/elo";
// or const { Pool } = require("@rocambille/elo");

let pool = Pool.from([{ a: 42 }, { z: 43 }, { x: 44 }]);

const odds = pool.player(0).oddsAgainst(1);

console.log(`player 0 has a ${odds * 100}% chance of winning against player 1`);

pool = pool.player(0).wins(1);
// or pool = pool.player(0).loses(1);
// or pool = pool.player(0).ties(1);

console.log(pool);

Should print something like this:

player 0 has a 50% chance of winning against player 1
[
  {
    a: 42,
    elo: {
      rating: 1516,
      matchCount: 1,
      lastDelta: 16,
      lastPlayedAt: 1629554837908
    }
  },
  {
    z: 43,
    elo: {
      rating: 1484,
      matchCount: 1,
      lastDelta: -16,
      lastPlayedAt: 1629554837908
    }
  },
  {
    x: 44,
    elo: {
      rating: 1500,
      matchCount: 0,
      lastDelta: NaN,
      lastPlayedAt: NaN
    }
  }
]

The Pool provides a useful method to pick player indices using multiple options:

import { Pool } from "@rocambille/elo";
// or const { Pool } = require("@rocambille/elo");

let pool = Pool.from([{ a: 42 }, { z: 43 }, { x: 44 }]);

// use one of the following lines to pick players

const [i, j] = pool.pick("random"); // randomly pick
const [i, j] = pool.pick("matchCount"); // pick players with a low match count
const [i, j] = pool.pick("lastPlayedAt"); // pick players who have last played in the longest time
const [i, j] = pool.pick(); // pick players by randomly choosing one the above method (recommended)

// then you can use i and j to trigger a match

pool = pool.player(i).wins(j);

Configuration

You can pass a configuration object as parameter to the elo function. Here is an example with the default values:

import elo from "elo";
// or const elo = require("@rocambille/elo");

const player = elo({
  initialRating: 1500,
  DMax: 400,
  kGenerator: (eloData) => {
    if (eloData.matchCount < 30) {
      return 32;
    }

    return 24;
  },
  propsKey: "elo",
});

Same works with the Pool object, using the Pool.config method:

import { Pool } from "elo";
// or const { Pool } = require("@rocambille/elo");

const pool = Pool.config({
  initialRating: 1500,
  DMax: 400,
  kGenerator: (eloData) => {
    if (eloData.matchCount < 30) {
      return 32;
    }

    return 24;
  },
  propsKey: "elo",
}).from(...);

The module will use initialRating for players with an undefined rating. See details of Elo rating system for the DMax and k factors.

License

elo is open source software licensed as MIT.