Giter Club home page Giter Club logo

elo-rating-swift's Introduction

EloRatingSystem

This is an Elo rating system built using Swift. It has win estimation and rating calculations for an unlimited amount of players. The rating calculator can handle games for 1 vs 1, 2 vs 2, 3 vs 100 or etc. It will provide you with the chance of winning as a percentage for player A versus player B.

Install with Swift Package Manager

  1. Open your Package.swift file and add this to your package dependancies:

.package(url: "https://github.com/BryanNorden/elo-rating-swift.git", from: "1.2.0"

  1. In Package.swift find main target and add "EloRatingSystem"

  2. Add this to any file where you would like to use the rating system: import EloRatingSystem

  3. Build / Run, then enjoy

Notes

  • To update the package manually open terminal and run swift package update

  • If you have issues you can run this in termial to reset your packages rm -rf .build/ *.xcodeproj/ Package.resolved

Calculate the chance of winning

EloRating().chanceOfWinning(forPlayer: 100.0, vs: 1.0) -> chance of winning as a percentage in decimal form

Chance of winning Example

We have two players. Bob with a rating of 800 and Jake with a rating of 1500.

To find out the likely hood that Bob will beat Jake you would use this let chanceOfBobBeatingJake = chanceOfWinning(forPlayer 800, vs ratingB: 1500) Then chanceOfBobBeatingJake = 0.17472092062234879 or 17.5% chance Bob will beat Jake

The chance of Jake beating Bob would be let chanceOfJakeBeatingBob = chanceOfWinning(forPlayer 1500, vs ratingB: 800) So chanceOfJakeBeatingBob = 0.98252791166305542 or 98.25% chance Jake will beat Bob

Calculate Ratings after Game has been played

EloRating().calculateWinLossRatings(_ players: [EloPlayer]) This will take an array of EloPlayers and will return back an array of EloPlayers with their updated rating. You will need to create an EloPlayer for each of the players that played. The EloPlayer is just for that game. You can use your own player models.

Important: EloPlayer has an optional property id and uuid to help you identify your user. The function will work if you do not assign the EloPlayer your user's id or uuid, but it will be harder to identify your user when the rating calculations are returned

The players will be sorted into a winning team vs losing team based on the game result you assign the player (This is how it can handle any amounts of players). Then it will calculate the rating for everyone based on the ratings each player had before the start of the game. So you would then update your player model with their new rating, which would be EloPlayer.ratingAfter

Example of how to use calculateWinLossRatings method

let players = [
EloPlayer(gameResult: .won, ratingBefore: 900, ratingAfter: 0, ratingChange: 0, id: 112),
EloPlayer(gameResult: .won, ratingBefore: 600, ratingAfter: 0, ratingChange: 0, id: 200),
EloPlayer(gameResult: .lost, ratingBefore: 1500, ratingAfter: 0, ratingChange: 0, id: 6),
EloPlayer(gameResult: .lost, ratingBefore: 1100, ratingAfter: 0, ratingChange: 0, id: 22),
EloPlayer(gameResult: .lost, ratingBefore: 1100, ratingAfter: 0, ratingChange: 0, id: 56)
\]

let calculatedPlayers = EloRating().calculateWinLossRatings(players)

This would return these players

calculatedPlayers = [
EloPlayer(gameResult: .won, ratingBefore: 900, ratingAfter: 943.9107, ratingChange: 43.9107, id: 112),
EloPlayer(gameResult: .won, ratingBefore: 600, ratingAfter: 647.1034, ratingChange: 47.1034, id: 200),
EloPlayer(gameResult: .lost, ratingBefore: 1500, ratingAfter: 1468.2899, ratingChange: -31.7100, id: 6),
EloPlayer(gameResult: .lost, ratingBefore: 1100, ratingAfter: 1070.3479, ratingChange: -29.6520, id: 22),
EloPlayer(gameResult: .lost, ratingBefore: 1100, ratingAfter: 1070.3479, ratingChange: -29.6520, id: 56)
]

elo-rating-swift's People

Contributors

bryannorden avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

kevinenax

elo-rating-swift's Issues

Vapor Dependency

Why does the package have a dependency to Vapor?

I am trying to add the rating calculation to a small app I am making for our chess club, but I don't think this huge dependency should be necessary.

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.