Generalize packet functions

This commit is contained in:
Holly Stubbs 2021-09-24 01:39:46 +01:00
parent 99329d1c70
commit 05aefff23f
5 changed files with 54 additions and 33 deletions

View file

@ -142,7 +142,7 @@ module.exports = class {
} }
} }
updateMatch(MatchData) { updateMatch(MatchUser, MatchData) {
// Update match with new data // Update match with new data
this.inProgress = MatchData.inProgress; this.inProgress = MatchData.inProgress;
@ -221,7 +221,7 @@ module.exports = class {
global.StreamsHandler.sendToStream(this.matchStreamName, osuPacketWriter.toBuffer, null); global.StreamsHandler.sendToStream(this.matchStreamName, osuPacketWriter.toBuffer, null);
} }
setReadyState(MatchUser, ReadyState) { setStateReady(MatchUser) {
// Get the match the user is in // Get the match the user is in
const osuPacketWriter = new osu.Bancho.Writer; const osuPacketWriter = new osu.Bancho.Writer;
@ -230,9 +230,29 @@ module.exports = class {
const slot = this.slots[i]; const slot = this.slots[i];
// Check if the player in this slot is this user // Check if the player in this slot is this user
if (slot.playerId == MatchUser.id) { if (slot.playerId == MatchUser.id) {
// Turn on or off the user's ready state // Turn on the user's ready state
if (ReadyState) slot.status = 8; // Ready slot.status = 8; // Ready
else 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);
}
setStateNotReady(MatchUser) {
// Get the match the user is in
const osuPacketWriter = new osu.Bancho.Writer;
// Loop though all slots in the match
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; break;
} }
} }
@ -364,7 +384,7 @@ module.exports = class {
} }
} }
transferHost(SlotIDToTransferTo) { transferHost(MatchUser, SlotIDToTransferTo) {
const osuPacketWriter = new osu.Bancho.Writer; const osuPacketWriter = new osu.Bancho.Writer;
// Set the lobby's host to the new user // Set the lobby's host to the new user

View file

@ -14,7 +14,7 @@ module.exports = function(CurrentUser) {
// Remove user from user list // Remove user from user list
global.removeUser(CurrentUser); global.removeUser(CurrentUser);
const osuPacketWriter = new osu.Bancho.Writer; const osuPacketWriter = new osu.Bancho.Writer();
osuPacketWriter.SendMessage({ osuPacketWriter.SendMessage({
sendingClient: global.users["bot"].username, sendingClient: global.users["bot"].username,
message: `User ${CurrentUser.username} has logged out.`, message: `User ${CurrentUser.username} has logged out.`,

View file

@ -3,13 +3,13 @@ const osu = require("osu-packet"),
module.exports = function(CurrentUser, CurrentPacket) { module.exports = function(CurrentUser, CurrentPacket) {
const osuPacketWriter = new osu.Bancho.Writer; const osuPacketWriter = new osu.Bancho.Writer;
const userSentTo = getUserByUsername(CurrentPacket.data.target); const userSentTo = getUserByUsername(CurrentPacket.target);
if (userSentTo == null) return; if (userSentTo == null) return;
osuPacketWriter.SendMessage({ osuPacketWriter.SendMessage({
sendingClient: CurrentUser.username, sendingClient: CurrentUser.username,
message: CurrentPacket.data.message, message: CurrentPacket.message,
target: CurrentUser.username, target: CurrentUser.username,
senderId: CurrentUser.id senderId: CurrentUser.id
}); });

View file

@ -4,22 +4,22 @@ const osu = require("osu-packet"),
module.exports = function(CurrentUser, CurrentPacket) { module.exports = function(CurrentUser, CurrentPacket) {
let isSendingChannelLocked = false; let isSendingChannelLocked = false;
for (let i = 0; i < global.channels.length; i++) { for (let i = 0; i < global.channels.length; i++) {
if (!CurrentPacket.data.target.includes("#")) break; if (!CurrentPacket.target.includes("#")) break;
if (global.channels[i].channelName == CurrentPacket.data.target) { if (global.channels[i].channelName == CurrentPacket.target) {
isSendingChannelLocked = global.channels[i].locked; isSendingChannelLocked = global.channels[i].locked;
break; break;
} }
} }
if (isSendingChannelLocked) { if (isSendingChannelLocked) {
if (CurrentPacket.data.message.includes("!")) { if (CurrentPacket.message.includes("!")) {
botCommandHandler(CurrentUser, CurrentPacket.data.message, CurrentPacket.data.target); botCommandHandler(CurrentUser, CurrentPacket.message, CurrentPacket.target);
} else { } else {
const osuPacketWriter = new osu.Bancho.Writer; const osuPacketWriter = new osu.Bancho.Writer;
osuPacketWriter.SendMessage({ osuPacketWriter.SendMessage({
sendingClient: global.users["bot"].username, sendingClient: global.users["bot"].username,
message: "The channel you are currently trying to send to is locked, please check back later!", message: "The channel you are currently trying to send to is locked, please check back later!",
target: CurrentPacket.data.target, target: CurrentPacket.target,
senderId: global.users["bot"].id senderId: global.users["bot"].id
}); });
CurrentUser.addActionToQueue(osuPacketWriter.toBuffer); CurrentUser.addActionToQueue(osuPacketWriter.toBuffer);
@ -27,29 +27,30 @@ module.exports = function(CurrentUser, CurrentPacket) {
return; return;
} }
global.consoleHelper.printChat(`${CurrentUser.username} in ${CurrentPacket.data.target} sent: ${CurrentPacket.data.message}`); global.consoleHelper.printChat(`${CurrentUser.username} in ${CurrentPacket.target} sent: ${CurrentPacket.message}`);
const osuPacketWriter = new osu.Bancho.Writer; const osuPacketWriter = new osu.Bancho.Writer;
osuPacketWriter.SendMessage({ osuPacketWriter.SendMessage({
sendingClient: CurrentUser.username, sendingClient: CurrentUser.username,
message: CurrentPacket.data.message, message: CurrentPacket.message,
target: CurrentPacket.data.target, target: CurrentPacket.target,
senderId: CurrentUser.id senderId: CurrentUser.id
}); });
if (CurrentPacket.data.target == "#multiplayer") { if (CurrentPacket.target == "#multiplayer") {
global.StreamsHandler.sendToStream(CurrentUser.currentMatch.matchChatStreamName, osuPacketWriter.toBuffer, CurrentUser.uuid); global.StreamsHandler.sendToStream(CurrentUser.currentMatch.matchChatStreamName, osuPacketWriter.toBuffer, CurrentUser.uuid);
botCommandHandler(CurrentUser, CurrentPacket.data.message, CurrentUser.currentMatch.matchChatStreamName, true); botCommandHandler(CurrentUser, CurrentPacket.message, CurrentUser.currentMatch.matchChatStreamName, true);
return; return;
} }
// Check the stream that we're sending to even exists // Check the stream that we're sending to even exists
if (!global.StreamsHandler.doesStreamExist(CurrentPacket.data.target)) return; if (!global.StreamsHandler.doesStreamExist(CurrentPacket.target)) return;
// Write chat message to stream asociated with chat channel // Write chat message to stream asociated with chat channel
global.StreamsHandler.sendToStream(CurrentPacket.data.target, osuPacketWriter.toBuffer, CurrentUser.uuid); global.StreamsHandler.sendToStream(CurrentPacket.target, osuPacketWriter.toBuffer, CurrentUser.uuid);
if (CurrentPacket.data.target == "#osu") if (CurrentPacket.target == "#osu")
global.addChatMessage(`${CurrentUser.username}: ${CurrentPacket.data.message}`); global.addChatMessage(`${CurrentUser.username}: ${CurrentPacket.message}`);
botCommandHandler(CurrentUser, CurrentPacket.data.message, CurrentPacket.data.target);
botCommandHandler(CurrentUser, CurrentPacket.message, CurrentPacket.target);
return; return;
} }

View file

@ -178,7 +178,7 @@ module.exports = async function(req, res) {
break; break;
case packetIDs.client_sendPublicMessage: case packetIDs.client_sendPublicMessage:
SendPublicMessage(PacketUser, CurrentPacket); SendPublicMessage(PacketUser, CurrentPacket.data);
break; break;
case packetIDs.client_logout: case packetIDs.client_logout:
@ -206,7 +206,7 @@ module.exports = async function(req, res) {
break; break;
case packetIDs.client_sendPrivateMessage: case packetIDs.client_sendPrivateMessage:
SendPrivateMessage(PacketUser, CurrentPacket); SendPrivateMessage(PacketUser, CurrentPacket.data);
break; break;
case packetIDs.client_joinLobby: case packetIDs.client_joinLobby:
@ -230,15 +230,15 @@ module.exports = async function(req, res) {
break; break;
case packetIDs.client_matchReady: case packetIDs.client_matchReady:
PacketUser.currentMatch.setReadyState(PacketUser, true); PacketUser.currentMatch.setStateReady(PacketUser);
break; break;
case packetIDs.client_matchChangeSettings: case packetIDs.client_matchChangeSettings:
PacketUser.currentMatch.updateMatch(CurrentPacket.data); PacketUser.currentMatch.updateMatch(PacketUser, CurrentPacket.data);
break; break;
case packetIDs.client_matchNotReady: case packetIDs.client_matchNotReady:
PacketUser.currentMatch.setReadyState(PacketUser, false); PacketUser.currentMatch.setStateNotReady(PacketUser);
break; break;
case packetIDs.client_partMatch: case packetIDs.client_partMatch:
@ -263,7 +263,7 @@ module.exports = async function(req, res) {
break; break;
case packetIDs.client_matchTransferHost: case packetIDs.client_matchTransferHost:
PacketUser.currentMatch.transferHost(CurrentPacket.data); PacketUser.currentMatch.transferHost(PacketUser, CurrentPacket.data);
break; break;
case packetIDs.client_matchChangeMods: case packetIDs.client_matchChangeMods:
@ -331,7 +331,7 @@ module.exports = async function(req, res) {
break; break;
case packetIDs.client_userPresenceRequest: case packetIDs.client_userPresenceRequest:
UserPresence(PacketUser, PacketUser.id); UserPresence(PacketUser, PacketUser.id); // Can't really think of a way to generalize this?
break; break;
default: default: