Index: trunk/extensions/CentralNotice/newCentralNotice.js |
— | — | @@ -52,24 +52,22 @@ |
53 | 53 | // Make sure there are some banners to choose from |
54 | 54 | if ( bannerList.length == 0 ) return false; |
55 | 55 | |
56 | | - var groomedBannerList = []; |
57 | | - |
| 56 | + var totalWeight = 0; |
58 | 57 | for( var i = 0; i < bannerList.length; i++ ) { |
59 | | - // only include this banner if it's inteded for the current user |
60 | | - if( ( wgUserName ? bannerList[i].display_account == 1 : bannerList.display_anon == 1 ) ) { |
61 | | - // add the banner to our list once per weight |
62 | | - for( var j=0; j < bannerList[i].weight; j++ ) { |
63 | | - groomedBannerList.push( bannerList[i] ); |
64 | | - } |
| 58 | + totalWeight += bannerList[i].weight; |
| 59 | + } |
| 60 | + var pointer = Math.floor( Math.random() * totalWeight ), |
| 61 | + selectedBanner = bannerList[0], |
| 62 | + w = 0; |
| 63 | + for( var i = 0; i < bannerList.length; i++ ) { |
| 64 | + w += bannerList[i].weight; |
| 65 | + if( w < pointer ) { |
| 66 | + selectedBanner = bannerList[i]; |
| 67 | + break; |
65 | 68 | } |
66 | 69 | } |
67 | | - |
68 | | - // return if there's nothing left after the grooming |
69 | | - if( groomedBannerList.length == 0 ) return false; |
70 | | - // load a random banner from our groomed list |
71 | | - |
72 | 70 | $.centralNotice.fn.loadBanner( |
73 | | - groomedBannerList[ Math.floor( Math.random() * groomedBannerList.length ) ].name |
| 71 | + selectedBanner.name |
74 | 72 | ); |
75 | 73 | }, |
76 | 74 | 'displayBanner': function( bannerHTML ) { |