diff --git b/.idea/.gitignore a/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ a/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git b/.idea/compiler.xml a/.idea/compiler.xml new file mode 100644 index 0000000..c3ba8e2 --- /dev/null +++ a/.idea/compiler.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git b/.idea/encodings.xml a/.idea/encodings.xml new file mode 100644 index 0000000..63e9001 --- /dev/null +++ a/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git b/.idea/jarRepositories.xml a/.idea/jarRepositories.xml new file mode 100644 index 0000000..5a2f139 --- /dev/null +++ a/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git b/.idea/misc.xml a/.idea/misc.xml new file mode 100644 index 0000000..132404b --- /dev/null +++ a/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git b/.idea/uiDesigner.xml a/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ a/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git b/.idea/vcs.xml a/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ a/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git b/pom.xml a/pom.xml new file mode 100644 index 0000000..15593f6 --- /dev/null +++ a/pom.xml @@ -0,0 +1,92 @@ + + + 4.0.0 + + org.example + InferenceGame + 1.0-SNAPSHOT + jar + InferenceGame + + InferenceGame + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + + + + org.springframework.boot + spring-boot-starter-parent + 2.3.0.RELEASE + + + + + org.springframework.boot + spring-boot-autoconfigure + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-logging + + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.0 + + + org.springframework.boot + spring-boot-starter-log4j2 + + + mysql + mysql-connector-java + runtime + + + com.alibaba + druid + 1.2.11 + + + com.alibaba + fastjson + 1.2.70 + + + org.projectlombok + lombok + 1.18.24 + compile + + + net.sourceforge.jexcelapi + jxl + 2.6.12 + + + + + 8 + 8 + + + \ No newline at end of file diff --git b/src/main/java/com/qiwu/inference/InferenceApplication.java a/src/main/java/com/qiwu/inference/InferenceApplication.java new file mode 100644 index 0000000..5ca6b26 --- /dev/null +++ a/src/main/java/com/qiwu/inference/InferenceApplication.java @@ -0,0 +1,21 @@ +package com.qiwu.inference; + +import com.qiwu.inference.common.TimingAcquisition; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; + +@SpringBootApplication +public class InferenceApplication { + @Bean + public RestTemplate restTemplate(RestTemplateBuilder builder) { + return builder.build(); + } + public static void main(String[] args) { + SpringApplication.run(InferenceApplication.class, args); + //加载话术 + TimingAcquisition.speechLoading(); + } +} diff --git b/src/main/java/com/qiwu/inference/common/Constants.java a/src/main/java/com/qiwu/inference/common/Constants.java new file mode 100644 index 0000000..7a28e0c --- /dev/null +++ a/src/main/java/com/qiwu/inference/common/Constants.java @@ -0,0 +1,89 @@ +package com.qiwu.inference.common; + +public class Constants { + + /** + * 玩家离线-0 + */ + public static final int OFFLINE = 0; + /** + * 玩家在线-1 + */ + public static final int ONLINE = 1; + + + /** + * 很少 + */ + public static final String FEW = "few"; + /** + * 很多 + */ + public static final String LOT = "lot"; + + + /** + * 请求的名称不存在 + */ + public static final int REQ_NON = -1; + /** + * 请求的名称随机 + */ + public static final int REQ_RANDOM = -2; + /** + * 请求的数量为"都", "统统", "全数", "所有", "全", "全部", "全都", "一起"之类 + */ + public static final int REQ_ALL = -3; + /** + * 请求的名称为首个、首位之类 + */ + public static final int REQ_FIRST = -4; + /** + * 请求的名称为中间 + */ + public static final int REQ_MIDDLE = -5; + /** + * 请求的名称为最后 + */ + public static final int REQ_LAST = -6; + /** + * 请求的名称为"其它/其他" + */ + public static final Integer REQ_OTHER = -7; + + + + /** + * --------------------操作ID,用于记录每轮操作------------------- + * + */ + + + + + /** + * 话术-旁白 + */ + public static final String SPEECH_ASIDE = "aside"; + /** + * 话术-男园丁 + */ + public static final String SPEECH_MAN = "man"; + /** + * 话术-女园丁 + */ + public static final String SPEECH_WOMAN = "woman"; + + + + + /** + * 不存在-0 + */ + public static final Integer NON = 0; + /** + * 不存在-0 + */ + public static final String NON_STR = "0"; + +} diff --git b/src/main/java/com/qiwu/inference/common/ProxyFactory.java a/src/main/java/com/qiwu/inference/common/ProxyFactory.java new file mode 100644 index 0000000..f927c62 --- /dev/null +++ a/src/main/java/com/qiwu/inference/common/ProxyFactory.java @@ -0,0 +1,69 @@ +package com.qiwu.inference.common; + +import com.qiwu.inference.common.utils.CacheUtil; +import com.qiwu.inference.common.utils.YamlUtil; +import com.qiwu.inference.dto.request.OperationRequest; +import com.qiwu.inference.dto.response.CommonResponse; +import com.qiwu.inference.dto.response.ResultVarsResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.StringUtils; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +public class ProxyFactory { + Logger log = LoggerFactory.getLogger(ProxyFactory.class); + //从yml配置文件中取出对话过期时间 + String offlineTime = YamlUtil.getValueByKey("offlineTime"); + + private Object target; + public ProxyFactory(Object target) { + this.target = target; + } + + public Object getProxyInstance() { + return Proxy.newProxyInstance( + target.getClass().getClassLoader(), + target.getClass().getInterfaces(), + new InvocationHandler() { + @Override + public Object invoke(Object o, Method method, Object[] args) throws Throwable { + OperationRequest request = (OperationRequest) args[0]; + + //该玩家以唯一标识加入全局缓存,用于玩家离线判断 + String uniqueUid = request.getUid() + "%#%online%#%"; + CacheUtil.put(uniqueUid, "", Long.parseLong(offlineTime)); + //通过反射获取方法返参 + Object var = method.invoke(target, args); + //检索到有新消息 + CommonResponse commonResponse = (CommonResponse) var; + ResultVarsResponse data = commonResponse.getData(); + if (!StringUtils.isEmpty(data)) { + //所有“后台附加返回”为null值的都给转成“无效返回” + if (StringUtils.isEmpty(data.getVars().get后台附加返回())) { + data.getVars().set后台附加返回("无效返回"); + } + //所有“后台附加返回”为null值的都给转成“无效返回” + if (StringUtils.isEmpty(data.getVars().get后台附加返回2())) { + data.getVars().set后台附加返回2("无效返回"); + } + + String feedContent = "后台返回参数 = [" + + "code:" + commonResponse.getCode() + + ", msg:" + commonResponse.getMsg() + + ", 反馈ID:" + data.getVars().get反馈ID() + + ", 反馈文本:" + data.getVars().get反馈文本() + + ", 后台附加返回:" + data.getVars().get后台附加返回() + + ", 后台附加返回2:" + data.getVars().get后台附加返回2() + + "]"; + //玩家有新消息提示 + log.info(feedContent); + } + return var; + } + } + ); + } +} diff --git b/src/main/java/com/qiwu/inference/common/TimingAcquisition.java a/src/main/java/com/qiwu/inference/common/TimingAcquisition.java new file mode 100644 index 0000000..46580f2 --- /dev/null +++ a/src/main/java/com/qiwu/inference/common/TimingAcquisition.java @@ -0,0 +1,49 @@ +package com.qiwu.inference.common; + +import com.qiwu.inference.common.utils.YamlUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; + +@Component +public class TimingAcquisition { + static Logger log = LoggerFactory.getLogger(TimingAcquisition.class); + + /** + * 询问模块话术 + */ + public volatile static Map>> askSpeechMap = new ConcurrentHashMap<>(); + /** + * 任务模块话术 + */ + public volatile static Map>> taskSpeechMap = new ConcurrentHashMap<>(); + /** + * 查看模块话术 + */ + public volatile static Map>> checkSpeechMap = new ConcurrentHashMap<>(); + + /** + * 通过yaml文件加载所有话术到缓存中 + */ + public static void speechLoading() { + log.info("---------开始加载话术到缓存中---------"); + Map>> askSpeech = new HashMap<>(); + Map>> taskSpeech = new HashMap<>(); + Map>> checkSpeech = new HashMap<>(); + try { + askSpeech = YamlUtil.loadSpeechToMap("ask.yaml"); + taskSpeech = YamlUtil.loadSpeechToMap("task.yaml"); + checkSpeech = YamlUtil.loadSpeechToMap("check.yaml"); + } catch (Exception e) { + e.printStackTrace(); + } + //JSON转map对象 + askSpeechMap.putAll(askSpeech); + taskSpeechMap.putAll(taskSpeech); + checkSpeechMap.putAll(checkSpeech); + + log.info("---------加载话术到缓存的操作完成---------"); + } +} diff --git b/src/main/java/com/qiwu/inference/common/cache/CacheGlobal.java a/src/main/java/com/qiwu/inference/common/cache/CacheGlobal.java new file mode 100644 index 0000000..7ef2460 --- /dev/null +++ a/src/main/java/com/qiwu/inference/common/cache/CacheGlobal.java @@ -0,0 +1,11 @@ +package com.qiwu.inference.common.cache; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +public class CacheGlobal { + /** + * cache全局缓存对象 + * */ + public volatile static ConcurrentMap concurrentMap = new ConcurrentHashMap<>(); +} diff --git b/src/main/java/com/qiwu/inference/common/cache/ExpireThread.java a/src/main/java/com/qiwu/inference/common/cache/ExpireThread.java new file mode 100644 index 0000000..ba9d8ce --- /dev/null +++ a/src/main/java/com/qiwu/inference/common/cache/ExpireThread.java @@ -0,0 +1,42 @@ +package com.qiwu.inference.common.cache; + +import com.alibaba.fastjson.JSON; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.stereotype.Component; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; + +/** + * 过期缓存检测线程 + * */ +@EnableAsync +@Component +public class ExpireThread implements Runnable{ + @Override + public void run() { + while (true) { + try { + //每60秒检测一次 + TimeUnit.SECONDS.sleep(60); + expireCache(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + /** + * 清除全局缓存对象中过期缓存 + */ + public void expireCache() { + for (String key : CacheGlobal.concurrentMap.keySet()) { + MyCache myCache = CacheGlobal.concurrentMap.get(key); + long timeoutTime = System.currentTimeMillis() - myCache.getWriteTime(); + if (timeoutTime < myCache.getExpireTime()) { + continue; + } + //清除过期缓存 + CacheGlobal.concurrentMap.remove(key); + } + } +} diff --git b/src/main/java/com/qiwu/inference/common/cache/MyCache.java a/src/main/java/com/qiwu/inference/common/cache/MyCache.java new file mode 100644 index 0000000..7b3d02f --- /dev/null +++ a/src/main/java/com/qiwu/inference/common/cache/MyCache.java @@ -0,0 +1,36 @@ +package com.qiwu.inference.common.cache; + +import lombok.Data; + +@Data +public class MyCache implements Comparable{ + /** + * 缓存键 + * */ + private Object key; + /** + * 缓存值 + * */ + private Object value; + /** + * 最后访问时间 + * */ + private long lastTime; + /** + * 创建时间 + * */ + private long writeTime; + /** + * 存活时间 + * */ + private long expireTime; + /** + * 命中次数 + * */ + private Integer hitCount; + + @Override + public int compareTo(MyCache mycache) { + return hitCount.compareTo(mycache.hitCount); + } +} diff --git b/src/main/java/com/qiwu/inference/common/utils/CacheUtil.java a/src/main/java/com/qiwu/inference/common/utils/CacheUtil.java new file mode 100644 index 0000000..77ccee8 --- /dev/null +++ a/src/main/java/com/qiwu/inference/common/utils/CacheUtil.java @@ -0,0 +1,66 @@ +package com.qiwu.inference.common.utils; + +import com.qiwu.inference.common.cache.CacheGlobal; +import com.qiwu.inference.common.cache.MyCache; +import org.springframework.util.StringUtils; + +/** + * 缓存操作工具 + * */ +public class CacheUtil { + /** + * 添加缓存 + * @param key + * @param value + * @param expire + */ + public static void put(String key, Object value, long expire) { + if (StringUtils.isEmpty(key)) { + return; + } + if (CacheGlobal.concurrentMap.containsKey(key)) { + MyCache myCache = CacheGlobal.concurrentMap.get(key); + myCache.setHitCount(myCache.getHitCount() + 1); + myCache.setLastTime(System.currentTimeMillis()); + myCache.setExpireTime(System.currentTimeMillis()); + myCache.setValue(value); + } + MyCache myCache = new MyCache(); + myCache.setKey(key); + myCache.setValue(value); + myCache.setWriteTime(System.currentTimeMillis()); + myCache.setLastTime(System.currentTimeMillis()); + myCache.setExpireTime(expire); + myCache.setHitCount(1); + CacheGlobal.concurrentMap.put(key, myCache); + } + + /** + * 获取缓存 + * @param key + */ + public static Object get(String key) { + if (StringUtils.isEmpty(key)) { + return null; + } + if (CacheGlobal.concurrentMap.isEmpty()) { + return null; + } + if (!CacheGlobal.concurrentMap.containsKey(key)) { + return null; + } + MyCache myCache = CacheGlobal.concurrentMap.get(key); + if (myCache == null) { + return null; + } + //惰性删除 + long timeOutTime = System.currentTimeMillis() - myCache.getWriteTime(); + if (timeOutTime >= myCache.getExpireTime()) { + CacheGlobal.concurrentMap.remove(key); + return null; + } + myCache.setHitCount(myCache.getHitCount() + 1); + myCache.setWriteTime(System.currentTimeMillis()); + return myCache.getValue(); + } +} diff --git b/src/main/java/com/qiwu/inference/common/utils/RequestConversionUtil.java a/src/main/java/com/qiwu/inference/common/utils/RequestConversionUtil.java new file mode 100644 index 0000000..bce23f5 --- /dev/null +++ a/src/main/java/com/qiwu/inference/common/utils/RequestConversionUtil.java @@ -0,0 +1,29 @@ +package com.qiwu.inference.common.utils; + +import com.qiwu.inference.dto.request.CallRequest; +import com.qiwu.inference.dto.request.CommonRequest; +import com.qiwu.inference.dto.request.OperationRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.StringUtils; + +public class RequestConversionUtil { + Logger log = LoggerFactory.getLogger(RequestConversionUtil.class); + /** + * 将请求的中文转成系统数据 + * @param commonRequest + * @return OperationRequest + */ + public static OperationRequest request2Operation(CommonRequest commonRequest) { + OperationRequest operationRequest = new OperationRequest(); + CallRequest callRequest = commonRequest.getVars(); + operationRequest.setUid(commonRequest.getUid()); + + //---------------------存在姓名----------------------- + String name = callRequest.get玩家称呼(); + if (!StringUtils.isEmpty(name)) { + operationRequest.setName(name); + } + return operationRequest; + } +} diff --git b/src/main/java/com/qiwu/inference/common/utils/SpeechUtil.java a/src/main/java/com/qiwu/inference/common/utils/SpeechUtil.java new file mode 100644 index 0000000..fe863e2 --- /dev/null +++ a/src/main/java/com/qiwu/inference/common/utils/SpeechUtil.java @@ -0,0 +1,206 @@ +package com.qiwu.inference.common.utils; + +import com.qiwu.inference.common.TimingAcquisition; + +import java.util.List; +import java.util.Random; + +public class SpeechUtil { + /** + * 取指定范围内的随机数 + * @param num1 + * @param num2 + * @return Integer + */ + public static Integer getRandomNum(Integer num1, Integer num2) { + Integer min = num1 > num2 ? num2 : num1; + int sub = num1 > num2 ? num1-num2 : num2-num1; + sub = sub == 0 ? 1 : sub; + Random random = new Random(); + //random.nextInt(1)结果为0,random.nextInt(0)会报错 + return random.nextInt(sub) + min; + } + /** + * 从询问模块话术获取话术 + * @param someone + * @param feedText + * @return String + */ + public static String getAskSpeech(String someone, Integer feedText) { + String result = ""; + List speech = TimingAcquisition.askSpeechMap.get(someone).get(feedText); + Integer i = getRandomNum(0, speech.size()); + result = speech.get(i); + return result; + } + + /** + * 从任务模块话术获取话术 + * @param someone + * @param feedText + * @return String + */ + public static String getTaskSpeech(String someone, Integer feedText) { + String result = ""; + List speech = TimingAcquisition.taskSpeechMap.get(someone).get(feedText); + Integer i = getRandomNum(0, speech.size()); + result = speech.get(i); + return result; + } + + /** + * 从查看模块话术获取话术 + * @param someone + * @param feedText + * @return String + */ + public static String getHelpSpeech(String someone, Integer feedText) { + String result = ""; + List speech = TimingAcquisition.checkSpeechMap.get(someone).get(feedText); + Integer i = getRandomNum(0, speech.size()); + result = speech.get(i); + return result; + } + + /** + * 替换话术中的一个数字 + * @param one + * @return String + */ + public static String speechReplaceNum(String text, String one) { + return text.replace("【1】", one); + } + + /** + * 替换话术中的两个数字 + * @param one + * @param two + * @return String + */ + public static String speechReplaceNum(String text, String one, String two) { + return text.replace("【1】", one).replace("【2】", two); + } + + /** + * 替换话术中的三个数字 + * @param one + * @param two + * @param three + * @return String + */ + public static String speechReplaceNum(String text, String one, String two, String three) { + return text.replace("【1】", one).replace("【2】", two).replace("【3】", three); + } + + /** + * 替换话术中的四个数字 + * @param one + * @param two + * @param three + * @param four + * @return String + */ + public static String speechReplaceNum(String text, String one, String two, String three, String four) { + return text.replace("【1】", one).replace("【2】", two).replace("【3】", three).replace("【4】", four); + } + + /** + * 替换话术中的五个数字 + * @param one + * @param two + * @param three + * @param four + * @param five + * @return String + */ + public static String speechReplaceNum(String text, String one, String two, String three, String four, String five) { + return text.replace("【1】", one).replace("【2】", two).replace("【3】", three) + .replace("【4】", four).replace("【5】", five); + } + + /** + * 替换话术中的六个数字 + * @param one + * @param two + * @param three + * @param four + * @param five + * @param six + * @return String + */ + public static String speechReplaceNum(String text, String one, String two, String three, String four, String five, String six) { + return text.replace("【1】", one).replace("【2】", two).replace("【3】", three) + .replace("【4】", four).replace("【5】", five).replace("【6】", six); + } + + /** + * 替换话术中的七个数字 + * @param one + * @param two + * @param three + * @param four + * @param five + * @param six + * @param seven + * @return String + */ + public static String speechReplaceNum(String text, String one, String two, String three, String four, String five, String six, String seven) { + return text.replace("【1】", one).replace("【2】", two).replace("【3】", three) + .replace("【4】", four).replace("【5】", five).replace("【6】", six).replace("【7】", seven); + } + + /** + * 替换话术中的八个数字 + * @param one + * @param two + * @param three + * @param four + * @param five + * @param six + * @param seven + * @param eight + * @return String + */ + public static String speechReplaceNum(String text, String one, String two, String three, String four, String five, String six, String seven, String eight) { + return text.replace("【1】", one).replace("【2】", two).replace("【3】", three) + .replace("【4】", four).replace("【5】", five).replace("【6】", six) + .replace("【7】", seven).replace("【8】", eight); + } + + /** + * 替换话术中的九个数字 + * @param one + * @param two + * @param three + * @param four + * @param five + * @param six + * @param seven + * @param eight + * @param nine + * @return String + */ + public static String speechReplaceNum(String text, String one, String two, String three, String four, String five, String six, String seven, String eight, String nine) { + return text.replace("【1】", one).replace("【2】", two).replace("【3】", three) + .replace("【4】", four).replace("【5】", five).replace("【6】", six) + .replace("【7】", seven).replace("【8】", eight).replace("【9】", nine); + } + + /** + * 删除话术的开头“【旁白@正常】” + * @param text + */ + public static String deleteAsideHead(String text) { + return text.replaceAll("【(.+)@(.{2,4})】", ""); + } + + /** + * 合并话术 + * @param text1 + * @param text2 + */ + public static String mergeSpeech(String text1, String text2) { + text2 = deleteAsideHead(text2); + return text1 + text2; + } +} diff --git b/src/main/java/com/qiwu/inference/common/utils/YamlUtil.java a/src/main/java/com/qiwu/inference/common/utils/YamlUtil.java new file mode 100644 index 0000000..b7bacd5 --- /dev/null +++ a/src/main/java/com/qiwu/inference/common/utils/YamlUtil.java @@ -0,0 +1,57 @@ +package com.qiwu.inference.common.utils; + +import lombok.Data; +import org.springframework.core.io.ClassPathResource; +import org.springframework.stereotype.Component; +import org.yaml.snakeyaml.Yaml; + +import java.io.FileInputStream; +import java.io.InputStream; +import java.util.*; + +@Component +@Data +public class YamlUtil { + //获取话术存放的路径 + public static String speechFilePath = YamlUtil.getValueByKey("speechFilePath"); + + private static LinkedHashMap>> properties = new LinkedHashMap<>(); + /** + * 将话术转成map格式 + * @param + * @return Map>> + */ + public static Map>> loadSpeechToMap(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; + } + + /** + * 读取配置文件yml中的某个值,只支持取第一层的值 + * @param + * @return Map>> + */ + public static String getValueByKey(String key) { + String value = ""; + try { + Yaml yaml = new Yaml(); + ClassPathResource resource = new ClassPathResource("\\application.yml"); + properties = yaml.load(resource.getInputStream()); + String val = properties.get("spring").get("profiles").get("active"); + ClassPathResource resourceNext = new ClassPathResource("\\application-" + val + ".yml"); + Map map = yaml.load(resourceNext.getInputStream()); + value = String.valueOf(map.get(key)); + } catch (Exception e) { + e.printStackTrace(); + } + return value; + } + +} diff --git b/src/main/java/com/qiwu/inference/controller/CommonController.java a/src/main/java/com/qiwu/inference/controller/CommonController.java new file mode 100644 index 0000000..6d5fad7 --- /dev/null +++ a/src/main/java/com/qiwu/inference/controller/CommonController.java @@ -0,0 +1,58 @@ +package com.qiwu.inference.controller; + +import com.qiwu.inference.common.ProxyFactory; +import com.qiwu.inference.common.utils.RequestConversionUtil; +import com.qiwu.inference.dto.request.CommonRequest; +import com.qiwu.inference.dto.request.OperationRequest; +import com.qiwu.inference.dto.response.CommonResponse; +import com.qiwu.inference.dto.response.ResultResponse; +import com.qiwu.inference.dto.response.ResultVarsResponse; +import com.qiwu.inference.service.ask.AskService; +import com.qiwu.inference.service.ask.impl.AskServiceImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.StringUtils; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/inference") +public class CommonController { + Logger log = LoggerFactory.getLogger(CommonController.class); + + //询问模块动态代理 + AskService askService = new AskServiceImpl(); + AskService askServiceProxy = (AskService) new ProxyFactory(askService).getProxyInstance(); + + @PostMapping(value = "/ask", produces = "application/json;charset=UTF-8") + public CommonResponse startGame(@RequestBody CommonRequest commonRequest) { + log.info(commonRequest.getUid() + "当前操作的原始请求参数:" + commonRequest.getVars()); + CommonResponse result = new CommonResponse(); + + OperationRequest operationReq = RequestConversionUtil.request2Operation(commonRequest); + String operateName = commonRequest.getVars().get操作名(); + log.info(operationReq.getUid() + "开始执行操作:【" + operateName + "】, 请求参数:" + operationReq); + //操作名为null + if (StringUtils.isEmpty(operateName)) { + log.info(operationReq.getUid() + "未提供操作名,无法操作"); + ResultVarsResponse resultVarsResponse = new ResultVarsResponse(); + ResultResponse resultResponse = new ResultResponse(); + result.setCode(1); + result.setMsg("成功"); + resultResponse.set反馈ID(100000); + resultResponse.set反馈文本("【旁白@正常】你想做什么呢?"); + resultVarsResponse.setVars(resultResponse); + result.setData(resultVarsResponse); + return result; + } + switch (operateName) { + case "开始游戏": + result = askServiceProxy.ask(operationReq); + break; + } + log.info(operationReq.getUid() + "已完成操作:【" + commonRequest.getVars().get操作名() + "】"); + return result; + } +} diff --git b/src/main/java/com/qiwu/inference/dto/request/CallRequest.java a/src/main/java/com/qiwu/inference/dto/request/CallRequest.java new file mode 100644 index 0000000..5b449e5 --- /dev/null +++ a/src/main/java/com/qiwu/inference/dto/request/CallRequest.java @@ -0,0 +1,17 @@ +package com.qiwu.inference.dto.request; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +@Data +public class CallRequest { + /** + * 操作指令:查看,种植,收取,取消,选择,重置,升级,更名,移动,删除,排序,购买,出售等 + */ + private String 操作名; + /** + * 玩家称呼 + */ + @JsonProperty(value = "GB_玩家称呼") + private String 玩家称呼; +} diff --git b/src/main/java/com/qiwu/inference/dto/request/CommonRequest.java a/src/main/java/com/qiwu/inference/dto/request/CommonRequest.java new file mode 100644 index 0000000..21fe7bf --- /dev/null +++ a/src/main/java/com/qiwu/inference/dto/request/CommonRequest.java @@ -0,0 +1,35 @@ +package com.qiwu.inference.dto.request; + +import lombok.Data; + +@Data +public class CommonRequest { + /** + * 对话标识 + */ + private String chatKey; + /** + * 对话标识 + */ + private String channelId; + /** + * 用户唯一ID + */ + private String uid; + /** + * 预处理之后的用户话语 + */ + private String queryText; + /** + * 用户原始话语 + */ + private String originText; + /** + * bot帐号 + */ + private String botAccount; + /** + * bot帐号 + */ + private CallRequest vars; +} diff --git b/src/main/java/com/qiwu/inference/dto/request/OperationRequest.java a/src/main/java/com/qiwu/inference/dto/request/OperationRequest.java new file mode 100644 index 0000000..f5e55f6 --- /dev/null +++ a/src/main/java/com/qiwu/inference/dto/request/OperationRequest.java @@ -0,0 +1,27 @@ +package com.qiwu.inference.dto.request; + +import lombok.Data; + +@Data +public class OperationRequest { + /** + * 操作名 + */ + private String operate; + /** + * 用户唯一ID + */ + private String uid; + /** + * 名称 + */ + private String name; + /** + * 性别: 男-1,女-2 + */ + private Integer gender; + /** + * 地点 + */ + private String place; +} diff --git b/src/main/java/com/qiwu/inference/dto/response/CommonResponse.java a/src/main/java/com/qiwu/inference/dto/response/CommonResponse.java new file mode 100644 index 0000000..4a88350 --- /dev/null +++ a/src/main/java/com/qiwu/inference/dto/response/CommonResponse.java @@ -0,0 +1,22 @@ +package com.qiwu.inference.dto.response; + +import lombok.Data; + +import java.io.Serializable; + +@Data +public class CommonResponse implements Serializable { + private static final long serialVersionUID = 1L; + /** + * 返回码 + */ + private int code; + /** + * 返回说明 + */ + private String msg; + /** + * 返回数据 + * */ + private ResultVarsResponse data; +} diff --git b/src/main/java/com/qiwu/inference/dto/response/ResultResponse.java a/src/main/java/com/qiwu/inference/dto/response/ResultResponse.java new file mode 100644 index 0000000..a120ce4 --- /dev/null +++ a/src/main/java/com/qiwu/inference/dto/response/ResultResponse.java @@ -0,0 +1,28 @@ +package com.qiwu.inference.dto.response; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +@Data +public class ResultResponse { + /** + * 反馈ID即编号 + */ + private int 反馈ID; + /** + * 反馈文本 + */ + private String 反馈文本; + /** + * 后台附加返回,bot二次确认需要的参数 + */ + private String 后台附加返回; + /** + * 后台附加返回2,bot二次确认升级任务完成情况需要的参数 + */ + private String 后台附加返回2; + /** + * 保存附加返回 + */ + private String 保存附加返回; +} diff --git b/src/main/java/com/qiwu/inference/dto/response/ResultVarsResponse.java a/src/main/java/com/qiwu/inference/dto/response/ResultVarsResponse.java new file mode 100644 index 0000000..212f2c6 --- /dev/null +++ a/src/main/java/com/qiwu/inference/dto/response/ResultVarsResponse.java @@ -0,0 +1,19 @@ +package com.qiwu.inference.dto.response; + +import lombok.Data; + +@Data +public class ResultVarsResponse { + /** + * 返回变量集合 + */ + private ResultResponse vars; + /** + * 返回组属性集合 + */ + private ResultResponse groupVars; + /** + * 返回组属性集合 + */ + private ResultResponse botPostData; +} diff --git b/src/main/java/com/qiwu/inference/service/ask/AskService.java a/src/main/java/com/qiwu/inference/service/ask/AskService.java new file mode 100644 index 0000000..7022bad --- /dev/null +++ a/src/main/java/com/qiwu/inference/service/ask/AskService.java @@ -0,0 +1,8 @@ +package com.qiwu.inference.service.ask; + +import com.qiwu.inference.dto.request.OperationRequest; +import com.qiwu.inference.dto.response.CommonResponse; + +public interface AskService { + CommonResponse ask(OperationRequest operationReq); +} diff --git b/src/main/java/com/qiwu/inference/service/ask/impl/AskServiceImpl.java a/src/main/java/com/qiwu/inference/service/ask/impl/AskServiceImpl.java new file mode 100644 index 0000000..fcbb28a --- /dev/null +++ a/src/main/java/com/qiwu/inference/service/ask/impl/AskServiceImpl.java @@ -0,0 +1,41 @@ +package com.qiwu.inference.service.ask.impl; + +import com.qiwu.inference.common.Constants; +import com.qiwu.inference.common.utils.SpeechUtil; +import com.qiwu.inference.dto.request.OperationRequest; +import com.qiwu.inference.dto.response.CommonResponse; +import com.qiwu.inference.dto.response.ResultResponse; +import com.qiwu.inference.dto.response.ResultVarsResponse; +import com.qiwu.inference.service.ask.AskService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +@Service +public class AskServiceImpl implements AskService { + @Value("${inferenceUrl}") + private String url; +// @Autowired +// private RestTemplate restTemplate; + + @Override + public CommonResponse ask(OperationRequest operationReq) { + CommonResponse commonResponse = new CommonResponse(); + ResultVarsResponse resultVarsResponse = new ResultVarsResponse(); + ResultResponse resultResponse = new ResultResponse(); + String uid = operationReq.getUid(); + +// String result = restTemplate.getForObject(url + "/garden/steal", String.class); +// System.out.println("返回结果:" + result); + //获取对应话术 + String text = SpeechUtil.getAskSpeech(Constants.SPEECH_ASIDE, 100102); + commonResponse.setCode(1); + commonResponse.setMsg("成功"); + resultResponse.set反馈ID(100001); + resultResponse.set反馈文本(text); + resultVarsResponse.setVars(resultResponse); + commonResponse.setData(resultVarsResponse); + return commonResponse; + } +} diff --git b/src/main/resources/application-dev.yml a/src/main/resources/application-dev.yml new file mode 100644 index 0000000..8e246a8 --- /dev/null +++ a/src/main/resources/application-dev.yml @@ -0,0 +1,33 @@ +server: + port: 8080 + serlvet: + context-type:/inference + +#设置当前服务名 +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/inference_game?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC + username: root + password: root + type: com.alibaba.druid.pool.DruidDataSource + druid: + initial-size: 5 + min-idle: 5 + max-active: 20 + max-wait: 60000 + #配置间隔多久才进行一次检测,检测需要关闭的空闲连接 + time-between-eviction-runs-millis: 20000 + #配置一个连接在池中最小生存的时间 + min-evictable-idle-time-millis: 300000 + + + +#玩家无操作时长,默认下线 +offlineTime: 180000 + +speechFilePath: /home/inferenceGame/speech/ + +#分享关系查看地址 +inferenceUrl: http://127.0.0.1:8083 + diff --git b/src/main/resources/application-pro.yml a/src/main/resources/application-pro.yml new file mode 100644 index 0000000..e69de29 --- /dev/null +++ a/src/main/resources/application-pro.yml diff --git b/src/main/resources/application-uat.yml a/src/main/resources/application-uat.yml new file mode 100644 index 0000000..a22f64f --- /dev/null +++ a/src/main/resources/application-uat.yml @@ -0,0 +1,33 @@ +server: + port: 60018 + serlvet: + context-type:/inferenceGame + +#设置当前服务名 +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/inference_game?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC + username: root + password: root + type: com.alibaba.druid.pool.DruidDataSource + druid: + initial-size: 5 + min-idle: 5 + max-active: 20 + max-wait: 60000 + #配置间隔多久才进行一次检测,检测需要关闭的空闲连接 + time-between-eviction-runs-millis: 20000 + #配置一个连接在池中最小生存的时间 + min-evictable-idle-time-millis: 300000 + + + +#玩家无操作时长,默认下线 +offlineTime: 180000 + +speechFilePath: /home/secretGarden/secretGardenSpeech/ + +#分享关系查看地址 +inferenceUrl: https://192.168.1.156/api/ + diff --git b/src/main/resources/application.yml a/src/main/resources/application.yml new file mode 100644 index 0000000..caf4dfc --- /dev/null +++ a/src/main/resources/application.yml @@ -0,0 +1,3 @@ +spring: + profiles: + active: dev \ No newline at end of file diff --git b/src/main/resources/speech/ask.yaml a/src/main/resources/speech/ask.yaml new file mode 100755 index 0000000..dc09135 --- /dev/null +++ a/src/main/resources/speech/ask.yaml @@ -0,0 +1,13 @@ +#存放路径已改为:/home/inferenceGame/speech/ +aside: + 100102: + - 【旁白@正常】我又不认识你,为什么要告诉你? + 100103: + - 【旁白@正常】不好意思,我不知道 + + +person: + 100102: + - 【姓名@正常】我又不认识你,为什么要告诉你? + 100103: + - 【姓名@正常】不好意思,我不知道 \ No newline at end of file