def clean(signum): logger = Logger("clean_union.log") logger.write("Start") try: UnionBattle.cron_job() Member.cron_job() except: logger.error(traceback.format_exc()) else: logger.write("Clean Union Complete.") finally: logger.close()
def create(self, name): if len(name) > UNION_NAME_MAX_LENGTH: raise SanguoException( errormsg.UNION_NAME_TOO_LONG, self.char_id, "Union Create", "name too long: {0}".format(name.encode('utf-8'))) if MongoUnion.objects.filter(name=name).count() > 0: raise SanguoException( errormsg.UNION_NAME_ALREADY_EXIST, self.char_id, "Union Create", "name already exist: {0}".format(name.encode('utf-8'))) resource = Resource(self.char_id, "Union Create") with resource.check(sycee=-UNION_CREATE_NEEDS_SYCEE): new_id = id_generator('union')[0] mu = MongoUnion(id=new_id) mu.owner = self.char_id mu.name = name mu.bulletin = UNION_DEFAULT_DES mu.level = 1 mu.contribute_points = 0 mu.score = get_battle_init_score() mu.save() Member(self.char_id).join_union(new_id) send_notify(self.char_id)
def get_joined_union(request): char_id = int(request.POST['char_id']) union = Union(char_id) if isinstance(union, UnionDummy): data = None else: m = Member(char_id) data = { 'char_id': char_id, 'union': union.union_id, 'union_owner': union.mongo_union.owner, 'union_name': union.mongo_union.name, 'union_bulletin': union.mongo_union.bulletin, 'union_level': union.mongo_union.level, 'union_contribute_points': union.mongo_union.contribute_points, 'union_score': union.mongo_union.score, 'member_coin': m.mongo_union_member.coin, 'member_contribute_points': m.mongo_union_member.contribute_points, 'member_position': m.mongo_union_member.position, 'member_checkin_times': m.mongo_union_member.checkin_times, 'member_buy_buff_times': m.mongo_union_member.buy_buff_times, 'member_boss_times': m.mongo_union_member.boss_times } return {'ret': 0, 'data': data}
def checkin(request): char_id = request._char_id drop_msg = Member(char_id).checkin() response = UnionCheckinResponse() response.ret = 0 response.drop.MergeFrom(drop_msg) return pack_msg(response)
def refuse_join(self, char_id): # 拒绝 from core.union.member import Member m = Member(char_id) if self.union_id in m.mongo_union_member.applied: m.mongo_union_member.applied.remove(self.union_id) m.mongo_union_member.save() self.send_apply_list_notify() UnionList.send_list_notify(char_id)
def kick_member(self, member_id): # 踢人 from core.union.member import Member if not self.is_member(member_id): raise SanguoException( errormsg.INVALID_OPERATE, self.char_id, "Union Kick Member", "char {0} is not member of union {1}".format( member_id, self.union_id)) Member(member_id).quit_union() self.send_notify() Union(member_id, self.union_id).send_notify()
def after_battle(self, boss_id, damage, kill=False): # 每次打完给予奖励 member = Member(self.char_id) boss = UNION_BOSS[boss_id] contribute_points = int( float(damage)/boss.hp * boss.contribute_points ) lowest = 1 highest = int(boss.contribute_points * 0.05) if contribute_points < lowest: contribute_points = lowest if contribute_points > highest: contribute_points = highest coin = 9 + contribute_points member.add_coin(coin, send_notify=False) member.add_contribute_points(contribute_points, send_notify=True) if not kill: self.union.add_contribute_points(contribute_points) drop = make_standard_drop_from_template() drop['union_coin'] = coin drop['union_contribute_points'] = contribute_points return standard_drop_to_attachment_protomsg(drop)
def send_notify(self, to_owner=False): from core.union.member import Member msg = protomsg.UnionNotify() msg.in_union = True msg.union.MergeFrom(self.make_basic_information()) msg.leader = self.mongo_union.owner for mid in self.member_list: msg_member = msg.members.add() msg_member.MergeFrom(Member(mid).make_member_message()) if to_owner: char_id = self.mongo_union.owner else: char_id = self.char_id publish_to_char(char_id, pack_msg(msg))
def add_member(self, char_id): # 添加成员 from core.union.member import Member if self.current_member_amount >= self.max_member_amount: raise SanguoException( errormsg.UNION_CANNOT_JOIN_FULL, self.char_id, "Union add member", "full. {0} >= {1}".format(self.current_member_amount, self.max_member_amount)) if not isinstance(Union(char_id), UnionDummy): raise SanguoException(errormsg.UNION_CANNOT_AGREE_JOIN, self.char_id, "Union Apply Join", "char {0} already in union".format(char_id)) Member(char_id).join_union(self.union_id) self.send_notify() Union(char_id).send_notify() UnionList.send_list_notify(char_id)
def send_notify(char_id): from core.union.battle import UnionBattle from core.union.boss import UnionBoss from core.union.store import UnionStore from core.union.union import Union, UnionList, UnionOwner from core.union.member import Member # 工会列表 UnionList.send_list_notify(char_id) # 个人信息 Member(char_id).send_personal_notify() u = Union(char_id) # UnionNotify u.send_notify() if isinstance(u, UnionOwner): # 会长才能看见的申请者列表 u.send_apply_list_notify() # 商店 UnionStore(char_id).send_notify() # 工会战 UnionBattle(char_id).send_notify()
def after_battle(self, boss_id, damage, kill=False): # 每次打完给予奖励 member = Member(self.char_id) boss = UNION_BOSS[boss_id] contribute_points = int( float(damage) / boss.hp * boss.contribute_points) lowest = 1 highest = int(boss.contribute_points * 0.05) if contribute_points < lowest: contribute_points = lowest if contribute_points > highest: contribute_points = highest coin = 9 + contribute_points member.add_coin(coin, send_notify=False) member.add_contribute_points(contribute_points, send_notify=True) if not kill: self.union.add_contribute_points(contribute_points) drop = make_standard_drop_from_template() drop['union_coin'] = coin drop['union_contribute_points'] = contribute_points return standard_drop_to_attachment_protomsg(drop)
def __init__(self, char_id): super(UnionStore, self).__init__(char_id) self.member = Member(char_id)
class UnionStore(UnionLoadBase): def __init__(self, char_id): super(UnionStore, self).__init__(char_id) self.member = Member(char_id) def get_add_buffs_with_resource_id(self): cur_buy_times = self.buff_cur_buy_times def _get_value(buff_id): store_id = UNION_STORE_BUFF_STORE_ID_DICT[buff_id] value = UNION_STORE[store_id].value * cur_buy_times[buff_id] return value return {k: _get_value(k) for k in BUFFS} def get_add_buffs_with_string_name(self): buffs = self.get_add_buffs_with_resource_id() return {BUFF_NAME_TABLE[k]: v for k, v in buffs.items()} @property def buff_max_buy_times(self): return UNION_LEVEL[self.union.mongo_union.level].buff_max_buy_times @property def buff_cur_buy_times(self): cur_times = {} for i in BUFFS: cur_times[i] = self.member.mongo_union_member.buy_buff_times.get( str(i), 0) return cur_times @property def buff_cur_buy_cost(self): times = self.buff_cur_buy_times costs = {} for i in BUFFS: costs[i] = 20 + (times[i] + 1) * 20 return costs @union_instance_check(UnionBase, errormsg.UNION_NOT_EXIST, "UnionStore Buy", "has no union") def buy(self, _id, amount): try: item = UNION_STORE[_id] except KeyError: raise SanguoException(errormsg.INVALID_OPERATE, "UnionStore Buy", "item {0} not exist".format(_id)) if item.tp in BUFFS: cost_coin = self.buff_cur_buy_cost[item.tp] else: cost_coin = item.union_coin self.member.check_coin(cost_coin, raise_exception=True, func_name="UnionStore Buy") if item.tp in BUFFS: self._buy_buff(_id, item.tp, amount) elif item.tp == 10: self._buy_horse(_id, item.value, amount) else: self._buy_items(_id, item.value, amount) self.member.cost_coin(cost_coin) def _buy_buff(self, _id, item_id, amount): cur_buy_times = self.buff_cur_buy_times max_buy_times = self.buff_max_buy_times if cur_buy_times[item_id] + amount > max_buy_times: raise SanguoException( errormsg.UNION_STORE_BUY_REACH_MAX_TIMES, self.char_id, "UnionStore Buy", "buff {0} has reached the max buy times {1}".format( item_id, max_buy_times)) self.member.mongo_union_member.buy_buff_times[str( item_id)] = cur_buy_times[item_id] + amount self.member.mongo_union_member.save() self.send_notify() global_buff_changed_signal.send(sender=None, char_id=self.char_id) def _buy_horse(self, _id, item_id, amount): if item_id not in HORSE: raise SanguoException(errormsg.INVALID_OPERATE, self.char_id, "UnionStore Buy", "horse {0} not exist".format(item_id)) resources = Resource(self.char_id, "UnionStore Buy") resources.add(horses=[(item_id, amount)]) def _buy_items(self, _id, item_id, amount): if item_id not in STUFFS: raise SanguoException(errormsg.INVALID_OPERATE, self.char_id, "UnionStore Buy", "stuff {0} not exist".format(item_id)) resources = Resource(self.char_id, "UnionStore Buy") resources.add(stuffs=[(item_id, amount)]) @union_instance_check(UnionBase, None, "UnionStore Send Notify") def send_notify(self): msg = protomsg.UnionStoreNotify() max_times = self.buff_max_buy_times cur_times = self.buff_cur_buy_times buy_cost = self.buff_cur_buy_cost add_buffs = self.get_add_buffs_with_resource_id() for i in BUFFS: msg_buff = msg.buffs.add() msg_buff.id = i msg_buff.max_times = max_times msg_buff.cur_times = cur_times[i] msg_buff.add_value = add_buffs[i] msg_buff.cost = buy_cost[i] publish_to_char(self.char_id, pack_msg(msg))
def add(self, **kwargs): from core.character import Char from core.hero import save_hero, HeroSoul, FakeSaveHeroResult from core.item import Item from core.horse import Horse from core.union.member import Member notify_settings = {} notify_settings['purchase_notify'] = kwargs.get('purchase_notify', True) data = _get_resource_data(**kwargs) purchase_got = kwargs.get('purchase_got', 0) purchase_actual_got = kwargs.get('purchase_actual_got', 0) if data['gold'] or data['sycee'] or data['exp'] or data['official_exp'] or purchase_got: char = Char(self.char_id) char.update(gold=data['gold'], sycee=data['sycee'], exp=data['exp'], official_exp=data['official_exp'], purchase_got=purchase_got, purchase_actual_got=purchase_actual_got, notify_settings=notify_settings ) union_coin = kwargs.get('union_coin', 0) union_contribute_points = kwargs.get('union_contribute_points', 0) if union_coin or union_contribute_points: m = Member(self.char_id) m.add_coin(union_coin, send_notify=False) m.add_contribute_points(union_contribute_points) if data['heros']: heros = [] for _id, _amount in data['heros']: heros.extend([_id] * _amount) sh_res = save_hero(self.char_id, heros) else: sh_res = FakeSaveHeroResult if data['souls']: hs = HeroSoul(self.char_id) hs.add_soul(data['souls']) item = Item(self.char_id) for _id, _level, _amount in data['equipments']: for i in range(_amount): item.equip_add(_id, _level) if data['gems']: item.gem_add(data['gems']) if data['stuffs']: item.stuff_add(data['stuffs']) if data['horses']: horse = Horse(self.char_id) for _id, _amount in data['horses']: for i in range(_amount): horse.add(_id) # normalize the data if data['heros']: data['heros'] = sh_res.actual_heros souls = dict(data['souls']) for _sid, _samount in sh_res.to_souls: souls[_sid] = souls.get(_sid, 0) + _samount data['souls'] = souls.items() data['income'] = 1 data['func_name'] = self.func_name data['des'] = self.des resource_logger(self.char_id, data) data.pop('income') data.pop('func_name') data.pop('des') return data
def __init__(self, char_id): super(UnionBoss, self).__init__(char_id) if isinstance(self.union, UnionBase): self.load_data() self.member = Member(self.char_id)
class UnionStore(UnionLoadBase): def __init__(self, char_id): super(UnionStore, self).__init__(char_id) self.member = Member(char_id) def get_add_buffs_with_resource_id(self): cur_buy_times = self.buff_cur_buy_times def _get_value(buff_id): store_id = UNION_STORE_BUFF_STORE_ID_DICT[buff_id] value = UNION_STORE[store_id].value * cur_buy_times[buff_id] return value return {k: _get_value(k) for k in BUFFS} def get_add_buffs_with_string_name(self): buffs = self.get_add_buffs_with_resource_id() return {BUFF_NAME_TABLE[k]: v for k, v in buffs.items()} @property def buff_max_buy_times(self): return UNION_LEVEL[self.union.mongo_union.level].buff_max_buy_times @property def buff_cur_buy_times(self): cur_times = {} for i in BUFFS: cur_times[i] = self.member.mongo_union_member.buy_buff_times.get(str(i), 0) return cur_times @property def buff_cur_buy_cost(self): times = self.buff_cur_buy_times costs = {} for i in BUFFS: costs[i] = 20 + (times[i]+1) * 20 return costs @union_instance_check(UnionBase, errormsg.UNION_NOT_EXIST, "UnionStore Buy", "has no union") def buy(self, _id, amount): try: item = UNION_STORE[_id] except KeyError: raise SanguoException( errormsg.INVALID_OPERATE, "UnionStore Buy", "item {0} not exist".format(_id) ) if item.tp in BUFFS: cost_coin = self.buff_cur_buy_cost[item.tp] else: cost_coin = item.union_coin self.member.check_coin(cost_coin, raise_exception=True, func_name="UnionStore Buy") if item.tp in BUFFS: self._buy_buff(_id, item.tp, amount) elif item.tp == 10: self._buy_horse(_id, item.value, amount) else: self._buy_items(_id, item.value, amount) self.member.cost_coin(cost_coin) def _buy_buff(self, _id, item_id, amount): cur_buy_times = self.buff_cur_buy_times max_buy_times = self.buff_max_buy_times if cur_buy_times[item_id] + amount > max_buy_times: raise SanguoException( errormsg.UNION_STORE_BUY_REACH_MAX_TIMES, self.char_id, "UnionStore Buy", "buff {0} has reached the max buy times {1}".format(item_id, max_buy_times) ) self.member.mongo_union_member.buy_buff_times[str(item_id)] = cur_buy_times[item_id] + amount self.member.mongo_union_member.save() self.send_notify() global_buff_changed_signal.send( sender=None, char_id=self.char_id ) def _buy_horse(self, _id, item_id, amount): if item_id not in HORSE: raise SanguoException( errormsg.INVALID_OPERATE, self.char_id, "UnionStore Buy", "horse {0} not exist".format(item_id) ) resources = Resource(self.char_id, "UnionStore Buy") resources.add(horses=[(item_id, amount)]) def _buy_items(self, _id, item_id, amount): if item_id not in STUFFS: raise SanguoException( errormsg.INVALID_OPERATE, self.char_id, "UnionStore Buy", "stuff {0} not exist".format(item_id) ) resources = Resource(self.char_id, "UnionStore Buy") resources.add(stuffs=[(item_id, amount)]) @union_instance_check(UnionBase, None, "UnionStore Send Notify") def send_notify(self): msg = protomsg.UnionStoreNotify() max_times = self.buff_max_buy_times cur_times = self.buff_cur_buy_times buy_cost = self.buff_cur_buy_cost add_buffs = self.get_add_buffs_with_resource_id() for i in BUFFS: msg_buff = msg.buffs.add() msg_buff.id = i msg_buff.max_times = max_times msg_buff.cur_times = cur_times[i] msg_buff.add_value = add_buffs[i] msg_buff.cost = buy_cost[i] publish_to_char(self.char_id, pack_msg(msg))
def quit(self): # 主动退出 from core.union.member import Member Member(self.char_id).quit_union() Union(self.char_id).send_notify()
def cmd(request): req = request._proto char_id = request._char_id if req.action == 2: print "Unsupported" return HttpResponse('', content_type='text/plain') drop = make_standard_drop_from_template() if req.tp == 1: drop['exp'] = req.param elif req.tp == 2: drop['official_exp'] = req.param elif req.tp == 3: drop['gold'] = req.param elif req.tp == 4: drop['sycee'] = req.param elif req.tp == 5: if req.param not in EQUIPMENTS: return HttpResponse('', content_type='text/plain') drop['equipments'].append((req.param, 1, 1)) elif req.tp == 6: if req.param not in GEMS: return HttpResponse('', content_type='text/plain') drop['gems'].append((req.param, 1)) elif req.tp == 7: if req.param not in HEROS: return HttpResponse('', content_type='text/plain') drop['heros'].append((req.param, 1)) elif req.tp == 8: if req.param not in STUFFS: return HttpResponse('', content_type='text/plain') drop['stuffs'].append((req.param, 1)) elif req.tp == 9: if req.param not in HEROS: return HttpResponse('', content_type='text/plain') drop['souls'].append((req.param, 1)) elif req.tp == 10: drop['purchase_got'] = req.param drop['purchase_actual_got'] = req.param elif req.tp == 12: if req.param not in HORSE: return HttpResponse('', content_type='text/plain') drop['horses'].append((req.param, 1)) elif req.tp == 13: Member(char_id).add_coin(req.param) resource = Resource(char_id, "CMD", "tp: {0}, param: {1}".format(req.tp, req.param)) standard_drop = resource.add(**drop) print standard_drop if req.tp == 11: fo = FunctionOpen(char_id) fo._open_all() fo.send_notify() return HttpResponse('', content_type='text/plain')
def add(self, **kwargs): from core.character import Char from core.hero import save_hero, HeroSoul, FakeSaveHeroResult from core.item import Item from core.horse import Horse from core.union.member import Member update_settings = {} update_settings['purchase_notify'] = kwargs.get( 'purchase_notify', True) update_settings['as_vip_exp'] = kwargs.get('as_vip_exp', True) data = _get_resource_data(**kwargs) purchase_got = kwargs.get('purchase_got', 0) purchase_actual_got = kwargs.get('purchase_actual_got', 0) if data['gold'] or data['sycee'] or data['exp'] or data[ 'official_exp'] or purchase_got: char = Char(self.char_id) char.update(gold=data['gold'], sycee=data['sycee'], exp=data['exp'], official_exp=data['official_exp'], purchase_got=purchase_got, purchase_actual_got=purchase_actual_got, update_settings=update_settings) union_coin = kwargs.get('union_coin', 0) union_contribute_points = kwargs.get('union_contribute_points', 0) if union_coin or union_contribute_points: m = Member(self.char_id) m.add_coin(union_coin, send_notify=False) m.add_contribute_points(union_contribute_points) if data['heros']: heros = [] for _id, _amount in data['heros']: heros.extend([_id] * _amount) sh_res = save_hero(self.char_id, heros) else: sh_res = FakeSaveHeroResult if data['souls']: hs = HeroSoul(self.char_id) hs.add_soul(data['souls']) item = Item(self.char_id) for _id, _level, _amount in data['equipments']: for i in range(_amount): item.equip_add(_id, _level) if data['gems']: item.gem_add(data['gems']) if data['stuffs']: item.stuff_add(data['stuffs']) if data['horses']: horse = Horse(self.char_id) for _id, _amount in data['horses']: for i in range(_amount): horse.add(_id) # normalize the data if data['heros']: data['heros'] = sh_res.actual_heros souls = dict(data['souls']) for _sid, _samount in sh_res.to_souls: souls[_sid] = souls.get(_sid, 0) + _samount data['souls'] = souls.items() data['income'] = 1 data['func_name'] = self.func_name data['des'] = self.des resource_logger(self.char_id, data) data.pop('income') data.pop('func_name') data.pop('des') return data
def apply_join(request): req = request._proto char_id = request._char_id Member(char_id).apply_union(req.id) return None