public AdInformation[] GetAdsToDisplayInOrder(List <AdInformation> ads) { var sumPriorities = ads.Sum(x => x.Priority); var adsToDisplayInOrder = new AdInformation[sumPriorities]; var adsOrderedByPriority = ads.OrderBy(x => x.Priority).ToList(); for (int index = 0; index < adsToDisplayInOrder.Length; index++) { adsToDisplayInOrder[index] = null; } // Ads are ordered ascending by Priority for (int index = adsOrderedByPriority.Count - 1; index >= 0; index--) { var currentPriority = adsOrderedByPriority[index].Priority; var currentProbability = (double)sumPriorities / currentPriority; for (int i = 0; i < currentPriority; i++) { var adIndex = Round(i * currentProbability); while (adsToDisplayInOrder[adIndex] != null) { adIndex++; } adsToDisplayInOrder[adIndex] = adsOrderedByPriority[index]; } } return(adsToDisplayInOrder); }
private void DisplayAds() { var adsToDisplayInOrder = adsOrderProvider.GetAdsToDisplayInOrder(AdInformationList); while (true) { foreach (var adInformation in adsToDisplayInOrder) { currentAd = adInformation; DisplayCurrentAd(5000); } } }