def __init__(self, char_id): self.char_id = char_id try: self.mongo_hs = MongoHeroSoul.objects.get(id=self.char_id) except DoesNotExist: self.mongo_hs = MongoHeroSoul(id=self.char_id) self.mongo_hs.souls = {} self.mongo_hs.save()
class HeroSoul(object): def __init__(self, char_id): self.char_id = char_id try: self.mongo_hs = MongoHeroSoul.objects.get(id=self.char_id) except DoesNotExist: self.mongo_hs = MongoHeroSoul(id=self.char_id) self.mongo_hs.souls = {} self.mongo_hs.save() def soul_amount(self, _id): return self.mongo_hs.souls.get(str(_id), 0) def has_soul(self, _id, amount=1): return self.soul_amount(_id) >= amount def add_soul(self, souls): new_souls = [] update_souls = [] for _id, amount in souls: str_id = str(_id) if str_id in self.mongo_hs.souls: self.mongo_hs.souls[str_id] += amount update_souls.append((_id, self.mongo_hs.souls[str_id])) else: self.mongo_hs.souls[str_id] = amount new_souls.append((_id, amount)) self.mongo_hs.save() if new_souls: msg = protomsg.AddHeroSoulNotify() for _id, amount in new_souls: s = msg.herosouls.add() s.id = _id s.amount = amount publish_to_char(self.char_id, pack_msg(msg)) if update_souls: msg = protomsg.UpdateHeroSoulNotify() for _id, amount in update_souls: s = msg.herosouls.add() s.id = _id s.amount = amount publish_to_char(self.char_id, pack_msg(msg)) def remove_soul(self, souls): remove_souls = [] update_souls = [] for _id, amount in souls: if not self.has_soul(_id, amount): raise SanguoException( errormsg.SOUL_NOT_ENOUGH, self.char_id, "HeroSoul Remove", "HeroSoul {0} not enough/exist, expected amount {1}". format(_id, amount)) for _id, amount in souls: str_id = str(_id) self.mongo_hs.souls[str_id] -= amount if self.mongo_hs.souls[str_id] <= 0: remove_souls.append(_id) self.mongo_hs.souls.pop(str_id) else: update_souls.append((_id, self.mongo_hs.souls[str_id])) self.mongo_hs.save() if remove_souls: msg = protomsg.RemoveHeroSoulNotify() msg.ids.extend(remove_souls) publish_to_char(self.char_id, pack_msg(msg)) if update_souls: msg = protomsg.UpdateHeroSoulNotify() for _id, amount in update_souls: s = msg.herosouls.add() s.id = _id s.amount = amount publish_to_char(self.char_id, pack_msg(msg)) def purge_soul(self, _id): self.mongo_hs.souls.pop(str(_id)) self.mongo_hs.save() msg = protomsg.RemoveHeroSoulNotify() msg.ids.append(_id) publish_to_char(self.char_id, pack_msg(msg)) def send_notify(self): msg = protomsg.HeroSoulNotify() for _id, amount in self.mongo_hs.souls.iteritems(): s = msg.herosouls.add() s.id = int(_id) s.amount = amount publish_to_char(self.char_id, pack_msg(msg))
class HeroSoul(object): def __init__(self, char_id): self.char_id = char_id try: self.mongo_hs = MongoHeroSoul.objects.get(id=self.char_id) except DoesNotExist: self.mongo_hs = MongoHeroSoul(id=self.char_id) self.mongo_hs.souls = {} self.mongo_hs.save() def soul_amount(self, _id): return self.mongo_hs.souls.get(str(_id), 0) def has_soul(self, _id, amount=1): return self.soul_amount(_id) >= amount def add_soul(self, souls): new_souls = [] update_souls = [] for _id, _ in souls: if _id not in HEROS: raise RuntimeError("soul {0} not exist".format(_id)) for _id, amount in souls: str_id = str(_id) if str_id in self.mongo_hs.souls: self.mongo_hs.souls[str_id] += amount update_souls.append((_id, self.mongo_hs.souls[str_id])) else: self.mongo_hs.souls[str_id] = amount new_souls.append((_id, amount)) self.mongo_hs.save() if new_souls: msg = protomsg.AddHeroSoulNotify() for _id, amount in new_souls: s = msg.herosouls.add() s.id = _id s.amount = amount publish_to_char(self.char_id, pack_msg(msg)) if update_souls: msg = protomsg.UpdateHeroSoulNotify() for _id, amount in update_souls: s = msg.herosouls.add() s.id = _id s.amount = amount publish_to_char(self.char_id, pack_msg(msg)) def remove_soul(self, souls): remove_souls = [] update_souls = [] for _id, amount in souls: if not self.has_soul(_id, amount): raise SanguoException( errormsg.SOUL_NOT_ENOUGH, self.char_id, "HeroSoul Remove", "HeroSoul {0} not enough/exist, expected amount {1}".format(_id, amount) ) for _id, amount in souls: str_id = str(_id) self.mongo_hs.souls[str_id] -= amount if self.mongo_hs.souls[str_id] <= 0: remove_souls.append(_id) self.mongo_hs.souls.pop(str_id) else: update_souls.append((_id, self.mongo_hs.souls[str_id])) self.mongo_hs.save() if remove_souls: msg = protomsg.RemoveHeroSoulNotify() msg.ids.extend(remove_souls) publish_to_char(self.char_id, pack_msg(msg)) if update_souls: msg = protomsg.UpdateHeroSoulNotify() for _id, amount in update_souls: s = msg.herosouls.add() s.id = _id s.amount = amount publish_to_char(self.char_id, pack_msg(msg)) def purge_soul(self, _id): self.mongo_hs.souls.pop(str(_id)) self.mongo_hs.save() msg = protomsg.RemoveHeroSoulNotify() msg.ids.append(_id) publish_to_char(self.char_id, pack_msg(msg)) def send_notify(self): msg = protomsg.HeroSoulNotify() for _id, amount in self.mongo_hs.souls.iteritems(): s = msg.herosouls.add() s.id = int(_id) s.amount = amount publish_to_char(self.char_id, pack_msg(msg))