Nullguard across multiple dialogueController methods to prevent missing profile/dialog from killing client

This commit is contained in:
Dev 2023-07-26 12:47:42 +01:00
parent 9cc4cba105
commit 76dce7a548

View File

@ -254,41 +254,87 @@ export class DialogueController
return messages.some(x => x.items?.data?.length > 0); return messages.some(x => x.items?.data?.length > 0);
} }
/** Handle client/mail/dialog/remove */ /**
public removeDialogue(dialogueID: string, sessionID: string): void * Handle client/mail/dialog/remove
* Remove an entire dialog with an entity (trader/user)
* @param dialogueId id of the dialog to remove
* @param sessionId Player id
*/
public removeDialogue(dialogueId: string, sessionId: string): void
{ {
delete this.saveServer.getProfile(sessionID).dialogues[dialogueID]; const profile = this.saveServer.getProfile(sessionId);
const dialog = profile.dialogues[dialogueId];
if (!dialog)
{
this.logger.error(`No dialog in profile: ${sessionId} found with id: ${dialogueId}`);
return;
} }
public setDialoguePin(dialogueID: string, shouldPin: boolean, sessionID: string): void delete profile.dialogues[dialogueId];
{
this.saveServer.getProfile(sessionID).dialogues[dialogueID].pinned = shouldPin;
} }
/** Handle client/mail/dialog/read */ /** Handle client/mail/dialog/pin && Handle client/mail/dialog/unpin */
public setRead(dialogueIDs: string[], sessionID: string): void public setDialoguePin(dialogueId: string, shouldPin: boolean, sessionId: string): void
{ {
const dialogueData = this.saveServer.getProfile(sessionID).dialogues; const profile = this.saveServer.getProfile(sessionId);
for (const dialogID of dialogueIDs) const dialog = profile.dialogues[dialogueId];
if (!dialog)
{ {
dialogueData[dialogID].new = 0; this.logger.error(`No dialog in profile: ${sessionId} found with id: ${dialogueId}`);
dialogueData[dialogID].attachmentsNew = 0;
return;
}
dialog.pinned = shouldPin;
}
/**
* Handle client/mail/dialog/read
* Set a dialog to be read (no number alert/attachment alert)
* @param dialogueIds Dialog ids to set as read
* @param sessionId Player profile id
*/
public setRead(dialogueIds: string[], sessionId: string): void
{
const profile = this.saveServer.getProfile(sessionId);
const dialogs = profile.dialogues;
if (!dialogs)
{
this.logger.error(`No dialog object in profile: ${sessionId}`);
return;
}
for (const dialogId of dialogueIds)
{
dialogs[dialogId].new = 0;
dialogs[dialogId].attachmentsNew = 0;
} }
} }
/** /**
* Handle client/mail/dialog/getAllAttachments * Handle client/mail/dialog/getAllAttachments
* Get all uncollected items attached to mail in a particular dialog * Get all uncollected items attached to mail in a particular dialog
* @param dialogueID Dialog to get mail attachments from * @param dialogueId Dialog to get mail attachments from
* @param sessionID Session id * @param sessionId Session id
* @returns * @returns
*/ */
public getAllAttachments(dialogueID: string, sessionID: string): IGetAllAttachmentsResponse public getAllAttachments(dialogueId: string, sessionId: string): IGetAllAttachmentsResponse
{ {
// Removes corner 'new messages' tag const profile = this.saveServer.getProfile(sessionId);
this.saveServer.getProfile(sessionID).dialogues[dialogueID].attachmentsNew = 0; const dialog = profile.dialogues[dialogueId];
if (!dialog)
{
this.logger.error(`No dialog in profile: ${sessionId} found with id: ${dialogueId}`);
const activeMessages = this.getActiveMessagesFromDialog(sessionID, dialogueID); return;
}
// Removes corner 'new messages' tag
dialog.attachmentsNew = 0;
const activeMessages = this.getActiveMessagesFromDialog(sessionId, dialogueId);
const messagesWithAttachments = this.getMessagesWithAttachments(activeMessages); const messagesWithAttachments = this.getMessagesWithAttachments(activeMessages);
return { return {