Codewars style ranking system
JavaScript
class User {
constructor() {
this.rank = -8;
this.progress = 0;
}
rank() {
return this.rank;
}
progress() {
return this.progress;
}
incProgress(rank) {
if (rank > 8 || rank < -8 || rank == 0) {
throw "Outside range";
return null;
}
let diff = Math.abs(this.rank - rank);
if (this.rank < 0 && rank > 0) diff -= 1;
if (this.rank == 8) {
this.progress = 0;
} else {
if (this.rank == rank + 1) {
this.progress += 1;
} else if (this.rank == rank) {
this.progress += 3;
} else if (rank > this.rank) {
this.progress += 10 * diff * diff;
} else {
this.progress += 1;
}
this.incRank();
}
}
incRank() {
if (this.progress > 100 && this.rank != 8) {
for (let i = this.progress; i > 99; i -= 100) {
this.rank += (this.rank == -1) ? 2 : 1;
this.progress -= 100;
if (this.rank == 8) this.progress = 0;
}
}
}
}
CoffeeScript
class User
constructor: ->
this.rank = -8;
this.progress = 0;
rank: ->
return this.rank;
progress: ->
return this.progress;
incProgress: (rank) ->
if rank > 8 || rank < -8 || rank == 0
throw "Outside range";
return null;
diff = Math.abs(this.rank - rank);
if this.rank < 0 && rank > 0
diff -= 1;
if this.rank == 8
this.progress = 0;
else
if this.rank == rank + 1
this.progress += 1;
else if this.rank == rank
this.progress += 3;
else if rank > this.rank
this.progress += 10 * diff * diff;
else
this.progress += 1;
this.incRank();
incRank: ->
if this.progress > 100 && this.rank != 8
while this.progress > 99
this.rank += `(this.rank == -1) ? 2 : 1`;
this.progress -= 100;
if this.rank == 8
this.progress = 0;
- javascript (Private)
- coffeescript (Private)
- 6-kyu (Private)
- codewars (Private)
- answer (Private)