@@ -2,8 +2,9 @@ import eventManager from '../../utils/eventManager.js';
22import * as settings from '../../utils/settings/index.js' ;
33import { global } from '../../utils/global.js' ;
44import { 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+
1625const toasts = [ ] ;
1726let 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']) }) })
2933const events = [ 'chat-legendary-notification' , 'chat-legendary-shiny-notification' ] ;
3034eventManager . 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