package com.qiwu.inference.common.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.qiwu.inference.common.Constants; import com.qiwu.inference.common.TimingAcquisition; import com.qiwu.inference.dto.*; import com.qiwu.inference.pojo.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.StringUtils; import org.yaml.snakeyaml.Yaml; import java.io.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.PriorityBlockingQueue; public class PersistenceUtil { static Logger log = LoggerFactory.getLogger(PersistenceUtil.class); /** * 保存玩家数据到json文件 * @param fileName * @param data * @return void */ public static void saveDataToFile(String path, String fileName, String data) { FileWriter fw = null; File file = new File(path); if (!file.exists()) { try { file.mkdirs(); } catch (Exception e) { e.printStackTrace(); } } try { fw = new FileWriter(path + "/" + fileName + ".json", false); fw.write(data); } catch (Exception e) { e.printStackTrace(); } finally { try { if (fw != null) { fw.close(); } } catch (Exception e) { e.printStackTrace(); } } } /** * 从配置文件夹的关卡中读取关卡数据和关卡话术 * @param filePath * @return String */ public static void getDataFromFile(String filePath) { BufferedReader reader = null; File file = new File(filePath); try { if (file.exists()) { File[] fileList = file.listFiles(); if (!StringUtils.isEmpty(fileList)) { if (fileList.length != 0) { //遍历所有系列名称 for (File series : fileList) { String seriesName = series.getName(); //系列名称 String gateFilePath = series.getAbsolutePath() + "/关卡数据/"; //关卡数据路径 File gateFile = new File(gateFilePath); //关卡数据目录下的所有关卡 File[] gateNosList = gateFile.listFiles(); //所有关卡目录 List gateNoList = new ArrayList<>(); //记录该系列所有的关卡编号 if (!StringUtils.isEmpty(gateNosList)) { //遍历系列下的所有目录 for (File value : gateNosList) { String gateName = value.getName(); //关卡名称 StringBuilder result = new StringBuilder(); FileInputStream fileInputStream = new FileInputStream(value.getAbsolutePath() + "/data/results.json"); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); reader = new BufferedReader(inputStreamReader); String stringTmp = null; while ((stringTmp = reader.readLine()) != null) { result.append(stringTmp); } Map> gateInfoMap = JSON.parseObject(result.toString(), new TypeReference>>(){}); TimingAcquisition.gateInfoMap.putAll(gateInfoMap); //-------------------------缓存关卡攻略数据--------------------- String gateNo = null; for (Map.Entry> entry : gateInfoMap.entrySet()) { gateNo = entry.getKey(); } gateNoList.add(gateNo); //缓存关卡名称和编号的对应关系 TimingAcquisition.gateNameToNoMap.put(gateName, gateNo); log.info("开始缓存关卡攻略数据"); String barrierTaskKey = getConfigFromFile(value.getAbsolutePath() + "/data/", "results_final_task.json"); List barrierTaskKeyList = JSON.parseObject(barrierTaskKey, new TypeReference>(){}); Map> barrierTaskKeyMap = new ConcurrentHashMap<>(); barrierTaskKeyMap.put(gateNo, barrierTaskKeyList); TimingAcquisition.barrierTaskKeyMap.putAll(barrierTaskKeyMap); log.info("关卡攻略数据缓存完成"); String speechFilePath = value.getAbsolutePath() + "/speech/"; File speechFile = new File(speechFilePath); if (speechFile.exists()) { File[] speechFileList = speechFile.listFiles(); //所有配置的话术 if (!StringUtils.isEmpty(speechFileList)) { if (speechFileList.length != 0) { //--------------------加载后台配置的话术到关卡中----------------- TimingAcquisition.speechLoadingToGate(gateNo, speechFilePath); //--------------------加载闲聊话术到关卡中---------------- log.info("加载闲聊话术到关卡中"); Map> chatSpeechMap = SpeechUtil.getChatSpeechModel(speechFilePath); TimingAcquisition.gateNoChatSpeechMap.put(gateNo, chatSpeechMap); log.info("闲聊话术加载到关卡中完成"); //--------------------加载线索成功话术到关卡中---------------- log.info("加载线索成功话术到关卡中"); Map> clueSpeechMap = SpeechUtil.getClueSpeechModel(speechFilePath); TimingAcquisition.gateNoSuccessClueSpeechMap.put(gateNo, clueSpeechMap); log.info("线索成功话术加载到关卡中完成"); //--------------------加载任务成功话术到关卡中---------------- log.info("加载任务成功话术到关卡中"); Map> taskSpeechMap = SpeechUtil.getTaskSpeechModel(speechFilePath); TimingAcquisition.gateNoSuccessTaskSpeechMap.put(gateNo, taskSpeechMap); log.info("任务成功话术加载到关卡中完成"); //--------------------加载任务成功话术到关卡中---------------- log.info("加载任务提示话术到关卡中"); Map> tipSpeechMap = SpeechUtil.getTipSpeechModel(speechFilePath); TimingAcquisition.gateNoTipSpeechMap.put(gateNo, tipSpeechMap); log.info("任务提示话术加载到关卡中完成"); //--------------------加载人物见面话术到关卡中---------------- log.info("加载人物见面话术到关卡中"); Map> personSpeechMap = SpeechUtil.getPersonSpeechModel(speechFilePath); TimingAcquisition.gateNoPersonSpeechMap.put(gateNo, personSpeechMap); log.info("人物见面话术加载到关卡中完成"); //--------------------加载操作提示话术到关卡中---------------- log.info("加载操作提示话术到关卡中"); Map operateSpeechMap = SpeechUtil.getOperateSpeechModel(speechFilePath); TimingAcquisition.gateNoOperateSpeechMap.put(gateNo, operateSpeechMap); log.info("操作提示话术加载到关卡中完成"); //--------------------加载任务异常话术到关卡中---------------- log.info("加载任务异常话术到关卡中"); Map taskExceptionSpeechMap = SpeechUtil.getTaskExceptionSpeechModel(speechFilePath); TimingAcquisition.gateNoTaskExceptionSpeechMap.put(gateNo, taskExceptionSpeechMap); log.info("任务异常话术加载到关卡中完成"); } } } } } log.info("开始缓存系列名和关卡编号对应关系"); Map> seriesGateNosMap = new ConcurrentHashMap<>(); seriesGateNosMap.put(seriesName, gateNoList); TimingAcquisition.seriesGateNosMap.putAll(seriesGateNosMap); log.info("系列名和关卡编号对应关系完成"); } } } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (Exception e) { e.printStackTrace(); } } } /** * 从配置文件的系列话术中加载话术表到系列中 * @param filePath * @return String */ public static void getSpeechFromFile(String filePath) { File file = new File(filePath); try { if (file.exists()) { File[] fileList = file.listFiles(); if (!StringUtils.isEmpty(fileList)) { if (fileList.length != 0) { //遍历所有系列名称 for (File series : fileList) { String seriesName = series.getName(); //系列名称 String speechFilePath = series.getAbsolutePath() + "/speech/"; //--------------------加载后台配置的话术----------------- TimingAcquisition.speechLoading(seriesName, speechFilePath); //--------------------加载闲聊话术的csv表---------------- log.info("加载闲聊话术到系列"); Map> chatSpeechMap = SpeechUtil.getChatSpeechModel(speechFilePath); TimingAcquisition.seriesChatSpeechMap.put(seriesName, chatSpeechMap); log.info("闲聊话术加载到系列完成"); //--------------------加载线索成功话术的csv表---------------- log.info("加载线索成功话术到系列"); Map> clueSpeechMap = SpeechUtil.getClueSpeechModel(speechFilePath); TimingAcquisition.seriesSuccessClueSpeechMap.put(seriesName, clueSpeechMap); log.info("线索成功话术加载到系列完成"); //--------------------加载任务成功话术的csv表---------------- log.info("加载任务成功话术到系列"); Map> taskSpeechMap = SpeechUtil.getTaskSpeechModel(speechFilePath); TimingAcquisition.seriesSuccessTaskSpeechMap.put(seriesName, taskSpeechMap); log.info("任务成功话术加载到系列完成"); //--------------------加载任务成功话术的csv表---------------- log.info("加载任务提示话术到系列"); Map> tipSpeechMap = SpeechUtil.getTipSpeechModel(speechFilePath); TimingAcquisition.seriesTipSpeechMap.put(seriesName, tipSpeechMap); log.info("任务提示话术加载到系列完成"); //--------------------加载人物见面话术的csv表---------------- log.info("加载人物见面话术到系列"); Map> personSpeechMap = SpeechUtil.getPersonSpeechModel(speechFilePath); TimingAcquisition.seriesPersonSpeechMap.put(seriesName, personSpeechMap); log.info("人物见面话术加载到系列完成"); //--------------------加载操作提示话术的csv表---------------- log.info("加载操作提示话术到系列"); Map operateSpeechMap = SpeechUtil.getOperateSpeechModel(speechFilePath); TimingAcquisition.seriesOperateSpeechMap.put(seriesName, operateSpeechMap); log.info("操作提示话术加载到系列完成"); //--------------------加载任务异常话术的csv表---------------- log.info("加载任务异常话术到系列"); Map taskExceptionSpeechMap = SpeechUtil.getTaskExceptionSpeechModel(speechFilePath); TimingAcquisition.seriesTaskExceptionSpeechMap.put(seriesName, taskExceptionSpeechMap); log.info("任务异常话术加载到系列中完成"); } } } } } catch (Exception e) { e.printStackTrace(); } } /** * 从配置文件的系列配置中加载配置表到系列中 * @param filePath * @return String */ public static void getConfigFromFile(String filePath) { File file = new File(filePath); try { if (file.exists()) { File[] fileList = file.listFiles(); if (!StringUtils.isEmpty(fileList)) { if (fileList.length != 0) { //遍历所有系列名称 for (File series : fileList) { String seriesName = series.getName(); //系列名称 String configFilePath = series.getAbsolutePath() + "/config/"; //--------------------加载场所表到系列中---------------- log.info("加载场所表到系列:" + seriesName); Map locationMap = FileUtil.getLocationModel(configFilePath); TimingAcquisition.seriesLocationMap.put(seriesName, locationMap); log.info("场所表加载到系列完成"); //--------------------加载线索表到系列中---------------- log.info("加载线索表到系列:" + seriesName); Map clueMap = FileUtil.getClueModel(configFilePath); TimingAcquisition.seriesClueMap.put(seriesName, clueMap); log.info("线索表加载到系列完成"); //--------------------加载线索类型表到系列中---------------- log.info("加载线索类型表到系列:" + seriesName); Map clueTypeMap = FileUtil.getClueTypeModel(configFilePath); TimingAcquisition.seriesClueTypeMap.put(seriesName, clueTypeMap); log.info("线索类型表加载到系列完成"); //--------------------加载性格表到系列中---------------- log.info("加载性格表到系列:" + seriesName); Map dispositionMap = FileUtil.getDispositionModel(configFilePath); TimingAcquisition.seriesDispositionMap.put(seriesName, dispositionMap); log.info("性格表加载到系列完成"); //--------------------加载系统配置表到系列中---------------- log.info("加载系统配置表到系列:" + seriesName); Map systemConfigMap = FileUtil.getSystemConfigModel(configFilePath); TimingAcquisition.seriesSystemConfigMap.put(seriesName, systemConfigMap); log.info("系统配置表加载到系列完成"); //--------------------加载任务条件表到系列中---------------- log.info("加载任务条件表到系列:" + seriesName); Map taskConditionMap = FileUtil.getTaskConditionModel(configFilePath); TimingAcquisition.seriesTaskConditionMap.put(seriesName, taskConditionMap); log.info("任务条件表加载到系列完成"); //--------------------加载伙伴配置表到系列中---------------- log.info("加载伙伴配置表到系列:" + seriesName); Map partnerItemMap = FileUtil.getPartnerItemModel(configFilePath); TimingAcquisition.seriesPartnerItemMap.put(seriesName, partnerItemMap); log.info("伙伴配置表加载到系列完成"); Map> result = new HashMap<>(); //--------------------加载伙伴人数上限表到系列中---------------- log.info("加载伙伴人数上限表到系列:" + seriesName); Map partnerNumMap = FileUtil.getPartnerNumModel(configFilePath); result.put(Constants.ITEM_PARTNER, partnerNumMap); //--------------------加载背包数量上限表到系列中---------------- log.info("加载道具数量上限表到系列:" + seriesName); Map propNumMap = FileUtil.getBackpackNumModel(configFilePath); result.put(Constants.ITEM_PROPS, propNumMap); //--------------------加载普通物品数量上限表到系列中---------------- log.info("加载普通物品数量上限表到系列:" + seriesName); Map itemNumMap = FileUtil.getItemNumModel(configFilePath); result.put(Constants.ITEM_NOR, itemNumMap); TimingAcquisition.seriesBackpackNumMap.put(seriesName, result); log.info("背包数量上限表加载到系列完成"); //--------------------加载NPC配置表到系列中---------------- log.info("加载NPC配置表到系列:" + seriesName); Map personMap = FileUtil.getPersonModel(configFilePath); TimingAcquisition.seriesPersonMap.put(seriesName, personMap); log.info("NPC配置表加载到系列完成"); } } } } } catch (Exception e) { e.printStackTrace(); } } /** * 从json文件中读取各配置文件的信息 * @param path * @param fileName * @return String */ public static String getConfigFromFile(String path, String fileName) { BufferedReader reader = null; String lastStr = ""; try { FileInputStream fileInputStream = new FileInputStream(path + fileName); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); reader = new BufferedReader(inputStreamReader); String stringTmp = null; while ((stringTmp = reader.readLine()) != null) { lastStr += stringTmp; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (Exception e) { e.printStackTrace(); } } return lastStr; } /** * 读取json文件数据 * @param path json文件路径 * @param fileName * @return 字符串 */ public static String readJson(String path, String fileName) { String jsonPath = path + fileName; File jsonFile = new File(jsonPath); try { FileReader fileReader = new FileReader(jsonFile); BufferedReader reader = new BufferedReader(fileReader); StringBuilder sb = new StringBuilder(); while (true) { int ch = reader.read(); if (ch != -1) { sb.append((char) ch); } else { break; } } fileReader.close(); reader.close(); return sb.toString(); } catch (IOException e) { return ""; } } /** * 将话术转成map格式 * @param * @return Map>> */ public static Map>> loadSpeechToMap(String speechFilePath, String fileName) { Map>> speechMap = new HashMap<>(); try { InputStream in = new FileInputStream(speechFilePath + fileName); Yaml yaml = new Yaml(); speechMap = yaml.load(in); } catch (Exception e) { e.printStackTrace(); } return speechMap; } /** * 读取玩家json文件的数据 * @param path json文件路径 * @param gateNo * @return 字符串 */ public static String loadPlayerData(String path, String gateNo) { BufferedReader reader = null; String fileName = gateNo + ".json"; String lastStr = ""; File file = new File(path + fileName); if (!file.exists()) { return lastStr; } try { FileInputStream fileInputStream = new FileInputStream(path + fileName); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); reader = new BufferedReader(inputStreamReader); String stringTmp = null; while ((stringTmp = reader.readLine()) != null) { lastStr += stringTmp; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (Exception e) { e.printStackTrace(); } } return lastStr; } /** * 读取json文件的数据 * @param path json文件路径 * @param fileName * @return 字符串 */ public static String readJsonFile(String path, String fileName) { BufferedReader reader = null; String lastStr = ""; File file = new File(path + "/" + fileName); if (!file.exists()) { return lastStr; } try { FileInputStream fileInputStream = new FileInputStream(path + "/" + fileName); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); reader = new BufferedReader(inputStreamReader); String stringTmp = null; while ((stringTmp = reader.readLine()) != null) { lastStr += stringTmp; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (Exception e) { e.printStackTrace(); } } return lastStr; } /** * 加载玩家所有关卡的声望值 */ public static void loadSeriesReputation(String uid, String filePath) { File file = new File(filePath); ReputationDto reputation = new ReputationDto(); try { if (file.exists()) { File[] fileList = file.listFiles(); //玩家的所有系列 if (!StringUtils.isEmpty(fileList)) { if (fileList.length != 0) { //遍历所有系列 for (File series : fileList) { String seriesName = series.getName(); //系列名称 String playerFilePath = series.getAbsolutePath(); File[] gateNoList = series.listFiles(); //玩家该系列的所有关卡 if (!StringUtils.isEmpty(gateNoList)) { if (gateNoList.length != 0) { int count = 0; //统计声望值 reputation.setUid(uid); //遍历系列下的所有关卡 for (File gate : gateNoList) { String gateNo = gate.getName(); //关卡编号 String playerStr = readJsonFile(playerFilePath, gateNo); Player player = JSON.parseObject(playerStr, new TypeReference(){}); reputation.setReputation(player.getReputation()); //更新关卡排行榜 PlayerUtil.updateGateNoReputation(player, reputation); count += player.getReputation(); } reputation.setReputation(count); //更新关卡排行榜 PlayerUtil.updateSeriesReputation(uid, seriesName, reputation); } } } } } } } catch (Exception e) { e.printStackTrace(); } } /** * 加载玩家系列数据 */ public static void loadSeriesPlayer(String uid, String seriesName) { String dataPath = MD5Util.changeToDataPath(uid, seriesName); Map> seriesReputationQueueMap = TimingAcquisition.seriesReputationQueueMap; //取出存放玩家系列数据 String playerSeriesStr = readJsonFile(dataPath, uid + ".json"); if (!StringUtils.isEmpty(playerSeriesStr)) { PlayerSeriesDto playerSeries = JSON.parseObject(playerSeriesStr, new TypeReference(){}); //还没有该玩家的系列汇总数据 if (StringUtils.isEmpty(TimingAcquisition.seriesPlayerMap.get(seriesName))) { Map playerSeriesMap = new HashMap<>(); playerSeriesMap.put(uid, playerSeries); TimingAcquisition.seriesPlayerMap.put(seriesName, playerSeriesMap); } else { if (StringUtils.isEmpty(TimingAcquisition.seriesPlayerMap.get(seriesName).get(uid))) { TimingAcquisition.seriesPlayerMap.get(seriesName).put(uid, playerSeries); } } //更新系列排行榜 if (!StringUtils.isEmpty(playerSeries)) { if (StringUtils.isEmpty(seriesReputationQueueMap.get(seriesName))) { //新增系列排行榜 PriorityBlockingQueue seriesReputationQueue = new PriorityBlockingQueue<>(); seriesReputationQueue.add(playerSeries.getTotalReputation()); seriesReputationQueueMap.put(seriesName, seriesReputationQueue); } else { //更新系列排行榜中用户的旧声望 seriesReputationQueueMap.get(seriesName).add(playerSeries.getTotalReputation()); } } } } }