package com.qiwu.chatbotbackend.GameEnvironment; import java.io.Serializable; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSON; import com.qiwu.chatbotbackend.GameEnvironment.bean.Attr; import com.qiwu.chatbotbackend.GameEnvironment.bean.GoodsInstance; import com.qiwu.chatbotbackend.GameEnvironment.bean.SuperClass; import com.qiwu.chatbotbackend.GameEnvironment.bean.NpcInstance; import com.qiwu.chatbotbackend.GameEnvironment.bean.Place; import com.qiwu.chatbotbackend.GameEnvironment.bean.PlaceLink; public class GameEnv implements Serializable{ private static final long serialVersionUID = 1L; public Map npcClassIndex; public Map npcInstanceIndex; public Map followNpcIndex; public Map goodsClassIndex; public Map goodsInstanceIndex; public Map globalAttrs; public Map placeIndex; public Map placeLinkDirectGo; public Map placeNoLinkDirectGo; public byte[][] mapGraph; public MapmapNames; public String[] mapIndexName; @SuppressWarnings("unchecked") public GameEnv(String botName) throws Exception { // 通过文件来初始化游戏环境 // String fullName = "test.json"; // String jsonString = Util.getJsonString(fullName); // System.out.println(jsonString); String jsonString = DBUtil.queryEnvJsonByBotName(botName); Map gameProjectMap = JSON.parseObject(jsonString); System.out.println(gameProjectMap); this.followNpcIndex = new HashMap(); this.placeLinkDirectGo = new HashMap(); this.placeNoLinkDirectGo = new HashMap(); if(!gameProjectMap.isEmpty()) { // 地图 Map map = (Map)gameProjectMap.get("map"); this.placeIndex = new HashMap(); Map placeMapTmp = new HashMap(); this.mapGraph = new byte[map.size()][map.size()]; this.mapNames = new HashMap(); if(!map.isEmpty()) { // 第一步先把除了 canGo 之外的信息创建一个对象 int i= 0; for(String key:map.keySet()) { Map one = (Map)map.get(key); placeMapTmp.put(key, (String)one.get("name")); Place place = new Place(); place.setIntro((String)one.get("intro")); place.setName((String)one.get("name")); place.setGoFlag((Integer)one.get("goFlag")); if(((Integer)one.get("goFlag")).equals(new Integer(2))) { this.placeLinkDirectGo.put(place.getName(), place); }else if(((Integer)one.get("goFlag")).equals(new Integer(3))) { this.placeNoLinkDirectGo.put(place.getName(), place); } List nickname = (List)one.get("nickname"); place.setGoodsInstances(new ArrayList()); place.setNpcInstances(new ArrayList()); place.setNickname(nickname); mapNames.put((String)one.get("name"), i); i++; placeIndex.put((String)one.get("name"), place); } // 添加 canGo 指针关联 for(String tmp:placeMapTmp.keySet()) { List ableGos = new ArrayList(); List ableGoLongs = (List)((Map)map.get(tmp)).get("canGo"); for(int j=0; j map name this.mapIndexName = new String[mapGraph.length]; for(i=0; i npcClasses = (Map)gameProjectMap.get("npc"); Map npcNames = new HashMap(); this.npcClassIndex = new HashMap(); this.npcInstanceIndex = new HashMap(); if(!npcClasses.isEmpty()) { for(String classKey:npcClasses.keySet()) { Map one = (Map)npcClasses.get(classKey); // class Map oneClass = (Map)one.get("class"); SuperClass npcClass = new SuperClass(); npcClass.setName(classKey); npcClass.setIntro((String)oneClass.get("intro")); List attrs = new ArrayList(); for(String attrKey:oneClass.keySet()) { if(!attrKey.equals("intro") && !attrKey.equals("class") && !attrKey.equals("")) { Attr attr = new Attr(); attr.setName(attrKey); attr.setValue((String)oneClass.get(attrKey)); attrs.add(attr); } } npcClass.setAttrs(attrs); // instance Map instances = (Map)one.get("instance"); for(String instanceKey:instances.keySet()) { NpcInstance npcInstance = new NpcInstance(); Map instanceAttrMap = (Map)instances.get(instanceKey); npcNames.put(instanceKey, (String)instanceAttrMap.get("name")); npcInstance.setName((String)instanceAttrMap.get("name")); npcInstance.setIntro((String)instanceAttrMap.get("intro")); npcInstance.setNickname((List)instanceAttrMap.get("nickname")); List npcInstanceAttrs = new ArrayList(); for(String instanceAttrKey:instanceAttrMap.keySet()) { if(!instanceAttrKey.equals("name") &&!instanceAttrKey.equals("intro") &&!instanceAttrKey.equals("placeId") &&!instanceAttrKey.equals("所在地点") &&!instanceAttrKey.equals("placeName") &&!instanceAttrKey.equals("class") &&!instanceAttrKey.equals("nickname")) { Attr attr = new Attr(); attr.setName(instanceAttrKey); attr.setValue((String)instanceAttrMap.get(instanceAttrKey)); npcInstanceAttrs.add(attr); if(instanceAttrKey.equals("跟随玩家")&&((String)instanceAttrMap.get(instanceAttrKey)).equals("是")) { this.followNpcIndex.put(npcInstance.getName(), npcInstance); } } } npcInstance.setAttrs(npcInstanceAttrs); Place place = null; if(instanceAttrMap.get("所在地点")!=null) { Map placeMap = (Map)map.get((String)instanceAttrMap.get("所在地点")); if(placeMap!=null) { place = placeIndex.get(placeMap.get("name")); List npcInstances = place.getNpcInstances(); if(npcInstances == null) { npcInstances = new ArrayList(); } npcInstances.add(npcInstance); place.setNpcInstances(npcInstances); } } // if(instanceAttrMap.get("placeId")!=null) { // Map placeMap = (Map)map.get(String.valueOf(instanceAttrMap.get("placeId"))); // if(placeMap!=null) { // place = placeIndex.get(placeMap.get("name")); // List npcInstances = place.getNpcInstances(); // if(npcInstances == null) { // npcInstances = new ArrayList(); // } // npcInstances.add(npcInstance); // place.setNpcInstances(npcInstances); // } // } List npcInstanceClass = npcClass.getNpcInstances(); if(npcInstanceClass == null) { npcInstanceClass = new ArrayList(); } npcInstanceClass.add(npcInstance); npcClass.setNpcInstances(npcInstanceClass); npcInstance.setPlace(place); this.npcInstanceIndex.put((String)instanceAttrMap.get("name"), npcInstance); } this.npcClassIndex.put(classKey, npcClass); } } // goods Map goodsClasses = (Map)gameProjectMap.get("goods"); this.goodsClassIndex = new HashMap(); this.goodsInstanceIndex = new HashMap(); if(!goodsClasses.isEmpty()) { for(String classKey:goodsClasses.keySet()) { Map one = (Map)goodsClasses.get(classKey); // class Map oneClass = (Map)one.get("class"); SuperClass goodsClass = new SuperClass(); goodsClass.setName(classKey); goodsClass.setIntro((String)oneClass.get("intro")); List attrs = new ArrayList(); 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 instances = (Map)one.get("instance"); for(String instanceKey:instances.keySet()) { GoodsInstance goodsInstance = new GoodsInstance(); Map instanceAttrMap = (Map)instances.get(instanceKey); goodsInstance.setName((String)instanceAttrMap.get("name")); goodsInstance.setIntro((String)instanceAttrMap.get("intro")); goodsInstance.setNickname((List)instanceAttrMap.get("nickname")); List goodsInstanceAttrs = new ArrayList(); 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) { Map placeMap = (Map)map.get((String)instanceAttrMap.get("所在地点")); Place place = placeIndex.get(placeMap.get("name")); goodsInstance.setPlace(place); List placeGoodsInstances= place.getGoodsInstances(); if(placeGoodsInstances == null) { placeGoodsInstances = new ArrayList(); } placeGoodsInstances.add(goodsInstance); place.setGoodsInstances(placeGoodsInstances); } if(instanceAttrMap.get("所属角色")!=null){ System.out.println(npcNames.get((String)instanceAttrMap.get("所属角色"))); NpcInstance npc = this.npcInstanceIndex.get(npcNames.get((String)instanceAttrMap.get("所属角色"))); goodsInstance.setNpc(npc); List npcGoodsInstances = npc.getGoodsInstances(); if(npcGoodsInstances == null) { npcGoodsInstances = new ArrayList(); } npcGoodsInstances.add(goodsInstance); npc.setGoodsInstances(npcGoodsInstances); } // if(instanceAttrMap.get("placeId")!=null) { // Map placeMap = (Map)map.get(String.valueOf(instanceAttrMap.get("placeId"))); // Place place = placeIndex.get(placeMap.get("name")); // goodsInstance.setPlace(place); // List placeGoodsInstances= place.getGoodsInstances(); // if(placeGoodsInstances == null) { // placeGoodsInstances = new ArrayList(); // } // 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 npcGoodsInstances = npc.getGoodsInstances(); // if(npcGoodsInstances == null) { // npcGoodsInstances = new ArrayList(); // } // npcGoodsInstances.add(goodsInstance); // npc.setGoodsInstances(npcGoodsInstances); // } List goodsInstancesClass = goodsClass.getGoodsInstances(); if(goodsInstancesClass == null) { goodsInstancesClass = new ArrayList(); } goodsInstancesClass.add(goodsInstance); goodsClass.setGoodsInstances(goodsInstancesClass); this.goodsInstanceIndex.put((String)instanceAttrMap.get("name"), goodsInstance); } this.goodsClassIndex.put(classKey, goodsClass); } } // global attr this.globalAttrs = (Map)gameProjectMap.get("globalAttr"); } } /********************************************地点******************************************/ // 周围有什么 public List queryAroundAllBySite(String site){ List queryGoods = this.queryAroundGoodsBySite(site); List queryNpc = this.queryAroundNpcBySite(site); queryGoods.addAll(queryNpc); return queryGoods; } // 周围有什么人 public List queryAroundNpcBySite(String site){ Place place = this.placeIndex.get(site); List queryNpc = new ArrayList(); if(place !=null) { List npcInstances = place.getNpcInstances(); if(npcInstances!=null) { for(int i=0; i queryAroundGoodsBySite(String site){ Place place = this.placeIndex.get(site); List queryGoods = new ArrayList(); if(place !=null) { List goodsInstances = place.getGoodsInstances(); if(goodsInstances!=null) { for(int i=0; i queryCanGoBySite(String site){ List ableGoString = new ArrayList(); int x = this.mapNames.get(site); for(int i=0; i=0) { return 0; } for(int j=0;j=0) { return 1; } } } for(String placeName:this.placeLinkDirectGo.keySet()) { if(placeName.equals(placeB) || this.placeLinkDirectGo.get(placeName).getNickname().indexOf(placeB)>=0) { return 3; } } for(String placeName:this.placeNoLinkDirectGo.keySet()) { if(placeName.equals(placeB) || this.placeNoLinkDirectGo.get(placeName).getNickname().indexOf(placeB)>=0) { return 4; } } return 2; }catch(Exception e) { return 2; } } // 通过别名查询当前地点最近的别名地点 public String getPlaceNameByNickname(String curPlaceName, String nickname) { if(this.placeIndex.get(curPlaceName).getNickname().indexOf(nickname)>=0) { return curPlaceName; }else { List placePath = getAllPath(curPlaceName, nickname); if(placePath.size()>=2) { return placePath.get(placePath.size()-1); }else { return nickname; } } } public String queryCurPlaceNpcInstanceByNickname(String curPlaceName, String nickname) { List npcInstances = this.placeIndex.get(curPlaceName).getNpcInstances(); for(int i=0; i=0) { return npcInstance.getName(); } } return null; } public String queryCurPlaceGoodsInstanceByNickname(String curPlaceName, String nickname) { List goodsInstances = this.placeIndex.get(curPlaceName).getGoodsInstances(); for(int i=0; i=0) { return goodsInstance.getName(); } } return null; } // 通过别名查询当前地点最近的npc public String getNpcNameByNickname(String curPlaceName, String nickname) { // 寻找两层 // 查询当前地点的所有,和当前地点可以去到的地点相应的所有 String npcInstanceName = this.queryCurPlaceNpcInstanceByNickname(curPlaceName, nickname); if(npcInstanceName!=null) { return npcInstanceName; } List places = this.placeIndex.get(curPlaceName).getAbleGo(); for(int i=0; i places = this.placeIndex.get(curPlaceName).getAbleGo(); for(int i=0; i getAllPath(String placeA, String placeB){ List paths = new ArrayList(); Deque placeDeque = new ArrayDeque(); 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 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 queryPathBySite(String placeA, String placeB){ List paths = this.getAllPath(placeA, placeB); System.out.println(paths); List path = new ArrayList(); if(paths.size()>1) { for(int i=0; i queryGoodsByNpcName(String npcName){ List goodsNames = new ArrayList(); if(this.npcInstanceIndex.get(npcName)!=null) { List goodsInstances = this.npcInstanceIndex.get(npcName).getGoodsInstances(); if(goodsInstances != null) { for(int i=0; i queryGoodsByClassAndSite(String className, String site){ List goodsNames = new ArrayList(); List placeGoodsInstances = null; List goodsInstances = null; if(this.placeIndex.get(site)!=null && this.goodsClassIndex.get(className)!=null) { placeGoodsInstances = this.placeIndex.get(site).getGoodsInstances(); goodsInstances = this.goodsClassIndex.get(className).getGoodsInstances(); for(int i=0; i attrs = goodsInstance.getAttrs(); if(attrName.equals("location")) { if(goodsInstance.getPlace()!=null) { return goodsInstance.getPlace().getName(); } } if(attrName.equals("owner")) { if(goodsInstance.getNpc()!=null) { return goodsInstance.getNpc().getName(); } } for(int i=0; i queryGoodsAllAttrByGoodsName(String name){ List attrnames = new ArrayList(); GoodsInstance goodsInstance = queryGoodsByGoodsName(name); if(goodsInstance != null) { List attrs = goodsInstance.getAttrs(); for(int i=0; i attrs = goodsInstance.getAttrs(); for(int i=0;i attrs = npcInstance.getAttrs(); for(int i=0; i queryNpcByClassAndSite(String className, String site){ List npcNames = new ArrayList(); List placeNpcInstances = this.placeIndex.get(site).getNpcInstances(); List npcInstances = this.goodsClassIndex.get(className).getNpcInstances(); for(int i=0; i attrs = npcInstance.getAttrs(); for(int i=0; i queryNpcAttrsByNpcName(String name){ List attrNames = new ArrayList(); NpcInstance npcInstance = queryNpcByNpcName(name); if(npcInstance != null) { List attrs = npcInstance.getAttrs(); for(int i=0; i goodsClassAttrs = goodsClass.getAttrs(); GoodsInstance goodsInstance = new GoodsInstance(); List goodsInstanceAttr = new ArrayList(); goodsInstance.setName(names[2]); goodsInstance.setIntro(goodsClass.getIntro()); for(int i=0; i npcClassAttrs = npcClass.getAttrs(); NpcInstance npcInstance = new NpcInstance(); List npcInstanceAttr = new ArrayList(); npcInstance.setName(names[2]); npcInstance.setIntro(npcClass.getIntro()); for(int i=0; i goodsInstancesByClass = this.goodsClassIndex.get(names[1]).getGoodsInstances(); boolean goodsInstanceBoolean = false; GoodsInstance goodsInstance = this.goodsInstanceIndex.get(names[2]); for(int i=0; i attrs = goodsInstance.getAttrs(); if(names[3].equals("所在地点")) { Place placeOrigin = goodsInstance.getPlace(); List goodsInstances = placeOrigin.getGoodsInstances(); goodsInstances.remove(goodsInstance); Place place= this.placeIndex.get(value); System.out.println(place); goodsInstance.setPlace(place); goodsInstances = place.getGoodsInstances(); goodsInstances.add(goodsInstance); return true; }else if(names[3].equals("所属角色")) { NpcInstance npcInstance= goodsInstance.getNpc(); List goodsInstances = npcInstance.getGoodsInstances(); goodsInstances.remove(goodsInstance); npcInstance = this.npcInstanceIndex.get(value); goodsInstance.setNpc(npcInstance); goodsInstances = npcInstance.getGoodsInstances(); goodsInstances.add(goodsInstance); return true; }else { for(int j=0; j npcInstancesByClass = this.npcClassIndex.get(names[1]).getNpcInstances(); boolean npcInstanceBoolean = false; NpcInstance npcInstance = this.npcInstanceIndex.get(names[2]); for(int i=0; i attrs = npcInstance.getAttrs(); for(int j=0; j npcInstances = placeOrigin.getNpcInstances(); npcInstances.remove(npcInstance); Place place= this.placeIndex.get(value); npcInstance.setPlace(place); npcInstances = place.getNpcInstances(); npcInstances.add(npcInstance); return true; }else { if(names[3].equals(attrs.get(j).getName())) { attrs.get(j).setValue(value); return true; } } } return true; }else { return false; } }else if(names[0].equals("place")) { Place place = this.placeIndex.get(names[2]); if(names[3].equals("到达条件")) { place.setGoFlag(Integer.parseInt(value)); if(place.getGoFlag().equals(2)) { this.placeLinkDirectGo.put(place.getName(),place); this.placeNoLinkDirectGo.remove(place.getName()); }else if(place.getGoFlag().equals(3)) { this.placeNoLinkDirectGo.put(place.getName(),place); this.placeLinkDirectGo.remove(place.getName()); } return true; }else if(names[3].equals("可去地点")) { String[] ableGos = value.split("~"); List places = new ArrayList(); byte[] line = this.mapGraph[this.mapNames.get(place.getName())]; for(int ag=0; ag