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

teslo

v0.5.2

Published

Elo rating calculator

Downloads

4,003

Readme

teslo

Build status Dependencies Downloads Build size

teslo is a TypeScript package for calculating elo rating in multiplayer games.

Supports duels, free-for-alls, and team matches. Compatible with both browser and Node.js environments.

Table of Contents

Install

Install the teslo package.

# npm
npm install teslo

# yarn
yarn add teslo

# pnpm
pnpm add teslo

Usage

Duel

import { Player, Duel } from 'teslo'

const match = new Duel()

match.addPlayers(new Player('1', 1000), new Player('2', 900))

const results = match.calculate('1')

/*
  [
    {
      id: '1',
      elo: 1012
    },
    {
      id: '2',
      elo: 888
    }
  ]
*/

Free-For-All

import { Player, FreeForAll } from 'teslo'

const match = new FreeForAll()

match.addPlayers(
  new Player('1', 1000),
  new Player('2', 900),
  new Player('3', 800)
)

const results = match.calculate('1', '2', '3')

/*
  [
    {
      id: '1',
      elo: 1010
    },
    {
      id: '2',
      elo: 900
    },
    {
      id: '3',
      elo: 790
    }
  ]
*/

Team Duel

import { Player, Team, TeamDuel } from 'teslo'

const match = new TeamDuel()
const team1 = new Team('1')
const team2 = new Team('2')

team1.addPlayers(new Player('1', 1000), new Player('2', 900))
team2.addPlayers(new Player('3', 800), new Player('4', 700))
match.addTeams(team1, team2)

const results = match.calculate('1')

/*
  [
    {
      id: '1',
      players: [
        {
          id: '1',
          elo: 1006
        },
        {
          id: '2',
          elo: 909
        }
      ]
    },
    {
      id: '2',
      players: [
        {
          id: '3',
          elo: 791
        },
        {
          id: '4',
          elo: 694
        }
      ]
    }
  ]
*/

Team Free-For-All

import { Player, Team, TeamFreeForAll } from 'teslo'

const match = new TeamFreeForAll()
const team1 = new Team('1')
const team2 = new Team('2')
const team3 = new Team('3')

team1.addPlayers(new Player('1', 1000), new Player('2', 900))
team2.addPlayers(new Player('3', 800), new Player('4', 700))
team3.addPlayers(new Player('5', 600), new Player('6', 500))
match.addTeams(team1, team2, team3)

const results = match.calculate('1', '2', '3')

/*
  [
    {
      id: '1',
      players: [
        {
          id: '1',
          elo: 1004
        },
        {
          id: '2',
          elo: 906
        }
      ]
    },
    {
      id: '2',
      players: [
        {
          id: '3',
          elo: 798
        },
        {
          id: '4',
          elo: 701
        }
      ]
    },
    {
      id: '3',
      players: [
        {
          id: '5',
          elo: 593
        },
        {
          id: '6',
          elo: 496
        }
      ]
    }
  ]
*/

API

Player

Constructor

new Player(id: string, elo: number)

Methods

static create(id: string, elo: number): Player
getExpectedResult(opponentElo: number): number
calculate(opponentElo: number, won: boolean, kFactor: number): number

Team

Constructor

new Team(id: string)

Methods

static create(id: string, ...players: Player[]): Team
addPlayer(player: Player): this
addPlayers(...players: Player[]): this
getAverageElo(): number

Duel

Constructor

interface Options {
  kFactor?: number
}

new Duel(options?: Options)

Methods

static create(options?: Options, ...players: Player[]): Duel
addPlayer(player: Player): this
addPlayers(...players: Player[]): this
calculate(playerId: string): PlayerResult[]
getResults(): PlayerResult[]

Description

A duel is between 2 players. playerId determines the winner.

FreeForAll

Constructor

interface Options {
  kFactor?: number
  minPlayers?: number
  maxPlayers?: number
}

new FreeForAll(options?: Options)

Methods

static create(options?: Options, ...players: Player[]): FreeForAll
addPlayer(player: Player): this
addPlayers(...players: Player[]): this
calculate(...playerIds: string[]): PlayerResult[]
getResults(): PlayerResult[]

Description

A free-for-all is between minPlayers and maxPlayers. playerIds determines the winning order.

TeamDuel

Constructor

interface Options {
  kFactor?: number
}

new TeamDuel(options?: Options)

Methods

static create(options?: Options, ...teams: Team[]): TeamDuel
addTeam(team: Team): this
addTeams(...teams: Team[]): this
calculate(teamId: string): TeamResult[]
getResults(): TeamResult[]

Description

A team duel is between 2 teams. teamId determines the winner.

TeamFreeForAll

Constructor

interface Options {
  kFactor?: number
  minTeams?: number
  maxTeams?: number
}

new TeamFreeForAll(options?: Options)

Methods

static create(options?: Options, ...teams: Team[]): TeamFreeForAll
addTeam(team: Team): this
addTeams(...teams: Team[]): this
calculate(...teamIds: string[]): TeamResult[]
getResults(): TeamResult[]

Description

A team free-for-all is between minTeams and maxTeams. teamIds determines the winning order.

Elo Calculation

Elo calculation starts with a player to player comparison. Initially, we calculate the probability of a player winning against their opponent.

Expected Result = 1 / (1 + 10 ^ ((Opponent Elo - Player Elo) / 400))

Once we have the expected result of a player against their opponent, we calculate their new elo with the actual result. Result is either a 1 or 0 based on the player winning or losing respectively.

New Elo = Current Elo + KFactor * (Result - Expected Result)

In a duel, each player either gains or loses elo based on winning or losing. In a free-for-all, elo is calculated by player placement; players lose elo to those who placed higher and gain elo from those who placed lower. In a team match, each player's elo is calculated based on the average elo of the opposing team(s).

You can learn more about the elo rating system via the Wiki.

Development

Local

pnpm install
pnpm build

Tests

pnpm test

License

MIT