|
| 1 | +settings.register({ |
| 2 | + name: 'Winstreak Handling', |
| 3 | + key: 'underscript.winstreak', |
| 4 | + options: ['Chat', 'Toast', 'Both', 'Hidden'], |
| 5 | + default: 'Both', |
| 6 | + type: 'select', |
| 7 | + page: 'Chat', |
| 8 | +}); |
| 9 | + |
| 10 | +settings.register({ |
| 11 | + name: 'Winstreak - Friends Only', |
| 12 | + key: 'underscript.winstreak.friendsOnly', |
| 13 | + page: 'Chat', |
| 14 | +}); |
| 15 | + |
| 16 | +wrap(() => { |
| 17 | + const empty = { close() {} } |
| 18 | + const toasts = { |
| 19 | + v: [], |
| 20 | + i: 0, |
| 21 | + add(toast) { |
| 22 | + (this.v[this.i] || empty).close(); |
| 23 | + this.v[this.i] = toast; |
| 24 | + this.i = (this.i + 1) % 3; |
| 25 | + return toast; |
| 26 | + }, |
| 27 | + }; |
| 28 | + const regex = /(?:(.*) is on a (\d+))|(?:(.*) has just stopped (.*)'s (\d+))/; |
| 29 | + function friendsOnly() { |
| 30 | + return settings.value('underscript.winstreak.friendsOnly'); |
| 31 | + } |
| 32 | + function checkFriend(name) { |
| 33 | + return !friendsOnly() || fn.isFriend(name); |
| 34 | + } |
| 35 | + function checkCount(amt) { |
| 36 | + //return parseInt(amt, 10) >= settings.value('underscript.winstreak.count'); |
| 37 | + return true; |
| 38 | + } |
| 39 | + eventManager.on('preChat:getMessageAuto', function winstreaks({message: text}) { |
| 40 | + if (this.canceled || !~text.indexOf('game winning streak !')) return; |
| 41 | + const handling = settings.value('underscript.winstreak'); |
| 42 | + if (handling === 'Chat' && !friendsOnly()) return; // All default |
| 43 | + this.canceled = handling !== 'Chat' && handling !== 'Both'; |
| 44 | + if (handling === 'Hidden') return; |
| 45 | + const results = text.match(regex); |
| 46 | + const username = results[1] || results[4]; |
| 47 | + const streak = results[2] || results[5]; |
| 48 | + if (!checkFriend(username) || !checkCount(streak)) { |
| 49 | + this.canceled = true; |
| 50 | + return; |
| 51 | + } |
| 52 | + // At this point Toast is guaranteed |
| 53 | + const toast = toasts.add(fn.toast({ |
| 54 | + text, |
| 55 | + //timeout: 10000, |
| 56 | + css: { |
| 57 | + color: 'yellow', |
| 58 | + footer: {color: 'white'}, |
| 59 | + }, |
| 60 | + })); |
| 61 | + toast.time = Date.now(); |
| 62 | + }); |
| 63 | +}, 'winstreak'); |
0 commit comments