Index: trunk/extensions/CentralNotice/SpecialBannerController.php |
— | — | @@ -82,24 +82,28 @@ |
83 | 83 | // Make sure there are some banners to choose from |
84 | 84 | if ( bannerList.length == 0 ) return false; |
85 | 85 | |
86 | | - var groomedBannerList = []; |
| 86 | + var totalWeight = 0; |
| 87 | + // run through the bannerlist and sum the weights of all banners |
| 88 | + for( var i = 0; i < bannerList.length; i++ ) { |
| 89 | + totalWeight += bannerList[i].weight; |
| 90 | + } |
87 | 91 | |
| 92 | + // Select a random integer between 0 and our total weight |
| 93 | + var pointer = Math.floor( Math.random() * totalWeight ), |
| 94 | + selectedBanner = bannerList[0], |
| 95 | + w = 0; |
| 96 | + // Run through the banner list and start accumulating weights |
88 | 97 | for( var i = 0; i < bannerList.length; i++ ) { |
89 | | - // Only include this banner if it's inteded for the current user |
90 | | - if( ( wgUserName && bannerList[i].display_account ) || ( !wgUserName && bannerList[i].display_anon == 1 ) ) { |
91 | | - // add the banner to our list once per weight |
92 | | - for( var j=0; j < bannerList[i].weight; j++ ) { |
93 | | - groomedBannerList.push( bannerList[i] ); |
94 | | - } |
| 98 | + w += bannerList[i].weight; |
| 99 | + // when the weight tally exceeds the random integer, return the banner and stop the loop |
| 100 | + if( w < pointer ) { |
| 101 | + selectedBanner = bannerList[i]; |
| 102 | + break; |
95 | 103 | } |
96 | 104 | } |
97 | | - |
98 | | - // Return if there's nothing left after the grooming |
99 | | - if( groomedBannerList.length == 0 ) return false; |
100 | | - |
101 | | - // Load a random banner from our groomed list |
| 105 | + // Return our selected banner |
102 | 106 | $.centralNotice.fn.loadBanner( |
103 | | - groomedBannerList[ Math.floor( Math.random() * groomedBannerList.length ) ].name |
| 107 | + selectedBanner.name |
104 | 108 | ); |
105 | 109 | }, |
106 | 110 | 'displayBanner': function( bannerHTML ) { |