Commit 2bab9e40 authored by Hugo Denizart's avatar Hugo Denizart
Browse files

Implement multiplayer scores sorting.

parent 0ade7066
Loading
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ const Teams = require("./Enums/BanchoLobbyTeams");
 * @prop {Array<BanchoMod>} mods 
 * @prop {boolean} freemod
 * @prop {boolean} playing Whether we're currently playing or not
 * @prop {Array<BanchoLobbyPlayerScore>} scores Scores set during the currently ongoing match, or the previous match. Emptied when a new match starts. Sorted by pass and score once match is finished.
 */
class BanchoLobby extends EventEmitter {
	constructor(channel) {
@@ -30,6 +31,7 @@ class BanchoLobby extends EventEmitter {
		this.channel = channel;
		this.banchojs = this.channel.banchojs;
		this.id = Number(channel.name.substring("#mp_".length));
		this.scores = [];
		this._name = "";
		this._beatmapId = null;
		this._beatmap = null;
@@ -289,6 +291,7 @@ class BanchoLobby extends EventEmitter {
							player.score = null;
						}
					}
					this.scores.length = 0;
					this.playing = true;
					this.emit("matchStarted");
					break;
@@ -299,6 +302,7 @@ class BanchoLobby extends EventEmitter {
					 */
					this.getPlayerByName(ret.username).then((player) => {
						player.score = new BanchoLobbyPlayerScore(ret.score, ret.pass, player);
						this.scores.push(player.score);
						this.emit("playerFinished", player.score);
					}, (err) => this.banchojs.emit("error", err));
					break;
@@ -311,6 +315,7 @@ class BanchoLobby extends EventEmitter {
					 * @event BanchoLobby#matchAborted
					 */
					this.playing = false;
					this.sortScores();
					this.emit(regex.name);
					break;
				case "invalidBeatmapId":
@@ -944,6 +949,24 @@ class BanchoLobby extends EventEmitter {
		return Object.seal(slots);
	}

	/**
	 * Sort scores by pass and score.
	 */
	sortScores() {
		this.scores.sort((a, b) => {
			if(a.pass && !b.pass)
				return -1;
			else if(!a.pass && b.pass)
				return 1;
			else if(a.score > b.score)
				return -1;
			else if(b.score > a.score)
				return 1;
			else
				return 0;
		});
	}

	get beatmap() {
		return this._beatmap;
	}
+1 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ const TestGoals = {
	Where: Symbol("Where"),
	Stats: Symbol("Stats"),
	BanchoMods: Symbol("BanchoMods"),
	MultiplayerScoresSorting: Symbol("MultiplayerScoresSorting"),

	MultiplayerLobbyExists: Symbol("MultiplayerLobbyExists"),
	MultiplayerLobbySetMap: Symbol("MultiplayerLobbySetMap"),
+37 −0
Original line number Diff line number Diff line
const TestUnit = require("../TestUnit").TestUnit;
const TestGoals = require("../TestGoals");
const BanchoLobbyPlayerScore = require("../../lib/Multiplayer/BanchoLobbyPlayerScore");
const BanchoLobby = require("../../lib/Multiplayer/BanchoLobby");

class MultiplayerScoresSortingUnit extends TestUnit {
	constructor() {
		super();
		this.name = "MultiplayerScoresSortingUnit";
	}

	async run() {
		const _this = {
			scores: [
				new BanchoLobbyPlayerScore(500000, false, null),
				new BanchoLobbyPlayerScore(500000, true, null),
				new BanchoLobbyPlayerScore(1000000, true, null),
				new BanchoLobbyPlayerScore(800000, true, null),
				new BanchoLobbyPlayerScore(900000, false, null)
			]
		};
		const expectedScores = [
			new BanchoLobbyPlayerScore(1000000, true, null),
			new BanchoLobbyPlayerScore(800000, true, null),
			new BanchoLobbyPlayerScore(500000, true, null),
			new BanchoLobbyPlayerScore(900000, false, null),
			new BanchoLobbyPlayerScore(500000, false, null)
		];
		BanchoLobby.prototype.sortScores.apply(_this);
		if(JSON.stringify(_this.scores) == JSON.stringify(expectedScores))
			this.fulFillGoal(TestGoals.MultiplayerScoresSorting);
		else
			throw new Error("Sorted scores and expected sorting don't match! expected: " + JSON.stringify(expectedScores) + ", got: " + JSON.stringify(_this.scores));
	}
}

module.exports = new MultiplayerScoresSortingUnit();
 No newline at end of file