GithubHelp home page GithubHelp logo

calculate-score's Introduction

Calculate Score

This is the advanced scoring algorithm used by famous apps like the 'Wer besiegt Paul?' live game.

Usage

The algorithm expects three arguments: users, games and submissions. They take the following shape:

const users = [
  {
    userId: 101,
    firstName: 'Jonas',
    lastName: 'Klein',
  },
  {
    userId: 102,
    firstName: 'Paul',
    lastName: 'Aurin',
  },
  // ...
];

const games = [
  {
    id: 1,
    question: 'How many things are there in the universe?',
    answer: 400,
  },
  {
    id: 2,
    question: 'How many objects does JavaScript have?',
    answer: 1000,
  },
  // ...
];

const submissions = [
  {
    id: 1,
    gameId: 1,
    userId: 101,
    guess: 300,
  },
  {
    id: 2,
    gameId: 1,
    userId: 102,
    guess: 350,
  },
  {
    id: 3,
    gameId: 2,
    userId: 103,
    guess: 400,
  },
  {
    id: 4,
    gameId: 3,
    userId: 104,
    guess: 450,
  },
  // ...
];

You now can use rankUsers:

const rankedUsers = rankUsers(users, games, submissions);
// returns a result similar to this:

[
  {
    userId: 101,
    rank: 1,
    points: 6,
  },
  {
    userId: 102,
    rank: 3,
    points: 7,
  },
  {
    userId: 103,
    rank: 4,
    points: 8,
  },
  // ...
];

How does it work?

First we have to answer how the basic ranking algorithm works. For every game, users get points. The more points a user has the worse he is. The user with the least points is the best. For every game, the submissions are compared with the actual answers. For each submissions, we calculate the deviation to the answer. The best user gets 1 points, the second 2 and so on. If two users have the same deviation they get the same points, but the following user then gets two points more instead of one. Now all points are summed and the same algorithm is applied to the ranks.

Example

Imagine we have a game that looks like this:

{
  id: 1,
  question: 'How many things are there in the universe?',
  answer: 400,
}

Now we have four submissions by four different users for that game:

{
  id: 1,
  gameId: 1,
  userId: 101,
  guess: 300,
},
{
  id: 2,
  gameId: 1,
  userId: 102,
  guess: 350,
},
{
  id: 3,
  gameId: 2,
  userId: 103,
  guess: 400,
},
{
  id: 4,
  gameId: 3,
  userId: 104,
  guess: 450,
}

If we calculate the deviation and apply the algorithm we get this:

{
  userId: 101,
  deviation: 100,
  points: 4,
},
{
  userId: 102,
  deviation: 50,
  points: 2,
},
{
  userId: 103,
  deviation: 0,
  points: 1,
},
{
  userId: 104,
  deviation: 50,
  points: 2,
}

As you can see, because two users had the same deviation they got the same amount of points. The following user got one more, it's like there's a gap between them.

Now we sum all the points for every game and can treat them like if they were points and apply the same calculation. We can get a result that might look like this:

{
  userId: 101,
  points: 40,
  rank: 1,
},
{
  userId: 102,
  points: 40,
  rank: 1,
},
{
  userId: 103,
  points: 48,
  rank: 3,
},
{
  userId: 104,
  points: 52,
  rank: 4,
}

calculate-score's People

Contributors

remarcable avatar

Watchers

 avatar

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.