I'm working on an online games portal built with Angular js. I want my players to have ratings that increase when they win/lose a game. How do I implement this? If if could get this working in pure JS then I think it could adapt it to my app. To sketch what I want:
var players = [
{
id: 1,
name: "James",
rating: 100
},
{
id: 2,
name: "Matt",
rating: 200
},
{
id: 3,
name: "Tara"
rating: 300
]
UpdateRating = function(winnerId, LoserId){
//changes these players ratings
}
Loop through your players. If the ID matches one of the given IDs, update the rating.
function updateRating(winnerId, loserId) {
for (var i = 0; i < players.length; i++) {
var p = players[i];
if (p.id == winnerId) {
p.rating++;
} else if (p.id == loserId) {
p.rating--;
}
}
}
It would be simpler if you made players
an object whose keys were the player IDs, or used the player ID as the index into the array. Then you wouldn't need a loop.
How about:
players = {James: 100, Matt: 200, Tara: 300}
function updateScores(winner, loser) {
players[winner]++;
players[loser]--;
}
updateScores("James", "Tara");
Just use loop through the players
array and update the property rating
for the matched users
UpdateRating = function(winnerId, LoserId){
for(var i = 0; players.length; i++){
if(players[i].id === winnerId)
players[i].rating = players[i].rating + 10;
else if(players[i].id === LoserId)
players[i].rating = players[i].rating - 10;
}
}
the ELO rating system would be possibility here. The benefit of this would be that players point change would vary based on points difference between the two players.
http://en.wikipedia.org/wiki/Elo_rating_system
Player A's expectation (of winning) when playing B is calculated as: E_A = 1/ (1 + 10^((R_B - R_A)/400)). (This ranges between 0 and 1 according to the initial rating difference). If B wins then then their rating increases by your chosen 'k factor' (how quickly you want your player's ratings to adjust) * by this expectation and A's rating decreases by the corresponding amount. I actually implemented this in my on JS game website. Can players draw your game? If not, here is my code adapted for your example.
var kFactor = 15; (adjust this according to how quickly you want ratings to adapt, lower number --> adapts more quickly).
function EloRatingCalc(winner, loser, players) {
var winElo = winner.rating;
var loseElo = loser.rating;
var qw = Math.pow(10,(winElo/400));
var ql = Math.pow(10,(loseElo/400));
var lossExpectation = ql/(ql+qw);
winner.rating += parseInt(kFactor*lossExpectation);
loser.rating -= parseInt(kFactor*lossExpectation);
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.