Commit fdca5a9f by 李进峰

game environment rebuild!

parent 0a43d064
package com.qiwu.chatbotbackend.GameEnvironment;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.log4j.Logger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class C3p0Utils {
static Logger logger = Logger.getLogger(C3p0Utils.class.getName());
//通过标识名来创建相应连接池
static ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql");
//从连接池中取用一个连接
public static Connection getConnection() throws Exception {
try {
return dataSource.getConnection();
} catch (Exception e) {
logger.error("Exception in C3p0Utils!", e);
throw e;
}
}
//释放连接回连接池
public static void close(Connection conn, PreparedStatement pst, ResultSet rs) throws SQLException {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
logger.error("Exception in C3p0Utils!", e);
throw e;
}
}
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
logger.error("Exception in C3p0Utils!", e);
throw e;
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
logger.error("Exception in C3p0Utils!", e);
throw e;
}
}
}
}
package com.qiwu.chatbotbackend.GameEnvironment; package com.qiwu.chatbotbackend.GameEnvironment;
import com.qiwu.chatbotbackend.db.C3p0Utils;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
......
...@@ -10,420 +10,483 @@ import java.util.List; ...@@ -10,420 +10,483 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.qiwu.chatbotbackend.GameEnvironment.bean.GoodsClass;
import com.qiwu.chatbotbackend.GameEnvironment.bean.Attr;
import com.qiwu.chatbotbackend.GameEnvironment.bean.GoodsInstance; import com.qiwu.chatbotbackend.GameEnvironment.bean.GoodsInstance;
import com.qiwu.chatbotbackend.GameEnvironment.bean.SuperClass; import com.qiwu.chatbotbackend.GameEnvironment.bean.Nickname;
import com.qiwu.chatbotbackend.GameEnvironment.bean.NpcClass;
import com.qiwu.chatbotbackend.GameEnvironment.bean.NpcInstance; import com.qiwu.chatbotbackend.GameEnvironment.bean.NpcInstance;
import com.qiwu.chatbotbackend.GameEnvironment.bean.Place; import com.qiwu.chatbotbackend.GameEnvironment.bean.Place;
import com.qiwu.chatbotbackend.GameEnvironment.bean.PlaceLink; import com.qiwu.chatbotbackend.GameEnvironment.bean.PlaceLink;
public class GameEnv implements Serializable{ public class GameEnv implements Serializable{
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 2L;
public Map<String, SuperClass> npcClassIndex; // 角色快速查找数据
public Map<String, NpcInstance> npcInstanceIndex; public Map<String, NpcClass> npcClassesMap = new HashMap<String, NpcClass>();
public Map<String, NpcInstance> followNpcIndex; public Map<String, NpcInstance> npcInstancesMap = new HashMap<String, NpcInstance>();
public Map<String, SuperClass> goodsClassIndex;
public Map<String, GoodsInstance> goodsInstanceIndex; // 玩家跟随的角色列表
public Map<String, String> globalAttrs; public Map<String, NpcInstance> followNpcesMap = new HashMap<String, NpcInstance>();
public Map<String, Place> placeIndex;
public Map<String, Place> placeLinkDirectGo; // 物品快速查找数据
public Map<String, Place> placeNoLinkDirectGo; public Map<String, GoodsClass> goodsClassesMap = new HashMap<String, GoodsClass>();
public byte[][] mapGraph; public Map<String, GoodsInstance> goodsInstancesMap = new HashMap<String, GoodsInstance>();
public Map<String, Integer>mapNames;
public String[] mapIndexName; // 全局变量快速查找数据
public Map<String, String> globalAttrsMap = new HashMap<String, String>();
// 地点相关快速查找数据
public Map<String, Place> placesMap = new HashMap<String, Place>();
// goGlag=2 联通不相邻可达
public Map<String, Place> placeLinkDirectGoesMap = new HashMap<String, Place>();
// goGlag=2 任意可达到
public Map<String, Place> placeNoLinkDirectGoesMap = new HashMap<String, Place>();
// 二维联通地图
public byte[][] mapUnitMatrix;
// 联通图助手
public Map<String, Integer> placeNameIndexMap = new HashMap<String, Integer>();
public Map<Integer, String> indexPlaceNameMap = new HashMap<Integer, String>();
// 数据别名集合
public Map<String, Nickname> nicknamesMap = new HashMap<String, Nickname>();
// 获取别名的助手,帮助创建
private Nickname getNicknameUtil(String nickname) {
Nickname nicknameObject = this.nicknamesMap.get(nickname);
if(nicknameObject == null) {
nicknameObject = new Nickname();
}
this.nicknamesMap.put(nickname, nicknameObject);
return nicknameObject;
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public GameEnv(String botName) throws Exception { public GameEnv(String botName) throws Exception {
// 通过文件来初始化游戏环境
// String fullName = "test.json";
// String jsonString = Util.getJsonString(fullName); // 从数据库获取环境数据的json字符串
// System.out.println(jsonString);
String jsonString = DBUtil.queryEnvJsonByBotName(botName); String jsonString = DBUtil.queryEnvJsonByBotName(botName);
// 从文件中获取json字符串
// String jsonString = Util.getJsonString("./data/195.json");
// System.out.println(jsonString);
Map<String, Object> gameProjectMap = JSON.parseObject(jsonString); Map<String, Object> gameProjectMap = JSON.parseObject(jsonString);
this.followNpcIndex = new HashMap<String, NpcInstance>(); if(gameProjectMap!=null && !gameProjectMap.isEmpty()) {
this.placeLinkDirectGo = new HashMap<String, Place>(); // 地图数据处理
this.placeNoLinkDirectGo = new HashMap<String, Place>(); Map<String, Object> placeTmpMap = (Map<String, Object>)gameProjectMap.get("place");
if(!gameProjectMap.isEmpty()) { if(placeTmpMap!=null && !placeTmpMap.isEmpty()) {
// 地图 // 地图基本数据定义
Map<String, Object> map = (Map<String, Object>)gameProjectMap.get("map"); List<String> placeNames = new ArrayList<>(placeTmpMap.keySet());
this.placeIndex = new HashMap<String, Place>(); for(int i=0; i<placeNames.size(); i++) {
Map<String, String> placeMapTmp = new HashMap<String, String>(); String placeName = placeNames.get(i);
this.mapGraph = new byte[map.size()][map.size()]; Map<String,Object> placeMap = (Map<String,Object>)placeTmpMap.get(placeName);
this.mapNames = new HashMap<String, Integer>();
if(!map.isEmpty()) {
// 第一步先把除了 canGo 之外的信息创建一个对象
int i= 0;
for(String key:map.keySet()) {
Map<String, Object> one = (Map<String, Object>)map.get(key);
placeMapTmp.put(key, (String)one.get("name"));
Place place = new Place(); Place place = new Place();
place.setIntro((String)one.get("intro")); place.setGoFlag((Integer)placeMap.get("goFlag"));
place.setName((String)one.get("name")); place.setIntro((String)placeMap.get("intro"));
if(one.get("goFlag")!=null) { place.setName(placeName);
place.setGoFlag((Integer)one.get("goFlag")); this.placesMap.put(placeName, place);
if(((Integer)one.get("goFlag")).equals(new Integer(2))) { // 为了生成单位矩阵使用
this.placeLinkDirectGo.put(place.getName(), place); this.placeNameIndexMap.put(placeName, new Integer(i));
}else if(((Integer)one.get("goFlag")).equals(new Integer(3))) { this.indexPlaceNameMap.put(new Integer(i), placeName);
this.placeNoLinkDirectGo.put(place.getName(), place);
}
}else {
place.setGoFlag(new Integer(1));
} }
List<String> nickname = (List<String>)one.get("nickname"); // 定义的单位矩阵
place.setGoodsInstances(new ArrayList<GoodsInstance>()); this.mapUnitMatrix = new byte[this.placesMap.size()][this.placesMap.size()];
place.setNpcInstances(new ArrayList<NpcInstance>());
place.setNickname(nickname); // 可去地点、别名、图的数据完善
mapNames.put((String)one.get("name"), i); for(String placeName:placeTmpMap.keySet()) {
i++; Map<String,Object> placeMap = (Map<String,Object>)placeTmpMap.get(placeName);
placeIndex.put((String)one.get("name"), place); Place place = this.placesMap.get(placeName);
// 可去地点
Map<String, Place> ableGoPlacesMap = new HashMap<String, Place>();
List<String> ableGoPlaceNames = (List<String>)placeMap.get("ableGoes");
for(int i=0; i<ableGoPlaceNames.size(); i++) {
String ableGoPlaceName = ableGoPlaceNames.get(i);
ableGoPlacesMap.put("placeName", this.placesMap.get(ableGoPlaceName));
// 添加图结构数据 有向性单位矩阵
int x = this.placeNameIndexMap.get(placeName);
int y = this.placeNameIndexMap.get(ableGoPlaceName);
this.mapUnitMatrix[x][y] = 1;
}
place.setAbleGoes(ableGoPlacesMap);
// 别名
List<String> nicknames = (List<String>)placeMap.get("nicknames");
for(int i=0; i<nicknames.size(); i++) {
Nickname nickname = this.getNicknameUtil(nicknames.get(i));
Map<String,Place> nicknamePlaceMap = nickname.getPlaceMap();
nicknamePlaceMap.put(place.getName(), place);
}
place.setNickname(nicknames);
place.setName(placeName);
} }
// 添加 canGo 指针关联 // 格式化图显示结构
for(String tmp:placeMapTmp.keySet()) { System.out.println("\n\n");
List<Place> ableGos = new ArrayList<Place>(); System.out.print("\t\t");
List<Long> ableGoLongs = (List<Long>)((Map<String, Object>)map.get(tmp)).get("canGo"); for(int i=0; i<mapUnitMatrix.length; i++) {
for(int j=0; j<ableGoLongs.size();j++) { System.out.printf("%8s\t", this.indexPlaceNameMap.get(new Integer(i)));
String idName = String.valueOf(ableGoLongs.get(j)); }
ableGos.add(placeIndex.get(placeMapTmp.get(idName))); System.out.print("\n");
// 添加图结构数据 for(int i=0; i<this.mapUnitMatrix.length; i++) {
int x = this.mapNames.get(placeMapTmp.get(tmp)); System.out.printf("%8s\t", this.indexPlaceNameMap.get(new Integer(i)));
int y = this.mapNames.get(placeMapTmp.get(idName)); for(int j=0; j<this.mapUnitMatrix[i].length; j++) {
this.mapGraph[x][y] = 1; System.out.printf("%8s\t",this.mapUnitMatrix[i][j]);
// 有方向的添加 }
// this.mapGraph[y][x] = 1; System.out.print("\n\n");
} }
placeIndex.get(placeMapTmp.get(tmp)).setAbleGo(ableGos);
} }
// 映射index -> map name /*************************************** 角色 **********************************/
this.mapIndexName = new String[mapGraph.length]; Map<String, Object> npcDataTmpMap = (Map<String, Object>)gameProjectMap.get("npc");
for(i=0; i<mapGraph.length; i++) { // 角色类
for(String mapName:mapNames.keySet()) { Map<String, Object> npcClassTmpMap = (Map<String, Object>)npcDataTmpMap.get("class");
if(mapNames.get(mapName).equals(i)) { for(String npcClassName:npcClassTmpMap.keySet()) {
mapIndexName[i] = mapName; Map<String, String> npcClassAttr = (Map<String, String>)npcClassTmpMap.get(npcClassName);
NpcClass npcClass = new NpcClass();
npcClass.setName(npcClassName);
npcClass.setPlace(this.placesMap.get(npcClassAttr.get("所在地点")));
npcClassAttr.remove("所在地点");
npcClass.setAttrs(npcClassAttr);
this.npcClassesMap.put(npcClassName, npcClass);
}
// 角色实例
Map<String, Object> npcInstanceTmpMap = (Map<String, Object>)npcDataTmpMap.get("instance");
for(String npcInstanceName:npcInstanceTmpMap.keySet()){
Map<String, String> npcInstanceAttr = (Map<String, String>)npcInstanceTmpMap.get(npcInstanceName);
NpcInstance npcInstance = new NpcInstance();
npcInstance.setName(npcInstanceName);
// 角色类
NpcClass npcClass = this.npcClassesMap.get(npcInstanceAttr.get("className"));
npcInstance.setNpcClass(npcClass);
Map<String, NpcInstance> npcInstancesClassMap = npcClass.getNpcInstances();
npcInstancesClassMap.put(npcInstanceName, npcInstance);
npcClass.setNpcInstances(npcInstancesClassMap);
// 地点
Place place = this.placesMap.get(npcInstanceAttr.get("所在地点"));
Map<String, NpcInstance> npcInstancesPlaceMap = place.getNpcInstances();
npcInstancesPlaceMap.put(npcInstanceName, npcInstance);
npcInstance.setPlace(place);
// 别名
List<String> nicknames = (List<String>)((Map<String, Object>)npcInstanceTmpMap.get(npcInstanceName)).get("nicknames");
for(int i=0; i<nicknames.size(); i++) {
Nickname nickname = this.getNicknameUtil(nicknames.get(i));
Map<String,NpcInstance> npcInstanceMap = nickname.getNpcInstanceMap();
npcInstanceMap.put(npcInstanceName, npcInstance);
nickname.setNpcInstanceMap(npcInstanceMap);
}
npcInstance.setNickname(nicknames);
npcInstanceAttr.remove("nicknames");
npcInstanceAttr.remove("className");
npcInstanceAttr.remove("所在地点");
npcInstance.setAttrs((Map<String, String>)npcInstanceAttr);
this.npcInstancesMap.put(npcInstanceName, npcInstance);
} }
/************************************* 物品 **********************************/
Map<String, Object> goodsDataTmpMap = (Map<String,Object>)gameProjectMap.get("goods");
// 物品类
Map<String, Object> goodsClassTmpMap = (Map<String, Object>)goodsDataTmpMap.get("class");
for(String goodsClassName:goodsClassTmpMap.keySet()) {
Map<String, String> goodsClassAttr = (Map<String, String>)goodsClassTmpMap.get(goodsClassName);
GoodsClass goodsClass = new GoodsClass();
goodsClass.setName(goodsClassName);
goodsClass.setPlace(this.placesMap.get(goodsClassAttr.get("所在地点")));
goodsClassAttr.remove("所在地点");
goodsClass.setAttrs(goodsClassAttr);
this.goodsClassesMap.put(goodsClassName, goodsClass);
} }
// 物品实例
Map<String, Object> goodsInstanceTmpMap = (Map<String, Object>)goodsDataTmpMap.get("instance");
for(String goodsInstanceName:goodsInstanceTmpMap.keySet()){
Map<String, String> goodsInstanceAttr = (Map<String, String>)goodsInstanceTmpMap.get(goodsInstanceName);
GoodsInstance goodsInstance = new GoodsInstance();
goodsInstance.setName(goodsInstanceName);
// 物品类
GoodsClass goodsClass = this.goodsClassesMap.get(goodsInstanceAttr.get("className"));
goodsInstance.setGoodsClass(goodsClass);
Map<String, GoodsInstance> goodsInstancesClassMap = goodsClass.getGoodsInstances();
goodsInstancesClassMap.put(goodsInstanceName, goodsInstance);
goodsClass.setGoodsInstances(goodsInstancesClassMap);
// 地点
Place place = this.placesMap.get(goodsInstanceAttr.get("所在地点"));
Map<String, GoodsInstance> goodsInstancesPlaceMap = place.getGoodsInstances();
goodsInstancesPlaceMap.put(goodsInstanceName, goodsInstance);
goodsInstance.setPlace(place);
// 别名
List<String> nicknames = (List<String>)(((Map<String, Object>)goodsInstanceTmpMap.get(goodsInstanceName)).get("nicknames"));
for(int i=0; i<nicknames.size(); i++) {
Nickname nickname = this.getNicknameUtil(nicknames.get(i));
Map<String,GoodsInstance> goodsInstanceMap = nickname.getGoodsInstanceMap();
goodsInstanceMap.put(goodsInstanceName, goodsInstance);
nickname.setGoodsInstanceMap(goodsInstanceMap);
}
// 所属角色
// 更新NPC的物品列表
NpcInstance npcInstance = this.npcInstancesMap.get(goodsInstanceAttr.get("所属角色"));
Map<String, GoodsInstance> goodsInstancesMap = npcInstance.getGoodsInstances();
goodsInstancesMap.put(goodsInstance.getName(), goodsInstance);
// 更新物品所属的NPC
goodsInstance.setNpcInstance(npcInstance);
goodsInstance.setNickname(nicknames);
goodsInstanceAttr.remove("nicknames");
goodsInstanceAttr.remove("className");
goodsInstanceAttr.remove("所在地点");
goodsInstanceAttr.remove("所属角色");
goodsInstance.setAttrs((Map<String, String>)goodsInstanceAttr);
this.goodsInstancesMap.put(goodsInstanceName, goodsInstance);
} }
System.out.println("\n"); // 全局变量
System.out.print("\t"); this.globalAttrsMap = (Map<String,String>)gameProjectMap.get("globalAttr");
for(i=0; i<mapGraph.length; i++) {
for(String mapName:mapNames.keySet()) {
if(mapNames.get(mapName).equals(i)) {
System.out.print(mapName+"\t");
} }
} }
// 查找别名数据
public List<String> queryNickname(String nicknameStr, String type) {
Nickname nickname = this.nicknamesMap.get(nicknameStr);
List<String> nameIds = new ArrayList<String>();
if(type.equals("place")) {
if(nickname != null && !nickname.getPlaceMap().isEmpty()) {
nameIds.addAll(nickname.getPlaceMap().keySet());
} }
System.out.print("\n"); }else if(type.equals("goods")) {
for(i=0; i<this.mapGraph.length; i++) { if(nickname != null && !nickname.getGoodsInstanceMap().isEmpty()) {
for(String mapName:mapNames.keySet()) { nameIds.addAll(nickname.getGoodsInstanceMap().keySet());
if(mapNames.get(mapName).equals(i)) {
System.out.print(mapName+"\t");
} }
}else if(type.equals("npc")) {
if(nickname != null && !nickname.getNpcInstanceMap().isEmpty()) {
nameIds.addAll(nickname.getNpcInstanceMap().keySet());
} }
for(int j=0; j<this.mapGraph[i].length; j++) {
System.out.print(this.mapGraph[i][j]+"\t");
} }
System.out.print("\n\n"); return nameIds;
} }
public GoodsInstance findGoodsInstanceByNameOrNickname(String str1) {
GoodsInstance goodsInstance = queryGoodsByGoodsName(str1);
if(goodsInstance == null) {
List<String> idNames = this.queryNickname(str1, "goods");
if(idNames.size()!=0) {
goodsInstance = this.goodsInstancesMap.get(idNames.get(0));
return goodsInstance;
}else {
return null;
} }
// npc }else {
// npc class return goodsInstance;
Map<String, Object> npcClasses = (Map<String, Object>)gameProjectMap.get("npc"); }
Map<String, Object> npcNames = new HashMap<String, Object>(); }
this.npcClassIndex = new HashMap<String, SuperClass>(); public NpcInstance findNpcInstanceByNameOrNickname(String str1) {
this.npcInstanceIndex = new HashMap<String, NpcInstance>(); NpcInstance npcInstance = this.queryNpcByNpcName(str1);
if(!npcClasses.isEmpty()) { if(npcInstance == null) {
for(String classKey:npcClasses.keySet()) { List<String> idNames = this.queryNickname(str1, "npc");
Map<String, Object> one = (Map<String, Object>)npcClasses.get(classKey); if(idNames.size()!=0) {
// class npcInstance = this.npcInstancesMap.get(idNames.get(0));
Map<String, Object> oneClass = (Map<String, Object>)one.get("class"); return npcInstance;
SuperClass npcClass = new SuperClass(); }else {
npcClass.setName(classKey); return null;
npcClass.setIntro((String)oneClass.get("intro")); }
}else {
List<Attr> attrs = new ArrayList<Attr>(); return npcInstance;
for(String attrKey:oneClass.keySet()) { }
if(!attrKey.equals("intro") && !attrKey.equals("class") && !attrKey.equals("")) { }
Attr attr = new Attr(); // 两点之间的路径 // 使用广度优先算法单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm。只有状态为2的时候使用
attr.setName(attrKey); public List<String> getAllPath(String placeA, String placeB){
attr.setValue((String)oneClass.get(attrKey)); List<String> paths = new ArrayList<String>();
attrs.add(attr); Deque<PlaceLink> placeDeque = new ArrayDeque<PlaceLink>();
} Integer start = this.placeNameIndexMap.get(placeA);
} PlaceLink placeLink = new PlaceLink();
npcClass.setAttrs(attrs); placeLink.setIndex(start);
// instance boolean[] marked = new boolean[this.placeNameIndexMap.size()];
Map<String, Object> instances = (Map<String, Object>)one.get("instance"); for(int j=0; j<marked.length; j++) {
for(String instanceKey:instances.keySet()) { marked[j] = false;
NpcInstance npcInstance = new NpcInstance(); }
Map<String, Object> instanceAttrMap = (Map<String, Object>)instances.get(instanceKey); placeDeque.push(placeLink);
npcNames.put(instanceKey, (String)instanceAttrMap.get("name")); int flag = 0;
npcInstance.setName((String)instanceAttrMap.get("name")); while(!placeDeque.isEmpty() && flag == 0) {
npcInstance.setIntro((String)instanceAttrMap.get("intro")); PlaceLink placeLinkNode = placeDeque.pop();
npcInstance.setNickname((List<String>)instanceAttrMap.get("nickname")); marked[placeLinkNode.getIndex()] = true;
List<Attr> npcInstanceAttrs = new ArrayList<Attr>(); for(int i=0; i<this.mapUnitMatrix[placeLinkNode.getIndex()].length; i++) {
for(String instanceAttrKey:instanceAttrMap.keySet()) { if(marked[i]==false) {
if(!instanceAttrKey.equals("name") if(this.mapUnitMatrix[placeLinkNode.getIndex()][i] == 1) {
&&!instanceAttrKey.equals("intro") marked[i] = true;
&&!instanceAttrKey.equals("placeId") PlaceLink placeLinkNodeSecond = new PlaceLink();
&&!instanceAttrKey.equals("所在地点") placeLinkNodeSecond.setIndex(i);
&&!instanceAttrKey.equals("placeName") placeLinkNodeSecond.setPlaceLink(placeLinkNode);
&&!instanceAttrKey.equals("class") List<String> nickname = this.placesMap.get(this.indexPlaceNameMap.get(new Integer(i))).getNickname();
&&!instanceAttrKey.equals("nickname")) { if(this.indexPlaceNameMap.get(new Integer(i)).equals(placeB) || (nickname!=null && nickname.indexOf(placeB)>=0)) {
Attr attr = new Attr(); while(placeLinkNodeSecond.getPlaceLink() != null) {
attr.setName(instanceAttrKey); paths.add(this.indexPlaceNameMap.get(new Integer(placeLinkNodeSecond.getIndex())));
attr.setValue((String)instanceAttrMap.get(instanceAttrKey)); placeLinkNodeSecond = placeLinkNodeSecond.getPlaceLink();
npcInstanceAttrs.add(attr); }
if(instanceAttrKey.equals("跟随玩家")&&((String)instanceAttrMap.get(instanceAttrKey)).equals("是")) { paths.add(this.indexPlaceNameMap.get(new Integer(placeLinkNodeSecond.getIndex())));
this.followNpcIndex.put(npcInstance.getName(), npcInstance); flag = 1;
} break;
} }
} placeDeque.push(placeLinkNodeSecond);
npcInstance.setAttrs(npcInstanceAttrs); }
Place place = null; }
if(instanceAttrMap.get("所在地点")!=null) { }
if(!instanceAttrMap.get("所在地点").equals("")) { }
Map<String, Object> placeMap = (Map<String, Object>)map.get((String)instanceAttrMap.get("所在地点")); Collections.reverse(paths);
if(placeMap!=null) { return paths;
place = placeIndex.get(placeMap.get("name")); }
List<NpcInstance> npcInstances = place.getNpcInstances(); // 两点之间的通过的地方
if(npcInstances == null) { public List<String> queryPathBySite(String placeA, String placeB){
npcInstances = new ArrayList<NpcInstance>(); List<String> paths = this.getAllPath(placeA, placeB);
} if(paths.size() < 2) {
npcInstances.add(npcInstance); // 返回null表示不能到达
place.setNpcInstances(npcInstances); return null;
} }else{
} // 返回空表示相邻直接去
} // 中间有数据说明需要经过...
return paths.subList(1, paths.size()-1);
// if(instanceAttrMap.get("placeId")!=null) {
// Map<String, Object> placeMap = (Map<String, Object>)map.get(String.valueOf(instanceAttrMap.get("placeId")));
// if(placeMap!=null) {
// place = placeIndex.get(placeMap.get("name"));
// List<NpcInstance> npcInstances = place.getNpcInstances();
// if(npcInstances == null) {
// npcInstances = new ArrayList<NpcInstance>();
// }
// npcInstances.add(npcInstance);
// place.setNpcInstances(npcInstances);
// }
// }
List<NpcInstance> npcInstanceClass = npcClass.getNpcInstances();
if(npcInstanceClass == null) {
npcInstanceClass = new ArrayList<NpcInstance>();
}
npcInstanceClass.add(npcInstance);
npcClass.setNpcInstances(npcInstanceClass);
npcInstance.setPlace(place);
this.npcInstanceIndex.put((String)instanceAttrMap.get("name"), npcInstance);
}
this.npcClassIndex.put(classKey, npcClass);
}
}
// goods
Map<String, Object> goodsClasses = (Map<String,Object>)gameProjectMap.get("goods");
this.goodsClassIndex = new HashMap<String, SuperClass>();
this.goodsInstanceIndex = new HashMap<String, GoodsInstance>();
if(!goodsClasses.isEmpty()) {
for(String classKey:goodsClasses.keySet()) {
Map<String, Object> one = (Map<String, Object>)goodsClasses.get(classKey);
// class
Map<String, Object> oneClass = (Map<String, Object>)one.get("class");
SuperClass goodsClass = new SuperClass();
goodsClass.setName(classKey);
goodsClass.setIntro((String)oneClass.get("intro"));
List<Attr> attrs = new ArrayList<Attr>();
for(String attrKey:oneClass.keySet()) {
if(!attrKey.equals("intro")&&!attrKey.equals("placeId")&&!attrKey.equals("npcName")&&!attrKey.equals("class")&&!attrKey.equals("placeName")) {
Attr attr = new Attr();
attr.setName(attrKey);
attr.setValue((String)oneClass.get(attrKey));
attrs.add(attr);
}
}
goodsClass.setAttrs(attrs);
// instance
Map<String, Object> instances = (Map<String, Object>)one.get("instance");
for(String instanceKey:instances.keySet()) {
GoodsInstance goodsInstance = new GoodsInstance();
Map<String, Object> instanceAttrMap = (Map<String, Object>)instances.get(instanceKey);
goodsInstance.setName((String)instanceAttrMap.get("name"));
goodsInstance.setIntro((String)instanceAttrMap.get("intro"));
goodsInstance.setNickname((List<String>)instanceAttrMap.get("nickname"));
List<Attr> goodsInstanceAttrs = new ArrayList<Attr>();
for(String instanceAttrKey:instanceAttrMap.keySet()) {
if(instanceAttrKey!="placeId"
&&instanceAttrKey!="npcId"
&&instanceAttrKey!="所在地点"
&&instanceAttrKey!="所属角色"
&&instanceAttrKey!="name"
&&instanceAttrKey!="intro"
&&instanceAttrKey!="nickname") {
Attr attr = new Attr();
attr.setName(instanceAttrKey);
attr.setValue((String)instanceAttrMap.get(instanceAttrKey));
goodsInstanceAttrs.add(attr);
}
}
goodsInstance.setAttrs(goodsInstanceAttrs);
if(instanceAttrMap.get("所在地点")!=null) {
if(!instanceAttrMap.get("所在地点").equals("")) {
Map<String, Object> placeMap = (Map<String, Object>)map.get((String)instanceAttrMap.get("所在地点"));
Place place = placeIndex.get(placeMap.get("name"));
goodsInstance.setPlace(place);
List<GoodsInstance> placeGoodsInstances= place.getGoodsInstances();
if(placeGoodsInstances == null) {
placeGoodsInstances = new ArrayList<GoodsInstance>();
}
placeGoodsInstances.add(goodsInstance);
place.setGoodsInstances(placeGoodsInstances);
}
}
if(instanceAttrMap.get("所属角色")!=null){
if(!instanceAttrMap.get("所属角色").equals("")) {
NpcInstance npc = this.npcInstanceIndex.get(npcNames.get((String)instanceAttrMap.get("所属角色")));
goodsInstance.setNpc(npc);
List<GoodsInstance> npcGoodsInstances = npc.getGoodsInstances();
if(npcGoodsInstances == null) {
npcGoodsInstances = new ArrayList<GoodsInstance>();
}
npcGoodsInstances.add(goodsInstance);
npc.setGoodsInstances(npcGoodsInstances);
}
}
// if(instanceAttrMap.get("placeId")!=null) {
// Map<String, Object> placeMap = (Map<String, Object>)map.get(String.valueOf(instanceAttrMap.get("placeId")));
// Place place = placeIndex.get(placeMap.get("name"));
// goodsInstance.setPlace(place);
// List<GoodsInstance> placeGoodsInstances= place.getGoodsInstances();
// if(placeGoodsInstances == null) {
// placeGoodsInstances = new ArrayList<GoodsInstance>();
// }
// placeGoodsInstances.add(goodsInstance);
// place.setGoodsInstances(placeGoodsInstances);
// }
// if(instanceAttrMap.get("npcId")!=null){
// NpcInstance npc = this.npcInstanceIndex.get(npcNames.get(String.valueOf(instanceAttrMap.get("npcId"))));
// goodsInstance.setNpc(npc);
// List<GoodsInstance> npcGoodsInstances = npc.getGoodsInstances();
// if(npcGoodsInstances == null) {
// npcGoodsInstances = new ArrayList<GoodsInstance>();
// }
// npcGoodsInstances.add(goodsInstance);
// npc.setGoodsInstances(npcGoodsInstances);
// }
List<GoodsInstance> goodsInstancesClass = goodsClass.getGoodsInstances();
if(goodsInstancesClass == null) {
goodsInstancesClass = new ArrayList<GoodsInstance>();
}
goodsInstancesClass.add(goodsInstance);
goodsClass.setGoodsInstances(goodsInstancesClass);
this.goodsInstanceIndex.put((String)instanceAttrMap.get("name"), goodsInstance);
}
this.goodsClassIndex.put(classKey, goodsClass);
}
}
// global attr
this.globalAttrs = (Map<String,String>)gameProjectMap.get("globalAttr");
} }
} }
/********************************************地点******************************************/ /********************************************地点******************************************/
// 周围有什么 // 周围有什么
public List<String> queryAroundAllBySite(String site){ public List<String> queryAroundAllBySite(String site){
List<String> queryGoods = this.queryAroundGoodsBySite(site); List<String> somethings = new ArrayList<String>();
List<String> queryNpc = this.queryAroundNpcBySite(site); Place place = this.placesMap.get(site);
queryGoods.addAll(queryNpc); if(place==null) {
return queryGoods; return somethings;
}
Map<String, GoodsInstance> goodsInstances = place.getGoodsInstances();
Map<String, NpcInstance> npcInstances = place.getNpcInstances();
somethings.addAll(goodsInstances.keySet());
somethings.addAll(npcInstances.keySet());
return somethings;
} }
// 周围有什么人 // 周围有什么人
public List<String> queryAroundNpcBySite(String site){ public List<String> queryAroundNpcBySite(String site){
Place place = this.placeIndex.get(site); Place place = this.placesMap.get(site);
List<String> queryNpc = new ArrayList<String>(); List<String> queryNpc = new ArrayList<String>();
if(place !=null) { if(place == null) {
List<NpcInstance> npcInstances = place.getNpcInstances(); return queryNpc;
if(npcInstances!=null) {
for(int i=0; i<npcInstances.size(); i++) {
queryNpc.add(npcInstances.get(i).getName());
}
}
} }
queryNpc.addAll(place.getNpcInstances().keySet());
return queryNpc; return queryNpc;
} }
// 周围有什么物品 // 周围有什么物品
public List<String> queryAroundGoodsBySite(String site){ public List<String> queryAroundGoodsBySite(String site){
Place place = this.placeIndex.get(site);
List<String> queryGoods = new ArrayList<String>(); List<String> queryGoods = new ArrayList<String>();
if(place !=null) { Place place = this.placesMap.get(site);
List<GoodsInstance> goodsInstances = place.getGoodsInstances(); if(place == null) {
if(goodsInstances!=null) { return queryGoods;
for(int i=0; i<goodsInstances.size(); i++) {
queryGoods.add(goodsInstances.get(i).getName());
}
}
} }
queryGoods.addAll(place.getGoodsInstances().keySet());
return queryGoods; return queryGoods;
} }
// 我现在能去哪 // 我现在能去哪 需要用到联通图算法
public List<String> queryCanGoBySite(String site){ public List<String> queryCanGoBySite(String site){
List<String> ableGoString = new ArrayList<String>(); List<String> ableGoStrs = new ArrayList<String>();
int x = this.mapNames.get(site); // 任意可达
for(int i=0; i<this.mapGraph[x].length; i++) { for(String placeName : this.placeNoLinkDirectGoesMap.keySet()) {
if(this.mapGraph[x][i]==1) { ableGoStrs.add(placeName);
ableGoString.add(this.mapIndexName[i]); }
// 查询附近地点
Integer x = this.placeNameIndexMap.get(site);
if(x == null) {
return ableGoStrs;
}
for(int y=0; y<this.mapUnitMatrix[x].length; y++) {
if(this.mapUnitMatrix[x][y]==1) {
ableGoStrs.add(this.indexPlaceNameMap.get(y));
} }
} }
for(String placeName:this.placeLinkDirectGo.keySet()) { // 联通地点
ableGoString.add(placeName); for(String placeName:this.placesMap.keySet()) {
if(ableGoStrs.indexOf(placeName) == -1) {
List<String> paths = this.getAllPath(site, placeName);
if(paths.size()>0) {
ableGoStrs.add(placeName);
} }
for(String placeName:this.placeNoLinkDirectGo.keySet()) {
ableGoString.add(placeName);
} }
return ableGoString;
} }
// 是否相邻 return ableGoStrs;
}
// 两个地点的位置关系 ### 这个接口有疑问
/*
* 0:就在当前地点
* 1:相邻可以到
* 2:不相邻,联通可以到达
* 3:联通直接可以到达
* 4:任意可达
* 5:不可到达
* */
public Integer isNeighbour(String placeA, String placeB) { public Integer isNeighbour(String placeA, String placeB) {
try { Place place = this.placesMap.get(placeA);
int x = this.mapNames.get(placeA); if(place == null) {
if(placeA.equals(placeB) || this.placeIndex.get(placeA).getNickname().indexOf(placeB)>=0) { return new Integer(5);
}
// 地点名称相同
if(placeA.equals(placeB)) {
return 0; return 0;
} }
for(int j=0;j<this.mapGraph[x].length;j++) {
if(this.mapGraph[x][j] ==1) { // 地点名和地点别名相同
if(this.mapIndexName[j].equals(placeB) || this.placeIndex.get(this.mapIndexName[j]).getNickname().indexOf(placeB)>=0) { if(this.queryNickname(placeB, "place").indexOf(placeA) != -1) {
return 0;
}
// 任意可达
for(String placeName : this.placeNoLinkDirectGoesMap.keySet()) {
if(placeName.equals(placeB) || this.queryNickname(placeB, "place").indexOf(placeName) != -1) {
return 4;
}
}
// 相邻可以去的地点
int x = this.placeNameIndexMap.get(placeA);
for(int y=0; y<this.mapUnitMatrix[x].length; y++) {
if(this.mapUnitMatrix[x][y] == 1) {
String placeName = this.indexPlaceNameMap.get(new Integer(y));
if(placeName.equals(placeB) || this.queryNickname(placeB, "place").indexOf(placeName) != -1) {
return 1; return 1;
} }
} }
} }
for(String placeName:this.placeLinkDirectGo.keySet()) {
if(placeName.equals(placeB) || this.placeLinkDirectGo.get(placeName).getNickname().indexOf(placeB)>=0) { // 联通不相邻可直达
for(String placeName:this.placeLinkDirectGoesMap.keySet()) {
if(placeName.equals(placeB) || this.queryNickname(placeB, "place").indexOf(placeName) != -1) {
List<String> paths = this.getAllPath(placeA, placeName);
if(paths.size()>0) {
return 3; return 3;
} }
} }
for(String placeName:this.placeNoLinkDirectGo.keySet()) {
if(placeName.equals(placeB) || this.placeNoLinkDirectGo.get(placeName).getNickname().indexOf(placeB)>=0) {
return 4;
}
} }
// 联通不相邻不可以直达
for(String placeName:this.placesMap.keySet()) {
if(!this.placeLinkDirectGoesMap.keySet().contains(placeName) && !this.placeNoLinkDirectGoesMap.keySet().contains(placeName)) {
if(placeName.equals(placeB) || this.queryNickname(placeB, "place").indexOf(placeName) != -1) {
List<String> paths = this.getAllPath(placeA, placeName);
if(paths.size()>0) {
return 2; return 2;
}catch(Exception e) {
return 2;
} }
} }
// 通过别名查询当前地点最近的别名地点 }
}
// 既不相邻,也不能到达
return new Integer(5);
}
// 通过别名查询当前地点最近的地点
public String getPlaceNameByNickname(String curPlaceName, String nickname) { public String getPlaceNameByNickname(String curPlaceName, String nickname) {
Place place = this.placeIndex.get(curPlaceName); Place place = this.placesMap.get(curPlaceName);
if(place == null) { if(place == null) {
return nickname; return nickname;
} }
if(place.getNickname().indexOf(nickname)>=0) { if(place.getNickname().indexOf(nickname)>=0) {
return curPlaceName; return curPlaceName;
}else { }else {
List<String> placePath = getAllPath(curPlaceName, nickname); List<String> placePath = this.getAllPath(curPlaceName, nickname);
if(placePath.size()>=2) { if(placePath.size()>=2) {
return placePath.get(placePath.size()-1); return placePath.get(placePath.size()-1);
}else { }else {
...@@ -431,28 +494,29 @@ public class GameEnv implements Serializable{ ...@@ -431,28 +494,29 @@ public class GameEnv implements Serializable{
} }
} }
} }
// 通过别名查询最近地点的角色
public String queryCurPlaceNpcInstanceByNickname(String curPlaceName, String nickname) { public String queryCurPlaceNpcInstanceByNickname(String curPlaceName, String nickname) {
List<NpcInstance> npcInstances = this.placeIndex.get(curPlaceName).getNpcInstances(); Map<String, NpcInstance> npcInstancesMap = this.placesMap.get(curPlaceName).getNpcInstances();
for(int i=0; i<npcInstances.size();i++) { for(String npcInstanceName:npcInstancesMap.keySet()) {
NpcInstance npcInstance = npcInstances.get(i); NpcInstance npcInstance = npcInstancesMap.get(npcInstanceName);
if(npcInstance.getNickname().indexOf(nickname)>=0) { if(npcInstance.getNickname().indexOf(nickname) >= 0) {
return npcInstance.getName(); return npcInstance.getName();
} }
} }
return null; return null;
} }
// 通过别名查询最近地点的物品
public String queryCurPlaceGoodsInstanceByNickname(String curPlaceName, String nickname) { public String queryCurPlaceGoodsInstanceByNickname(String curPlaceName, String nickname) {
List<GoodsInstance> goodsInstances = this.placeIndex.get(curPlaceName).getGoodsInstances(); Map<String, GoodsInstance> goodsInstancesMap = this.placesMap.get(curPlaceName).getGoodsInstances();
for(int i=0; i<goodsInstances.size();i++) { for(String goodsInstanceName:goodsInstancesMap.keySet()) {
GoodsInstance goodsInstance = goodsInstances.get(i); GoodsInstance goodsInstance = goodsInstancesMap.get(goodsInstanceName);
if(goodsInstance.getNickname().indexOf(nickname)>=0) { if(goodsInstance.getNickname().indexOf(nickname)>=0) {
return goodsInstance.getName(); return goodsInstance.getName();
} }
} }
return null; return null;
} }
// 通过别名查询当前地点最近的npc // 通过别名查询当前地点最近的npc
public String getNpcNameByNickname(String curPlaceName, String nickname) { public String getNpcNameByNickname(String curPlaceName, String nickname) {
// 寻找两层 // 寻找两层
...@@ -461,9 +525,9 @@ public class GameEnv implements Serializable{ ...@@ -461,9 +525,9 @@ public class GameEnv implements Serializable{
if(npcInstanceName!=null) { if(npcInstanceName!=null) {
return npcInstanceName; return npcInstanceName;
} }
List<Place> places = this.placeIndex.get(curPlaceName).getAbleGo(); Map<String,Place> placesMap = this.placesMap.get(curPlaceName).getAbleGoes();
for(int i=0; i<places.size(); i++) { for(String placeName:placesMap.keySet()) {
Place place = places.get(i); Place place = placesMap.get(placeName);
npcInstanceName = this.queryCurPlaceNpcInstanceByNickname(place.getName(), nickname); npcInstanceName = this.queryCurPlaceNpcInstanceByNickname(place.getName(), nickname);
if(npcInstanceName!=null) { if(npcInstanceName!=null) {
return npcInstanceName; return npcInstanceName;
...@@ -471,6 +535,7 @@ public class GameEnv implements Serializable{ ...@@ -471,6 +535,7 @@ public class GameEnv implements Serializable{
} }
return nickname; return nickname;
} }
// 通过别名查询当前地点最近的物品 // 通过别名查询当前地点最近的物品
public String getGoodsNameByNickname(String curPlaceName, String nickname) { public String getGoodsNameByNickname(String curPlaceName, String nickname) {
// 寻找两层 // 寻找两层
...@@ -479,9 +544,9 @@ public class GameEnv implements Serializable{ ...@@ -479,9 +544,9 @@ public class GameEnv implements Serializable{
if(npcInstanceName!=null) { if(npcInstanceName!=null) {
return npcInstanceName; return npcInstanceName;
} }
List<Place> places = this.placeIndex.get(curPlaceName).getAbleGo(); Map<String, Place> cangoPlacesMap = this.placesMap.get(curPlaceName).getAbleGoes();
for(int i=0; i<places.size(); i++) { for(String placeName:cangoPlacesMap.keySet()) {
Place place = places.get(i); Place place = cangoPlacesMap.get(placeName);
npcInstanceName = this.queryCurPlaceGoodsInstanceByNickname(place.getName(), nickname); npcInstanceName = this.queryCurPlaceGoodsInstanceByNickname(place.getName(), nickname);
if(npcInstanceName!=null) { if(npcInstanceName!=null) {
return npcInstanceName; return npcInstanceName;
...@@ -489,73 +554,16 @@ public class GameEnv implements Serializable{ ...@@ -489,73 +554,16 @@ public class GameEnv implements Serializable{
} }
return nickname; return nickname;
} }
// 两点之间的路径 // 使用广度优先算法单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm。只有状态为2的时候使用
public List<String> getAllPath(String placeA, String placeB){
List<String> paths = new ArrayList<String>();
Deque<PlaceLink> placeDeque = new ArrayDeque<PlaceLink>();
Integer start = this.mapNames.get(placeA);
PlaceLink placeLink = new PlaceLink();
placeLink.setIndex(start);
boolean[] marked = new boolean[this.mapNames.size()];
for(int j=0; j<marked.length; j++) {
marked[j] = false;
}
placeDeque.push(placeLink);
int flag = 0;
while(!placeDeque.isEmpty() && flag == 0) {
PlaceLink placeLinkNode = placeDeque.pop();
marked[placeLinkNode.getIndex()] = true;
for(int i=0; i<this.mapGraph[placeLinkNode.getIndex()].length; i++) {
if(marked[i]==false) {
if(this.mapGraph[placeLinkNode.getIndex()][i] == 1) {
marked[i] = true;
PlaceLink placeLinkNodeSecond = new PlaceLink();
placeLinkNodeSecond.setIndex(i);
placeLinkNodeSecond.setPlaceLink(placeLinkNode);
List<String> nickname = this.placeIndex.get(this.mapIndexName[i]).getNickname();
if(this.mapIndexName[i].equals(placeB) || (nickname!=null && nickname.indexOf(placeB)>=0)) {
while(placeLinkNodeSecond.getPlaceLink() != null) {
paths.add(this.mapIndexName[placeLinkNodeSecond.getIndex()]);
placeLinkNodeSecond = placeLinkNodeSecond.getPlaceLink();
}
paths.add(this.mapIndexName[placeLinkNodeSecond.getIndex()]);
flag = 1;
break;
}
placeDeque.push(placeLinkNodeSecond);
}
}
}
}
Collections.reverse(paths);
return paths;
}
// 两点之间的通过的地方
public List<String> queryPathBySite(String placeA, String placeB){
List<String> paths = this.getAllPath(placeA, placeB);
System.out.println(paths);
List<String> path = new ArrayList<String>();
if(paths.size()>1) {
for(int i=0; i<paths.size(); i++) {
if(i!=0&&i!=(paths.size()-1)) {
path.add(paths.get(i));
}
}
return path;
}else {
return null;
}
}
/*********************************通过npc获取物品列表**********************************/ /*********************************通过npc获取物品列表**********************************/
// 通过npcName获取所有物品名称列表 // 通过npcName获取所有物品名称列表
public List<String> queryGoodsByNpcName(String npcName){ public List<String> queryGoodsByNpcName(String npcName){
List<String> goodsNames = new ArrayList<String>(); List<String> goodsNames = new ArrayList<String>();
if(this.npcInstanceIndex.get(npcName)!=null) { if(this.npcInstancesMap.get(npcName)!=null) {
List<GoodsInstance> goodsInstances = this.npcInstanceIndex.get(npcName).getGoodsInstances(); Map<String, GoodsInstance> goodsInstancesMap = this.npcInstancesMap.get(npcName).getGoodsInstances();
if(goodsInstances != null) { if(goodsInstancesMap != null && !goodsInstancesMap.isEmpty()) {
for(int i=0; i<goodsInstances.size(); i++) { for(String goodsInstanceName:goodsInstancesMap.keySet()) {
goodsNames.add(goodsInstances.get(i).getName()); goodsNames.add(goodsInstancesMap.get(goodsInstanceName).getName());
} }
} }
} }
...@@ -564,17 +572,15 @@ public class GameEnv implements Serializable{ ...@@ -564,17 +572,15 @@ public class GameEnv implements Serializable{
/**************************************goods****************************************/ /**************************************goods****************************************/
// 通过类和地点查询所有物品名称 // 通过类和地点查询所有物品名称
// 只有当可以询问的属性为是的时候才可以得到相应的物品列表 // 只有当可以询问的属性为是的时候才可以得到相应的物品列表
public List<String> queryGoodsByClassAndSite(String className, String site){ public List<String> queryGoodsByClassAndSite(String site, String className){
List<String> goodsNames = new ArrayList<String>(); List<String> goodsNames = new ArrayList<String>();
List<GoodsInstance> placeGoodsInstances = null; if(this.placesMap.get(site)!=null && this.goodsClassesMap.get(className)!=null) {
List<GoodsInstance> goodsInstances = null; Map<String, GoodsInstance> placeGoodsInstances = this.placesMap.get(site).getGoodsInstances();
if(this.placeIndex.get(site)!=null && this.goodsClassIndex.get(className)!=null) { Map<String, GoodsInstance> classGoodsInstances = this.goodsClassesMap.get(className).getGoodsInstances();
placeGoodsInstances = this.placeIndex.get(site).getGoodsInstances(); for(String placeGoodsInstanceName:placeGoodsInstances.keySet()) {
goodsInstances = this.goodsClassIndex.get(className).getGoodsInstances(); for(String classGoodsInstanceName:classGoodsInstances.keySet()) {
for(int i=0; i<placeGoodsInstances.size(); i++) { if(placeGoodsInstanceName.equals(classGoodsInstanceName)) {
for(int j=0; j<goodsInstances.size(); j++) { goodsNames.add(placeGoodsInstanceName);
if(placeGoodsInstances.get(i) == goodsInstances.get(j)) {
goodsNames.add(placeGoodsInstances.get(i).getName());
} }
} }
} }
...@@ -583,43 +589,40 @@ public class GameEnv implements Serializable{ ...@@ -583,43 +589,40 @@ public class GameEnv implements Serializable{
} }
// 查询指定物品 // 查询指定物品
public GoodsInstance queryGoodsByGoodsName(String name){ public GoodsInstance queryGoodsByGoodsName(String name){
GoodsInstance goodsInstance = this.goodsInstanceIndex.get(name); GoodsInstance goodsInstance = this.goodsInstancesMap.get(name);
return goodsInstance; return goodsInstance;
} }
// 查询指定物品的简介 // 查询指定物品的简介
public String queryGoodsIntroByGoodsName(String name){ public String queryGoodsIntroByGoodsName(String name){
GoodsInstance goodsInstance = this.queryGoodsByGoodsName(name); GoodsInstance goodsInstance = this.queryGoodsByGoodsName(name);
if(goodsInstance !=null) { if(goodsInstance !=null) {
return goodsInstance.getIntro(); return goodsInstance.getAttrs().get("intro");
}else { }else {
return null; return null;
} }
} }
// 查询指定物品的属性值 // 查询指定物品的属性值
public String queryGoodsAttrByGoodsName(String name, String attrName){ public String queryGoodsAttrByGoodsName(String str1, String str2){
GoodsInstance goodsInstance = queryGoodsByGoodsName(name); GoodsInstance goodsInstance = this.findGoodsInstanceByNameOrNickname(str1);
String attrName = str2;
if(goodsInstance == null) { if(goodsInstance == null) {
goodsInstance = queryGoodsByGoodsName(attrName); goodsInstance = this.findGoodsInstanceByNameOrNickname(str2);
attrName = name; attrName = str1;
} }
if(goodsInstance != null) { if(goodsInstance != null) {
List<Attr> attrs = goodsInstance.getAttrs(); Map<String, String> attrsMap = goodsInstance.getAttrs();
if(attrName.equals("location")) { if(attrName.equals("location")) {
if(goodsInstance.getPlace()!=null) { if(goodsInstance.getPlace()!=null) {
return goodsInstance.getPlace().getName(); return goodsInstance.getPlace().getName();
} }
} }
if(attrName.equals("owner")) { if(attrName.equals("owner")) {
if(goodsInstance.getNpc()!=null) { if(goodsInstance.getNpcInstance()!=null) {
return goodsInstance.getNpc().getName(); return goodsInstance.getNpcInstance().getName();
} }
} }
for(int i=0; i<attrs.size(); i++) { return attrsMap.get(attrName);
if(attrs.get(i).getName().equals(attrName)) {
return attrs.get(i).getValue();
}
}
return null;
}else { }else {
return null; return null;
} }
...@@ -627,99 +630,62 @@ public class GameEnv implements Serializable{ ...@@ -627,99 +630,62 @@ public class GameEnv implements Serializable{
// 查询指定物品的所有属性 // 查询指定物品的所有属性
public List<String> queryGoodsAllAttrByGoodsName(String name){ public List<String> queryGoodsAllAttrByGoodsName(String name){
List<String> attrnames = new ArrayList<String>(); List<String> attrnames = new ArrayList<String>();
GoodsInstance goodsInstance = queryGoodsByGoodsName(name); GoodsInstance goodsInstance = findGoodsInstanceByNameOrNickname(name);
if(goodsInstance != null) { if(goodsInstance != null) {
List<Attr> attrs = goodsInstance.getAttrs(); Map<String, String> attrsMap = goodsInstance.getAttrs();
for(int i=0; i<attrs.size(); i++) { attrnames.addAll(attrsMap.keySet());
attrnames.add(attrs.get(i).getName());
}
return attrnames; return attrnames;
}else { }else {
return null; return null;
} }
} }
// 是否可以被询问 npc和物品的属性名称 可被询问 // 查询物品的地点
public boolean isVisible(String name) { public String queryGoodsSiteByGoodsName(String goodsName) {
GoodsInstance goodsInstance = this.goodsInstanceIndex.get(name); return this.goodsInstancesMap.get(goodsName).getPlace().getName();
NpcInstance npcInstance = this.npcInstanceIndex.get(name);
if(goodsInstance !=null) {
List<Attr> attrs = goodsInstance.getAttrs();
for(int i=0;i<attrs.size();i++) {
if(attrs.get(i).getName().equals("可被询问")) {
if(attrs.get(i).getValue().equals("是")){
return true;
}else {
return false;
}
}
}
return false;
}else if(npcInstance != null) {
List<Attr> attrs = npcInstance.getAttrs();
for(int i=0; i<attrs.size(); i++) {
if(attrs.get(i).getName().equals("可被询问")) {
if(attrs.get(i).getValue().equals("是")){
return true;
}else {
return false;
}
}
}
return false;
}else {
return false;
}
} }
/**************************************npc*****************************************/ /**************************************npc*****************************************/
// 通过地点和类查询所有的npc名陈 // 通过地点和类查询所有的NPC实例
public List<String> queryNpcByClassAndSite(String className, String site){ public List<String> queryNpcByClassAndSite(String site, String className){
List<String> npcNames = new ArrayList<String>(); List<String> npcNames = new ArrayList<String>();
List<NpcInstance> placeNpcInstances = this.placeIndex.get(site).getNpcInstances(); Map<String,NpcInstance> placeNpcInstancesMap = this.placesMap.get(site).getNpcInstances();
List<NpcInstance> npcInstances = this.goodsClassIndex.get(className).getNpcInstances(); Map<String,NpcInstance> classNpcInstances = this.npcClassesMap.get(className).getNpcInstances();
for(int i=0; i<placeNpcInstances.size(); i++) { for(String placeNpcInstanceName:placeNpcInstancesMap.keySet()) {
for(int j=0; j<npcInstances.size(); j++) { for(String classNpcInstance:classNpcInstances.keySet()) {
if(placeNpcInstances.get(i) == npcInstances.get(j)) { if(placeNpcInstanceName.equals(classNpcInstance)) {
npcNames.add(placeNpcInstances.get(i).getName()); npcNames.add(placeNpcInstanceName);
} }
} }
} }
return npcNames; return npcNames;
} }
// 查询指定的npc // 查询指定的NPC
public NpcInstance queryNpcByNpcName(String npcName){ public NpcInstance queryNpcByNpcName(String npcName){
NpcInstance npcInstance = this.npcInstanceIndex.get(npcName); NpcInstance npcInstance = this.npcInstancesMap.get(npcName);
return npcInstance; return npcInstance;
} }
// 查询指定NPC简介 // 查询指定NPC简介
public String queryNpcIntroByNpcName(String name){ public String queryNpcIntroByNpcName(String name){
NpcInstance npcInstance = queryNpcByNpcName(name); NpcInstance npcInstance = queryNpcByNpcName(name);
if(npcInstance != null) { return npcInstance.getAttrs().get("intro");
return npcInstance.getIntro();
}else {
return null;
}
} }
// 查询指定npc的属性值 // 查询指定npc的属性值
public String queryNpcAttrByNpcName(String name, String attrName){ public String queryNpcAttrByNpcName(String str1, String str2){
NpcInstance npcInstance = queryNpcByNpcName(name); NpcInstance npcInstance = this.findNpcInstanceByNameOrNickname(str1);
String attrName = str2;
if(npcInstance == null) { if(npcInstance == null) {
npcInstance = queryNpcByNpcName(attrName); npcInstance = this.findNpcInstanceByNameOrNickname(str2);
attrName = name; attrName = str1;
} }
if(npcInstance != null) {
if(attrName.equals("location")) { if(attrName.equals("location")) {
if(npcInstance.getPlace()!=null) { if(npcInstance.getPlace()!=null) {
return npcInstance.getPlace().getName(); return npcInstance.getPlace().getName();
} }
} }
if(npcInstance != null) { Map<String, String> npcInstanceAttrsMap = npcInstance.getAttrs();
List<Attr> attrs = npcInstance.getAttrs(); return npcInstanceAttrsMap.get(attrName);
for(int i=0; i<attrs.size(); i++) {
if(attrs.get(i).getName().equals(attrName)) {
return attrs.get(i).getValue();
}
}
return null;
}else { }else {
return null; return null;
} }
...@@ -729,176 +695,125 @@ public class GameEnv implements Serializable{ ...@@ -729,176 +695,125 @@ public class GameEnv implements Serializable{
List<String> attrNames = new ArrayList<String>(); List<String> attrNames = new ArrayList<String>();
NpcInstance npcInstance = queryNpcByNpcName(name); NpcInstance npcInstance = queryNpcByNpcName(name);
if(npcInstance != null) { if(npcInstance != null) {
List<Attr> attrs = npcInstance.getAttrs(); attrNames.addAll(npcInstance.getAttrs().keySet());
for(int i=0; i<attrs.size(); i++) {
attrNames.add(attrs.get(i).getName());
}
return attrNames; return attrNames;
}else { }else {
return null; return null;
} }
} }
// 查询角色的地点
public String queryNpcSiteByNpcName(String npcName) {
return this.queryNpcByNpcName(npcName).getPlace().getName();
}
// 增加 // 增加
public boolean insert(String name){ public boolean insert(String name){
String[] names = name.split("_");
if(names.length == 3){
if(names[0].equals("goods")){
SuperClass goodsClass= this.goodsClassIndex.get(names[1]);
List<Attr> goodsClassAttrs = goodsClass.getAttrs();
GoodsInstance goodsInstance = new GoodsInstance();
List<Attr> goodsInstanceAttr = new ArrayList<Attr>();
goodsInstance.setName(names[2]);
goodsInstance.setIntro(goodsClass.getIntro());
for(int i=0; i<goodsClassAttrs.size(); i++){
Attr attr = new Attr();
attr.setName(goodsClassAttrs.get(i).getName());
attr.setValue(goodsClassAttrs.get(i).getValue());
goodsInstanceAttr.add(attr);
}
goodsInstance.setAttrs(goodsInstanceAttr);
this.goodsInstanceIndex.put(names[2], goodsInstance);
return true;
}else if(names[0].equals("npc")) {
SuperClass npcClass= this.npcClassIndex.get(names[1]);
List<Attr> npcClassAttrs = npcClass.getAttrs();
NpcInstance npcInstance = new NpcInstance();
List<Attr> npcInstanceAttr = new ArrayList<Attr>();
npcInstance.setName(names[2]);
npcInstance.setIntro(npcClass.getIntro());
for(int i=0; i<npcClassAttrs.size(); i++){
Attr attr = new Attr();
attr.setName(npcClassAttrs.get(i).getName());
attr.setValue(npcClassAttrs.get(i).getValue());
npcInstanceAttr.add(attr);
}
npcInstance.setAttrs(npcInstanceAttr);
this.npcInstanceIndex.put(names[2], npcInstance);
return true; return true;
}else {
return false;
}
}else{
return false;
}
} }
// 改变 // 改变
public boolean update(String name, String value) { public boolean update(String name, String value) {
String[] names = name.split("_"); String[] names = name.split("_");
if(names.length == 4){ if(names.length == 4){
if(names[0].equals("goods")){ if(names[0].equals("goods")){
List<GoodsInstance> goodsInstancesByClass = this.goodsClassIndex.get(names[1]).getGoodsInstances(); Map<String, GoodsInstance> goodsInstancesByClass = this.goodsClassesMap.get(names[1]).getGoodsInstances();
boolean goodsInstanceBoolean = false; GoodsInstance goodsInstance = this.goodsInstancesMap.get(names[2]);
GoodsInstance goodsInstance = this.goodsInstanceIndex.get(names[2]); if(goodsInstancesByClass.get(goodsInstance.getName())!=null) {
for(int i=0; i<goodsInstancesByClass.size(); i++) { Map<String, String> goodsInstanceAttrsMap = goodsInstance.getAttrs();
if(goodsInstance == goodsInstancesByClass.get(i)) {
goodsInstanceBoolean = true;
}
}
if(goodsInstanceBoolean) {
List<Attr> attrs = goodsInstance.getAttrs();
if(names[3].equals("所在地点")) { if(names[3].equals("所在地点")) {
Place placeOrigin = goodsInstance.getPlace(); Place placeOrigin = goodsInstance.getPlace();
List<GoodsInstance> goodsInstances = placeOrigin.getGoodsInstances(); Map<String, GoodsInstance> goodsInstancesMap = placeOrigin.getGoodsInstances();
goodsInstances.remove(goodsInstance); goodsInstancesMap.remove(goodsInstance.getName());
Place place= this.placesMap.get(value);
Place place= this.placeIndex.get(value);
System.out.println(place);
goodsInstance.setPlace(place); goodsInstance.setPlace(place);
goodsInstances = place.getGoodsInstances(); goodsInstancesMap = place.getGoodsInstances();
goodsInstances.add(goodsInstance); goodsInstancesMap.put(goodsInstance.getName(), goodsInstance);
return true; return true;
}else if(names[3].equals("所属角色")) { }else if(names[3].equals("所属角色")) {
NpcInstance npcInstance= goodsInstance.getNpc(); NpcInstance npcInstance= goodsInstance.getNpcInstance();
List<GoodsInstance> goodsInstances = npcInstance.getGoodsInstances(); if(npcInstance != null) {
goodsInstances.remove(goodsInstance); Map<String, GoodsInstance> goodsInstancesMap = npcInstance.getGoodsInstances();
npcInstance = this.npcInstanceIndex.get(value); goodsInstancesMap.remove(goodsInstance.getName());
goodsInstance.setNpc(npcInstance); }
goodsInstances = npcInstance.getGoodsInstances(); npcInstance = this.npcInstancesMap.get(value);
goodsInstances.add(goodsInstance); goodsInstance.setNpcInstance(npcInstance);
Map<String,GoodsInstance> goodsInstances = npcInstance.getGoodsInstances();
goodsInstances.put(goodsInstance.getName(), goodsInstance);
npcInstance.setGoodsInstances(goodsInstances);
return true; return true;
}else { }else {
for(int j=0; j<attrs.size(); j++) { goodsInstanceAttrsMap.put(names[3], value);
if(names[3].equals(attrs.get(j).getName())) {
attrs.get(j).setValue(value);
return true; return true;
} }
}
}
}else { }else {
return false; return false;
} }
}else if(names[0].equals("npc")) { }else if(names[0].equals("npc")) {
List<NpcInstance> npcInstancesByClass = this.npcClassIndex.get(names[1]).getNpcInstances(); NpcInstance npcInstance = this.npcInstancesMap.get(names[2]);
boolean npcInstanceBoolean = false; if(npcInstance.getNpcClass().getName().equals(names[1])) {
NpcInstance npcInstance = this.npcInstanceIndex.get(names[2]); Map<String, String> npcInstanceAttrMap = npcInstance.getAttrs();
for(int i=0; i<npcInstancesByClass.size(); i++) {
if(npcInstance == npcInstancesByClass.get(i)) {
npcInstanceBoolean = true;
}
}
if(npcInstanceBoolean) {
List<Attr> attrs = npcInstance.getAttrs();
for(int j=0; j<attrs.size(); j++) {
if(names[3].equals("跟随玩家")) { if(names[3].equals("跟随玩家")) {
if(value.equals("是")) { if(value.equals("是")) {
this.followNpcIndex.put(npcInstance.getName(), npcInstance); this.followNpcesMap.put(npcInstance.getName(), npcInstance);
}else if(value.equals("否")) { }else if(value.equals("否")) {
this.followNpcIndex.remove(npcInstance.getName()); this.followNpcesMap.remove(npcInstance.getName());
}else { }else {
continue;
}
return true; return true;
}
}else if(names[3].equals("所在地点")) { }else if(names[3].equals("所在地点")) {
Place placeOrigin = npcInstance.getPlace(); Place placeOrigin = npcInstance.getPlace();
List<NpcInstance> npcInstances = placeOrigin.getNpcInstances(); Map<String, NpcInstance> npcInstancesMap = placeOrigin.getNpcInstances();
npcInstances.remove(npcInstance); npcInstancesMap.remove(npcInstance.getName());
Place place= this.placeIndex.get(value); Place place= this.placesMap.get(value);
npcInstance.setPlace(place); npcInstance.setPlace(place);
npcInstances = place.getNpcInstances(); npcInstancesMap = place.getNpcInstances();
npcInstances.add(npcInstance); npcInstancesMap.put(npcInstance.getName(), npcInstance);
// 更新跟随的NPC物品的位置
return true; return true;
}else { }else {
if(names[3].equals(attrs.get(j).getName())) { npcInstanceAttrMap.put(names[3], value);
attrs.get(j).setValue(value);
return true; return true;
} }
}
}
return true;
}else { }else {
return false; return false;
} }
}else if(names.length == 3 && names[0].equals("place")) { }else if(names.length == 3 && names[0].equals("place")) {
Place place = this.placeIndex.get(names[1]); Place place = this.placesMap.get(names[1]);
if(names[2].equals("到达条件")) { if(names[2].equals("到达条件")) {
place.setGoFlag(Integer.parseInt(value)); place.setGoFlag(Integer.parseInt(value));
if(place.getGoFlag().equals(1)) { if(place.getGoFlag().equals(1)) {
this.placeLinkDirectGo.put(place.getName(),place); this.placeLinkDirectGoesMap.put(place.getName(),place);
this.placeNoLinkDirectGo.remove(place.getName()); this.placeNoLinkDirectGoesMap.remove(place.getName());
}else if(place.getGoFlag().equals(2)) { }else if(place.getGoFlag().equals(2)) {
this.placeNoLinkDirectGo.put(place.getName(),place); this.placeLinkDirectGoesMap.put(place.getName(),place);
this.placeLinkDirectGo.remove(place.getName()); this.placeNoLinkDirectGoesMap.remove(place.getName());
} }
return true; return true;
}else if(names[2].equals("可去地点")) { }else if(names[2].equals("可去地点")) {
String[] ableGos = value.split("~"); String[] ableGos = value.split("~");
List<Place> places = new ArrayList<Place>(); if(this.placeNameIndexMap.get(place.getName()) != null){
byte[] line = this.mapGraph[this.mapNames.get(place.getName())]; // 找到可去地点的那一行
byte[] line = this.mapUnitMatrix[this.placeNameIndexMap.get(place.getName())];
// 将可去地点清空
for(int ag=0; ag<line.length; ag++) { for(int ag=0; ag<line.length; ag++) {
line[ag]=0; line[ag]=0;
} }
Map<String, Place> ableGoes = new HashMap<String, Place>();
for(int ag=0; ag<ableGos.length; ag++) { for(int ag=0; ag<ableGos.length; ag++) {
places.add(this.placeIndex.get(ableGos[ag])); Place p = this.placesMap.get(ableGos[ag]);
line[this.mapNames.get(ableGos[ag])]=1; if(p == null) {
continue;
} }
place.setAbleGo(places); line[this.placeNameIndexMap.get(ableGos[ag])]=1;
ableGoes.put(place.getName(), place);
}
place.setAbleGoes(placesMap);
return true; return true;
}
return false;
}else { }else {
return false; return false;
} }
}else if(names[0].equals("globalVariable")) { }else if(names[0].equals("globalVariable")) {
this.globalAttrs.put(names[1], value); this.globalAttrsMap.put(names[1], value);
} }
} }
return false; return false;
...@@ -917,14 +832,14 @@ public class GameEnv implements Serializable{ ...@@ -917,14 +832,14 @@ public class GameEnv implements Serializable{
} }
// 更新npc的位置 // 更新npc的位置
public boolean updateFollowerLocation(String location) { public boolean updateFollowerLocation(String location) {
Place place = this.placeIndex.get(location); Place place = this.placesMap.get(location);
for(String instanceKey:this.followNpcIndex.keySet()) { for(String instanceKey:this.followNpcesMap.keySet()) {
this.followNpcIndex.get(instanceKey).setPlace(place); this.followNpcesMap.get(instanceKey).setPlace(place);
} }
return true; return true;
} }
// 查询玩家 // 查询玩家
public String queryPlayerName() { public String queryPlayerName() {
return this.npcClassIndex.get("玩家").getNpcInstances().get(0).getName(); return this.npcClassesMap.get("玩家").getNpcInstances().get(0).getName();
} }
} }
...@@ -4,6 +4,9 @@ import java.io.FileInputStream; ...@@ -4,6 +4,9 @@ import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Util { public class Util {
public static String getJsonString(String fullName) { public static String getJsonString(String fullName) {
......
package com.qiwu.chatbotbackend.GameEnvironment.bean;
import java.io.Serializable;
public class Attr implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "Attr [name=" + name + ", value=" + value + "]";
}
}
package com.qiwu.chatbotbackend.GameEnvironment.bean; package com.qiwu.chatbotbackend.GameEnvironment.bean;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.HashMap;
import java.util.Map;
public class SuperClass implements Serializable{ public class GoodsClass implements Serializable{
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 2L;
private String name; private String name;
private String intro; private Place place;
private List<Attr> attrs; private Map<String, String> attrs;
private List<GoodsInstance> goodsInstances; // 类下面有哪些实例
private List<NpcInstance> npcInstances; private Map<String, GoodsInstance> goodsInstances = new HashMap<String, GoodsInstance>();
public List<GoodsInstance> getGoodsInstances() {
return goodsInstances;
}
public void setGoodsInstances(List<GoodsInstance> goodsInstances) {
this.goodsInstances = goodsInstances;
}
public List<NpcInstance> getNpcInstances() {
return npcInstances;
}
public void setNpcInstances(List<NpcInstance> npcInstances) {
this.npcInstances = npcInstances;
}
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public String getIntro() { public Place getPlace() {
return intro; return place;
} }
public void setIntro(String intro) { public void setPlace(Place place) {
this.intro = intro; this.place = place;
} }
public List<Attr> getAttrs() { public Map<String, String> getAttrs() {
return attrs; return attrs;
} }
public void setAttrs(List<Attr> attrs) { public void setAttrs(Map<String, String> attrs) {
this.attrs = attrs; this.attrs = attrs;
} }
public Map<String, GoodsInstance> getGoodsInstances() {
return goodsInstances;
}
public void setGoodsInstances(Map<String, GoodsInstance> goodsInstances) {
this.goodsInstances = goodsInstances;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override @Override
public String toString() { public String toString() {
int goodsInstanceSize = 0; return "GoodsClass [name=" + name + "place" +place.getName()+ ", attrs=" + attrs + ", goodsInstances=" + goodsInstances + "]";
if(goodsInstances!=null) {
goodsInstanceSize = goodsInstances.size();
}
return "SuperClass [name=" + name + ", intro=" + intro + "goodsInstancesSize="+goodsInstanceSize+", attrs=" + attrs + "]";
} }
} }
package com.qiwu.chatbotbackend.GameEnvironment.bean; package com.qiwu.chatbotbackend.GameEnvironment.bean;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class GoodsInstance extends SuperClass implements Serializable{ public class GoodsInstance implements Serializable{
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 2L;
private List<String> nickname; private String name;
private List<String> nickname = new ArrayList<String>();
private Map<String, String> attrs = new HashMap<String, String>();
private GoodsClass goodsClass;
private NpcInstance npcInstance;
private Place place; private Place place;
private NpcInstance npc;
public Place getPlace() { public String getName() {
return place; return name;
}
public void setPlace(Place place) {
this.place = place;
}
public NpcInstance getNpc() {
return npc;
} }
public void setNpc(NpcInstance npc) { public void setName(String name) {
this.npc = npc; this.name = name;
} }
public List<String> getNickname() { public List<String> getNickname() {
return nickname; return nickname;
...@@ -26,17 +27,38 @@ public class GoodsInstance extends SuperClass implements Serializable{ ...@@ -26,17 +27,38 @@ public class GoodsInstance extends SuperClass implements Serializable{
public void setNickname(List<String> nickname) { public void setNickname(List<String> nickname) {
this.nickname = nickname; this.nickname = nickname;
} }
@Override public Map<String, String> getAttrs() {
public String toString() { return attrs;
String placeName = ""; }
if(place != null) { public void setAttrs(Map<String, String> attrs) {
placeName = place.getName(); this.attrs = attrs;
}
public NpcInstance getNpcInstance() {
return npcInstance;
}
public void setNpcInstance(NpcInstance npcInstance) {
this.npcInstance = npcInstance;
} }
String npcName = ""; public GoodsClass getGoodsClass() {
if(npc != null) { return goodsClass;
npcName = npc.getName();
} }
return super.toString()+"GoodsInstance [placeName=" + placeName + ", npcName=" + npcName +",nickname="+nickname+"]"; public void setGoodsClass(GoodsClass goodsClass) {
this.goodsClass = goodsClass;
} }
public Place getPlace() {
return place;
}
public void setPlace(Place place) {
this.place = place;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public String toString() {
return "GoodsInstance [nickname=" + nickname + "name="+name+", attrs=" + attrs + ", goodsClass=" + goodsClass.getName()
+ ", npcInstance=" + npcInstance.getName() + ", place=" + place.getName() + "]";
}
} }
package com.qiwu.chatbotbackend.GameEnvironment.bean;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class Nickname implements Serializable{
private static final long serialVersionUID = 2L;
private Map<String, GoodsInstance> goodsInstanceMap = new HashMap<String, GoodsInstance>();
private Map<String, NpcInstance> npcInstanceMap = new HashMap<String, NpcInstance>();
private Map<String, Place> placeMap = new HashMap<String, Place>();
public Map<String, GoodsInstance> getGoodsInstanceMap() {
return goodsInstanceMap;
}
public void setGoodsInstanceMap(Map<String, GoodsInstance> goodsInstanceMap) {
this.goodsInstanceMap = goodsInstanceMap;
}
public Map<String, NpcInstance> getNpcInstanceMap() {
return npcInstanceMap;
}
public void setNpcInstanceMap(Map<String, NpcInstance> npcInstanceMap) {
this.npcInstanceMap = npcInstanceMap;
}
public Map<String, Place> getPlaceMap() {
return placeMap;
}
public void setPlaceMap(Map<String, Place> placeMap) {
this.placeMap = placeMap;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public String toString() {
return "Nickname [goodsInstanceMap=" + goodsInstanceMap + ", npcInstanceMap=" + npcInstanceMap + ", placeMap="
+ placeMap + "]";
}
}
package com.qiwu.chatbotbackend.GameEnvironment.bean;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class NpcClass implements Serializable{
private static final long serialVersionUID = 2L;
private String name;
private Place place;
private Map<String, String> attrs = new HashMap<String, String>();
// 类下面有哪些实例
private Map<String, NpcInstance> npcInstances = new HashMap<String, NpcInstance>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Place getPlace() {
return place;
}
public void setPlace(Place place) {
this.place = place;
}
public Map<String, String> getAttrs() {
return attrs;
}
public void setAttrs(Map<String, String> attrs) {
this.attrs = attrs;
}
public Map<String, NpcInstance> getNpcInstances() {
return npcInstances;
}
public void setNpcInstances(Map<String, NpcInstance> npcInstances) {
this.npcInstances = npcInstances;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public String toString() {
return "NpcClass [name=" + name + ",place" + place.getName()+", attrs=" + attrs + ", npcInstances=" + npcInstances + "]";
}
}
package com.qiwu.chatbotbackend.GameEnvironment.bean; package com.qiwu.chatbotbackend.GameEnvironment.bean;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class NpcInstance extends SuperClass implements Serializable{
private static final long serialVersionUID = 1L; public class NpcInstance implements Serializable{
private List<String> nickname; private static final long serialVersionUID = 2L;
private String name;
private List<String> nickname = new ArrayList<String>();
private Map<String, String> attrs = new HashMap<String, String>();
private NpcClass npcClass;
private Place place; private Place place;
private List<GoodsInstance> goodsInstances; private Map<String,GoodsInstance> goodsInstances = new HashMap<String, GoodsInstance>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getNickname() { public List<String> getNickname() {
return nickname; return nickname;
} }
public void setNickname(List<String> nickname) { public void setNickname(List<String> nickname) {
this.nickname = nickname; this.nickname = nickname;
} }
public Map<String, String> getAttrs() {
return attrs;
}
public void setAttrs(Map<String, String> attrs) {
this.attrs = attrs;
}
public NpcClass getNpcClass() {
return npcClass;
}
public void setNpcClass(NpcClass npcClass) {
this.npcClass = npcClass;
}
public Place getPlace() { public Place getPlace() {
return place; return place;
} }
public void setPlace(Place place) { public void setPlace(Place place) {
this.place = place; this.place = place;
} }
public Map<String, GoodsInstance> getGoodsInstances() {
public List<GoodsInstance> getGoodsInstances() {
return goodsInstances; return goodsInstances;
} }
public void setGoodsInstances(Map<String, GoodsInstance> goodsInstances) {
public void setGoodsInstances(List<GoodsInstance> goodsInstances) {
this.goodsInstances = goodsInstances; this.goodsInstances = goodsInstances;
} }
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override @Override
public String toString() { public String toString() {
String placeName = ""; return "NpcInstance [name=" + name + ", nickname=" + nickname + ", attrs=" + attrs + ", npcClass=" + npcClass.getName()
if(place != null) { + ", place=" + place.getName() + ", goodsInstances=" + goodsInstances + "]";
placeName = place.getName();
}
int goodsInstancesSize = 0;
if(goodsInstances!=null) {
goodsInstancesSize = goodsInstances.size();
} }
return super.toString()+"NpcInstance [placeName=" + placeName + ", goodsInstancesSize=" + goodsInstancesSize +",nickname="+nickname+"]";
}
} }
package com.qiwu.chatbotbackend.GameEnvironment.bean; package com.qiwu.chatbotbackend.GameEnvironment.bean;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class Place implements Serializable{ public class Place implements Serializable{
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 2L;
private String name; private String name;
/*
* 1:联通相邻可去
* 2:联通不相邻可去
* 3:不联通不相邻可去
* */
private String intro; private String intro;
private Integer goFlag; private Integer goFlag;
private List<String> nickname; private List<String> nickname = new ArrayList<String>();
private List<Place> ableGo; private Map<String, Place> ableGoes = new HashMap<String, Place>();
private List<NpcInstance> npcInstances; private Map<String, NpcInstance> npcInstances = new HashMap<String, NpcInstance>();
private List<GoodsInstance> goodsInstances; private Map<String, GoodsInstance> goodsInstances = new HashMap<String, GoodsInstance>();
public String transformGoFlag(Integer goFlag) {
if(goFlag.equals(new Integer(1))) {
return "联通相邻可去";
}else if(goFlag.equals(new Integer(2))){
return "联通不相邻可去";
}else if(goFlag.equals(new Integer(3))) {
return "不联通不相邻可去";
}else {
return "错误 go flag!";
}
}
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public String getIntro() { public String getIntro() {
return intro; return intro;
} }
public void setIntro(String intro) { public void setIntro(String intro) {
this.intro = intro; this.intro = intro;
} }
public Integer getGoFlag() { public Integer getGoFlag() {
return goFlag; return goFlag;
} }
public void setGoFlag(Integer goFlag) { public void setGoFlag(Integer goFlag) {
this.goFlag = goFlag; this.goFlag = goFlag;
} }
public List<String> getNickname() { public List<String> getNickname() {
return nickname; return nickname;
} }
public void setNickname(List<String> nickname) { public void setNickname(List<String> nickname) {
this.nickname = nickname; this.nickname = nickname;
} }
public Map<String, Place> getAbleGoes() {
return ableGoes;
public List<Place> getAbleGo() {
return ableGo;
} }
public void setAbleGoes(Map<String, Place> ableGoes) {
this.ableGoes = ableGoes;
public void setAbleGo(List<Place> ableGo) {
this.ableGo = ableGo;
} }
public Map<String, NpcInstance> getNpcInstances() {
public List<NpcInstance> getNpcInstances() {
return npcInstances; return npcInstances;
} }
public void setNpcInstances(Map<String, NpcInstance> npcInstances) {
public void setNpcInstances(List<NpcInstance> npcInstances) {
this.npcInstances = npcInstances; this.npcInstances = npcInstances;
} }
public Map<String, GoodsInstance> getGoodsInstances() {
public List<GoodsInstance> getGoodsInstances() {
return goodsInstances; return goodsInstances;
} }
public void setGoodsInstances(Map<String, GoodsInstance> goodsInstances) {
public void setGoodsInstances(List<GoodsInstance> goodsInstances) {
this.goodsInstances = goodsInstances; this.goodsInstances = goodsInstances;
} }
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override @Override
public String toString() { public String toString() {
int ableGoSize = 0; return "Place [name=" + name + ",intro" + intro +", goFlag=" + this.transformGoFlag(goFlag) + ", nickname=" + nickname + ", ableGo=" + ableGoes.keySet()
int npcInstancesSize = 0; + ", npcInstances=" + npcInstances + ", goodsInstances=" + goodsInstances + "]";
int goodsInstancesSize = 0;
if(ableGo != null) {
ableGoSize = ableGo.size();
} }
if(npcInstances != null) {
npcInstancesSize = npcInstances.size();
}
if(goodsInstances != null) {
goodsInstancesSize = goodsInstances.size();
}
return "Place [name=" + name + ", intro=" + intro + "nickname=" + nickname + ", ableGo=" + ableGoSize + ", npcInstances=" + npcInstancesSize
+ ", goodsInstances=" + goodsInstancesSize + "]";
}
} }
...@@ -3,7 +3,7 @@ package com.qiwu.chatbotbackend.GameEnvironment.bean; ...@@ -3,7 +3,7 @@ package com.qiwu.chatbotbackend.GameEnvironment.bean;
import java.io.Serializable; import java.io.Serializable;
public class PlaceLink implements Serializable{ public class PlaceLink implements Serializable{
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 2L;
private Integer index; private Integer index;
private PlaceLink placeLink; private PlaceLink placeLink;
public Integer getIndex() { public Integer getIndex() {
...@@ -18,6 +18,11 @@ public class PlaceLink implements Serializable{ ...@@ -18,6 +18,11 @@ public class PlaceLink implements Serializable{
public void setPlaceLink(PlaceLink placeLink) { public void setPlaceLink(PlaceLink placeLink) {
this.placeLink = placeLink; this.placeLink = placeLink;
} }
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public String toString() {
return "PlaceLink [index=" + index + ", placeLink=" + placeLink + "]";
}
} }
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