Skip to content

Commit 9361fd6

Browse files
committed
feat: ignore self options for legend toasts
1 parent b33cc33 commit 9361fd6

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

src/base/chat/legend.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as settings from '../../utils/settings/index.js';
33
import { global } from '../../utils/global.js';
44
import { toast } from '../../utils/2.toasts.js';
55
import isFriend from '../../utils/isFriend.js';
6+
import { self, name } from '../../utils/user.js';
67

78
const setting = settings.register({
89
name: 'Announcement',
@@ -14,17 +15,30 @@ const setting = settings.register({
1415
category: 'Legendary User',
1516
});
1617

18+
const ignoreSelf = settings.register({
19+
name: 'Ignore Self',
20+
key: 'underscript.announcement.legend.notSelf',
21+
page: 'Chat',
22+
category: 'Legendary User',
23+
default: true,
24+
});
25+
1726
const friends = settings.register({
1827
name: 'Friends Only',
1928
key: 'underscript.announcement.legend.friendsOnly',
2029
page: 'Chat',
2130
category: 'Legendary User',
2231
});
2332

33+
// test method: plugin.events.emit.cancelable('preChat:getMessageAuto', { message: JSON.stringify({ args: JSON.stringify(['chat-new-legend', 'user']) }) })
2434
const events = ['chat-new-legend'];
2535
eventManager.on('preChat:getMessageAuto', function legend(data) {
2636
const [type, user] = JSON.parse(JSON.parse(data.message).args);
2737
if (this.canceled || !events.includes(type)) return;
38+
if (ignoreSelf.value() && name(self()) === user) {
39+
this.canceled = true;
40+
return;
41+
}
2842
const handling = setting.value();
2943
const friendsOnly = friends.value();
3044
if (handling === 'Chat' && !friendsOnly) return; // All default

src/base/chat/legendary.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import eventManager from '../../utils/eventManager.js';
22
import * as settings from '../../utils/settings/index.js';
33
import { global } from '../../utils/global.js';
44
import { toast as SimpleToast } from '../../utils/2.toasts.js';
5+
import { self, name } from '../../utils/user.js';
56

6-
settings.register({
7+
const setting = settings.register({
78
name: 'Announcement',
89
key: 'underscript.announcement.draws',
910
type: 'select',
@@ -13,37 +14,41 @@ settings.register({
1314
category: 'Legendary Card',
1415
});
1516

17+
const ignoreSelf = settings.register({
18+
name: 'Ignore Self',
19+
key: 'underscript.announcement.draws.notSelf',
20+
page: 'Chat',
21+
category: 'Legendary Card',
22+
default: true,
23+
});
24+
1625
const toasts = [];
1726
let toastIndex = 0;
18-
function getToast(owner) {
27+
function getToast(user) {
1928
const now = Date.now();
20-
for (let i = 0; i < toasts.length; i++) {
21-
const toast = toasts[i];
22-
if (toast && toast.exists() && owner === toast.owner && toast.time + 1000 > now) {
23-
return toast;
24-
}
25-
}
26-
return null;
29+
return toasts.find(({ exists, owner, time }) => exists() && owner === user && time + 1000 > now);
2730
}
2831

32+
// test method: plugin.events.emit.cancelable('preChat:getMessageAuto', { message: JSON.stringify({ args: JSON.stringify(['chat-legendary-notification', 'user', 'card']) }) })
2933
const events = ['chat-legendary-notification', 'chat-legendary-shiny-notification'];
3034
eventManager.on('preChat:getMessageAuto', function drawAnnouncement(data) {
31-
const message = JSON.parse(JSON.parse(data.message).args);
32-
if (this.canceled || !events.includes(message[0])) return;
33-
const setting = settings.value('underscript.announcement.draws');
34-
if (setting === 'chat') return;
35-
const both = setting === 'both';
35+
const [event, user, card] = JSON.parse(JSON.parse(data.message).args);
36+
if (this.canceled || !events.includes(event)) return;
37+
if (ignoreSelf.value() && name(self()) === user) {
38+
this.canceled = true;
39+
return;
40+
}
41+
const type = setting.value();
42+
if (type === 'chat') return;
43+
const both = type === 'both';
3644
this.canceled = !both;
37-
if (both || setting === 'toast') {
38-
const owner = message[1];
39-
const card = message[2];
40-
45+
if (both || type === 'toast') {
4146
const translateFromServerJson = global('translateFromServerJson');
42-
const last = getToast(owner);
47+
const last = getToast(user);
4348
if (last) {
4449
last.cards.unshift(card);
45-
message[2] = last.cards.join(', ');
46-
last.setText(translateFromServerJson(JSON.stringify({ args: JSON.stringify(message) })));
50+
const newText = last.cards.join(', ');
51+
last.setText(translateFromServerJson(JSON.stringify({ args: JSON.stringify([event, user, newText]) })));
4752
last.time = Date.now(); // This toast is still relevant!
4853
return;
4954
}
@@ -61,7 +66,7 @@ eventManager.on('preChat:getMessageAuto', function drawAnnouncement(data) {
6166
},
6267
});
6368
toast.cards = [card];
64-
toast.owner = owner;
69+
toast.owner = user;
6570
toast.time = Date.now();
6671
toasts[toastIndex] = toast;
6772
toastIndex = (toastIndex + 1) % 3;

0 commit comments

Comments
 (0)