def give_reward_by_mail(p): now = int(time.time()) if not p.groupID: return g = Group.simple_load( p.groupID, ["name", "gve_rewards", "members", "gve_activateds_detail"]) g.gve_rewards.load() g.gve_activateds_detail.load() if p.entityID not in g.gve_rewards: return rewards, lock, addition = g.gve_rewards[p.entityID] detail = g.gve_activateds_detail.get(p.entityID, {}) addition = detail.get("addition", 0) score = detail.get("score", 0) damage = detail.get("damage", 0) total_damage = detail.get("total_damage", 0) title, content, ID = get_mail("GroupBaseReward") content = content.format(g.name, total_damage, damage, score, addition) if now >= lock: for reward in rewards: reward["count"] = int(reward["count"] * (1 + addition / float(100))) rewards = parse_reward(rewards) if rewards: send_mail(p.entityID, title, content, addition=rewards, configID=ID) del g.gve_rewards[p.entityID] g.members.load() member = g.members[p.entityID] member["intimate"] = member.get("intimate", 0) + 10 g.members[p.entityID] = member g.save()
def reset_player_gve(p): if not p.groupID: return now = int(time.time()) g = Group.simple_load(p.groupID, ["gve_end_cd"]) flag = False if g_campaignManager.gve_campaign.is_open(): if p.gve_last_reset_time < \ g_campaignManager.gve_campaign.get_start_time() or \ p.gve_last_reset_time > \ g_campaignManager.gve_campaign.get_end_time(): flag = True ts = now elif gve_is_end(p) and p.gve_last_reset_time < g.gve_end_cd: flag = True ts = g.gve_end_cd elif p.gve_last_reset_time < g.gve_end_cd: flag = True ts = g.gve_end_cd if flag: logger.info("%d reseted", p.entityID) p.gve_damage = 0 p.gve_score = 0 # p.gve_index = 0 p.gve_target = 0 p.gve_state = GveEntity.GveEntityStateNormal p.gve_addition = 0 p.gve_groupdamage = 0 p.gve_buff = 0 # gve_basereward p.gve_reborn_rest_count = 1 # TODO p.gve_last_reset_time = ts p.save()
def quit_group(entityID, groupID): g = Group.simple_load(groupID, ["leaderID", "members", "invites"]) if not g: return msgTips.FAIL_MSG_GROUP_NOT_IN_THIS g.members.load() g.invites.load() p = g_entityManager.get_player(entityID) if not p: p = Player.simple_load(entityID, ["groupID", "group_last_kicked_time"]) if not p.groupID: return msgTips.FAIL_MSG_GROUP_HAS_NOT_JOINED # "未加入同门了" if p.entityID not in g.members: return msgTips.FAIL_MSG_GROUP_NOT_IN_THIS del g.members[p.entityID] if p.entityID in g.invites: g.invites.remove(p.entityID) if p.entityID == g.leaderID: rest = sorted(g.members.items(), key=lambda v: v[1]["jointime"]) if rest: new_leaderID, _ = rest[0] g.leaderID = new_leaderID recommend(groupID) g.save() p.groupID = 0 now = int(time.time()) p.group_last_kicked_time = now p.save() return SUCCESS
def get_group_info(groupID): g = Group.simple_load(groupID, ["name", "leaderID", "members"]) if not g: return {} g.members.load() intimate = 0 members = OrderedDict( sorted(g.members.items(), key=lambda v: (v[0] == g.leaderID) or v[1]["jointime"])) memberlist = [] for entityID, member in members.items(): member.setdefault("intimate", 0) intimate += member["intimate"] member.update(get_opponent_detail(entityID)) members[entityID] = member memberlist.append(member) rs = { "groupID": g.groupID, "name": g.name, "members": memberlist, "membercount": len(members), "intimate": intimate, "leaderID": g.leaderID } return rs
def give_ranking_reward(sdate=None): if sdate: date = datetime.strptime(sdate, "%Y-%m-%d").date() else: date = datedate.today() sdate = datetime.strftime(date, "%Y-%m-%d") sceneID = get_sceneID(date=date) url = "/".join( [urljoin(PROXY["host"], PROXY["port"]), "give_gve_ranking_reward"]) pending = [] group = get_config(GveRankingRewardBySceneIDConfig).get(sceneID) configs = get_config(GveRankingRewardConfig) for rank, (regionID, groupID, score) in enumerate( get_group_ranking(sceneID, count=50, date=date), 1): for config in group: config = configs.get(config.ID) start, end = config.range if rank >= start and rank <= end: g = Group.simple_load(groupID, ["name", "members"]) g.members.load() data = { "group_name": encode_utf8(g.name), "group_damage": score, "group_rank": rank, "configID": config.ID, "date": sdate, } for entityID in g.members: uri = "/".join([url, str(entityID)]) uri = set_query_string(uri, regionID=regionID) pending.append([uri, data]) for uri, data in pending: get_poolmanager().urlopen("POST", uri, body=urlencode(data))
def join_group(entityID, groupID): now = int(time.time()) p = g_entityManager.get_player(entityID) if not p: p = Player.simple_load(entityID, ["groupID", "group_last_kicked_time"]) if p.groupID: return msgTips.FAIL_MSG_GROUP_ALREADY_JOINED if now < p.group_last_kicked_time + ONE_DAY: return msgTips.FAIL_MSG_GROUP_KICKED_RECENTLY g = Group.simple_load(groupID, ["members", "applys"]) g.members.load() g.applys.load() if len(g.members) >= MAX_MEMBERS_COUNT: return msgTips.FAIL_MSG_GROUP_LIMITED_EXCEED if p.entityID in g.members: return msgTips.FAIL_MSG_GROUP_ALREADY_JOINED_THIS g.members[p.entityID] = {"jointime": now, "intimate": 0} if len(g.members) >= MAX_MEMBERS_COUNT: unrecommend(groupID) g.applys.clear() else: recommend(groupID) try: del g.applys[p.entityID] except KeyError: pass g.save() p.groupID = groupID p.save() return SUCCESS
def gve_group_ranking_reward(self, msgtype, body): p = self.player req = poem_pb.GroupRankingRewardRequest() req.ParseFromString(body) sceneID = req.sceneID if not sceneID: sceneID = get_sceneID() damage = get_group_score(sceneID, p.groupID) rsp = poem_pb.GroupRankingReward() rank = get_group_rank(sceneID, p.groupID) g = Group.simple_load( p.groupID, ["gve_max_damage"]) maxdamage = g.gve_max_damage rsp.damage = damage if damage: rsp.rank = rank rsp.maxdamage = maxdamage configs = get_config(GveRankingRewardConfig) sceneID = get_sceneID() for k, v in configs.items(): if v.sceneID != sceneID: continue start, final = v.range rsp.items.add( rewards=v.rewards, start=start, final=final) return success_msg(msgtype, rsp)
def gve_reborn(self, msgtype, body): p = self.player if not gve_is_open(p): return fail_msg(msgtype, msgTips.FAIL_MSG_GROUP_GVE_NOT_OPEN) if gve_is_end(p): return fail_msg(msgtype, msgTips.FAIL_MSG_GROUP_GVE_ALREADY_END) end_time = g_campaignManager.gve_campaign.get_end_time() if p.gve_state != GveEntity.GveEntityStateDead: return fail_msg(msgtype, reason="没有死亡") if p.gve_reborn_rest_count <= 0: return fail_msg(msgtype, reason="没有复活次数了") try: apply_reward( p, {}, cost={"gold": get_cons_value("GveRebornCost")}, type=RewardType.GveReborn) except AttrNotEnoughError: return fail_msg(msgtype, reason="钻石不足") count = int(Group.pool.execute( "SREM", "gve_deads_g{%s}" % p.groupID, p.entityID)) g = Group.simple_load( p.groupID, ["gve_end_cd", "gve_activateds"]) g.gve_activateds.load() if count < len(g.gve_activateds): g.gve_end_cd = end_time g.save() p.gve_state = GveEntity.GveEntityStateNormal p.gve_buff = get_cons_value("GveBuffAddition") p.gve_reborn_rest_count -= 1 sync_gve_entity(g.gve_activateds, p.entityID) p.save() p.sync() return success_msg(msgtype, "")
def group_apply_list(self, msgtype, body): p = self.player g = Group.simple_load(p.groupID, ["leaderID"]) if p.entityID != g.leaderID: return fail_msg(msgtype, msgTips.FAIL_MSG_GROUP_PERMISSION_DENIED) rsp = poem_pb.GroupApplyList() for i in p.group_applys or []: info = get_opponent_detail(i) rsp.members.add(**info) return success_msg(msgtype, rsp)
def kick_group_member_offline(entityID, groupID): rs = quit_group(entityID, groupID) dt = datetime.now() g = Group.simple_load(groupID, ["name"]) title, content, ID = get_mail("GroupKick") send_mail(entityID, title, content.format(dt.strftime(DATETIMEFMT), g.name), configID=ID) return rs
def update_gve_entity_index(entityID): p = g_entityManager.get_player(entityID) if p: g = Group.simple_load(p.groupID, ["gve_joineds"]) if g: g.gve_joineds.load() try: p.gve_index = g.gve_joineds.index(entityID) p.save() except ValueError: pass
def gve_group_is_end(groupID): if gve_group_is_open(groupID): if gve_group_is_started(groupID): # 活动开始了,则如果当前大于活动结束时间,则活动已经结束 now = int(time.time()) g = Group.simple_load(groupID, ["gve_end_cd"]) if not g.gve_end_cd: return False return now >= g.gve_end_cd # 活动未开启,则不可能已经结束 return False
def gve_group_is_started(groupID): if g_campaignManager.gve_campaign.is_open(): g = Group.simple_load(groupID, ["gve_start_time"]) if not g.gve_start_time: return False if g.gve_start_time >= \ g_campaignManager.gve_campaign.get_start_time() and\ g.gve_start_time <= \ g_campaignManager.gve_campaign.get_end_time(): return True return False
def group_invite(self, msgtype, body): p = self.player req = poem_pb.GroupInvite() req.ParseFromString(body) err = proxy.invite_group_member(req.entityID, p.groupID, p.name) if err: return fail_msg(msgtype, err) g = Group.simple_load(p.groupID, "invites") g.invites.load() g.invites.add(req.entityID) g.save() return success_msg(msgtype, "")
def group_apply(self, msgtype, body): p = self.player req = poem_pb.GroupApply() req.ParseFromString(body) g = Group.simple_load(req.groupID, ["leaderID"]) now = int(time.time()) if now < p.group_last_kicked_time + ONE_DAY: return fail_msg(msgtype, msgTips.FAIL_MSG_GROUP_KICKED_RECENTLY) err = proxy.apply_group_member(g.leaderID, req.groupID, p.entityID) if err: return fail_msg(msgtype, err) return success_msg(msgtype, "")
def kick_group_member(entityID, groupID): rs = quit_group(entityID, groupID) dt = datetime.now() g = Group.simple_load(groupID, ["name"]) title, content, ID = get_mail("GroupKick") send_mail(entityID, title, content.format(dt.strftime(DATETIMEFMT), g.name), configID=ID) p = g_entityManager.get_player(entityID) if p: p.sync() return rs
def leave_gve(p): if not p.groupID: return g = Group.simple_load(p.groupID, ["gve_joineds", "gve_activateds"]) g.gve_joineds.load() g.gve_activateds.load() if p.entityID not in g.gve_joineds: return if p.entityID not in g.gve_activateds: return set_gve_state(p, GveEntity.GveEntityStateLeave) sync_gve_entity(g.gve_joineds, p.entityID) p.save()
def update_group_damage(sceneID, groupID, damage, date=None): if not date: date = datedate.today() key = get_group_ranking_key(sceneID, date=date) g = Group.simple_load(groupID, ["gve_max_damage"]) groupID = get_group_key(groupID) pool = settings.REDISES["index"] with pool.ctx() as conn: damage = int(conn.execute("ZINCRBY", key, damage, groupID)) if damage > g.gve_max_damage: g.gve_max_damage = damage g.save() return damage
def broadcast_group_message(m): msg = success_msg(msgid.RECV_MESSAGE, m) from entity.manager import g_entityManager p = g_entityManager.get_player(m.entityID) if not p: p = Player.simple_load(m.entityID, ["groupID"]) from player.manager import g_playerManager from group.model import Group if p.groupID: g_chatManager.cache_message(m, groupID=p.groupID) g = Group.simple_load(p.groupID, ["members"]) g.members.load() if g: g_playerManager.broadcast(list(g.members), msg)
def get_group_across_region(regionID, groupID): assert regionID == settings.REGION["ID"] g = Group.simple_load(groupID, ["name", "leaderID", "members"]) g.members.load() members = [] for k in g.members: p = Player.simple_load(k, ["prototypeID"]) members.append({"entityID": p.entityID, "prototypeID": p.prototypeID}) return { "name": g.name, "groupID": groupID, "regionID": regionID, "members": members, }
def load_group(self, now=None): from group.model import Group if self.groupID: g = Group.simple_load( self.groupID, ["leaderID", "applys", "lastlogin"]) g.applys.load() if not now: now = datetime.now() if g.lastlogin and (now - g.lastlogin).days >= 5: g.leaderID = self.entityID if g.leaderID == self.entityID: self.group_applys = g.applys g.lastlogin = self.lastlogin g.save() return
def invite_group_member_offline(entityID, groupID, name): now = int(time.time()) g = Group.simple_load(groupID, ["invites", "gve_last_kick_time"]) if now < g.gve_last_kick_time + ONE_DAY: return msgTips.FAIL_MSG_GROUP_KICK_TOO_FREQUENTLY_TO_INVITED g.invites.load() if entityID in g.invites: return msgTips.FAIL_MSG_GROUP_ALREADY_INVITED # 已经邀请过了 title, content, ID = get_mail("GroupInvite") send_mail(entityID, title, content.format(name, groupID), cd=ONE_DAY, configID=ID) return SUCCESS
def gve_join(self, msgtype, body): p = self.player sceneID = get_sceneID() g = Group.simple_load(p.groupID, ["gve_joineds", "gve_activateds"]) # reset_gve(g.groupID) g.gve_joineds.load() if p.entityID not in g.gve_joineds: g.gve_joineds.append(p.entityID) g.save() joiners = [] rsp = poem_pb.GveJoinResponse() for index, e in enumerate(g.gve_joineds): info = { "id": e, "index": index, "state": GveEntity.GveEntityStateNormal } if e == p.entityID: p.gve_index = index if not gve_is_open(p) and gve_is_end(p): p.gve_state = GveEntity.GveEntityStateNormal set_gve_state(p, GveEntity.GveEntityStateNormal) info.update( prototypeID=p.prototypeID, name=p.name) else: joiner = Player.simple_load( e, ["prototypeID", "name"]) info.update( prototypeID=joiner.prototypeID, name=joiner.name) rsp.entities.add(**info) joiners.append(e) rsp.sceneID = sceneID g.gve_activateds.load() rsp.activateds = g.gve_activateds rsp.is_end = gve_is_end(p) p.save() # if p.entityID not in g.gve_activateds: sync_gve_entity_index(joiners) # if p.gve_state == GveEntity.GveEntityStateDead: # sync_gve_entity(joiners, p.entityID) # else: # sync_gve_entity( # joiners, p.entityID, GveEntity.GveEntityStateNormal) sync_gve_entity(joiners, p.entityID, GveEntity.GveEntityStateJoin) logger.debug("join") logger.debug(rsp) return success_msg(msgtype, rsp)
def group_kick(self, msgtype, body): p = self.player req = poem_pb.GroupKick() req.ParseFromString(body) g = Group.simple_load(p.groupID, ["leaderID", "gve_last_kick_time"]) if p.entityID != g.leaderID: return fail_msg(msgtype, msgTips.FAIL_MSG_GROUP_PERMISSION_DENIED) now = int(time.time()) if now < g.gve_last_kick_time + ONE_DAY: return fail_msg( msgtype, msgTips.FAIL_MSG_GROUP_KICK_TOO_FREQUENTLY) err = proxy.kick_group_member(req.entityID, p.groupID) if err: return fail_msg(msgtype, err) g.gve_last_kick_time = now g.save() return success_msg(msgtype, "")
def group_deny(self, msgtype, body): p = self.player req = poem_pb.GroupDeny() req.ParseFromString(body) g = Group.simple_load(p.groupID, ["leaderID", "applys"]) g.applys.load() if p.entityID != g.leaderID: return fail_msg(msgtype, msgTips.FAIL_MSG_GROUP_PERMISSION_DENIED) try: del g.applys[req.entityID] except KeyError: pass p.group_applys = g.applys g.save() p.save() p.sync() return success_msg(msgtype, "")
def quit_gve(p): if not p.groupID: return g = Group.simple_load(p.groupID, ["gve_joineds", "gve_activateds"]) g.gve_joineds.load() g.gve_activateds.load() if p.entityID not in g.gve_joineds: return if p.entityID in g.gve_activateds: return try: g.gve_joineds.remove(p.entityID) g.save() except ValueError: pass set_gve_state(p, GveEntity.GveEntityStateQuit) sync_gve_entity(g.gve_joineds, p.entityID) p.save()
def group_alter_name(self, msgtype, body): p = self.player req = poem_pb.GroupAlterName() req.ParseFromString(body) name, err = validate_name(req.name) if err: return fail_msg(msgtype, err) try: GroupnameIndexing.register(0, name) # 占位 except DuplicateIndexException: GroupnameIndexing.unregister(name) return fail_msg( msgtype, msgTips.FAIL_MSG_GROUP_DUPLICATE_FACTION_NAME) g = Group.simple_load(p.groupID, ["name"]) g.name = name g.save() GroupnameIndexing.pool.execute( 'HSET', GroupnameIndexing.key, name, g.groupID) # 更新 return success_msg(msgtype, '')
def gve_end_panel(self, msgtype, body): p = self.player now = int(time.time()) g = Group.simple_load( p.groupID, [ "gve_end_cd", "gve_max_damage", "gve_end_activateds", "gve_activateds_detail"]) if now >= g.gve_end_cd: basereward = p.gve_basereward addition = p.gve_addition reset_player_gve(p) reset_gve(p.groupID) sceneID = get_sceneID() damage = get_group_score(sceneID, p.groupID) rank = get_group_rank(sceneID, p.groupID) count = get_group_count(sceneID) if count == 0: percent = 0 else: percent = math.ceil(rank / float(count) * 100) maxdamage = g.gve_max_damage rsp = poem_pb.GveEndPanel( damage=damage, rank=rank, count=count, percent=percent, maxdamage=maxdamage, rewards=basereward, addition=addition, ) g.gve_end_activateds.load() g.gve_activateds_detail.load() for i in g.gve_end_activateds: info = get_gve_entity(i) info.update(g.gve_activateds_detail.get(i, {})) rsp.members.add(entityID=i, **info) give_reward_by_mail(p) return success_msg(msgtype, rsp) else: return fail_msg(msgtype, reason="活动还没有结束")
def gve_fight(self, msgtype, body): p = self.player if not gve_is_open(p): return fail_msg(msgtype, msgTips.FAIL_MSG_GROUP_GVE_NOT_OPEN) if gve_is_end(p): return fail_msg(msgtype, msgTips.FAIL_MSG_GROUP_GVE_ALREADY_END) req = poem_pb.GveFight() req.ParseFromString(body) g = Group.simple_load( p.groupID, ["gve_activateds", "gve_progress"]) g.gve_progress.load() config = get_config(GveFbInfoConfig)[req.fbID] maxhp = config.boss_hp hp = g.gve_progress.get(req.fbID, maxhp) if hp <= 0: return fail_msg(msgtype, reason="目标已经死亡") rsp = poem_pb.GveFightResponse(hp=hp, maxhp=maxhp) p.gve_state = GveEntity.GveEntityStateFight p.gve_target = req.fbID p.save() g.gve_activateds.load() sync_gve_entity(g.gve_activateds, p.entityID) rsp.verify_code = PlayerFightLock.lock(p.entityID, force=True) return success_msg(msgtype, rsp)
def apply_group(entityID, groupID, applyID): g = Group.simple_load(groupID, ["members", "applys"]) g.applys.load() if applyID in g.applys: return msgTips.FAIL_MSG_GROUP_ALREADY_APPLIED # 重复 g.members.load() if not len(g.members): return msgTips.FAIL_MSG_GROUP_DISMISSED if len(g.members) >= MAX_MEMBERS_COUNT: unrecommend(groupID) return msgTips.FAIL_MSG_GROUP_LIMITED_EXCEED a = g_entityManager.get_player(applyID) if not a: a = Player.simple_load(applyID, ["groupID"]) if a.groupID: return msgTips.FAIL_MSG_GROUP_ALREADY_JOINED # 已经加入 now = int(time.time()) g.applys.load() g.applys[applyID] = {"applytime": now} g.save() p = g_entityManager.get_player(entityID) if p: p.group_applys = g.applys return SUCCESS