def __init__(self, char_id, condition_id, active_time): """ :type active_time: ActivityTime """ self.char_id = char_id self.condition_id = str(condition_id) self.loop_times = active_time.loop_times self.open_time = active_time.nearest_open_date.timestamp self.key = "{0}#{1}#{2}".format(self.condition_id, self.loop_times, self.open_time) try: self.mongo = MongoActivityStatic.objects.get(id=char_id) except DoesNotExist: self.mongo = MongoActivityStatic(id=char_id) self.mongo.reward_times = {} self.mongo.send_times = {} self.mongo.save() self.clean()
class ActivityConditionRecord(object): # 记录活动领取/发放了哪些奖励,避免重复领取/发放 # 注意:现在活动可以不间断循环开启, # 也就是A活动的一个周期结束后,新一轮活动会立即开启 # 这样以前的 cron/clean_expired_activites就没法用了 # 因为这个定时任务会一直判断这个A活动是正在进行的 # 当新一轮活动开始后,玩家将无法领奖 # 已经判断到已经发过了(上一轮发的) # 所以这里处理,把条件ID加上loop id作为唯一标识 def __init__(self, char_id, condition_id, active_time): """ :type active_time: ActivityTime """ self.char_id = char_id self.condition_id = str(condition_id) self.loop_times = active_time.loop_times self.open_time = active_time.nearest_open_date.timestamp self.key = "{0}#{1}#{2}".format(self.condition_id, self.loop_times, self.open_time) try: self.mongo = MongoActivityStatic.objects.get(id=char_id) except DoesNotExist: self.mongo = MongoActivityStatic(id=char_id) self.mongo.reward_times = {} self.mongo.send_times = {} self.mongo.save() self.clean() @classmethod def fix(cls, char_id): for k, v in ACTIVITY_STATIC.iteritems(): if v.condition_objs: ae = ActivityEntry(char_id, k) for c in v.condition_objs: cls(char_id, c.id, ae.activity_time) # NOTE F**K # 17002 累计充值送月卡是特殊处理的 x = Activity17002(char_id) cls(char_id, x.CONDITION_ID, x.activity_time) def send_times(self): return self.mongo.send_times.get(self.key, 0) def reward_times(self): return self.mongo.reward_times.get(self.key, 0) def in_send(self): return self.key in self.mongo.send_times def in_reward(self): return self.key in self.mongo.reward_times def add_send(self, times=1): self.mongo.send_times[self.key] = self.send_times() + times self.mongo.save() def add_reward(self, times=1): self.mongo.reward_times[self.key] = self.reward_times() + times self.mongo.save() def clean(self): # 清理过期的记录 for k, v in self.mongo.send_times.items(): key = k.rsplit('#', 2) if len(key) == 1: if k != self.condition_id: continue # 以前的情况,没有记录loop times的 new_key = "{0}#{1}#{2}".format(k, self.loop_times, self.open_time) self.mongo.send_times.pop(k) self.mongo.send_times[new_key] = v else: # 现在记录了loop times的 oid, loop_times, open_time = key if oid != self.condition_id: continue # 找到这个ID的条件,然后比较open time, # 不一样的就删除 if int(open_time) != self.open_time: self.mongo.send_times.pop(k) for k, v in self.mongo.reward_times.items(): key = k.rsplit('#', 2) if len(key) == 1: if k != self.condition_id: continue new_key = "{0}#{1}#{2}".format(k, self.loop_times, self.open_time) self.mongo.reward_times.pop(k) self.mongo.reward_times[new_key] = v else: oid, loop_times, open_time = key if oid != self.condition_id: continue if int(open_time) != self.open_time: self.mongo.reward_times.pop(k) self.mongo.save()