package com.qiwu.inference.common; import com.qiwu.inference.common.utils.PersistenceUtil; import com.qiwu.inference.common.utils.YamlUtil; import com.qiwu.inference.dto.Person; import com.qiwu.inference.dto.Player; import com.qiwu.inference.pojo.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @Component public class TimingAcquisition { static Logger log = LoggerFactory.getLogger(TimingAcquisition.class); /** * 关卡信息,Map> */ public volatile static Map> gateInfoMap = new ConcurrentHashMap<>(); /** * 用户id跟关卡编号对应关系 */ public volatile static Map uidGateNoMap = new ConcurrentHashMap<>(); /** * 玩家信息,Map */ public volatile static Map playerMap = new ConcurrentHashMap<>(); /** * 场所配置信息 */ public volatile static Map locationMap = new ConcurrentHashMap<>(); /** * 线索配置信息 */ public volatile static Map clueMap = new ConcurrentHashMap<>(); /** * 线索类型配置信息 */ public volatile static Map clueTypeMap = new ConcurrentHashMap<>(); /** * 需求任务配置信息 */ public volatile static Map demandTaskMap = new ConcurrentHashMap<>(); /** * 性格配置信息 */ public volatile static Map dispositionMap = new ConcurrentHashMap<>(); /** * 系统配置信息 */ public volatile static Map systemConfigMap = new ConcurrentHashMap<>(); /** * 询问模块话术 */ public volatile static Map>> askSpeechMap = new ConcurrentHashMap<>(); /** * 任务模块话术 */ public volatile static Map>> taskSpeechMap = new ConcurrentHashMap<>(); /** * 查看模块话术 */ public volatile static Map>> checkSpeechMap = new ConcurrentHashMap<>(); /** * 查看模块话术 */ public volatile static Map>> barrierSpeechMap = new ConcurrentHashMap<>(); /** * 通过yaml文件加载所有话术到缓存中 */ public static void speechLoading(String speechFilePath) { log.info("---------开始加载话术到缓存中---------"); Map>> askSpeech = new HashMap<>(); Map>> taskSpeech = new HashMap<>(); Map>> checkSpeech = new HashMap<>(); Map>> barrierSpeech = new HashMap<>(); try { askSpeech = PersistenceUtil.loadSpeechToMap(speechFilePath,"ask.yaml"); taskSpeech = PersistenceUtil.loadSpeechToMap(speechFilePath,"task.yaml"); checkSpeech = PersistenceUtil.loadSpeechToMap(speechFilePath,"check.yaml"); barrierSpeech = PersistenceUtil.loadSpeechToMap(speechFilePath,"barrier.yaml"); } catch (Exception e) { e.printStackTrace(); } //JSON转map对象 askSpeechMap.putAll(askSpeech); taskSpeechMap.putAll(taskSpeech); checkSpeechMap.putAll(checkSpeech); barrierSpeechMap.putAll(barrierSpeech); log.info("---------加载话术到缓存的操作完成---------"); } /** * 通过线索类型获取对应id */ public static Integer getClueTypeIdByName(String name) { for (Map.Entry entry : clueTypeMap.entrySet()) { if (name.equals(entry.getValue())) { return entry.getKey(); } } return Constants.REQ_NON; } /** * 通过场所名称获取对应id */ public static String getLocationIdByName(String name) { for (Map.Entry entry : locationMap.entrySet()) { if (name.equals(entry.getValue().getName())) { return entry.getKey(); } } return Constants.NON_STR; } /** * 通过玩家名称获取对应id */ public static Integer getPersonIdByName(String uid, String name) { Integer personId = TimingAcquisition.playerMap.get(uid).getName2IdMap().get(name); if (!StringUtils.isEmpty(personId)) { return personId; } return Constants.REQ_NON; } /** * 通过id获取玩家名称 */ public static String getNameByPersonId(String uid, Integer personId) { for (Map.Entry entry : TimingAcquisition.playerMap.get(uid).getName2IdMap().entrySet()) { if (personId.equals(entry.getValue())) { return entry.getKey(); } } return Constants.NON_STR; } /** * 获取当前正在对话的人物名称 */ public static String getTalkName(String uid) { Integer personId = TimingAcquisition.playerMap.get(uid).getPersonId(); return getNameByPersonId(uid, personId); } }