From afa39585665d91217692e7499ae7f21b1c1b8b25 Mon Sep 17 00:00:00 2001 From: duhui Date: Thu, 1 Jul 2021 10:46:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BF=83=E6=84=BF=E6=8A=BD=E5=A5=96=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ljsd/jieling/tools/Utils.java | 29 +++- .../jieling/handler/GetPlayerInfoHandler.java | 1 + .../handler/GetWishDrawCardInfoHandler.java | 46 ++++++ .../jieling/logic/GlobalDataManaager.java | 5 +- .../ljsd/jieling/logic/dao/HeroManager.java | 53 ++++++- .../jieling/logic/dao/WishDrawCardInfo.java | 48 ++++++ .../ljsd/jieling/logic/hero/HeroLogic.java | 139 +++++++++++++++++- .../com/ljsd/jieling/util/CBean2Proto.java | 33 +++++ .../src/main/java/config/SSpecialConfig.java | 3 + 9 files changed, 349 insertions(+), 8 deletions(-) create mode 100644 serverlogic/src/main/java/com/ljsd/jieling/handler/GetWishDrawCardInfoHandler.java create mode 100644 serverlogic/src/main/java/com/ljsd/jieling/logic/dao/WishDrawCardInfo.java diff --git a/netty/src/main/java/com/ljsd/jieling/tools/Utils.java b/netty/src/main/java/com/ljsd/jieling/tools/Utils.java index 0b2176eb4..c4bb4fb9a 100644 --- a/netty/src/main/java/com/ljsd/jieling/tools/Utils.java +++ b/netty/src/main/java/com/ljsd/jieling/tools/Utils.java @@ -3,6 +3,7 @@ package com.ljsd.jieling.tools; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.*; import java.security.MessageDigest; import java.util.Random; @@ -89,8 +90,30 @@ public class Utils { String randStr = String.valueOf(stringBuffer); return randStr; } + /** + * 对象拷贝 只要实现了Serializable接口都可以 + * @param obj + * @param + * @return + */ + public static T clone(T obj) { + T cloneObj = null; + try { + // 写入字节流 + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ObjectOutputStream obs = new ObjectOutputStream(out); + obs.writeObject(obj); + obs.close(); - - - + // 分配内存,写入原始对象,生成新对象 + ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray()); + ObjectInputStream ois = new ObjectInputStream(ios); + // 返回生成的新对象 + cloneObj = (T) ois.readObject(); + ois.close(); + } catch (Exception e) { + e.printStackTrace(); + } + return cloneObj; + } } diff --git a/serverlogic/src/main/java/com/ljsd/jieling/handler/GetPlayerInfoHandler.java b/serverlogic/src/main/java/com/ljsd/jieling/handler/GetPlayerInfoHandler.java index 3653f0d2b..6e6acdab9 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/handler/GetPlayerInfoHandler.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/handler/GetPlayerInfoHandler.java @@ -218,6 +218,7 @@ public class GetPlayerInfoHandler extends BaseHandler{ .addAllSealList(CBean2Proto.getPurpleMansionSealInfo(user)) .setDailyredpack(playerInfoManager.getDailyWelfareRedPacket()) .setRandomCount(randomCount) + .addAllDrawTimes(CBean2Proto.getRandomTypeNums(user)) .build(); ReportUtil.onReportEvent(user, ReportEventEnum.APP_LOGIN.getType()); try { diff --git a/serverlogic/src/main/java/com/ljsd/jieling/handler/GetWishDrawCardInfoHandler.java b/serverlogic/src/main/java/com/ljsd/jieling/handler/GetWishDrawCardInfoHandler.java new file mode 100644 index 000000000..f3dd42010 --- /dev/null +++ b/serverlogic/src/main/java/com/ljsd/jieling/handler/GetWishDrawCardInfoHandler.java @@ -0,0 +1,46 @@ +package com.ljsd.jieling.handler; + +import com.google.protobuf.GeneratedMessage; +import com.ljsd.jieling.exception.ErrorCode; +import com.ljsd.jieling.exception.ErrorCodeException; +import com.ljsd.jieling.logic.dao.UserManager; +import com.ljsd.jieling.logic.dao.root.User; +import com.ljsd.jieling.logic.hero.HeroLogic; +import com.ljsd.jieling.util.CBean2Proto; +import org.springframework.stereotype.Component; +import rpc.protocols.CommonProto; +import rpc.protocols.HeroInfoProto; +import rpc.protocols.MessageTypeProto; + +import java.util.List; + +/** + * @Author hj + * @Date 2021/6/29 14:38 + * @Description: + * @Version 1.0 + */ +@Component +public class GetWishDrawCardInfoHandler extends BaseHandler { + @Override + public MessageTypeProto.MessageType getMessageCode() { + return MessageTypeProto.MessageType.choiceWishHeroRequest; + } + + @Override + public GeneratedMessage processWithProto(int uid, HeroInfoProto.choiceWishHeroRequest proto) throws Exception { + // 用户信息 + User user = UserManager.getUser(uid); + if (user == null) { + throw new ErrorCodeException(ErrorCode.UNKNOWN); + } + + if (proto != null && proto.getInfoListCount() > 0){ + HeroLogic.getInstance().updateWishDrawCardInfo(user,proto.getInfoListList()); + } + + // 返回 + List wishCard = CBean2Proto.getWishCard(user); + return HeroInfoProto.choiceWishHeroResponse.newBuilder().addAllInfoList(wishCard).build(); + } +} \ No newline at end of file diff --git a/serverlogic/src/main/java/com/ljsd/jieling/logic/GlobalDataManaager.java b/serverlogic/src/main/java/com/ljsd/jieling/logic/GlobalDataManaager.java index 30bd87bb0..9c733d8a4 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/logic/GlobalDataManaager.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/logic/GlobalDataManaager.java @@ -15,6 +15,7 @@ import com.ljsd.jieling.logic.activity.RemoveEventHeroHandler; import com.ljsd.jieling.logic.activity.UserLevelEventHandler; import com.ljsd.jieling.logic.activity.event.Poster; import com.ljsd.jieling.logic.activity.event.SuperBoxEvent; +import com.ljsd.jieling.logic.hero.HeroLogic; import com.ljsd.jieling.logic.redpacket.WelfareRedPackSendHandler; import com.ljsd.jieling.logic.activity.eventhandler.ActivityStateChangeHandler; import com.ljsd.jieling.logic.activity.eventhandler.BuyGoodsDirectHandler; @@ -378,13 +379,13 @@ public class GlobalDataManaager implements IManager { } //刷新膜拜信息 user.getPlayerInfoManager().setProudInfo(new HashSet<>()); - // 道具刷新 BuyGoodsNewLogic.refreshWelfareState(user); user.getUserMissionManager().onGameEvent(user, GameEvent.LOGIN_GAME,0); - // 惊喜礼盒,事件推送 Poster.getPoster().dispatchEvent(new SuperBoxEvent(user.getId(),-1)); + // 心愿格子信息刷新 + HeroLogic.getInstance().resetWishDrawCardInfoMap(user); } user.getPlayerInfoManager().setLoginTime(TimeUtils.now()); } diff --git a/serverlogic/src/main/java/com/ljsd/jieling/logic/dao/HeroManager.java b/serverlogic/src/main/java/com/ljsd/jieling/logic/dao/HeroManager.java index 6341701f2..3d13da815 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/logic/dao/HeroManager.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/logic/dao/HeroManager.java @@ -72,9 +72,33 @@ public class HeroManager extends MongoBase { * 紫府神印 */ private Map purpleMansionSeal = new HashMap<>(); + /** + * 心愿抽卡 + */ + private HashMap wishDrawCardInfoMap = new HashMap<>(3); + + /** + * 抽奖次数记录 + * key: 招募类型 value: 次数 + */ + private Map randomCountByType = new HashMap<>(); + + public Map getRandomCountByType() { + return randomCountByType; + } + + public void setRandomCountByType(Map randomCountByType) { + this.randomCountByType = randomCountByType; + updateString("randomCountByType", randomCountByType); + } + + public void putRandomCount(int type,int value) { + randomCountByType.put(type,value); + updateString("randomCountByType."+type, value); + } public void updateRandCount(int type,int count){ - updateString("totalCount." + type, count); + updateString("totalCount." + type, count); totalCount.put(type,count); } @@ -349,7 +373,34 @@ public class HeroManager extends MongoBase { public void setTaSuiLingXiaoRankForce(int taSuiLingXiaoRankForce) { this.taSuiLingXiaoRankForce = taSuiLingXiaoRankForce; updateString("taSuiLingXiaoRankForce", taSuiLingXiaoRankForce); + } + public HashMap getWishDrawCardInfoMap() { + if (wishDrawCardInfoMap.isEmpty()){ + initWishDrawCardInfoMap(); + } + return wishDrawCardInfoMap; + } + + /** + * 初始化心愿格子信息 + */ + private void initWishDrawCardInfoMap(){ + for (int i = 1; i < 4; i++) { + wishDrawCardInfoMap.put(i,new WishDrawCardInfo(i,0,0)); + } + updateString("wishDrawCardInfoMap", wishDrawCardInfoMap); + } + + public void setWishDrawCardInfoMap(HashMap wishDrawCardInfoMap) { + this.wishDrawCardInfoMap = wishDrawCardInfoMap; + updateString("wishDrawCardInfoMap", wishDrawCardInfoMap); + } + + public void putWishDrawCardInfoMap(WishDrawCardInfo wishDrawCardInfo) { + int key = wishDrawCardInfo.getId(); + wishDrawCardInfoMap.put(key,wishDrawCardInfo); + updateString("wishDrawCardInfoMap." + key, wishDrawCardInfo); } } diff --git a/serverlogic/src/main/java/com/ljsd/jieling/logic/dao/WishDrawCardInfo.java b/serverlogic/src/main/java/com/ljsd/jieling/logic/dao/WishDrawCardInfo.java new file mode 100644 index 000000000..a99072339 --- /dev/null +++ b/serverlogic/src/main/java/com/ljsd/jieling/logic/dao/WishDrawCardInfo.java @@ -0,0 +1,48 @@ +package com.ljsd.jieling.logic.dao; + +import java.io.Serializable; + +/** + * @Author hj + * @Date 2021/6/29 11:11 + * @Description: + * @Version 1.0 + */ +public class WishDrawCardInfo implements Serializable { + + private static final long serialVersionUID = 1L; + + private int id; + private int heroTid; + private int status; + + public WishDrawCardInfo(int id, int heroTid, int status) { + this.id = id; + this.heroTid = heroTid; + this.status = status; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getHeroTid() { + return heroTid; + } + + public void setHeroTid(int heroTid) { + this.heroTid = heroTid; + } + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } +} diff --git a/serverlogic/src/main/java/com/ljsd/jieling/logic/hero/HeroLogic.java b/serverlogic/src/main/java/com/ljsd/jieling/logic/hero/HeroLogic.java index 974c4885f..db2f0586f 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/logic/hero/HeroLogic.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/logic/hero/HeroLogic.java @@ -38,6 +38,7 @@ import com.ljsd.jieling.logic.store.BuyGoodsNewLogic; import com.ljsd.jieling.logic.store.newRechargeInfo.PushRechargeType; import com.ljsd.jieling.network.server.ProtocolsManager; import com.ljsd.jieling.network.session.ISession; +import com.ljsd.jieling.tools.Utils; import com.ljsd.jieling.util.*; import config.*; import manager.STableManager; @@ -328,7 +329,17 @@ public class HeroLogic{ for(int i = 0 ;i= value){ + resultRandom[i] = wishDrawCardCheck(user,sLotteryRewardConfig.getReward()); + }else { + resultRandom[i] = sLotteryRewardConfig.getReward(); + } + //记录次数 + heroManager.putRandomCount(sLotterySetting.getLotteryType(),num+1); index = i; } //上报抽卡 @@ -359,8 +370,8 @@ public class HeroLogic{ fiveStarPushByRandom(user,resultRandom); fiveStarPokemonPushByRandom(user,resultRandom); builder.setDrop(drop); + builder.addAllDrawTimes(CBean2Proto.getRandomTypeNums(user)); MessageUtil.sendMessage(session, 1, MessageTypeProto.MessageType.HERO_RAND_RESPONSE_VALUE, builder.build(), true); -// System.out.println(System.currentTimeMillis()-time); if(sLotterySetting.getLotteryType() == GlobalsDef.RANDOM_LOTTERY_TYPE && !user.getPlayerInfoManager().isHasRandomLotteryType()&sLotterySetting.getPerCount()!=1){ //首次神将召唤 10连抽 给个攻略邮件 @@ -371,7 +382,131 @@ public class HeroLogic{ } } + /** + * 心愿抽卡校验 + * @param user + * @param item + * @return + */ + private int[] wishDrawCardCheck(User user,int[] item){ + // 查找物品 + SItem sItem = SItem.getsItemMap().get(item[0]); + // 非英雄奖励直接返回 + if (sItem.getItemType() != GlobalItemType.CARD){ + return item; + } + // 非五星英雄 + SCHero scHero = SCHero.getsCHero().get(item[0]); + if (scHero.getStar() < 5){ + return item; + } + // 随机权重 + int totalNum = 10000; + int anInt = MathUtils.randomInt(totalNum); + // 单个格子权重 + int value = SSpecialConfig.getIntegerValue(SSpecialConfig.CARDWISHBLOCKRATE); + // 获取玩家现在的心愿格子信息 + Map cardInfoMap = user.getHeroManager().getWishDrawCardInfoMap(); + // 计算权重map + HashMap map = new HashMap<>(4); + for (int i = 0; i < cardInfoMap.size(); i++) { + map.put(i+1,value); + } + int sum = map.values().stream().mapToInt(Integer::intValue).sum(); + map.put(map.size()+1,totalNum-sum); + + // 遍历权重 + for (Map.Entry entry : map.entrySet()) { + if (anInt <= entry.getValue()){ + // 读取格子信息 + WishDrawCardInfo cardInfo = cardInfoMap.get(entry.getKey()); + // null表示未中心愿, 该格子已锁定, 是空格子 + if (cardInfo == null || cardInfo.getStatus() == 1 || cardInfo.getHeroTid() == 0){ + return item; + } + // new int[] + int[] result = new int[2]; + // 赋值物品id和数量 + result[0] = cardInfo.getHeroTid(); + result[1] = item[1]; + // 修改格子信息,入库 + cardInfo.setStatus(1); + user.getHeroManager().putWishDrawCardInfoMap(cardInfo); + // 消息推送 + sendWishCardIndication(user); + return result; + } + // 权重递减 + anInt-=entry.getValue(); + } + return item; + } + + /** + * 推送心愿格子信息到客户端 + * @param user + */ + private void sendWishCardIndication(User user){ + List wishCard = CBean2Proto.getWishCard(user); + HeroInfoProto.wishDrawCardIndication.Builder list = HeroInfoProto.wishDrawCardIndication.newBuilder().addAllInfoList(wishCard); + MessageUtil.sendIndicationMessage(user.getId(),list.build()); + } + + /** + * 重置心愿格子信息 + */ + public void resetWishDrawCardInfoMap(User user){ + // 重置状态 + HashMap map = user.getHeroManager().getWishDrawCardInfoMap(); + map.values().forEach(v->v.setStatus(0)); + user.getHeroManager().setWishDrawCardInfoMap(map); + // 推送消息 + sendWishCardIndication(user); + } + + /** + * 修改英雄格子 + * @param user + * @param list + * @throws ErrorCodeException + */ + public void updateWishDrawCardInfo(User user, List list) throws ErrorCodeException { + HashMap map = Utils.clone(user.getHeroManager().getWishDrawCardInfoMap()); + // 遍历 + for (CommonProto.wishDrawCardInfo v : list) { + WishDrawCardInfo cardInfo = map.get(v.getId()); + // 英雄未锁定 + if (cardInfo != null && cardInfo.getStatus() == 0){ + // 替换英雄 + cardInfo.setHeroTid(v.getHeroTid()); + } + } + + // 获取已选择英雄的类型set集合 + Set race = new HashSet<>(); + map.values().forEach(v->{ + if (v.getHeroTid() != 0){ + SCHero scHero = SCHero.getsCHero().get(v.getHeroTid()); + race.add(scHero.getPropertyName()); + } + }); + + // 去除空格子后队列的长度 + List infos = new ArrayList<>(map.values()); + infos.removeIf(v->v.getHeroTid() == 0); + + // 类型唯一, 如果长度不一致代表有重复类型英雄 + if (race.size() != infos.size()){ + throw new ErrorCodeException("有重复类型英雄,替换失败"); + } + // 入库 + else { + user.getHeroManager().setWishDrawCardInfoMap(map); + } + // 推送消息 + sendWishCardIndication(user); + } private void changeRandomItem(User user,int activityId,int[][] random){ ActivityMission activityMission = user.getActivityManager().getActivityMissionMap().get(activityId); diff --git a/serverlogic/src/main/java/com/ljsd/jieling/util/CBean2Proto.java b/serverlogic/src/main/java/com/ljsd/jieling/util/CBean2Proto.java index 314d20a1e..016b28e60 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/util/CBean2Proto.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/util/CBean2Proto.java @@ -864,4 +864,37 @@ public class CBean2Proto { }); return list; } + + /** + * 获取心愿格子信息 + * @param user + * @return + */ + public static List getWishCard(User user){ + List list = new ArrayList<>(); + user.getHeroManager().getWishDrawCardInfoMap().values().forEach(v->{ + CommonProto.wishDrawCardInfo builder = CommonProto.wishDrawCardInfo.newBuilder() + .setId(v.getId()) + .setHeroTid(v.getHeroTid()) + .setStatus(v.getStatus()) + .build(); + list.add(builder); + }); + return list; + } + + /** + * 获取类型抽卡总次数 + * @param user + * @return + */ + public static List getRandomTypeNums(User user){ + List list = new ArrayList<>(); + Map countByType = user.getHeroManager().getRandomCountByType(); + for (Map.Entry entry : countByType.entrySet()) { + CommonProto.randomTypeNum.Builder builder = CommonProto.randomTypeNum.newBuilder().setType(entry.getKey()).setNum(entry.getValue()); + list.add(builder.build()); + } + return list; + } } diff --git a/tablemanager/src/main/java/config/SSpecialConfig.java b/tablemanager/src/main/java/config/SSpecialConfig.java index 49cc8fd6c..cd22eb0bb 100644 --- a/tablemanager/src/main/java/config/SSpecialConfig.java +++ b/tablemanager/src/main/java/config/SSpecialConfig.java @@ -102,6 +102,9 @@ public class SSpecialConfig implements BaseConfig { public static final String YIJIANGOUMAI = "YiJianGouMai";//一件购买 public static final String WELFAREREDPACKET_LIMIT = "RedPackMaxGainNum";//每日领取红包数量上限 public static final String SWEEP_ONEKEY = "floodprivilegeid";//一键扫荡 + + public static final String CARDWISHBLOCKRATE = "CardWishBlockRate";//钻石抽卡心愿单格子命中万分比 + public static final String UNLOCKCARDWISH = "UnlockCardWish";//解锁钻石心愿抽卡所需要的抽卡次数 @Override public void init() throws Exception {