Skip to content

Commit

Permalink
Merge pull request #38 from chrislayco/rank-players
Browse files Browse the repository at this point in the history
players are ranked by loss amount
  • Loading branch information
keldaan-ag authored Feb 19, 2022
2 parents 9cf7fba + ebd617d commit 3d19418
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/models/colyseus-models/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ schema.defineTypes(Player, {
synergies: Synergies,
itemsProposition: ['string'],
money: 'uint8',
life: 'uint8',
life: 'int8',
shopLocked: 'boolean',
streak: 'uint8',
interest: 'uint8',
Expand Down
30 changes: 22 additions & 8 deletions app/rooms/commands/game-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,18 +449,31 @@ class OnUpdatePhaseCommand extends Command {
}

rankPlayers() {
const rankArray = [];
const rankArray = []
this.state.players.forEach((player, key) => {
if (player.alive) {
rankArray.push({id: player.id, life: player.life});
if(!player.alive){
return
}

rankArray.push({
id: player.id,
life: player.life,
level: player.experienceManager.level
});
});
rankArray.sort(function(a, b) {
return b.life - a.life;
});

const sortPlayers = (a, b) => {
let diff = b.life - a.life
if(diff == 0){
diff = b.level - a.level
}
return diff
}

rankArray.sort(sortPlayers)

rankArray.forEach((rankPlayer, index)=>{
this.state.players.get(rankPlayer.id).rank = index + 1;
this.state.players.get(rankPlayer.id).exp = XP_PLACE[index];
});
}

Expand All @@ -471,7 +484,7 @@ class OnUpdatePhaseCommand extends Command {
const currentResult = player.getCurrentBattleResult()

if(currentResult == BATTLE_RESULT.DEFEAT || currentResult == BATTLE_RESULT.DRAW){
player.life = Math.max(0, player.life - this.computePlayerDamage(player.simulation.redTeam, player.experienceManager.level, this.state.stageLevel));
player.life = player.life - this.computePlayerDamage(player.simulation.redTeam, player.experienceManager.level, this.state.stageLevel)
}
player.addBattleResult(player.opponentName, currentResult, player.opponentAvatar, isPVE);
}
Expand Down Expand Up @@ -527,6 +540,7 @@ class OnUpdatePhaseCommand extends Command {
checkDeath() {
this.state.players.forEach((player, key) => {
if (player.life <= 0) {
player.life = 0
player.alive = false;
}
});
Expand Down

0 comments on commit 3d19418

Please sign in to comment.