track_contribution.py (5873B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\campaigns\client\v2\data_model\track_contribution.py 2 from campaigns.client.v2.data_model.trigger import EventMatchTrigger, ObjectiveCompletedTrigger, ObjectiveFailedTrigger 3 from campaigns.client.v2.data_model.ui_definition import UIDefinition 4 5 class TrackContribution(object): 6 7 def __eq__(self, other): 8 if type(other) is not type(self): 9 return NotImplemented 10 return self.__dict__ == other.__dict__ 11 12 def __ne__(self, other): 13 result = self.__eq__(other) 14 if result is NotImplemented: 15 return result 16 return not result 17 18 def __repr__(self): 19 return '<%s %r>' % (self.__class__.__name__, self.__dict__) 20 21 22 class EventMatchContribution(TrackContribution): 23 24 def __init__(self, trigger, fixed_progress_value, attributed_to_participant): 25 self._trigger = trigger 26 self._fixed_progress_value = fixed_progress_value 27 self._attributed_to_participant = attributed_to_participant 28 29 @property 30 def trigger(self): 31 return self._trigger 32 33 @property 34 def fixed_progress_value(self): 35 return self._fixed_progress_value 36 37 @property 38 def is_event_dependent(self): 39 return self._fixed_progress_value is None 40 41 @property 42 def attributed_to_participant(self): 43 return self._attributed_to_participant 44 45 @classmethod 46 def from_proto(cls, event_match): 47 progress_value = event_match.WhichOneof('progress_value') 48 if progress_value == 'fixed_value': 49 fixed_progress_value = event_match.fixed_value 50 elif progress_value == 'event_dependent': 51 fixed_progress_value = None 52 else: 53 raise NotImplementedError('Unknown event match progress value: %s' % progress_value) 54 return EventMatchContribution(trigger=EventMatchTrigger.from_proto(event_match.configuration), fixed_progress_value=fixed_progress_value, attributed_to_participant=event_match.attributed_to_participant) 55 56 57 class ObjectiveOutcomeContribution(TrackContribution): 58 _trigger_class = None 59 60 def __init__(self, trigger, progress_value): 61 self._trigger = trigger 62 self._progress_value = progress_value 63 64 @property 65 def trigger(self): 66 return self._trigger 67 68 @property 69 def progress_value(self): 70 return self._progress_value 71 72 @classmethod 73 def from_proto(cls, contribution): 74 return cls(cls._trigger_class.from_proto(contribution.configuration), contribution.progress_value) 75 76 77 class ObjectiveCompletedContribution(ObjectiveOutcomeContribution): 78 _trigger_class = ObjectiveCompletedTrigger 79 80 81 class ObjectiveFailedContribution(ObjectiveOutcomeContribution): 82 _trigger_class = ObjectiveFailedTrigger 83 84 85 class TickingContribution(TrackContribution): 86 87 def __init__(self, key, ui_definition, interval, evaluations): 88 self._key = key 89 self._ui_definition = ui_definition 90 self._interval = interval 91 self._evaluations = evaluations 92 93 @property 94 def key(self): 95 return self._key 96 97 @property 98 def ui_definition(self): 99 return self._ui_definition 100 101 @property 102 def interval(self): 103 return self._interval 104 105 @property 106 def evaluations(self): 107 return self._evaluations 108 109 @classmethod 110 def from_proto(cls, ticking_contribution): 111 interval = None 112 if ticking_contribution.HasField('interval'): 113 interval = ticking_contribution.interval.ToTimedelta() 114 return TickingContribution(key=ticking_contribution.key, ui_definition=UIDefinition.from_proto(ticking_contribution.ui), interval=interval, evaluations=[ ticking_evaluation_from_proto(evaluation) for evaluation in ticking_contribution.evaluations ]) 115 116 117 class TickingEvaluation(object): 118 119 def __init__(self, key, ui_definition, value): 120 self._key = key 121 self._ui_definition = ui_definition 122 self._value = value 123 124 @property 125 def key(self): 126 return self._key 127 128 @property 129 def ui_definition(self): 130 return self._ui_definition 131 132 @property 133 def value(self): 134 return self._value 135 136 def __eq__(self, other): 137 if type(other) is not type(self): 138 return NotImplemented 139 return self.__dict__ == other.__dict__ 140 141 def __ne__(self, other): 142 result = self.__eq__(other) 143 if result is NotImplemented: 144 return result 145 return not result 146 147 def __repr__(self): 148 return '<%s %r>' % (self.__class__.__name__, self.__dict__) 149 150 151 class ConstantTickingEvaluation(TickingEvaluation): 152 pass 153 154 155 class PerUnitTickingEvaluation(TickingEvaluation): 156 pass 157 158 159 def ticking_evaluation_from_proto(evaluation): 160 evaluation_type = evaluation.WhichOneof('evaluation') 161 ui_definition = UIDefinition.from_proto(evaluation.ui) 162 if evaluation_type == 'constant': 163 return ConstantTickingEvaluation(evaluation.key, ui_definition, evaluation.constant) 164 if evaluation_type == 'per_unit_value': 165 return PerUnitTickingEvaluation(evaluation.key, ui_definition, evaluation.per_unit_value) 166 raise NotImplementedError('Unknown ticking evaluation type: %s' % evaluation_type) 167 168 169 _CONTRIBUTION_CLASS_BY_TYPE = {'event_match': EventMatchContribution, 170 'ticking_contribution': TickingContribution, 171 'objective_completed': ObjectiveCompletedContribution, 172 'objective_failed': ObjectiveFailedContribution} 173 174 def contribution_from_proto(contribution): 175 contribution_type = contribution.WhichOneof('contribution_type') 176 contribution_class = _CONTRIBUTION_CLASS_BY_TYPE.get(contribution_type) 177 if contribution_class is None: 178 raise NotImplementedError('Unknown track contribution type: %s' % contribution_type) 179 return contribution_class.from_proto(getattr(contribution, contribution_type))