module.exports = {
  determineStream: async function (ip) {
    // existing embed?
    const current = await redis2.get(`currentstream:${ip}`)
    if (current && (current === 'streamspread' || await redis.get(`twitch:${current}:live`))) {
      await redis2.expire(`currentstream:${ip}`, 70)
      return current
    }

    // determine method stream or advert
    const streamOverride = global.EmbeddedStream || {streams:[]}
    // If enabled
    if (streamOverride.enabled && streamOverride.streams.length) {
      for (let i = 0; i < streamOverride.streams.length; i++) {
        // check exposure chance
        if (Math.random() * 100 < streamOverride.streams[i].exposure && await redis.get(`twitch:${streamOverride.streams[i].channel}:live`)) {
          // and we are not over max viewer count...
          const embedViewers = await redis2.zcount(`streamUsers:${streamOverride.streams[i].channel}`, '-inf', '+inf')
          // then show stream to user
          if (embedViewers < parseInt(streamOverride.streams[i].max)) {
            await redis2.set(`stream:${streamOverride.streams[i].channel}:${ip}`, 1, 'EX', 70)
            await redis2.set(`currentstream:${ip}`, streamOverride.streams[i].channel, 'EX', 70)
            return streamOverride.streams[i].channel
          }
        }
      }
    }

    const streamerList = await Streamer.find({online: {$ne: null}})
    if (streamerList.length) {
      const total = streamerList.map(c => c.viewers).reduce((acc, cur) => acc + cur);
      const rng = Math.random() * total
      let acc = 0
      for (let i = 0; i < streamerList.length; i++) {
        if (streamerList[i].wagoViewers >= streamerList[i].viewers) {
          acc = acc + streamerList[i].viewers
          continue
        }
        if (rng < streamerList[i].viewers + acc) {
          await redis2.set(`stream:${streamerList[i].name}:${ip}`, 1, 'EX', 70)
          await redis2.set(`currentstream:${ip}`, streamerList[i].name, 'EX', 70)
          return streamerList[i].name
        }
        acc = acc + streamerList[i].viewers
      }
    }

    await redis2.set(`currentstream:${ip}`, 'streamspread', 'EX', 70)
    return 'streamspread'
  }
}
@@ -1307,24 +1307,27 @@
 %7D%0A    %7D%0A%0A   
+ //
  const strea
@@ -1378,24 +1378,27 @@
  null%7D%7D)%0A   
+ //
  if (streame
@@ -1412,24 +1412,27 @@
 ength) %7B%0A   
+ //
    const tot
@@ -1505,16 +1505,19 @@
 ur);%0A   
+ //
    const
@@ -1549,18 +1549,21 @@
 tal%0A    
+//
   
+ 
 let acc 
@@ -1565,24 +1565,27 @@
  acc = 0%0A   
+ //
    for (let 
@@ -1622,24 +1622,27 @@
 ; i++) %7B%0A   
+ //
      if (str
@@ -1695,24 +1695,27 @@
 ewers) %7B%0A   
+ //
        acc =
@@ -1744,24 +1744,27 @@
 .viewers%0A   
+ //
        conti
@@ -1766,24 +1766,27 @@
 continue%0A   
+ //
      %7D%0A     
@@ -1779,24 +1779,27 @@
 //     %7D%0A   
+ //
      if (rng
@@ -1833,24 +1833,27 @@
 + acc) %7B%0A   
+ //
        await
@@ -1917,30 +1917,33 @@
 X', 70)%0A    
+//
       
+ 
 await redis2
@@ -2002,30 +2002,33 @@
 X', 70)%0A    
+//
       
+ 
 return strea
@@ -2042,24 +2042,27 @@
 %5Bi%5D.name%0A   
+ //
      %7D%0A     
@@ -2055,24 +2055,27 @@
 //     %7D%0A   
+ //
      acc = a
@@ -2102,32 +2102,35 @@
 .viewers%0A   
+ //
    %7D%0A   
  %7D%0A%0A    awai
@@ -2109,32 +2109,35 @@
 s%0A    //   %7D%0A   
+ //
  %7D%0A%0A    await re

disable embedded stream list