Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Last active March 22, 2025 10:12
Show Gist options
  • Save trikitrok/4bb567e21712b93c425800de58a236c2 to your computer and use it in GitHub Desktop.
Save trikitrok/4bb567e21712b93c425800de58a236c2 to your computer and use it in GitHub Desktop.
Port of example from Re-Engineering Legacy Software book
export class BannerAdChooser {
private readonly bannerDao = new BannerDao();
private readonly bannerCache = new BannerCache();
public getAd(player: Player, page: Page): Banner {
let banner: Banner;
let showBanner = true;
// First try the cache
banner = this.bannerCache.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 === "profile") {
// Don't show ads on player profile page
showBanner = false;
}
if (page.id === "top" && new Date().getDay() === 3) {
// No ads on top page on Wednesdays
showBanner = false;
}
if (player.id % 5 === 0) {
// A/B test - show banner 123 to these players
banner = this.bannerDao.findById(123);
}
if (showBanner && banner == null) {
banner = this.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
this.bannerCache.put(player, page, banner, 30 * 60);
// Dozens more checks and conditions ...
if (showBanner) {
// Make a record of what banner we chose
this.logImpression(player, page, banner);
}
return banner;
}
private logImpression(player: Player, page: Page, banner: Banner): void {
// Implementation for logging the impression
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment