Commit 61a5c717 by 周君强

1.推理项目后台搭建

parents
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="InferenceGame" />
</profile>
</annotationProcessing>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="InferenceGame" options="-parameters" />
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="http://maven.aliyun.com/nexus/content/groups/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>InferenceGame</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>InferenceGame</name>
<build>
<finalName>InferenceGame</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
\ No newline at end of file
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();
}
}
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";
}
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;
}
}
);
}
}
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<String, Map<Integer, List<String>>> askSpeechMap = new ConcurrentHashMap<>();
/**
* 任务模块话术
*/
public volatile static Map<String, Map<Integer, List<String>>> taskSpeechMap = new ConcurrentHashMap<>();
/**
* 查看模块话术
*/
public volatile static Map<String, Map<Integer, List<String>>> checkSpeechMap = new ConcurrentHashMap<>();
/**
* 通过yaml文件加载所有话术到缓存中
*/
public static void speechLoading() {
log.info("---------开始加载话术到缓存中---------");
Map<String, Map<Integer, List<String>>> askSpeech = new HashMap<>();
Map<String, Map<Integer, List<String>>> taskSpeech = new HashMap<>();
Map<String, Map<Integer, List<String>>> 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("---------加载话术到缓存的操作完成---------");
}
}
package com.qiwu.inference.common.cache;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class CacheGlobal {
/**
* cache全局缓存对象
* */
public volatile static ConcurrentMap<String, MyCache> concurrentMap = new ConcurrentHashMap<>();
}
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);
}
}
}
package com.qiwu.inference.common.cache;
import lombok.Data;
@Data
public class MyCache implements Comparable<MyCache>{
/**
* 缓存键
* */
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);
}
}
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();
}
}
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;
}
}
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<String> 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<String> 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<String> 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;
}
}
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<String, Map<String, Map<String, String>>> properties = new LinkedHashMap<>();
/**
* 将话术转成map格式
* @param
* @return Map<String, Map<String, List<String>>>
*/
public static Map<String, Map<Integer, List<String>>> loadSpeechToMap(String fileName) {
Map<String, Map<Integer, List<String>>> 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<String, Map<String, List<String>>>
*/
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;
}
}
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;
}
}
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 玩家称呼;
}
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;
}
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;
}
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;
}
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 保存附加返回;
}
package com.qiwu.inference.dto.response;
import lombok.Data;
@Data
public class ResultVarsResponse {
/**
* 返回变量集合
*/
private ResultResponse vars;
/**
* 返回组属性集合
*/
private ResultResponse groupVars;
/**
* 返回组属性集合
*/
private ResultResponse botPostData;
}
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);
}
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;
}
}
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
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/
spring:
profiles:
active: dev
\ No newline at end of file
#存放路径已改为:/home/inferenceGame/speech/
aside:
100102:
- 【旁白@正常】我又不认识你,为什么要告诉你?
100103:
- 【旁白@正常】不好意思,我不知道
person:
100102:
- 【姓名@正常】我又不认识你,为什么要告诉你?
100103:
- 【姓名@正常】不好意思,我不知道
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment