contextualOfferInnerService.py (4338B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\contextualOffers\client\contextualOfferInnerService.py 2 from carbon.common.script.util.countdownTimer import CountdownTimer 3 from contextualOffers.client.contextualOfferClientConst import SCATTER_EVENT_ON_OFFER_AVAILABILITY_CHANGE 4 from eve.common.script.util.notificationconst import notificationTypeNewOfferAvailable 5 from gametime import GetWallclockTimeNow, DAY 6 from notifications.common.formatters.contextualOffers import ContextualOfferNotification 7 from log import LogError 8 9 class ContextualOfferInnerService: 10 11 def __init__(self, scatterService): 12 self.scatterSvc = scatterService 13 self.InitializeData() 14 15 def InitializeData(self): 16 self.availableOffers = {} 17 self.purchasedOffers = {} 18 self.offerNotifications = [] 19 self.offerTimers = {} 20 21 def OnOfferReceived(self, offerID, offerObj): 22 hadOffers = self.HasOffers() 23 self.availableOffers[offerID] = offerObj 24 self.AddOfferTimer(offerID, offerObj) 25 self._OnAvailabilityChanged(hadOffers) 26 self.SendOfferAvailableNotification(offerObj) 27 28 def OnPurchaseSuccessReceived(self, offerID, purchaseObj): 29 self.purchasedOffers[offerID] = purchaseObj 30 if offerID in self.availableOffers: 31 del self.availableOffers[offerID] 32 self._NotifyOfAvailabilityChange() 33 34 def SendOfferAvailableNotification(self, offer): 35 offerID = offer['offerID'] 36 subject = offer['notification']['Title'] 37 body = offer['notification']['Body'] 38 notificationData = ContextualOfferNotification.MakeData(subject=subject, body=body, url='', offerID=offerID) 39 if self.scatterSvc: 40 notificationSvc = self.scatterSvc.GetService('notificationSvc') 41 notificationSvc.MakeAndScatterNotification(type=notificationTypeNewOfferAvailable, data=notificationData) 42 43 def _OnAvailabilityChanged(self, hadOffers): 44 if self.HasOffers() is not hadOffers: 45 self._NotifyOfAvailabilityChange() 46 47 def _NotifyOfAvailabilityChange(self): 48 if self.scatterSvc: 49 self.scatterSvc.ScatterEvent(SCATTER_EVENT_ON_OFFER_AVAILABILITY_CHANGE) 50 51 def GetOfferNotifications(self): 52 return list(self.offerNotifications) 53 54 def AddOfferNotification(self, offerID, viewType): 55 if (offerID, viewType) not in self.offerNotifications: 56 self.offerNotifications.append((offerID, viewType)) 57 58 def RemoveOfferNotification(self, offerID, viewType): 59 if (offerID, viewType) in self.offerNotifications: 60 self.offerNotifications.remove((offerID, viewType)) 61 62 def HasOffers(self): 63 return len(self.availableOffers) > 0 64 65 def GetOffers(self): 66 return self.availableOffers.copy() 67 68 def IsOfferAvailable(self, offerID): 69 return offerID in self.availableOffers.keys() 70 71 def GetOfferById(self, offerID): 72 return self.availableOffers[offerID] 73 74 def GetPurchasedOffers(self): 75 return self.purchasedOffers.copy() 76 77 def IsPurchaseConfirmed(self, offerID): 78 return offerID in self.purchasedOffers.keys() 79 80 def GetPurchasedOfferById(self, offerID): 81 return self.purchasedOffers[offerID] 82 83 def AddOfferTimer(self, offerID, offerObject): 84 expirationDate = offerObject.get('validTo', DAY + GetWallclockTimeNow()) 85 if offerID in self.offerTimers: 86 self.offerTimers[offerID].set_expiration_date(expirationDate) 87 else: 88 self.offerTimers[offerID] = CountdownTimer(expirationDate) 89 90 def SubscribeToOfferTimer(self, offerID, subscriber, callback): 91 if offerID not in self.offerTimers: 92 LogError('Failed to subscribe to offer countdown timer. Timer cannot be found.') 93 return 94 self.offerTimers[offerID].register_subscriber(subscriber, callback) 95 96 def UnsubscribeFromOfferTimer(self, offerID, subscriber): 97 if offerID not in self.offerTimers: 98 LogError('Failed to unsubscribe from offer countdown timer. Timer cannot be found.') 99 return 100 self.offerTimers[offerID].unregister_subscriber(subscriber) 101 102 def Debug_ClearOffers(self): 103 hadOffers = self.HasOffers() 104 self.InitializeData() 105 self._OnAvailabilityChanged(hadOffers)