multiplayer cleanup

This commit is contained in:
Holly Stubbs 2022-07-02 11:37:08 +01:00
parent eb53f2d164
commit 68c0f1a880
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
2 changed files with 85 additions and 196 deletions

View file

@ -202,6 +202,7 @@ module.exports = class {
} }
MatchUser.currentMatch = null; MatchUser.currentMatch = null;
MatchUser.matchSlotId = -1;
// Update the match listing to reflect this change (either removal or user leaving) // Update the match listing to reflect this change (either removal or user leaving)
this.updateMatchListing(); this.updateMatchListing();

View file

@ -1,11 +1,12 @@
const osu = require("osu-packet"), const osu = require("osu-packet"),
getUserById = require("./util/getUserById.js"), getUserById = require("./util/getUserById.js"),
StatusUpdate = require("./Packets/StatusUpdate.js"); StatusUpdate = require("./Packets/StatusUpdate.js"),
User = require("./User.js");
// TODO: Cache the player's slot position in their user class for a small optimisation // TODO: Cache the player's slot position in their user class for a small optimisation
module.exports = class { module.exports = class {
constructor(MatchHost, MatchData = {matchId: -1,inProgress: false,matchType: 0,activeMods: 0,gameName: "",gamePassword: '',beatmapName: '',beatmapId: 1250198,beatmapChecksum: '',slots: [],host: 0,playMode: 0,matchScoringType: 0,matchTeamType: 0,specialModes: 0,seed: 0}) { constructor(MatchHost = new User, MatchData = {matchId: -1,inProgress: false,matchType: 0,activeMods: 0,gameName: "",gamePassword: '',beatmapName: '',beatmapId: 1250198,beatmapChecksum: '',slots: [],host: 0,playMode: 0,matchScoringType: 0,matchTeamType: 0,specialModes: 0,seed: 0}) {
this.matchId = global.getAndAddToHistoricalMultiplayerMatches(); this.matchId = global.getAndAddToHistoricalMultiplayerMatches();
this.inProgress = MatchData.inProgress; this.inProgress = MatchData.inProgress;
@ -88,51 +89,30 @@ module.exports = class {
}; };
} }
leaveMatch(MatchUser) { leaveMatch(MatchUser = new User) {
try { try {
let userInMatch = false;
// Loop through all slots in the match
for (let i = 0; i < this.slots.length; i++) {
const slot = this.slots[i];
// Check if the user is in this slot
if (slot.playerId == MatchUser.id) {
userInMatch = true;
break;
}
}
// Make sure this leave call is valid // Make sure this leave call is valid
if (MatchUser.inMatch) return; if (MatchUser.inMatch) return;
// Get the user's slot
const slot = this.slots[MatchUser.matchSlotId];
// Make sure we don't run more than once // Set the slot's status to avaliable
// Again, client double firing packets. slot.playerId = -1;
if (!userInMatch) return; slot.status = 1;
MatchUser.currentMatch = null;
MatchUser.matchSlotId = -1;
let osuPacketWriter = new osu.Bancho.Writer;
// Loop through all slots in the match
for (let i = 0; i < this.slots.length; i++) {
const slot = this.slots[i];
// Make sure the user is in this slot
if (slot.playerId != MatchUser.id) continue;
// Set the slot's status to avaliable
slot.playerId = -1;
slot.status = 1;
break;
}
MatchUser.inMatch = false; MatchUser.inMatch = false;
osuPacketWriter.MatchUpdate(this.createOsuMatchJSON()); this.sendMatchUpdate();
// Remove the leaving user from the match's stream // Remove the leaving user from the match's stream
global.StreamsHandler.removeUserFromStream(this.matchStreamName, MatchUser.uuid); global.StreamsHandler.removeUserFromStream(this.matchStreamName, MatchUser.uuid);
global.StreamsHandler.removeUserFromStream(this.matchChatStreamName, MatchUser.uuid); global.StreamsHandler.removeUserFromStream(this.matchChatStreamName, MatchUser.uuid);
// Inform all users in the match that the leaving user has left const osuPacketWriter = new osu.Bancho.Writer;
global.StreamsHandler.sendToStream(this.matchStreamName, osuPacketWriter.toBuffer, null);
osuPacketWriter = new osu.Bancho.Writer;
// Remove user from the multiplayer channel for the match // Remove user from the multiplayer channel for the match
osuPacketWriter.ChannelRevoked("#multiplayer"); osuPacketWriter.ChannelRevoked("#multiplayer");
@ -145,7 +125,7 @@ module.exports = class {
} }
} }
updateMatch(MatchUser, MatchData) { updateMatch(MatchUser = new User, MatchData) {
// Update match with new data // Update match with new data
this.inProgress = MatchData.inProgress; this.inProgress = MatchData.inProgress;
@ -170,12 +150,8 @@ module.exports = class {
this.specialModes = MatchData.specialModes; this.specialModes = MatchData.specialModes;
this.seed = MatchData.seed; this.seed = MatchData.seed;
const osuPacketWriter = new osu.Bancho.Writer;
osuPacketWriter.MatchUpdate(this.createOsuMatchJSON()); this.sendMatchUpdate();
// Send this new match data to all users in the match
global.StreamsHandler.sendToStream(this.matchStreamName, osuPacketWriter.toBuffer, null);
// Update the match listing in the lobby to reflect these changes // Update the match listing in the lobby to reflect these changes
global.MultiplayerManager.updateMatchListing(); global.MultiplayerManager.updateMatchListing();
@ -190,8 +166,7 @@ module.exports = class {
global.StreamsHandler.sendToStream(this.matchStreamName, osuPacketWriter.toBuffer, null); global.StreamsHandler.sendToStream(this.matchStreamName, osuPacketWriter.toBuffer, null);
} }
moveToSlot(MatchUser, SlotToMoveTo) { moveToSlot(MatchUser = new User, SlotToMoveTo) {
const osuPacketWriter = new osu.Bancho.Writer;
const oldSlot = this.slots[MatchUser.matchSlotId]; const oldSlot = this.slots[MatchUser.matchSlotId];
// Set the new slot's data to the user's old slot data // Set the new slot's data to the user's old slot data
@ -203,72 +178,38 @@ module.exports = class {
oldSlot.playerId = -1; oldSlot.playerId = -1;
oldSlot.status = 1; oldSlot.status = 1;
osuPacketWriter.MatchUpdate(this.createOsuMatchJSON()); this.sendMatchUpdate();
// Send this change to all users in the match
global.StreamsHandler.sendToStream(this.matchStreamName, osuPacketWriter.toBuffer, null);
// Update the match listing in the lobby to reflect this change // Update the match listing in the lobby to reflect this change
global.MultiplayerManager.updateMatchListing(); global.MultiplayerManager.updateMatchListing();
} }
changeTeam(MatchUser) { changeTeam(MatchUser = new User) {
const slot = this.slots[MatchUser.matchSlotId]; const slot = this.slots[MatchUser.matchSlotId];
slot.team = slot.team == 0 ? 1 : 0; slot.team = slot.team == 0 ? 1 : 0;
const osuPacketWriter = new osu.Bancho.Writer; this.sendMatchUpdate();
osuPacketWriter.MatchUpdate(this.createOsuMatchJSON());
// Send this change to all users in the match
global.StreamsHandler.sendToStream(this.matchStreamName, osuPacketWriter.toBuffer, null);
} }
setStateReady(MatchUser) { setStateReady(MatchUser = new User) {
// Get the match the user is in if (MatchUser.inMatch) return;
const osuPacketWriter = new osu.Bancho.Writer;
// Set the user's ready state to ready
this.slots[MatchUser.matchSlotId].status = 8;
// Loop though all slots in the match this.sendMatchUpdate();
for (let i = 0; i < this.slots.length; i++) {
const slot = this.slots[i];
// Check if the player in this slot is this user
if (slot.playerId == MatchUser.id) {
// Turn on the user's ready state
slot.status = 8; // Ready
break;
}
}
osuPacketWriter.MatchUpdate(this.createOsuMatchJSON());
// Send this update to all users in the stream
global.StreamsHandler.sendToStream(this.matchStreamName, osuPacketWriter.toBuffer, null);
} }
setStateNotReady(MatchUser) { setStateNotReady(MatchUser = new User) {
// Get the match the user is in if (MatchUser.inMatch) return;
const osuPacketWriter = new osu.Bancho.Writer;
// Set the user's ready state to not ready
this.slots[MatchUser.matchSlotId].status = 4;
// Loop though all slots in the match this.sendMatchUpdate();
for (let i = 0; i < this.slots.length; i++) {
const slot = this.slots[i];
// Check if the player in this slot is this user
if (slot.playerId == MatchUser.id) {
// Turn off the user's ready state
slot.status = 4; // Not Ready
break;
}
}
osuPacketWriter.MatchUpdate(this.createOsuMatchJSON());
// Send this update to all users in the stream
global.StreamsHandler.sendToStream(this.matchStreamName, osuPacketWriter.toBuffer, null);
} }
lockMatchSlot(MatchUser, MatchUserToKick) { lockMatchSlot(MatchUser = new User, MatchUserToKick) {
const osuPacketWriter = new osu.Bancho.Writer;
// Make sure the user attempting to kick / lock is the host of the match // Make sure the user attempting to kick / lock is the host of the match
if (this.host != MatchUser.id) return; if (this.host != MatchUser.id) return;
@ -293,10 +234,7 @@ module.exports = class {
isSlotEmpty = false; isSlotEmpty = false;
} }
osuPacketWriter.MatchUpdate(this.createOsuMatchJSON()); this.sendMatchUpdate();
// Inform all users in the match of the change
global.StreamsHandler.sendToStream(this.matchStreamName, osuPacketWriter.toBuffer, null);
// Update the match listing in the lobby listing to reflect this change // Update the match listing in the lobby listing to reflect this change
global.MultiplayerManager.updateMatchListing(); global.MultiplayerManager.updateMatchListing();
@ -311,38 +249,27 @@ module.exports = class {
} }
} }
missingBeatmap(MatchUser) { missingBeatmap(MatchUser = new User) {
const osuPacketWriter = new osu.Bancho.Writer;
// User is missing the beatmap set the status to reflect it // User is missing the beatmap set the status to reflect it
this.slots[MatchUser.matchSlotId].status = 16; this.slots[MatchUser.matchSlotId].status = 16;
osuPacketWriter.MatchUpdate(this.createOsuMatchJSON()); this.sendMatchUpdate();
// Inform all users in the match of this change
global.StreamsHandler.sendToStream(this.matchStreamName, osuPacketWriter.toBuffer, null);
} }
notMissingBeatmap(MatchUser) { notMissingBeatmap(MatchUser = new User) {
const osuPacketWriter = new osu.Bancho.Writer;
// The user is not missing the beatmap, set the status to normal // The user is not missing the beatmap, set the status to normal
this.slots[MatchUser.matchSlotId].status = 4; this.slots[MatchUser.matchSlotId].status = 4;
osuPacketWriter.MatchUpdate(this.createOsuMatchJSON()); this.sendMatchUpdate();
// Inform all users in the match of this change
global.StreamsHandler.sendToStream(this.matchStreamName, osuPacketWriter.toBuffer, null);
} }
matchSkip(MatchUser) { matchSkip(MatchUser = new User) {
if (this.matchSkippedSlots == null) { if (this.matchSkippedSlots == null) {
this.matchSkippedSlots = []; this.matchSkippedSlots = [];
const skippedSlots = this.matchSkippedSlots; const skippedSlots = this.matchSkippedSlots;
for (let i = 0; i < this.slots.length; i++) { for (let slot of this.slots) {
const slot = this.slots[i];
// Make sure the slot has a user in it // Make sure the slot has a user in it
if (slot.playerId === -1 || slot.status === 1 || slot.status === 2) continue; if (slot.playerId === -1 || slot.status === 1 || slot.status === 2) continue;
@ -351,20 +278,16 @@ module.exports = class {
} }
} }
const skippedSlots = this.matchSkippedSlots;
for (let i = 0; i < skippedSlots.length; i++) {
// If loadslot belongs to this user then set loaded to true
if (skippedSlots[i].playerId == MatchUser.id) {
skippedSlots[i].skipped = true;
}
}
let allSkipped = true; let allSkipped = true;
for (let i = 0; i < skippedSlots.length; i++) { for (let skippedSlot of this.matchSkippedSlots) {
if (skippedSlots[i].skipped) continue; // If loadslot belongs to this user then set loaded to true
if (skippedSlot.playerId == MatchUser.id) {
skippedSlot.skipped = true;
}
// A user hasn't finished playing if (skippedSlot.skipped) continue;
// A user hasn't skipped
allSkipped = false; allSkipped = false;
} }
@ -387,41 +310,29 @@ module.exports = class {
} }
} }
transferHost(MatchUser, SlotIDToTransferTo) { transferHost(MatchUser = new User, SlotIDToTransferTo) {
const osuPacketWriter = new osu.Bancho.Writer;
// Set the lobby's host to the new user // Set the lobby's host to the new user
this.host = this.slots[SlotIDToTransferTo].playerId; this.host = this.slots[SlotIDToTransferTo].playerId;
osuPacketWriter.MatchUpdate(this.createOsuMatchJSON()); this.sendMatchUpdate();
// Inform all clients in the match of the change
global.StreamsHandler.sendToStream(this.matchStreamName, osuPacketWriter.toBuffer, null);
} }
// TODO: Fix not being able to add DT when freemod is active // TODO: Fix not being able to add DT when freemod is active
updateMods(MatchUser, MatchMods) { updateMods(MatchUser = new User, MatchMods) {
// Check if freemod is enabled // Check if freemod is enabled
if (this.specialModes === 1) { if (this.specialModes === 1) {
const osuPacketWriter = new osu.Bancho.Writer;
this.slots[MatchUser.matchSlotId].mods = MatchMods; this.slots[MatchUser.matchSlotId].mods = MatchMods;
osuPacketWriter.MatchUpdate(this.createOsuMatchJSON()); this.sendMatchUpdate();
global.StreamsHandler.sendToStream(this.matchStreamName, osuPacketWriter.toBuffer, null);
} else { } else {
// Make sure the person updating mods is the host of the match // Make sure the person updating mods is the host of the match
if (this.host !== MatchUser.id) return; if (this.host !== MatchUser.id) return;
const osuPacketWriter = new osu.Bancho.Writer;
// Change the matches mods to these new mods // Change the matches mods to these new mods
// TODO: Do this per user if freemod is enabled // TODO: Do this per user if freemod is enabled
this.activeMods = MatchMods; this.activeMods = MatchMods;
osuPacketWriter.MatchUpdate(this.createOsuMatchJSON()); this.sendMatchUpdate();
// Inform all users in the match of the change
global.StreamsHandler.sendToStream(this.matchStreamName, osuPacketWriter.toBuffer, null);
} }
// Update match listing in the lobby to reflect this change // Update match listing in the lobby to reflect this change
@ -435,31 +346,23 @@ module.exports = class {
this.inProgress = true; this.inProgress = true;
// Create array for monitoring users until they are ready to play // Create array for monitoring users until they are ready to play
this.matchLoadSlots = []; this.matchLoadSlots = [];
const loadedSlots = this.matchLoadSlots;
// Loop through all slots in the match // Loop through all slots in the match
for (let i = 0; i < this.slots.length; i++) { for (let slot of this.slots) {
const slot = this.slots[i];
// Make sure the slot has a user in it // Make sure the slot has a user in it
if (slot.playerId === -1 || slot.status === 1 || slot.status === 2) continue; if (slot.playerId === -1 || slot.status === 1 || slot.status === 2) continue;
// Add the slot's user to the loaded checking array // Add the slot's user to the loaded checking array
loadedSlots.push({ this.matchLoadSlots.push({
playerId: slot.playerId, playerId: slot.playerId,
loaded: false loaded: false
}); });
}
const osuPacketWriter = new osu.Bancho.Writer;
// Loop through all slots in the match
for (let i = 0; i < this.slots.length; i++) {
const slot = this.slots[i];
// Make sure the slot has a user in it
if (slot.playerId === -1 || slot.status === 1 || slot.status === 2) continue;
// Set the user's status to playing // Set the user's status to playing
slot.status = 32; slot.status = 32;
} }
const osuPacketWriter = new osu.Bancho.Writer;
osuPacketWriter.MatchStart(this.createOsuMatchJSON()); osuPacketWriter.MatchStart(this.createOsuMatchJSON());
// Inform all users in the match that it has started // Inform all users in the match that it has started
@ -472,25 +375,18 @@ module.exports = class {
global.MultiplayerManager.updateMatchListing(); global.MultiplayerManager.updateMatchListing();
} }
matchPlayerLoaded(MatchUser) { matchPlayerLoaded(MatchUser = new User) {
const loadedSlots = this.matchLoadSlots; // Loop through all user load check items and check if all users are loaded
// Loop through all user load check items
for (let i = 0; i < loadedSlots.length; i++) {
// If loadslot belongs to this user then set loaded to true
if (loadedSlots[i].playerId == MatchUser.id) {
loadedSlots[i].loaded = true;
}
}
// Loop through all loaded slots and check if all users are loaded
let allLoaded = true; let allLoaded = true;
for (let i = 0; i < loadedSlots.length; i++) { for (let loadedSlot of this.matchLoadSlots) {
if (loadedSlots[i].loaded) continue; // If loadslot belongs to this user then set loaded to true
if (loadedSlot.playerId == MatchUser.id) {
loadedSlot.loaded = true;
}
if (loadedSlot.loaded) continue;
// A user wasn't loaded, keep waiting.
allLoaded = false; allLoaded = false;
break;
} }
// All players have loaded the beatmap, start playing. // All players have loaded the beatmap, start playing.
@ -503,8 +399,7 @@ module.exports = class {
this.matchLoadSlots = null; this.matchLoadSlots = null;
this.playerScores = []; this.playerScores = [];
for (let i = 0; i < this.slots.length; i++) { for (let slot of this.slots) {
const slot = this.slots[i];
if (slot.playerId === -1 || slot.status === 1 || slot.status === 2) continue; if (slot.playerId === -1 || slot.status === 1 || slot.status === 2) continue;
this.playerScores.push({playerId: slot.playerId, slotId: i, score: 0, isCurrentlyFailed: false}); this.playerScores.push({playerId: slot.playerId, slotId: i, score: 0, isCurrentlyFailed: false});
@ -512,37 +407,31 @@ module.exports = class {
} }
} }
onPlayerFinishMatch(MatchUser) { onPlayerFinishMatch(MatchUser = new User) {
// If user loading slots do not exist
if (this.matchLoadSlots == null) { if (this.matchLoadSlots == null) {
this.matchLoadSlots = [];
// Repopulate user loading slots again // Repopulate user loading slots again
const loadedSlots = this.matchLoadSlots; this.matchLoadSlots = [];
for (let i = 0; i < this.slots.length; i++) { for (let slot of this.slots) {
const slot = this.slots[i];
// Make sure the slot has a user // Make sure the slot has a user
if (slot.playerId === -1 || slot.status === 1 || slot.status === 2) continue; if (slot.playerId === -1 || slot.status === 1 || slot.status === 2) continue;
// Populate user loading slots with this user's id and load status // Populate user loading slots with this user's id and load status
loadedSlots.push({ this.matchLoadSlots.push({
playerId: slot.playerId, playerId: slot.playerId,
loaded: false loaded: false
}); });
} }
}
const loadedSlots = this.matchLoadSlots;
// Loop through all loaded slots to make sure all users have finished playing
for (let i = 0; i < loadedSlots.length; i++) {
if (loadedSlots[i].playerId == MatchUser.id) {
loadedSlots[i].loaded = true;
}
} }
let allLoaded = true; let allLoaded = true;
for (let i = 0; i < loadedSlots.length; i++) {
if (loadedSlots[i].loaded) continue; // Loop through all loaded slots to make sure all users have finished playing
for (let loadedSlot of this.matchLoadSlots) {
if (loadedSlot.playerId == MatchUser.id) {
loadedSlot.loaded = true;
}
if (loadedSlot.loaded) continue;
// A user hasn't finished playing // A user hasn't finished playing
allLoaded = false; allLoaded = false;
@ -559,8 +448,7 @@ module.exports = class {
let osuPacketWriter = new osu.Bancho.Writer; let osuPacketWriter = new osu.Bancho.Writer;
// Loop through all slots in the match // Loop through all slots in the match
for (let i = 0; i < this.slots.length; i++) { for (let slot of this.slots) {
const slot = this.slots[i];
// Make sure the slot has a user // Make sure the slot has a user
if (slot.playerId === -1 || slot.status === 1 || slot.status === 2) continue; if (slot.playerId === -1 || slot.status === 1 || slot.status === 2) continue;
@ -584,7 +472,7 @@ module.exports = class {
this.playerScores = null; this.playerScores = null;
} }
updatePlayerScore(MatchPlayer, MatchScoreData) { updatePlayerScore(MatchPlayer = new User, MatchScoreData) {
const osuPacketWriter = new osu.Bancho.Writer; const osuPacketWriter = new osu.Bancho.Writer;
// Make sure the user's slot ID is not invalid // Make sure the user's slot ID is not invalid
@ -608,7 +496,7 @@ module.exports = class {
global.StreamsHandler.sendToStream(this.matchStreamName, osuPacketWriter.toBuffer, null); global.StreamsHandler.sendToStream(this.matchStreamName, osuPacketWriter.toBuffer, null);
} }
matchFailed(MatchUser) { matchFailed(MatchUser = new User) {
const osuPacketWriter = new osu.Bancho.Writer; const osuPacketWriter = new osu.Bancho.Writer;
// Make sure the user's slot ID is not invalid // Make sure the user's slot ID is not invalid