Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Created December 16, 2024 20:48
Show Gist options
  • Save trikitrok/3a7b0b1db3402137273e83879a6863a1 to your computer and use it in GitHub Desktop.
Save trikitrok/3a7b0b1db3402137273e83879a6863a1 to your computer and use it in GitHub Desktop.
Port of example from Re-Engineering Legacy Software book
public class BannerAdChooser
{
private readonly BannerDao bannerDao = new BannerDao();
private readonly BannerCache cache = new BannerCache();
public Banner GetAd(Player player, Page page)
{
Banner banner;
bool showBanner = true;
// First try the cache
banner = cache.Get(player, page);
if (player.Id == 23759)
{
// This player demands not to be shown any ads.
// See support ticket #4839
showBanner = false;
}
if (page.Id.Equals("profile"))
{
// Don't show ads on player profile page
showBanner = false;
}
if (page.Id.Equals("top") && DateTime.Now.DayOfWeek == DayOfWeek.Wednesday)
{
// No ads on top page on Wednesdays
showBanner = false;
}
if (player.Id % 5 == 0)
{
// A/B test - show banner 123 to these players
banner = bannerDao.FindById(123);
}
if (showBanner && banner == null)
{
banner = bannerDao.ChooseRandomBanner();
}
if (banner.ClientId == 393)
{
if (player.Id == 36645)
{
// Bad blood between this client and this player!
// Don't show the ad.
showBanner = false;
}
}
// Cache our choice for 30 minutes
cache.Put(player, page, banner, 30 * 60);
// Dozens more checks and conditions ...
if (showBanner)
{
// Make a record of what banner we chose
LogImpression(player, page, banner);
}
return banner;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment