四灵试炼战斗记录从redis转移到mongodb

back_recharge
duhui 2022-03-02 13:13:06 +08:00
parent 837174f0cd
commit 00b6a93c53
6 changed files with 195 additions and 78 deletions

View File

@ -390,6 +390,11 @@ public class RedisKey {
*/ */
public final static String POWER_RICE_RANK = "POWER_RICE_RANK"; public final static String POWER_RICE_RANK = "POWER_RICE_RANK";
/**
* mongo
*/
public static final String FOUR_CHALLENGE_DATA_TRANSFER = "FOUR_CHALLENGE_DATA_TRANSFER";
/** /**
* *
* *

View File

@ -13,7 +13,6 @@ import rpc.protocols.PlayerInfoProto;
* @discribe * @discribe
*/ */
@Component @Component
public class FourChallengeDoHandler extends BaseHandler<PlayerInfoProto.FourChallengeDoRequest> { public class FourChallengeDoHandler extends BaseHandler<PlayerInfoProto.FourChallengeDoRequest> {
@Override @Override
public MessageTypeProto.MessageType getMessageCode() { public MessageTypeProto.MessageType getMessageCode() {

View File

@ -309,6 +309,8 @@ public class GlobalDataManaager implements IManager {
new ShanHeSheJiTuEventHandler(); new ShanHeSheJiTuEventHandler();
new SummonTreasureHandler(); new SummonTreasureHandler();
new ArchitectureUnLockHandler(); new ArchitectureUnLockHandler();
// 四灵试炼转移数据,只执行一次
FourChallengeLogic.getInstance().redisToMongoDB();
ChampionshipLogic.minuteCheck(true); ChampionshipLogic.minuteCheck(true);
GuildFightLogic.minuteCheckForCarFight(); GuildFightLogic.minuteCheckForCarFight();
} catch (Exception e) { } catch (Exception e) {

View File

@ -1,10 +1,10 @@
package com.ljsd.jieling.logic.activity.fourChallenge; package com.ljsd.jieling.logic.activity.fourChallenge;
import com.ljsd.fight.ArenaRecord;
import com.ljsd.jieling.chat.logic.ChatLogic; import com.ljsd.jieling.chat.logic.ChatLogic;
import com.ljsd.jieling.core.FunctionIdEnum; import com.ljsd.jieling.core.FunctionIdEnum;
import com.ljsd.jieling.core.GlobalsDef;
import com.ljsd.jieling.core.HandlerLogicThread; import com.ljsd.jieling.core.HandlerLogicThread;
import com.ljsd.jieling.db.mongo.LjsdMongoTemplate;
import com.ljsd.jieling.db.mongo.MongoUtil;
import com.ljsd.jieling.db.redis.RedisKey; import com.ljsd.jieling.db.redis.RedisKey;
import com.ljsd.jieling.db.redis.RedisUtil; import com.ljsd.jieling.db.redis.RedisUtil;
import com.ljsd.jieling.exception.ErrorCode; import com.ljsd.jieling.exception.ErrorCode;
@ -16,6 +16,7 @@ import com.ljsd.jieling.logic.dao.Hero;
import com.ljsd.jieling.logic.dao.TeamEnum; import com.ljsd.jieling.logic.dao.TeamEnum;
import com.ljsd.jieling.logic.dao.TeamPosHeroInfo; import com.ljsd.jieling.logic.dao.TeamPosHeroInfo;
import com.ljsd.jieling.logic.dao.UserManager; import com.ljsd.jieling.logic.dao.UserManager;
import com.ljsd.jieling.logic.dao.root.ArenaRecord;
import com.ljsd.jieling.logic.dao.root.User; import com.ljsd.jieling.logic.dao.root.User;
import com.ljsd.jieling.logic.fight.FightDispatcher; import com.ljsd.jieling.logic.fight.FightDispatcher;
import com.ljsd.jieling.logic.fight.GameFightType; import com.ljsd.jieling.logic.fight.GameFightType;
@ -42,7 +43,10 @@ import rpc.protocols.PlayerInfoProto;
import util.StringUtil; import util.StringUtil;
import util.TimeUtils; import util.TimeUtils;
import java.util.*; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/** /**
* @author lvxinran * @author lvxinran
@ -79,11 +83,16 @@ public class FourChallengeLogic {
/** /**
* *
*/ */
public static final String FIRST = "FIRST"; public static final int FIRST = 1;
/** /**
* *
*/ */
public static final String MINI = "MINI"; public static final int MINI = 2;
/**
*
*/
public static final String fc = "fc";
/** /**
* *
@ -222,10 +231,7 @@ public class FourChallengeLogic {
drop = ItemUtil.drop(user, sCampTowerConfig.getFirstReward(), BIReason.FOUR_CHALLENGE_FIRST); drop = ItemUtil.drop(user, sCampTowerConfig.getFirstReward(), BIReason.FOUR_CHALLENGE_FIRST);
user.getPlayerInfoManager().updateFourChallengeByIndex(campId - 1, currentFloor + 1); user.getPlayerInfoManager().updateFourChallengeByIndex(campId - 1, currentFloor + 1);
user.getPlayerInfoManager().setFourChallengeRemainTimes(campId - 1, fourChallengeRemainTimes[campId - 1] - 1); user.getPlayerInfoManager().setFourChallengeRemainTimes(campId - 1, fourChallengeRemainTimes[campId - 1] - 1);
// 助战处理
// 计算战力
int myforce = HeroLogic.getInstance().calTeamTotalForce(user, teamId, false);
if (help == 1){ if (help == 1){
// 助战英雄使用状态修改 // 助战英雄使用状态修改
helpHero.setState(1); helpHero.setState(1);
@ -237,50 +243,12 @@ public class FourChallengeLogic {
// 删除队伍中英雄id // 删除队伍中英雄id
user.getTeamPosManager().removeTeamPosByHeroId(teamId,helpHero.getHero().getId()); user.getTeamPosManager().removeTeamPosByHeroId(teamId,helpHero.getHero().getId());
} }
// 层数排行榜 // 层数排行榜
rank.addRank(user.getId(),String.valueOf(campId),floorId); rank.addRank(user.getId(),String.valueOf(campId),floorId);
// 推送礼包 // 推送礼包
BuyGoodsNewLogic.openPush(session, user, PushRechargeType.FORE_SHILIAN.getType(), floorId, 1); BuyGoodsNewLogic.openPush(session, user, PushRechargeType.FORE_SHILIAN.getType(), floorId, 1);
// 更新记录
ArenaRecord arenaRecord = new ArenaRecord(); updateRecord(user,fightResult,teamId,campId,floorId);
// 唯一id
arenaRecord.setId(KeyGenUtils.produceIdByModule(UUIDEnum.FourChallenge,user.getId()));
// 创建时间
arenaRecord.setCreateTime(TimeUtils.nowInt());
// 战力
arenaRecord.setAttackForce(myforce);
// 战斗数据
arenaRecord.setFightData(fightResult.getFightData().toByteArray());
// 用户id
arenaRecord.setAttackId(user.getId());
// key
String floorIds = String.valueOf(floorId);
// 首通子key
String firstSubKey = FIRST + campId;
// 最低子key
String miniSubKey = MINI + campId;
// 首次通关记录
ArenaRecord first = RedisUtil.getInstence().getMapValue(RedisKey.FOUR_CHALLENGE, firstSubKey, floorIds, ArenaRecord.class);
if (first == null){
// 记录redis
RedisUtil.getInstence().putMapEntry(RedisKey.FOUR_CHALLENGE, firstSubKey, floorIds, arenaRecord);
}
// 最低战力记录
ArenaRecord force = RedisUtil.getInstence().getMapValue(RedisKey.FOUR_CHALLENGE, miniSubKey, floorIds, ArenaRecord.class);
if (force == null){
// 记录redis
RedisUtil.getInstence().putMapEntry(RedisKey.FOUR_CHALLENGE, miniSubKey, floorIds, arenaRecord);
}else {
int attackForce = force.getAttackForce();
if (myforce < attackForce){
// 记录redis
RedisUtil.getInstence().putMapEntry(RedisKey.FOUR_CHALLENGE, miniSubKey, floorIds, arenaRecord);
}
}
// 发送公告 // 发送公告
if (sCampTowerConfig.getNeedBroadCast() == 1){ if (sCampTowerConfig.getNeedBroadCast() == 1){
String nameColor = user.getPlayerInfoManager().getNameColor(); String nameColor = user.getPlayerInfoManager().getNameColor();
@ -306,6 +274,58 @@ public class FourChallengeLogic {
MessageUtil.sendMessage(session, 1, MessageTypeProto.MessageType.FOUR_CHALLENGE_DO_RESPONSE_VALUE, response.build(), true); MessageUtil.sendMessage(session, 1, MessageTypeProto.MessageType.FOUR_CHALLENGE_DO_RESPONSE_VALUE, response.build(), true);
} }
/**
*
* @param user
* @param fightResult
* @param teamId
* @param campId
* @param floorId
* @throws Exception
*/
private void updateRecord(User user, FightResult fightResult, int teamId, int campId, int floorId) throws Exception {
// 计算战力
int myforce = HeroLogic.getInstance().calTeamTotalForce(user, teamId, false);
ArenaRecord arenaRecord = new ArenaRecord();
// 创建时间
arenaRecord.setCreateTime(TimeUtils.nowInt());
// 战力
arenaRecord.setAttackForce(myforce);
// 战斗数据
arenaRecord.setFightData(fightResult.getFightData().toByteArray());
// 用户id
arenaRecord.setAttackId(user.getId());
// 首通子key
String firstKey = getKey(FIRST, campId, floorId);
// 最低子key
String miniKey = getKey(MINI, campId, floorId);
// 首次通关记录
ArenaRecord first = getRecord(firstKey);
if (first == null){
// 赋值对应的key
arenaRecord.setId(firstKey);
setRecord(user.getId(),arenaRecord);
}
// 最低战力记录
ArenaRecord mini = getRecord(miniKey);
if (mini == null){
// 赋值对应的key
arenaRecord.setId(miniKey);
setRecord(user.getId(),arenaRecord);
}else {
int attackForce = mini.getAttackForce();
if (myforce < attackForce){
// 赋值对应的key
arenaRecord.setId(miniKey);
setRecord(user.getId(),arenaRecord);
}
}
}
/** /**
* *
* @param type * @param type
@ -408,36 +428,109 @@ public class FourChallengeLogic {
/** /**
* *
* @param type * @param camp
* @param tier * @param tier
* @return * @return
*/ */
public List<CommonProto.UserRank> getFightRecord(int type,int tier) throws Exception { public List<CommonProto.UserRank> getFightRecord(int camp,int tier) throws Exception {
List<CommonProto.UserRank> lists = new ArrayList<>(); List<CommonProto.UserRank> lists = new ArrayList<>();
// 首次通关 // 首次通关
getFightRecord2(tier,0,FIRST + type,lists); ArenaRecord record = getRecord(getKey(FIRST, camp, tier));
if (record != null){
// 获取记录的user
User user1 = UserManager.getUser(record.getAttackId());
lists.add(CBean2Proto.getOneUserRank(user1,0,record).build());
}
// 最低战力 // 最低战力
getFightRecord2(tier,1,MINI + type,lists); ArenaRecord record2 = getRecord(getKey(MINI, camp, tier));
if (record2 != null){
// 获取记录的user
User user1 = UserManager.getUser(record2.getAttackId());
lists.add(CBean2Proto.getOneUserRank(user1,1,record2).build());
}
return lists; return lists;
} }
/** /**
* * keyid
* @param tier * fc()_(/)__
* @param function
* @param subKey
* @param lists
* @throws Exception
*/ */
private void getFightRecord2(int tier,int function, String subKey, List<CommonProto.UserRank> lists) throws Exception { public static String getKey(int type, int camp, int tier){
// 根据子key获取对应的回放信息 return fc+"_"+type+"_"+camp+"_"+tier;
ArenaRecord arenaRecord = RedisUtil.getInstence().getMapValue(RedisKey.FOUR_CHALLENGE, subKey, String.valueOf(tier), ArenaRecord.class);
if (arenaRecord == null){
return;
}
// 获取记录的user
User user1 = UserManager.getUser(arenaRecord.getAttackId());
lists.add(CBean2Proto.getOneUserRank(user1,function,arenaRecord).build());
} }
/**
*
* @param key
* @return
* @throws Exception
*/
public ArenaRecord getRecord(String key) throws Exception {
LjsdMongoTemplate ljsdMongoTemplate = MongoUtil.getLjsdMongoTemplate();
ArenaRecord arenaRecord = ljsdMongoTemplate.findByIdMy(ArenaRecord._COLLECTION_NAME, key, ArenaRecord.class);
return arenaRecord;
}
/**
*
* @param uid
* @param arenaRecord
* @throws Exception
*/
public void setRecord(int uid,ArenaRecord arenaRecord) throws Exception {
LjsdMongoTemplate ljsdMongoTemplate = MongoUtil.getLjsdMongoTemplate();
ljsdMongoTemplate.save(uid,arenaRecord);
}
public void redisToMongoDB() throws Exception {
long now = TimeUtils.now();
String key = RedisUtil.getInstence().getKey(RedisKey.FOUR_CHALLENGE_DATA_TRANSFER, "");
Object o = RedisUtil.getInstence().get(key);
if (o != null){
return;
}
LOGGER.info("四灵试炼战斗记录数据转移开始==========================");
//数据转移
redisToMongoData("FIRST",1);
redisToMongoData("FIRST",2);
redisToMongoData("FIRST",3);
redisToMongoData("FIRST",4);
redisToMongoData("MINI",1);
redisToMongoData("MINI",2);
redisToMongoData("MINI",3);
redisToMongoData("MINI",4);
RedisUtil.getInstence().set(key,"1");
LOGGER.info("四灵试炼战斗记录数据转移结束=========================={}ms",TimeUtils.now()-now);
}
private void redisToMongoData(String type, int camp) throws Exception {
Map<String, com.ljsd.fight.ArenaRecord> values = RedisUtil.getInstence().getMapValues(RedisKey.FOUR_CHALLENGE, type+camp, String.class, com.ljsd.fight.ArenaRecord.class);
if (values != null && !values.isEmpty()){
int typeInt;
if (type.equals("FIRST")){
typeInt = 1;
}else {
typeInt = 2;
}
for (Map.Entry<String, com.ljsd.fight.ArenaRecord> entry : values.entrySet()) {
ArenaRecord arenaRecord = new ArenaRecord();
String key = getKey(typeInt, camp, Integer.parseInt(entry.getKey()));
arenaRecord.setId(key);
// 创建时间
arenaRecord.setCreateTime(entry.getValue().getCreateTime());
// 战力
arenaRecord.setAttackForce(entry.getValue().getAttackForce());
// 战斗数据
arenaRecord.setFightData(entry.getValue().getFightData());
// 用户id
arenaRecord.setAttackId(entry.getValue().getAttackId());
// 入库
setRecord(arenaRecord.getAttackId(),arenaRecord);
}
LOGGER.info("四灵试炼战斗记录转移数量:{},类型:{},阵营:{}",values.size(),type,camp);
}
// 删除数据库
// RedisUtil.getInstence().del(RedisUtil.getInstence().getKey(RedisKey.FOUR_CHALLENGE, type+camp));
}
} }

View File

@ -523,7 +523,7 @@ public class ArenaLogic {
*/ */
public void getFightReplayData(ISession iSession,int type,String fightId) throws Exception { public void getFightReplayData(ISession iSession,int type,String fightId) throws Exception {
int uid = iSession.getUid(); int uid = iSession.getUid();
ArenaRecord arenaRecord=null; ArenaRecord arenaRecord = null;
if(type == 2){ if(type == 2){
arenaRecord = RedisUtil.getInstence().getMapEntry(RedisKey.CHAMPION_ARENA_RECORD,"", fightId, ArenaRecord.class); arenaRecord = RedisUtil.getInstence().getMapEntry(RedisKey.CHAMPION_ARENA_RECORD,"", fightId, ArenaRecord.class);
}else if(type == 1){ }else if(type == 1){
@ -534,19 +534,25 @@ public class ArenaLogic {
}else if (type == 4){ }else if (type == 4){
// 四灵试炼 // 四灵试炼
String[] split = fightId.split("#"); String[] split = fightId.split("#");
String key = null;
// 首通
if ("0".equals(split[2])){ if ("0".equals(split[2])){
// 首通 key = FourChallengeLogic.getKey(FourChallengeLogic.FIRST, Integer.parseInt(split[0]), Integer.parseInt(split[1]));
arenaRecord = RedisUtil.getInstence().getMapEntry(RedisKey.FOUR_CHALLENGE, FourChallengeLogic.FIRST+split[0], split[1], ArenaRecord.class); }
}else if ("1".equals(split[2])){ // 最低战力
// 最低战力 if ("1".equals(split[2])){
arenaRecord = RedisUtil.getInstence().getMapEntry(RedisKey.FOUR_CHALLENGE, FourChallengeLogic.MINI+split[0], split[1], ArenaRecord.class); key = FourChallengeLogic.getKey(FourChallengeLogic.MINI, Integer.parseInt(split[0]), Integer.parseInt(split[1]));
}
com.ljsd.jieling.logic.dao.root.ArenaRecord record = FourChallengeLogic.getInstance().getRecord(key);
if (record != null){
arenaRecord = new ArenaRecord();
arenaRecord.setFightData(record.getFightData());
} }
} }
if(arenaRecord == null){ if(arenaRecord == null){
throw new ErrorCodeException(ErrorCode.newDefineCode("arena_rrecord null")); throw new ErrorCodeException(ErrorCode.newDefineCode("arena_rrecord null"));
} }
byte[] fightData = arenaRecord.getFightData(); byte[] fightData = arenaRecord.getFightData();
CommonProto.FightData fightDataProto = CommonProto.FightData.parseFrom(fightData); CommonProto.FightData fightDataProto = CommonProto.FightData.parseFrom(fightData);
FightInfoProto.FightRePlayResponse build = FightInfoProto.FightRePlayResponse.newBuilder().setFightData(fightDataProto).build(); FightInfoProto.FightRePlayResponse build = FightInfoProto.FightRePlayResponse.newBuilder().setFightData(fightDataProto).build();
MessageUtil.sendMessage(iSession,1, MessageTypeProto.MessageType.VIEW_FIGHTREPLAY_RESPONSE_VALUE,build,true); MessageUtil.sendMessage(iSession,1, MessageTypeProto.MessageType.VIEW_FIGHTREPLAY_RESPONSE_VALUE,build,true);

View File

@ -352,7 +352,19 @@ public class CBean2Proto {
* @param function * @param function
* @return * @return
*/ */
public static CommonProto.UserRank.Builder getOneUserRank(User everyUser, int function, ArenaRecord arenaRecord){ // public static CommonProto.UserRank.Builder getOneUserRank(User everyUser, int function, ArenaRecord arenaRecord){
// return CommonProto.UserRank.newBuilder()
// .setUid(everyUser.getId())
// .setUserName(everyUser.getPlayerInfoManager().getNickName())
// .setHead(everyUser.getPlayerInfoManager().getHead())
// .setHeadFrame(everyUser.getPlayerInfoManager().getHeadFrame())
// .setLevel(everyUser.getPlayerInfoManager().getLevel())
// .setForce(arenaRecord.getAttackForce())
// .setSex(everyUser.getPlayerInfoManager().getSex())
// .setFirstOrMini(function);
// }
public static CommonProto.UserRank.Builder getOneUserRank(User everyUser, int function, com.ljsd.jieling.logic.dao.root.ArenaRecord arenaRecord){
return CommonProto.UserRank.newBuilder() return CommonProto.UserRank.newBuilder()
.setUid(everyUser.getId()) .setUid(everyUser.getId())
.setUserName(everyUser.getPlayerInfoManager().getNickName()) .setUserName(everyUser.getPlayerInfoManager().getNickName())