Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Q
qwgame
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
赵国君
qwgame
Commits
cb904f39
Commit
cb904f39
authored
Apr 24, 2019
by
赵国君
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
登陆后发送post请求,快速开始
parent
98a7786c
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
813 additions
and
737 deletions
+813
-737
EngineUtils.java
...om/qiwu/chatbotbackend/actiongenerate/DB/EngineUtils.java
+20
-0
ChatCache.java
src/main/java/com/qiwu/chatbotbackend/entity/ChatCache.java
+190
-128
GlobalVariableConstructor.java
.../chatbotbackend/gameDesign/GlobalVariableConstructor.java
+69
-60
MapConstructor.java
...va/com/qiwu/chatbotbackend/gameDesign/MapConstructor.java
+6
-7
ChatCacheController.java
...m/qiwu/chatbotbackend/processing/ChatCacheController.java
+1
-1
FormsPoster.java
.../java/com/qiwu/chatbotbackend/processing/FormsPoster.java
+0
-4
UserInfoController.java
...om/qiwu/chatbotbackend/processing/UserInfoController.java
+499
-507
GameDesignServlet.java
...va/com/qiwu/chatbotbackend/servlet/GameDesignServlet.java
+1
-14
GetAnswerServlet.java
...ava/com/qiwu/chatbotbackend/servlet/GetAnswerServlet.java
+27
-16
No files found.
src/main/java/com/qiwu/chatbotbackend/actiongenerate/DB/EngineUtils.java
View file @
cb904f39
...
...
@@ -199,6 +199,26 @@ public class EngineUtils {
return
nickname
;
}
public
String
queryPlayerNickNameByName
(
String
userName
){
String
sql
=
"select nickname from chats where bot = ? limit 1"
;
String
nickname
=
""
;
try
{
PreparedStatement
ps
=
conn
.
prepareStatement
(
sql
);
ps
.
setString
(
1
,
userName
);
ResultSet
rs
=
ps
.
executeQuery
();
if
(
rs
.
next
())
nickname
=
rs
.
getString
(
"nickname"
);
ps
.
close
();
rs
.
close
();
conn
.
close
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
nickname
;
}
}
src/main/java/com/qiwu/chatbotbackend/entity/ChatCache.java
View file @
cb904f39
package
com
.
qiwu
.
chatbotbackend
.
entity
;
import
com.qiwu.chatbotbackend.GameEnvironment.GameEnv
;
import
org.springframework.beans.factory.DisposableBean
;
import
java.io.IOException
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
java.util.Map
;
public
class
ChatCache
implements
DisposableBean
{
public
String
ENV_ERROR
=
"游戏平台初始化失败"
;
// private String chatKey = "不知道";
private
String
userKey
=
""
;
private
String
randKey
=
""
;
private
String
botAccount
=
""
;
private
Map
<
String
,
String
>
infoPoint
=
null
;
private
GameEnv
gameEnv
=
null
;
private
Boolean
gameStart
=
false
;
private
Boolean
reload
=
false
;
public
String
getBotAccount
()
{
return
botAccount
;
}
public
void
setBotAccount
(
String
botAccount
)
{
this
.
botAccount
=
botAccount
;
}
public
String
getUserKey
()
{
return
userKey
;
}
public
void
setUserKey
(
String
userKey
)
{
this
.
userKey
=
userKey
;
}
public
String
getRandKey
()
{
return
randKey
;
}
public
void
setRandKey
(
String
randKey
)
{
this
.
randKey
=
randKey
;
}
public
Map
<
String
,
String
>
getInfoPoint
()
{
return
infoPoint
;
}
public
void
setInfoPoint
(
Map
<
String
,
String
>
infoPoint
)
{
this
.
infoPoint
=
infoPoint
;
}
public
boolean
isGameStart
()
{
return
gameStart
;
}
public
void
setGameStart
(
boolean
gameStart
)
{
this
.
gameStart
=
gameStart
;
}
public
Boolean
getReload
()
{
return
reload
;
}
public
void
setReload
(
Boolean
reload
)
{
this
.
reload
=
reload
;
}
public
GameEnv
getGameEnv
()
{
return
gameEnv
;
}
public
void
setGameEnv
(
GameEnv
gameEnv
)
{
this
.
gameEnv
=
gameEnv
;
}
public
ChatCache
(
String
userKey
,
String
randKey
,
GameEnv
gameEnv
,
Map
<
String
,
String
>
infoPoint
)
{
this
.
userKey
=
userKey
;
this
.
randKey
=
randKey
;
this
.
gameEnv
=
gameEnv
;
this
.
infoPoint
=
infoPoint
;
}
@Override
public
void
destroy
()
throws
IOException
{
}
public
ChatCache
(
UserInfo
userInfo
,
String
userKey
,
String
randKey
,
String
botType
)
throws
Exception
{
String
k
;
this
.
botAccount
=
userInfo
.
botAccount
;
this
.
infoPoint
=
new
HashMap
<
String
,
String
>();
if
(
botType
.
equals
(
"game"
))
{
try
{
this
.
gameEnv
=
new
GameEnv
(
userInfo
.
botAccount
);
System
.
out
.
println
(
"初始化时:"
+
this
.
gameEnv
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
ENV_ERROR
);
this
.
infoPoint
.
put
(
"error_message"
,
ENV_ERROR
);
e
.
printStackTrace
();
}
}
Iterator
<
String
>
iit
=
userInfo
.
varInitialMap
.
keySet
().
iterator
();
while
(
iit
.
hasNext
())
{
k
=
iit
.
next
();
this
.
infoPoint
.
put
(
k
,
userInfo
.
varInitialMap
.
get
(
k
));
}
this
.
infoPoint
.
put
(
"lastUpdateTime"
,
String
.
valueOf
(
System
.
currentTimeMillis
()));
this
.
randKey
=
randKey
;
this
.
userKey
=
userKey
;
}
public
ChatCache
(
String
userKey
,
String
randKey
)
{
this
.
userKey
=
userKey
;
this
.
randKey
=
randKey
;
}
}
package
com
.
qiwu
.
chatbotbackend
.
entity
;
import
com.alibaba.fastjson.JSONObject
;
import
com.qiwu.chatbotbackend.GameEnvironment.GameEnv
;
import
com.qiwu.chatbotbackend.processing.SimpleUtil
;
import
org.apache.http.message.BasicNameValuePair
;
import
org.springframework.beans.factory.DisposableBean
;
import
java.io.IOException
;
import
java.io.UnsupportedEncodingException
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map.Entry
;
import
java.util.Set
;
public
class
ChatCache
implements
DisposableBean
{
public
String
ENV_ERROR
=
"游戏平台初始化失败"
;
// private String chatKey = "不知道";
private
String
userKey
=
""
;
private
String
randKey
=
""
;
private
String
botAccount
=
""
;
private
Map
<
String
,
String
>
infoPoint
=
null
;
private
GameEnv
gameEnv
=
null
;
private
Boolean
gameStart
=
false
;
private
Boolean
reload
=
false
;
private
Boolean
initial
=
false
;
public
Boolean
getInitial
()
{
return
initial
;
}
public
void
setInitial
(
Boolean
initial
)
{
this
.
initial
=
initial
;
}
public
String
getBotAccount
()
{
return
botAccount
;
}
public
void
setBotAccount
(
String
botAccount
)
{
this
.
botAccount
=
botAccount
;
}
public
String
getUserKey
()
{
return
userKey
;
}
public
void
setUserKey
(
String
userKey
)
{
this
.
userKey
=
userKey
;
}
public
String
getRandKey
()
{
return
randKey
;
}
public
void
setRandKey
(
String
randKey
)
{
this
.
randKey
=
randKey
;
}
public
Map
<
String
,
String
>
getInfoPoint
()
{
return
infoPoint
;
}
public
void
setInfoPoint
(
Map
<
String
,
String
>
infoPoint
)
{
this
.
infoPoint
=
infoPoint
;
}
public
boolean
isGameStart
()
{
return
gameStart
;
}
public
void
setGameStart
(
boolean
gameStart
)
{
this
.
gameStart
=
gameStart
;
}
public
Boolean
getReload
()
{
return
reload
;
}
public
void
setReload
(
Boolean
reload
)
{
this
.
reload
=
reload
;
}
public
GameEnv
getGameEnv
()
{
return
gameEnv
;
}
public
void
setGameEnv
(
GameEnv
gameEnv
)
{
this
.
gameEnv
=
gameEnv
;
}
public
ChatCache
(
String
userKey
,
String
randKey
,
GameEnv
gameEnv
,
Map
<
String
,
String
>
infoPoint
)
{
this
.
userKey
=
userKey
;
this
.
randKey
=
randKey
;
this
.
gameEnv
=
gameEnv
;
this
.
infoPoint
=
infoPoint
;
}
@Override
public
void
destroy
()
throws
IOException
{
}
public
ChatCache
(
UserInfo
userInfo
,
String
userKey
,
String
randKey
,
String
botType
)
throws
Exception
{
String
k
;
this
.
botAccount
=
userInfo
.
botAccount
;
this
.
infoPoint
=
new
HashMap
<
String
,
String
>();
if
(
botType
.
equals
(
"game"
))
{
try
{
this
.
gameEnv
=
new
GameEnv
(
userInfo
.
botAccount
);
System
.
out
.
println
(
"初始化时:"
+
this
.
gameEnv
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
ENV_ERROR
);
this
.
infoPoint
.
put
(
"error_message"
,
ENV_ERROR
);
e
.
printStackTrace
();
}
}
Iterator
<
String
>
iit
=
userInfo
.
varInitialMap
.
keySet
().
iterator
();
while
(
iit
.
hasNext
())
{
k
=
iit
.
next
();
this
.
infoPoint
.
put
(
k
,
userInfo
.
varInitialMap
.
get
(
k
));
}
this
.
infoPoint
.
put
(
"lastUpdateTime"
,
String
.
valueOf
(
System
.
currentTimeMillis
()));
this
.
randKey
=
randKey
;
this
.
userKey
=
userKey
;
this
.
initial
=
true
;
}
public
ChatCache
(
String
userKey
,
String
randKey
)
{
this
.
userKey
=
userKey
;
this
.
randKey
=
randKey
;
}
public
Boolean
sendPost
(
Map
<
String
,
String
>
curInfoPoint
)
{
if
(
initial
==
false
)
return
false
;
initial
=
false
;
String
postURL
=
infoPoint
.
get
(
"postURL"
);
if
(
postURL
==
null
||
postURL
.
trim
().
equals
(
""
))
return
false
;
List
<
BasicNameValuePair
>
pairList
=
new
ArrayList
<
BasicNameValuePair
>();
Set
<
Entry
<
String
,
String
>>
entrySet
=
curInfoPoint
.
entrySet
();
String
_key
=
null
,
_value
=
null
;
for
(
Map
.
Entry
<
String
,
String
>
entry
:
entrySet
)
{
_key
=
entry
.
getKey
();
_value
=
entry
.
getValue
().
trim
();
if
(
_value
==
null
||
_value
.
equals
(
""
))
{
continue
;
}
pairList
.
add
(
new
BasicNameValuePair
(
_key
,
_value
));
}
entrySet
=
infoPoint
.
entrySet
();
for
(
Map
.
Entry
<
String
,
String
>
entry
:
entrySet
)
{
_key
=
entry
.
getKey
();
_value
=
entry
.
getValue
().
trim
();
if
(
_value
==
null
||
_value
.
equals
(
""
))
{
continue
;
}
pairList
.
add
(
new
BasicNameValuePair
(
_key
,
_value
));
}
JSONObject
result
=
null
;
try
{
result
=
SimpleUtil
.
postFORM
(
postURL
,
pairList
);
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
();
}
System
.
out
.
println
(
"initial post:"
+
result
.
toJSONString
());
Set
<
Entry
<
String
,
Object
>>
entrySet2
=
result
.
entrySet
();
for
(
Entry
<
String
,
Object
>
entry
:
entrySet2
)
{
_key
=
entry
.
getKey
();
if
(
_key
==
"post_status"
)
continue
;
infoPoint
.
put
(
_key
,
entry
.
getValue
().
toString
());
}
return
true
;
}
}
src/main/java/com/qiwu/chatbotbackend/gameDesign/GlobalVariableConstructor.java
View file @
cb904f39
package
com
.
qiwu
.
chatbotbackend
.
gameDesign
;
import
com.alibaba.fastjson.JSONObject
;
import
com.qiwu.chatbotbackend.db.SqlHelper
;
import
com.qiwu.chatbotbackend.entity.UserInfo
;
import
java.util.HashSet
;
import
java.util.Iterator
;
import
java.util.Map
;
import
static
com
.
qiwu
.
chatbotbackend
.
Config
.
Config
.
CC_URL
;
public
class
GlobalVariableConstructor
extends
ElementConstructor
{
public
GlobalVariableConstructor
(
SqlHelper
helper
,
int
botId
,
String
botAccount
,
Map
<
String
,
StringBuffer
>
keywordMap
,
HashSet
<
String
>
exceptRelation
,
StringBuffer
POS
)
{
super
();
this
.
helper
=
helper
;
this
.
botId
=
botId
;
this
.
botAccount
=
botAccount
;
this
.
exceptRelation
=
exceptRelation
;
this
.
keywordMap
=
keywordMap
;
this
.
POS
=
POS
;
}
public
void
construct
(
JSONObject
gameElements
,
UserInfo
userinfo
)
{
try
{
String
category
,
name
,
description
,
attrValue
;
JSONObject
globalVariable
=
gameElements
.
getJSONObject
(
"globalVariable"
);
Map
<
String
,
String
>
attribute
=
null
;
Iterator
<
String
>
attrIter
=
null
;
for
(
String
attrName:
globalVariable
.
keySet
())
{
attrValue
=
(
String
)
globalVariable
.
get
(
attrName
);
varInitialMap
.
put
(
"globalVariable_"
+
attrName
,
attrValue
);
}
varInitialMap
.
put
(
"botType"
,
"game"
);
varInitialMap
.
put
(
"unknownTextList"
,
(
String
)
gameElements
.
get
(
"unknownTextList"
));
varInitialMap
.
put
(
"finalUnknownText"
,
(
String
)
gameElements
.
get
(
"finalUnknownText"
));
varInitialMap
.
put
(
"CC_URL"
,
CC_URL
);
JSONObject
script
=
gameElements
.
getJSONObject
(
"speechcraft"
);
for
(
String
key:
script
.
keySet
()){
attrValue
=
(
String
)
script
.
get
(
key
);
varInitialMap
.
put
(
key
,
attrValue
);
}
}
catch
(
Exception
e
){
operationStatus
=
0
;
message
=
"全局变量处理出现异常"
;
e
.
printStackTrace
();
return
;
}
}
}
package
com
.
qiwu
.
chatbotbackend
.
gameDesign
;
import
com.alibaba.fastjson.JSONObject
;
import
com.qiwu.chatbotbackend.db.SqlHelper
;
import
com.qiwu.chatbotbackend.entity.UserInfo
;
import
java.util.HashSet
;
import
java.util.Iterator
;
import
java.util.Map
;
import
static
com
.
qiwu
.
chatbotbackend
.
Config
.
Config
.
CC_URL
;
public
class
GlobalVariableConstructor
extends
ElementConstructor
{
public
GlobalVariableConstructor
(
SqlHelper
helper
,
int
botId
,
String
botAccount
,
Map
<
String
,
StringBuffer
>
keywordMap
,
HashSet
<
String
>
exceptRelation
,
StringBuffer
POS
)
{
super
();
this
.
helper
=
helper
;
this
.
botId
=
botId
;
this
.
botAccount
=
botAccount
;
this
.
exceptRelation
=
exceptRelation
;
this
.
keywordMap
=
keywordMap
;
this
.
POS
=
POS
;
}
public
void
construct
(
JSONObject
gameElements
,
UserInfo
userinfo
)
{
try
{
String
category
,
name
,
description
,
attrValue
;
JSONObject
globalVariable
=
gameElements
.
getJSONObject
(
"globalVariable"
);
Map
<
String
,
String
>
attribute
=
null
;
Iterator
<
String
>
attrIter
=
null
;
for
(
String
attrName:
globalVariable
.
keySet
())
{
attrValue
=
(
String
)
globalVariable
.
get
(
attrName
);
varInitialMap
.
put
(
"globalVariable_"
+
attrName
,
attrValue
);
}
varInitialMap
.
put
(
"botType"
,
"game"
);
varInitialMap
.
put
(
"unknownTextList"
,
(
String
)
gameElements
.
get
(
"unknownTextList"
));
varInitialMap
.
put
(
"finalUnknownText"
,
(
String
)
gameElements
.
get
(
"finalUnknownText"
));
varInitialMap
.
put
(
"CC_URL"
,
CC_URL
);
JSONObject
script
=
gameElements
.
getJSONObject
(
"speechcraft"
);
for
(
String
key:
script
.
keySet
()){
//attrValue = (String)script.get(key);
attrValue
=
script
.
getJSONObject
(
key
).
getString
(
"text"
);
varInitialMap
.
put
(
key
,
attrValue
);
}
Boolean
postActive
=
gameElements
.
getBoolean
(
"postActive"
);
if
(
postActive
!=
null
&&
postActive
==
true
)
{
String
postURL
=
gameElements
.
getString
(
"postURL"
);
if
(
postURL
!=
null
&
!
postURL
.
equals
(
""
))
{
varInitialMap
.
put
(
"postURL"
,
postURL
);
}
}
}
catch
(
Exception
e
){
operationStatus
=
0
;
message
=
"全局变量处理出现异常"
;
e
.
printStackTrace
();
return
;
}
}
}
src/main/java/com/qiwu/chatbotbackend/gameDesign/MapConstructor.java
View file @
cb904f39
...
...
@@ -20,7 +20,8 @@ public class MapConstructor extends ElementConstructor {
Map
<
String
,
ArrayList
<
String
>>
name2nickname
=
new
HashMap
<
String
,
ArrayList
<
String
>>();
JSONArray
locations
=
null
;
Map
<
String
,
String
>
script
=
null
;
//话术模板
//Map<String, String> script = null; //话术模板
JSONObject
script
=
null
;
//话术模板
...
...
@@ -85,11 +86,8 @@ public class MapConstructor extends ElementConstructor {
JSONObject
map
=
gameElements
.
getJSONObject
(
"map"
);
Map
<
String
,
String
>
question2keyword
=
JSONObject
.
toJavaObject
(
map
.
getJSONObject
(
"question"
),
Map
.
class
);
script
=
JSONObject
.
toJavaObject
(
gameElements
.
getJSONObject
(
"speechcraft"
),
Map
.
class
);
//script = JSONObject.toJavaObject(gameElements.getJSONObject("speechcraft"), Map.class);
script
=
gameElements
.
getJSONObject
(
"speechcraft"
);
if
(
script
==
null
){
throw
new
Exception
(
"话术不存在"
);
...
...
@@ -279,7 +277,8 @@ public class MapConstructor extends ElementConstructor {
//地点任务和地点回答的初始化
if
(!
varInitialMap
.
containsKey
(
lastTaskCondition
))
{
varInitialMap
.
put
(
lastTaskCondition
,
conditionName
);
varInitialMap
.
put
(
lastTaskReply
,
script
.
get
(
"SITE_NOT_ACTIVE"
).
replace
(
"[地点名]"
,
placeA
));
//varInitialMap.put(lastTaskReply, script.get("SITE_NOT_ACTIVE").replace("[地点名]", placeA));
varInitialMap
.
put
(
lastTaskReply
,
script
.
getJSONObject
(
"SITE_NOT_ACTIVE"
).
getString
(
"text"
).
replace
(
"[地点名]"
,
placeA
));
conditionDescription
.
put
(
lastTaskCondition
,
lastTaskCondition
);
conditionDescription
.
put
(
lastTaskReply
,
lastTaskReply
);
}
...
...
src/main/java/com/qiwu/chatbotbackend/processing/ChatCacheController.java
View file @
cb904f39
...
...
@@ -73,7 +73,7 @@ public class ChatCacheController {
chatCacheMap
.
put
(
userKey
,
chatCache
);
}
}
System
.
out
.
println
(
chatCache
.
getGameEnv
());
//
System.out.println(chatCache.getGameEnv());
return
chatCache
;
}
...
...
src/main/java/com/qiwu/chatbotbackend/processing/FormsPoster.java
View file @
cb904f39
...
...
@@ -111,10 +111,6 @@ public class FormsPoster {
else
ans
.
setAnswer
(
"提交表单出现异常"
);
}
}
/**
...
...
src/main/java/com/qiwu/chatbotbackend/processing/UserInfoController.java
View file @
cb904f39
package
com
.
qiwu
.
chatbotbackend
.
processing
;
import
com.qiwu.chatbotbackend.entity.Action
;
import
com.qiwu.chatbotbackend.entity.UserInfo
;
import
javax.servlet.ServletContext
;
import
java.io.*
;
import
java.util.*
;
import
static
com
.
qiwu
.
chatbotbackend
.
Config
.
Config
.
CONFIG_DIR
;
import
static
com
.
qiwu
.
chatbotbackend
.
Config
.
Config
.
TEXT_PARSE_ERROR
;
public
class
UserInfoController
{
/**
* 根据本地配置文件生成用户信息
*/
public
static
void
configInit
(
ServletContext
context
)
throws
IOException
{
String
line
;
File
dir
=
new
File
(
System
.
getProperty
(
"user.dir"
)
+
"/"
+
CONFIG_DIR
);
if
(!
dir
.
exists
())
dir
.
mkdir
();
for
(
File
configFile
:
dir
.
listFiles
())
{
FileReader
reader
=
new
FileReader
(
configFile
);
BufferedReader
br
=
new
BufferedReader
(
reader
);
String
fileName
=
configFile
.
getName
();
UserInfo
userinfo
=
new
UserInfo
(
fileName
.
split
(
"\\."
)[
0
]);
userinfo
=
buildUserInfo
(
br
,
userinfo
,
true
);
context
.
setAttribute
(
userinfo
.
botAccount
,
userinfo
);
reader
.
close
();
br
.
close
();
}
}
public
static
UserInfo
userConfigInit
(
String
botAccount
,
ServletContext
context
)
throws
IOException
{
String
line
;
UserInfo
userInfo
=
null
;
File
userFile
=
new
File
(
System
.
getProperty
(
"user.dir"
)
+
"/"
+
CONFIG_DIR
+
botAccount
+
".txt"
);
System
.
out
.
println
(
"配置文件地址:"
+
userFile
.
getAbsolutePath
());
if
(!
userFile
.
exists
())
{
BufferedReader
br
=
new
BufferedReader
(
new
StringReader
(
""
));
userInfo
=
new
UserInfo
(
botAccount
);
userInfo
=
buildUserInfo
(
br
,
userInfo
,
true
);
context
.
setAttribute
(
userInfo
.
botAccount
,
userInfo
);
}
else
{
FileReader
reader
=
new
FileReader
(
userFile
);
BufferedReader
br
=
new
BufferedReader
(
reader
);
UserInfo
userinfo
=
new
UserInfo
(
botAccount
);
userInfo
=
buildUserInfo
(
br
,
userinfo
,
true
);
context
.
setAttribute
(
userinfo
.
botAccount
,
userinfo
);
reader
.
close
();
br
.
close
();
}
return
userInfo
;
}
/**
* 解析一行配置文件
* @param action
* @param line
* @return
*/
public
static
Action
buildCondition
(
Action
action
,
String
line
)
{
String
feature
,
url
,
element
[],
pair
[],
next
[],
cur_premises
[],
history_premises
[],
premises
[],
features
[],
firstCnditionName
,
text
[];
try
{
element
=
line
.
replace
(
"\n"
,
""
).
split
(
"\\|=\\>\\|"
);
premises
=
element
[
0
].
split
(
"\\|"
);
feature
=
premises
[
2
];
cur_premises
=
premises
[
0
].
split
(
","
);
history_premises
=
premises
[
1
].
split
(
","
);
action
.
setAuxiliaryPriority
(
Integer
.
parseInt
(
premises
[
3
].
split
(
"="
,
2
)[
1
]));
//action.setConfigName(premises[4].split("=", 2)[1]);
next
=
element
[
1
].
split
(
"\\|"
);
if
(
next
.
length
<
4
)
return
action
;
if
(
premises
.
length
<
5
)
action
.
setConfigName
(
""
);
else
action
.
setConfigName
(
premises
[
4
].
split
(
"="
,
2
)[
1
]);
//回答可以有多个,然后可随机选择一个
action
.
setAnswer
(
next
[
0
].
split
(
"~"
));
//第二处
action
.
setNextStep
(
next
[
1
].
split
(
"="
,
2
)[
1
]);
action
.
setId
(
Integer
.
parseInt
(
next
[
3
].
split
(
"="
)[
1
]));
action
.
setStatus
(
next
[
2
].
split
(
"="
)[
1
]);
//featureMap的构建
features
=
feature
.
split
(
","
);
for
(
String
feat
:
features
)
{
if
(
feat
.
equals
(
""
)
||
feat
.
equals
(
" "
))
{
continue
;
}
else
if
(
feat
.
startsWith
(
"updateKnowledge"
))
{
String
[]
knowledge
=
feat
.
split
(
"-<-"
);
int
length
=
knowledge
.
length
;
action
.
getFeatureMap
().
put
(
"updateKnowledge"
,
""
);
for
(
int
i
=
1
;
i
<
length
;
i
++)
{
if
(
knowledge
[
i
].
equals
(
""
)
||
knowledge
[
i
].
equals
(
" "
))
continue
;
pair
=
knowledge
[
i
].
split
(
":"
,
2
);
action
.
getUpdateKnowledge
().
put
(
pair
[
0
],
pair
[
1
]);
}
}
else
if
(
feat
.
startsWith
(
"post"
))
{
pair
=
feat
.
split
(
"\\("
,
2
);
url
=
pair
[
1
];
url
=
url
.
substring
(
0
,
url
.
length
()
-
1
);
pair
=
pair
[
0
].
split
(
"::"
);
action
.
getFeatureMap
().
put
(
"post"
,
url
+
"|"
+
pair
[
1
]);
}
else
{
action
.
getFeatureMap
().
put
(
feat
,
""
);
}
}
insertPremise
(
action
.
getCurrentPremise
(),
cur_premises
);
insertPremise
(
action
.
getHistoryPremise
(),
history_premises
);
//满足条件越多,优先级越大
action
.
setPriority
(
action
.
getHistoryPremise
().
size
()
+
action
.
getCurrentPremise
().
size
());
firstCnditionName
=
cur_premises
[
0
];
if
(
firstCnditionName
.
equals
(
" "
))
action
.
setIndexName
(
"emptySet"
);
else
action
.
setIndexName
(
firstCnditionName
.
split
(
"="
)[
0
].
split
(
"::"
)[
0
]);
}
catch
(
IndexOutOfBoundsException
e
)
{
e
.
printStackTrace
();
System
.
out
.
println
(
line
);
System
.
out
.
println
(
TEXT_PARSE_ERROR
);
}
return
action
;
}
/**
* @param br
* @param userinfo
* @param overRide 覆盖之前的状态表
* @return
* @throws IOException
*/
public
static
UserInfo
buildUserInfo
(
BufferedReader
br
,
UserInfo
userinfo
,
boolean
overRide
){
String
line
=
""
,
infoName
,
extraElements
[];
HashMap
<
String
,
List
<
Action
>>
conditionTable
=
null
;
//是否覆盖原有的数据
if
(
overRide
)
{
conditionTable
=
new
HashMap
<
String
,
List
<
Action
>>();
userinfo
.
id2condition
.
clear
();
userinfo
.
id2config
.
clear
();
userinfo
.
configStr
=
""
;
userinfo
.
varInitialMap
.
clear
();
userinfo
.
name2condition
.
clear
();
}
else
{
conditionTable
=
userinfo
.
conditionTable
;
}
List
<
Action
>
actionList
=
null
;
try
{
while
((
line
=
br
.
readLine
())
!=
null
)
{
if
(
line
.
trim
().
equals
(
""
))
continue
;
line
=
line
.
replace
(
"\n"
,
""
);
//在用户信息中添加额外信息,如设为active的action
if
(
line
.
contains
(
"extraInfo"
))
{
extraElements
=
line
.
split
(
"\\|"
,
-
1
);
userinfo
.
extraInfo
.
put
(
extraElements
[
1
],
extraElements
[
2
]);
}
else
if
(
line
.
contains
(
"varInitial"
))
{
extraElements
=
line
.
split
(
"\\|"
,
-
1
);
userinfo
.
varInitialMap
.
put
(
extraElements
[
1
],
extraElements
[
2
]);
}
else
{
Action
action
=
new
Action
();
action
=
buildCondition
(
action
,
line
);
actionList
=
conditionTable
.
get
(
action
.
getIndexName
());
if
(
actionList
==
null
)
actionList
=
new
ArrayList
<
Action
>();
actionList
.
add
(
action
);
conditionTable
.
put
(
action
.
getIndexName
(),
actionList
);
if
(
action
.
getId
()
!=
0
)
{
if
(!
action
.
getConfigName
().
equals
(
""
))
{
userinfo
.
name2condition
.
put
(
action
.
getConfigName
(),
action
);
}
userinfo
.
id2config
.
put
(
action
.
getId
(),
line
);
userinfo
.
id2condition
.
put
(
action
.
getId
(),
action
);
}
}
}
}
catch
(
ArrayIndexOutOfBoundsException
e
)
{
System
.
out
.
println
(
TEXT_PARSE_ERROR
);
//e.printStackTrace();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
System
.
out
.
println
(
line
);
}
userinfo
.
conditionTable
=
conditionTable
;
return
userinfo
;
}
/**
* 通过split方法解析配置文件中的条件部分(如:xihuan::match-Include=+dota+英雄联盟)
*
* @param premiseMap 前提条件哈希表
*/
public
static
void
insertPremise
(
HashMap
<
String
,
HashSet
<
String
>>
premiseMap
,
String
[]
premisesString
)
{
String
pair
[];
HashSet
<
String
>
keyWordSet
=
null
;
for
(
String
premise
:
premisesString
)
{
// System.out.println(premise);
if
(
premise
.
equals
(
""
)
||
premise
.
equals
(
" "
))
continue
;
pair
=
premise
.
split
(
"="
,
2
);
keyWordSet
=
new
HashSet
<
String
>();
for
(
String
keyword
:
pair
[
1
].
split
(
"\\+"
))
//为了处理条件不满足(为空)时的特殊情况,
if
(
keyword
.
equals
(
"null"
))
keyWordSet
.
add
(
""
);
else
if
(!
keyword
.
equals
(
""
))
keyWordSet
.
add
(
keyword
);
premiseMap
.
put
(
pair
[
0
],
keyWordSet
);
}
}
/**
* 原为通过用户chat_key从数据库中获得用户名
* 根据用户名从servletContext中获得用户的数据userinfo(配置文件及相应的状态表)
* 如果context中没有,就重新加载本地配置文件(即初始化)
* 初始化后还是没有就创建一个userinfo
* -------------
* 修改后直接从request获得用户名,并从context根据用户名获得userinfo
*
* @param bot_name 用户名
* @return 用户信息
*/
public
UserInfo
userInfoQuery
(
String
bot_name
,
ServletContext
context
)
throws
IOException
{
String
botAccount
=
bot_name
;
UserInfo
userinfo
=
null
;
if
(
context
.
getAttribute
(
botAccount
)
==
null
)
{
userinfo
=
userConfigInit
(
botAccount
,
context
);
}
else
{
userinfo
=
(
UserInfo
)
context
.
getAttribute
(
botAccount
);
}
return
userinfo
;
}
public
static
boolean
deleteConfig
(
String
userName
,
String
configId
,
String
data
,
ServletContext
context
)
throws
IOException
{
int
id
=
Integer
.
parseInt
(
configId
);
Action
action
=
new
Action
();
UserInfoController
userInfoController
=
new
UserInfoController
();
String
conditionNameList
[]
=
data
.
split
(
","
);
String
conditionName
=
""
;
if
(
context
.
getAttribute
(
userName
)
==
null
)
{
userInfoController
.
userConfigInit
(
userName
,
context
);
}
UserInfo
userInfo
=
(
UserInfo
)
context
.
getAttribute
(
userName
);
if
(
userInfo
.
id2config
.
containsKey
(
id
))
{
Action
oldAction
=
userInfo
.
id2condition
.
get
(
id
);
//-------------------
conditionName
=
oldAction
.
getIndexName
();
if
(
userInfo
.
conditionTable
.
get
(
conditionName
)
!=
null
)
{
List
<
Action
>
actionList
=
userInfo
.
conditionTable
.
get
(
conditionName
);
Iterator
<
Action
>
condIter
=
userInfo
.
conditionTable
.
get
(
conditionName
).
iterator
();
while
(
condIter
.
hasNext
())
{
Action
c
=
condIter
.
next
();
if
(
c
.
getId
()
==
id
)
{
condIter
.
remove
();
}
}
}
userInfo
.
name2condition
.
remove
(
oldAction
.
getConfigName
());
userInfo
.
id2config
.
remove
(
id
);
userInfo
.
id2condition
.
remove
(
id
);
}
else
{
System
.
out
.
println
(
"未找到要删除的configId"
);
}
return
true
;
}
public
static
boolean
batchDeleteConfig
(
String
userName
,
List
<
Integer
>
idGroup
,
UserInfo
userInfo
)
throws
IOException
{
String
conditionName
=
""
;
for
(
int
configId
:
idGroup
)
{
if
(
userInfo
.
id2config
.
containsKey
(
configId
))
{
Action
oldAction
=
userInfo
.
id2condition
.
get
(
configId
);
conditionName
=
oldAction
.
getIndexName
();
if
(
userInfo
.
conditionTable
.
get
(
conditionName
)
!=
null
)
{
List
<
Action
>
actionList
=
userInfo
.
conditionTable
.
get
(
conditionName
);
Iterator
<
Action
>
condIter
=
userInfo
.
conditionTable
.
get
(
conditionName
).
iterator
();
while
(
condIter
.
hasNext
())
{
Action
c
=
condIter
.
next
();
if
(
c
.
getId
()
==
configId
)
{
condIter
.
remove
();
}
}
}
userInfo
.
name2condition
.
remove
(
oldAction
.
getConfigName
());
userInfo
.
id2config
.
remove
(
configId
);
userInfo
.
id2condition
.
remove
(
configId
);
}
else
{
System
.
out
.
println
(
"未找到要删除的configId:"
+
configId
);
}
}
return
true
;
}
public
static
boolean
insertConfig
(
String
userName
,
String
configId
,
String
data
,
ServletContext
context
)
throws
IOException
{
Action
action
=
new
Action
();
String
dataPair
[]
=
data
.
split
(
"---"
);
String
configLine
=
dataPair
[
0
];
UserInfoController
userInfoController
=
new
UserInfoController
();
int
id
=
Integer
.
parseInt
(
configId
);
action
=
userInfoController
.
buildCondition
(
action
,
configLine
);
if
(
context
.
getAttribute
(
userName
)
==
null
)
{
userInfoController
.
userConfigInit
(
userName
,
context
);
}
UserInfo
userinfo
=
(
UserInfo
)
context
.
getAttribute
(
userName
);
if
(
userinfo
.
conditionTable
.
get
(
action
.
getIndexName
())
==
null
)
{
List
<
Action
>
actionList
=
new
ArrayList
<
Action
>();
userinfo
.
conditionTable
.
put
(
action
.
getIndexName
(),
actionList
);
}
if
(!
action
.
getConfigName
().
equals
(
""
)){
userinfo
.
name2condition
.
put
(
action
.
getConfigName
(),
action
);
}
userinfo
.
conditionTable
.
get
(
action
.
getIndexName
()).
add
(
action
);
userinfo
.
id2config
.
put
(
id
,
configLine
);
userinfo
.
id2condition
.
put
(
id
,
action
);
return
true
;
}
public
static
boolean
updateVariable
(
String
userName
,
String
configId
,
String
data
,
ServletContext
context
)
throws
IOException
{
Action
action
=
new
Action
();
//data=configLine+"---"+conditionNameList
String
dataPair
[]
=
data
.
split
(
"---"
,
-
1
);
String
name
=
dataPair
[
0
],
value
=
dataPair
[
1
];
UserInfoController
userInfoController
=
new
UserInfoController
();
if
(
context
.
getAttribute
(
userName
)
==
null
)
{
userInfoController
.
userConfigInit
(
userName
,
context
);
}
UserInfo
userinfo
=
(
UserInfo
)
context
.
getAttribute
(
userName
);
userinfo
.
varInitialMap
.
put
(
name
,
value
);
return
true
;
}
public
static
boolean
deleteVariable
(
String
userName
,
String
configId
,
String
data
,
ServletContext
context
)
throws
IOException
{
//data=configLine+"---"+conditionNameList
String
name
=
data
;
UserInfoController
userInfoController
=
new
UserInfoController
();
if
(
context
.
getAttribute
(
userName
)
==
null
)
{
userInfoController
.
userConfigInit
(
userName
,
context
);
}
UserInfo
userinfo
=
(
UserInfo
)
context
.
getAttribute
(
userName
);
userinfo
.
varInitialMap
.
remove
(
name
);
return
true
;
}
public
static
boolean
updateExtraInfo
(
String
userName
,
String
data
,
ServletContext
context
)
throws
IOException
{
UserInfoController
userInfoController
=
new
UserInfoController
();
if
(
context
.
getAttribute
(
userName
)
==
null
)
{
userInfoController
.
userConfigInit
(
userName
,
context
);
}
UserInfo
userinfo
=
(
UserInfo
)
context
.
getAttribute
(
userName
);
String
extraElements
[]
=
data
.
split
(
"\\|"
);
userinfo
.
extraInfo
.
put
(
extraElements
[
1
],
extraElements
[
2
]);
return
true
;
}
public
static
String
writeConfigText
(
String
userName
,
UserInfo
userinfo
){
String
path
=
System
.
getProperty
(
"user.dir"
)
+
"/"
+
CONFIG_DIR
+
userName
+
".txt"
;
String
returnMessage
=
"success"
;
FileWriter
fw
=
null
;
BufferedWriter
bw
=
null
;
try
{
File
file
=
new
File
(
path
);
File
dir
=
new
File
(
System
.
getProperty
(
"user.dir"
)
+
"/"
+
CONFIG_DIR
);
if
(!
dir
.
exists
())
dir
.
mkdir
();
if
(!
file
.
exists
())
{
file
.
createNewFile
();
}
fw
=
new
FileWriter
(
file
);
bw
=
new
BufferedWriter
(
fw
);
Iterator
<
String
>
keyIter
=
userinfo
.
extraInfo
.
keySet
().
iterator
();
while
(
keyIter
.
hasNext
())
{
String
info
=
keyIter
.
next
();
if
(!
userinfo
.
extraInfo
.
get
(
info
).
equals
(
""
))
bw
.
write
(
"-extraInfo-|"
+
info
+
"|"
+
userinfo
.
extraInfo
.
get
(
info
)
+
"\n"
);
}
keyIter
=
userinfo
.
varInitialMap
.
keySet
().
iterator
();
while
(
keyIter
.
hasNext
())
{
String
info
=
keyIter
.
next
();
bw
.
write
(
"-varInitial-|"
+
info
+
"|"
+
userinfo
.
varInitialMap
.
get
(
info
)
+
"\n"
);
}
Iterator
<
Integer
>
idIter
=
userinfo
.
id2config
.
keySet
().
iterator
();
while
(
idIter
.
hasNext
())
{
int
id
=
idIter
.
next
();
bw
.
write
(
userinfo
.
id2config
.
get
(
id
)
+
"\n"
);
}
bw
.
write
(
"\n"
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
returnMessage
=
"config file write error"
;
}
finally
{
try
{
bw
.
close
();
fw
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
return
returnMessage
;
}
}
package
com
.
qiwu
.
chatbotbackend
.
processing
;
import
com.qiwu.chatbotbackend.entity.Action
;
import
com.qiwu.chatbotbackend.entity.UserInfo
;
import
javax.servlet.ServletContext
;
import
java.io.*
;
import
java.util.*
;
import
static
com
.
qiwu
.
chatbotbackend
.
Config
.
Config
.
CONFIG_DIR
;
import
static
com
.
qiwu
.
chatbotbackend
.
Config
.
Config
.
TEXT_PARSE_ERROR
;
public
class
UserInfoController
{
/**
* 根据本地配置文件生成用户信息
*/
public
static
void
configInit
(
ServletContext
context
)
throws
IOException
{
String
line
;
File
dir
=
new
File
(
System
.
getProperty
(
"user.dir"
)
+
"/"
+
CONFIG_DIR
);
if
(!
dir
.
exists
())
dir
.
mkdir
();
for
(
File
configFile
:
dir
.
listFiles
())
{
FileReader
reader
=
new
FileReader
(
configFile
);
BufferedReader
br
=
new
BufferedReader
(
reader
);
String
fileName
=
configFile
.
getName
();
UserInfo
userinfo
=
new
UserInfo
(
fileName
.
split
(
"\\."
)[
0
]);
userinfo
=
buildUserInfo
(
br
,
userinfo
,
true
);
context
.
setAttribute
(
userinfo
.
botAccount
,
userinfo
);
reader
.
close
();
br
.
close
();
}
}
public
static
UserInfo
userConfigInit
(
String
botAccount
,
ServletContext
context
)
throws
IOException
{
String
line
;
UserInfo
userInfo
=
null
;
File
userFile
=
new
File
(
System
.
getProperty
(
"user.dir"
)
+
"/"
+
CONFIG_DIR
+
botAccount
+
".txt"
);
System
.
out
.
println
(
"配置文件地址:"
+
userFile
.
getAbsolutePath
());
if
(!
userFile
.
exists
())
{
BufferedReader
br
=
new
BufferedReader
(
new
StringReader
(
""
));
userInfo
=
new
UserInfo
(
botAccount
);
userInfo
=
buildUserInfo
(
br
,
userInfo
,
true
);
context
.
setAttribute
(
userInfo
.
botAccount
,
userInfo
);
}
else
{
FileReader
reader
=
new
FileReader
(
userFile
);
BufferedReader
br
=
new
BufferedReader
(
reader
);
UserInfo
userinfo
=
new
UserInfo
(
botAccount
);
userInfo
=
buildUserInfo
(
br
,
userinfo
,
true
);
context
.
setAttribute
(
userinfo
.
botAccount
,
userinfo
);
reader
.
close
();
br
.
close
();
}
return
userInfo
;
}
/**
* 解析一行配置文件
* @param action
* @param line
* @return
*/
public
static
Action
buildCondition
(
Action
action
,
String
line
)
{
String
feature
,
url
,
element
[],
pair
[],
next
[],
cur_premises
[],
history_premises
[],
premises
[],
features
[],
firstCnditionName
,
text
[];
try
{
element
=
line
.
replace
(
"\n"
,
""
).
split
(
"\\|=\\>\\|"
);
premises
=
element
[
0
].
split
(
"\\|"
);
feature
=
premises
[
2
];
cur_premises
=
premises
[
0
].
split
(
","
);
history_premises
=
premises
[
1
].
split
(
","
);
action
.
setAuxiliaryPriority
(
Integer
.
parseInt
(
premises
[
3
].
split
(
"="
,
2
)[
1
]));
//action.setConfigName(premises[4].split("=", 2)[1]);
next
=
element
[
1
].
split
(
"\\|"
);
if
(
next
.
length
<
4
)
return
action
;
if
(
premises
.
length
<
5
)
action
.
setConfigName
(
""
);
else
action
.
setConfigName
(
premises
[
4
].
split
(
"="
,
2
)[
1
]);
//回答可以有多个,然后可随机选择一个
action
.
setAnswer
(
next
[
0
].
split
(
"~"
));
//第二处
action
.
setNextStep
(
next
[
1
].
split
(
"="
,
2
)[
1
]);
action
.
setId
(
Integer
.
parseInt
(
next
[
3
].
split
(
"="
)[
1
]));
action
.
setStatus
(
next
[
2
].
split
(
"="
)[
1
]);
//featureMap的构建
features
=
feature
.
split
(
","
);
for
(
String
feat
:
features
)
{
if
(
feat
.
equals
(
""
)
||
feat
.
equals
(
" "
))
{
continue
;
}
else
if
(
feat
.
startsWith
(
"updateKnowledge"
))
{
String
[]
knowledge
=
feat
.
split
(
"-<-"
);
int
length
=
knowledge
.
length
;
action
.
getFeatureMap
().
put
(
"updateKnowledge"
,
""
);
for
(
int
i
=
1
;
i
<
length
;
i
++)
{
if
(
knowledge
[
i
].
equals
(
""
)
||
knowledge
[
i
].
equals
(
" "
))
continue
;
pair
=
knowledge
[
i
].
split
(
":"
,
2
);
action
.
getUpdateKnowledge
().
put
(
pair
[
0
],
pair
[
1
]);
}
}
else
if
(
feat
.
startsWith
(
"post"
))
{
pair
=
feat
.
split
(
"\\("
,
2
);
url
=
pair
[
1
];
url
=
url
.
substring
(
0
,
url
.
length
()
-
1
);
pair
=
pair
[
0
].
split
(
"::"
);
action
.
getFeatureMap
().
put
(
"post"
,
url
+
"|"
+
pair
[
1
]);
}
else
{
action
.
getFeatureMap
().
put
(
feat
,
""
);
}
}
insertPremise
(
action
.
getCurrentPremise
(),
cur_premises
);
insertPremise
(
action
.
getHistoryPremise
(),
history_premises
);
//满足条件越多,优先级越大
action
.
setPriority
(
action
.
getHistoryPremise
().
size
()
+
action
.
getCurrentPremise
().
size
());
firstCnditionName
=
cur_premises
[
0
];
if
(
firstCnditionName
.
equals
(
" "
))
action
.
setIndexName
(
"emptySet"
);
else
action
.
setIndexName
(
firstCnditionName
.
split
(
"="
)[
0
].
split
(
"::"
)[
0
]);
}
catch
(
IndexOutOfBoundsException
e
)
{
e
.
printStackTrace
();
System
.
out
.
println
(
line
);
System
.
out
.
println
(
TEXT_PARSE_ERROR
);
}
return
action
;
}
/**
* @param br
* @param userinfo
* @param overRide 覆盖之前的状态表
* @return
* @throws IOException
*/
public
static
UserInfo
buildUserInfo
(
BufferedReader
br
,
UserInfo
userinfo
,
boolean
overRide
){
String
line
=
""
,
infoName
,
extraElements
[];
HashMap
<
String
,
List
<
Action
>>
conditionTable
=
null
;
//是否覆盖原有的数据
if
(
overRide
)
{
conditionTable
=
new
HashMap
<
String
,
List
<
Action
>>();
userinfo
.
id2condition
.
clear
();
userinfo
.
id2config
.
clear
();
userinfo
.
configStr
=
""
;
userinfo
.
varInitialMap
.
clear
();
userinfo
.
name2condition
.
clear
();
}
else
{
conditionTable
=
userinfo
.
conditionTable
;
}
List
<
Action
>
actionList
=
null
;
try
{
while
((
line
=
br
.
readLine
())
!=
null
)
{
if
(
line
.
trim
().
equals
(
""
))
continue
;
line
=
line
.
replace
(
"\n"
,
""
);
//在用户信息中添加额外信息,如设为active的action
if
(
line
.
contains
(
"extraInfo"
))
{
extraElements
=
line
.
split
(
"\\|"
,
-
1
);
userinfo
.
extraInfo
.
put
(
extraElements
[
1
],
extraElements
[
2
]);
}
else
if
(
line
.
contains
(
"varInitial"
))
{
extraElements
=
line
.
split
(
"\\|"
,
-
1
);
userinfo
.
varInitialMap
.
put
(
extraElements
[
1
],
extraElements
[
2
]);
}
else
{
Action
action
=
new
Action
();
action
=
buildCondition
(
action
,
line
);
actionList
=
conditionTable
.
get
(
action
.
getIndexName
());
if
(
actionList
==
null
)
actionList
=
new
ArrayList
<
Action
>();
actionList
.
add
(
action
);
conditionTable
.
put
(
action
.
getIndexName
(),
actionList
);
if
(
action
.
getId
()
!=
0
)
{
if
(!
action
.
getConfigName
().
equals
(
""
))
{
userinfo
.
name2condition
.
put
(
action
.
getConfigName
(),
action
);
}
userinfo
.
id2config
.
put
(
action
.
getId
(),
line
);
userinfo
.
id2condition
.
put
(
action
.
getId
(),
action
);
}
}
}
}
catch
(
ArrayIndexOutOfBoundsException
e
)
{
System
.
out
.
println
(
TEXT_PARSE_ERROR
);
//e.printStackTrace();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
System
.
out
.
println
(
line
);
}
userinfo
.
conditionTable
=
conditionTable
;
return
userinfo
;
}
/**
* 通过split方法解析配置文件中的条件部分(如:xihuan::match-Include=+dota+英雄联盟)
*
* @param premiseMap 前提条件哈希表
*/
public
static
void
insertPremise
(
HashMap
<
String
,
HashSet
<
String
>>
premiseMap
,
String
[]
premisesString
)
{
String
pair
[];
HashSet
<
String
>
keyWordSet
=
null
;
for
(
String
premise
:
premisesString
)
{
// System.out.println(premise);
if
(
premise
.
equals
(
""
)
||
premise
.
equals
(
" "
))
continue
;
pair
=
premise
.
split
(
"="
,
2
);
keyWordSet
=
new
HashSet
<
String
>();
for
(
String
keyword
:
pair
[
1
].
split
(
"\\+"
))
//为了处理条件不满足(为空)时的特殊情况,
if
(
keyword
.
equals
(
"null"
))
keyWordSet
.
add
(
""
);
else
if
(!
keyword
.
equals
(
""
))
keyWordSet
.
add
(
keyword
);
premiseMap
.
put
(
pair
[
0
],
keyWordSet
);
}
}
/**
* 原为通过用户chat_key从数据库中获得用户名
* 根据用户名从servletContext中获得用户的数据userinfo(配置文件及相应的状态表)
* 如果context中没有,就重新加载本地配置文件(即初始化)
* 初始化后还是没有就创建一个userinfo
* -------------
* 修改后直接从request获得用户名,并从context根据用户名获得userinfo
*
* @param bot_name 用户名
* @return 用户信息
*/
public
UserInfo
userInfoQuery
(
String
bot_name
,
ServletContext
context
)
throws
IOException
{
String
botAccount
=
bot_name
;
UserInfo
userinfo
=
null
;
if
(
context
.
getAttribute
(
botAccount
)
==
null
)
{
userinfo
=
userConfigInit
(
botAccount
,
context
);
}
else
{
userinfo
=
(
UserInfo
)
context
.
getAttribute
(
botAccount
);
}
return
userinfo
;
}
public
static
boolean
deleteConfig
(
String
userName
,
String
configId
,
String
data
,
ServletContext
context
)
throws
IOException
{
int
id
=
Integer
.
parseInt
(
configId
);
Action
action
=
new
Action
();
UserInfoController
userInfoController
=
new
UserInfoController
();
String
conditionNameList
[]
=
data
.
split
(
","
);
String
conditionName
=
""
;
if
(
context
.
getAttribute
(
userName
)
==
null
)
{
userInfoController
.
userConfigInit
(
userName
,
context
);
}
UserInfo
userInfo
=
(
UserInfo
)
context
.
getAttribute
(
userName
);
if
(
userInfo
.
id2config
.
containsKey
(
id
))
{
Action
oldAction
=
userInfo
.
id2condition
.
get
(
id
);
//-------------------
conditionName
=
oldAction
.
getIndexName
();
if
(
userInfo
.
conditionTable
.
get
(
conditionName
)
!=
null
)
{
List
<
Action
>
actionList
=
userInfo
.
conditionTable
.
get
(
conditionName
);
Iterator
<
Action
>
condIter
=
userInfo
.
conditionTable
.
get
(
conditionName
).
iterator
();
while
(
condIter
.
hasNext
())
{
Action
c
=
condIter
.
next
();
if
(
c
.
getId
()
==
id
)
{
condIter
.
remove
();
}
}
}
userInfo
.
name2condition
.
remove
(
oldAction
.
getConfigName
());
userInfo
.
id2config
.
remove
(
id
);
userInfo
.
id2condition
.
remove
(
id
);
}
else
{
System
.
out
.
println
(
"未找到要删除的configId"
);
}
return
true
;
}
public
static
boolean
batchDeleteConfig
(
String
userName
,
List
<
Integer
>
idGroup
,
UserInfo
userInfo
)
throws
IOException
{
String
conditionName
=
""
;
for
(
int
configId
:
idGroup
)
{
if
(
userInfo
.
id2config
.
containsKey
(
configId
))
{
Action
oldAction
=
userInfo
.
id2condition
.
get
(
configId
);
conditionName
=
oldAction
.
getIndexName
();
if
(
userInfo
.
conditionTable
.
get
(
conditionName
)
!=
null
)
{
List
<
Action
>
actionList
=
userInfo
.
conditionTable
.
get
(
conditionName
);
Iterator
<
Action
>
condIter
=
userInfo
.
conditionTable
.
get
(
conditionName
).
iterator
();
while
(
condIter
.
hasNext
())
{
Action
c
=
condIter
.
next
();
if
(
c
.
getId
()
==
configId
)
{
condIter
.
remove
();
}
}
}
userInfo
.
name2condition
.
remove
(
oldAction
.
getConfigName
());
userInfo
.
id2config
.
remove
(
configId
);
userInfo
.
id2condition
.
remove
(
configId
);
}
else
{
System
.
out
.
println
(
"未找到要删除的configId:"
+
configId
);
}
}
return
true
;
}
public
static
boolean
insertConfig
(
String
userName
,
String
configId
,
String
data
,
ServletContext
context
)
throws
IOException
{
Action
action
=
new
Action
();
String
dataPair
[]
=
data
.
split
(
"---"
);
String
configLine
=
dataPair
[
0
];
UserInfoController
userInfoController
=
new
UserInfoController
();
int
id
=
Integer
.
parseInt
(
configId
);
action
=
userInfoController
.
buildCondition
(
action
,
configLine
);
if
(
context
.
getAttribute
(
userName
)
==
null
)
{
userInfoController
.
userConfigInit
(
userName
,
context
);
}
UserInfo
userinfo
=
(
UserInfo
)
context
.
getAttribute
(
userName
);
if
(
userinfo
.
conditionTable
.
get
(
action
.
getIndexName
())
==
null
)
{
List
<
Action
>
actionList
=
new
ArrayList
<
Action
>();
userinfo
.
conditionTable
.
put
(
action
.
getIndexName
(),
actionList
);
}
if
(!
action
.
getConfigName
().
equals
(
""
)){
userinfo
.
name2condition
.
put
(
action
.
getConfigName
(),
action
);
}
userinfo
.
conditionTable
.
get
(
action
.
getIndexName
()).
add
(
action
);
userinfo
.
id2config
.
put
(
id
,
configLine
);
userinfo
.
id2condition
.
put
(
id
,
action
);
return
true
;
}
public
static
boolean
updateVariable
(
String
userName
,
String
configId
,
String
data
,
ServletContext
context
)
throws
IOException
{
Action
action
=
new
Action
();
//data=configLine+"---"+conditionNameList
String
dataPair
[]
=
data
.
split
(
"---"
,
-
1
);
String
name
=
dataPair
[
0
],
value
=
dataPair
[
1
];
UserInfoController
userInfoController
=
new
UserInfoController
();
if
(
context
.
getAttribute
(
userName
)
==
null
)
{
userInfoController
.
userConfigInit
(
userName
,
context
);
}
UserInfo
userinfo
=
(
UserInfo
)
context
.
getAttribute
(
userName
);
userinfo
.
varInitialMap
.
put
(
name
,
value
);
return
true
;
}
public
static
boolean
deleteVariable
(
String
userName
,
String
configId
,
String
data
,
ServletContext
context
)
throws
IOException
{
//data=configLine+"---"+conditionNameList
String
name
=
data
;
UserInfoController
userInfoController
=
new
UserInfoController
();
if
(
context
.
getAttribute
(
userName
)
==
null
)
{
userInfoController
.
userConfigInit
(
userName
,
context
);
}
UserInfo
userinfo
=
(
UserInfo
)
context
.
getAttribute
(
userName
);
userinfo
.
varInitialMap
.
remove
(
name
);
return
true
;
}
public
static
boolean
updateExtraInfo
(
String
userName
,
String
data
,
ServletContext
context
)
throws
IOException
{
UserInfoController
userInfoController
=
new
UserInfoController
();
if
(
context
.
getAttribute
(
userName
)
==
null
)
{
userInfoController
.
userConfigInit
(
userName
,
context
);
}
UserInfo
userinfo
=
(
UserInfo
)
context
.
getAttribute
(
userName
);
String
extraElements
[]
=
data
.
split
(
"\\|"
);
userinfo
.
extraInfo
.
put
(
extraElements
[
1
],
extraElements
[
2
]);
return
true
;
}
public
static
String
writeConfigText
(
String
userName
,
UserInfo
userinfo
){
String
path
=
System
.
getProperty
(
"user.dir"
)
+
"/"
+
CONFIG_DIR
+
userName
+
".txt"
;
String
returnMessage
=
"success"
;
FileWriter
fw
=
null
;
BufferedWriter
bw
=
null
;
try
{
File
file
=
new
File
(
path
);
File
dir
=
new
File
(
System
.
getProperty
(
"user.dir"
)
+
"/"
+
CONFIG_DIR
);
if
(!
dir
.
exists
())
dir
.
mkdir
();
if
(!
file
.
exists
())
{
file
.
createNewFile
();
}
fw
=
new
FileWriter
(
file
);
bw
=
new
BufferedWriter
(
fw
);
Iterator
<
String
>
keyIter
=
userinfo
.
extraInfo
.
keySet
().
iterator
();
while
(
keyIter
.
hasNext
())
{
String
info
=
keyIter
.
next
();
if
(!
userinfo
.
extraInfo
.
get
(
info
).
equals
(
""
))
bw
.
write
(
"-extraInfo-|"
+
info
+
"|"
+
userinfo
.
extraInfo
.
get
(
info
)
+
"\n"
);
}
keyIter
=
userinfo
.
varInitialMap
.
keySet
().
iterator
();
while
(
keyIter
.
hasNext
())
{
String
info
=
keyIter
.
next
();
bw
.
write
(
"-varInitial-|"
+
info
+
"|"
+
userinfo
.
varInitialMap
.
get
(
info
)
+
"\n"
);
}
Iterator
<
Integer
>
idIter
=
userinfo
.
id2config
.
keySet
().
iterator
();
while
(
idIter
.
hasNext
())
{
int
id
=
idIter
.
next
();
bw
.
write
(
userinfo
.
id2config
.
get
(
id
)
+
"\n"
);
}
bw
.
write
(
"\n"
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
returnMessage
=
"config file write error"
;
}
finally
{
try
{
bw
.
close
();
fw
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
return
returnMessage
;
}
}
src/main/java/com/qiwu/chatbotbackend/servlet/GameDesignServlet.java
View file @
cb904f39
...
...
@@ -115,10 +115,6 @@ public class GameDesignServlet extends HttpServlet {
//从文件中读取condition相关的关键词
readKeywords
(
keywordMap
,
KEYWORD_DIR
);
// ldt = LocalDateTime.now();
// now = ldt.format(dtf);
// timeUsedDetect.append("\n,预处理之后:").append(now);
String
moduleList
[]
=
{
"map"
,
"goods"
,
"globalVariable"
,
"npc"
,
"plots"
};
for
(
String
moduleName:
moduleList
){
...
...
@@ -267,7 +263,6 @@ public class GameDesignServlet extends HttpServlet {
*/
public
IntervalStatus
actionRegenerate
(
JSONObject
root
,
JSONObject
gameElements
,
UserInfo
userInfo
,
HashMap
<
String
,
HashSet
<
String
>>
additionalKeywords
,
HashSet
<
String
>
exceptRelation
,
StringBuffer
POS
,
SqlHelper
helper
){
String
startCondition
,
POS2
=
""
,
botName
=
gameElements
.
getString
(
"botName"
);
System
.
out
.
println
(
botName
);
Integer
startOption
=
gameElements
.
getInteger
(
"startOption"
);
JSONObject
returnMessage
=
null
;
EngineUtils
engine
=
null
;
...
...
@@ -276,7 +271,7 @@ public class GameDesignServlet extends HttpServlet {
JSONObject
actionPostData
=
new
JSONObject
();
startCondition
=
(
String
)
root
.
get
(
"conditionName"
);
actionPostData
.
put
(
"startCondition"
,
"start_options"
);
//
actionPostData.put("startCondition", "start_options");
if
(
startOption
!=
null
&&
startOption
.
equals
(
2
))
{
actionPostData
.
put
(
"welcome"
,
gameElements
.
getString
(
"plotStartText"
));
}
else
{
...
...
@@ -295,14 +290,9 @@ public class GameDesignServlet extends HttpServlet {
});
}
if
(
gameElements
.
containsKey
(
"POS"
))
POS2
=
(
String
)
gameElements
.
get
(
"POS"
);
actionPostData
.
put
(
"POS"
,
POS
.
toString
()
+
POS2
);
System
.
out
.
println
(
actionPostData
.
toJSONString
());
try
{
engine
=
new
EngineUtils
();
ActionUtils
actionUtils
=
new
ActionUtils
(
engine
,
helper
);
...
...
@@ -314,11 +304,8 @@ public class GameDesignServlet extends HttpServlet {
}
finally
{
engine
.
destroy
();
}
userInfo
.
extraInfo
.
put
(
"activeAction"
,
startCondition
);
return
intervalStatus
;
}
...
...
src/main/java/com/qiwu/chatbotbackend/servlet/GetAnswerServlet.java
View file @
cb904f39
...
...
@@ -43,32 +43,47 @@ public class GetAnswerServlet extends HttpServlet {
for
(
Map
.
Entry
<
String
,
String
[]>
entry
:
entrySet
)
{
if
(
entry
.
getValue
().
length
>
0
)
{
tmpStr
=
entry
.
getValue
()[
0
];
if
(
tmpStr
==
null
||
tmpStr
.
equals
(
""
))
if
(
tmpStr
==
null
||
tmpStr
.
trim
().
equals
(
""
))
continue
;
tmpStr
=
tmpStr
.
replace
(
"qiwu"
,
"我"
);
receiveParamters
.
put
(
entry
.
getKey
(),
tmpStr
);
}
}
String
botName
=
receiveParamters
.
get
(
"bot_name"
);
/*
EngineUtils
engineDBConn
=
null
;
String
gameName
=
null
,
playerName
=
null
;
try
{
engineDBConn
=
new
EngineUtils
();
String nickname = engineDBConn.queryBotNickNameByName(botName);
gameName
=
engineDBConn
.
queryBotNickNameByName
(
botName
);
engineDBConn
=
new
EngineUtils
();
playerName
=
engineDBConn
.
queryPlayerNickNameByName
(
botName
);
}
catch
(
SQLException
e1
)
{
e1
.
printStackTrace
();
}
finally
{
engineDBConn
.
destroy
();
}
Set<Entry<String, String>> entrySet2 = receiveParamters.entrySet();
for(Entry<String, String> entry : entrySet2) {
String value = entry.getValue();
if(value.indexOf("")) {
System
.
out
.
println
(
"project name: "
+
gameName
+
" player name: "
+
playerName
);
if
(
gameName
==
null
||
playerName
==
null
||
playerName
.
equals
(
""
)
||
gameName
.
equals
(
""
))
{
}
else
{
Set
<
Entry
<
String
,
String
>>
entrySet2
=
receiveParamters
.
entrySet
();
String
_key
=
null
,
_value
=
null
;
Boolean
_replace
=
false
;
for
(
Entry
<
String
,
String
>
entry
:
entrySet2
)
{
_replace
=
false
;
_key
=
entry
.
getKey
();
_value
=
entry
.
getValue
();
if
(
_value
.
indexOf
(
gameName
)
!=
-
1
)
{
_replace
=
true
;
_value
=
_value
.
replace
(
gameName
,
"你"
);
}
if
(
_value
.
indexOf
(
playerName
)
!=
-
1
)
{
_replace
=
true
;
_value
=
_value
.
replace
(
playerName
,
"我"
);
}
if
(
_replace
)
receiveParamters
.
put
(
_key
,
_value
);
}
}
*/
Map
<
String
,
String
>
curInfoPoint
=
new
HashMap
<
String
,
String
>();
Solution
bestMatch
=
null
;
...
...
@@ -112,13 +127,8 @@ public class GetAnswerServlet extends HttpServlet {
if
(
userinfo
.
varInitialMap
.
get
(
"botType"
)
!=
null
)
botType
=
userinfo
.
varInitialMap
.
get
(
"botType"
);
logWriter
.
record
(
"get chat cache start"
,
0
);
chatCache
=
ChatCacheController
.
getChatCache
(
userKey
,
randKey
,
context
,
userinfo
,
botType
);
logWriter
.
record
(
"get chat cache complete"
,
0
);
if
(
chatCache
==
null
)
{
throw
(
new
Exception
(
"chatCache初始化失败"
));
...
...
@@ -143,6 +153,7 @@ public class GetAnswerServlet extends HttpServlet {
//针对不同游戏类型对信息点做预处理
debugFlag
=
botUtil
.
preprocessing
(
receiveParamters
,
context
);
chatCache
.
sendPost
(
curInfoPoint
);
logWriter
.
record
(
"action match start"
,
0
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment