package GameEnvironment; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSON; import GameEnvironment.bean.Attr; import GameEnvironment.bean.GoodsInstance; import GameEnvironment.bean.SuperClass; import GameEnvironment.bean.NpcInstance; import GameEnvironment.bean.Place; public class GameEnv implements Serializable{ private static final long serialVersionUID = 1L; public Map npcClassIndex; public Map npcInstanceIndex; public Map goodsClassIndex; public Map goodsInstanceIndex; public Map placeIndex; 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); if(!gameProjectMap.isEmpty()) { // 地图 Map map = (Map)gameProjectMap.get("map"); this.placeIndex = new HashMap(); Map placeMapTmp = new HashMap(); if(!map.isEmpty()) { 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")); placeIndex.put((String)one.get("name"), place); } for(String tmp:placeMapTmp.keySet()) { List ableGos = new ArrayList(); List ableGoLongs = (List)((Map)map.get(tmp)).get("canGo"); for(int j=0; j 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()) { Map instance = new HashMap(); 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")); List npcInstanceAttrs = new ArrayList(); for(String instanceAttrKey:instanceAttrMap.keySet()) { if(!instanceAttrKey.equals("name")&&!instanceAttrKey.equals("intro")&&!instanceAttrKey.equals("placeId")&&!instanceAttrKey.equals("placeName")&&!instanceAttrKey.equals("class")) { Attr attr = new Attr(); attr.setName(instanceAttrKey); attr.setValue((String)instanceAttrMap.get(instanceAttrKey)); npcInstanceAttrs.add(attr); } } npcInstance.setAttrs(npcInstanceAttrs); Place place = null; if(instanceAttrMap.get("placeId")!=null) { place = placeIndex.get(((Map)map.get(String.valueOf(instanceAttrMap.get("placeId")))).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()) { Map instance = new HashMap(); GoodsInstance goodsInstance = new GoodsInstance(); Map instanceAttrMap = (Map)instances.get(instanceKey); goodsInstance.setName((String)instanceAttrMap.get("name")); goodsInstance.setIntro((String)instanceAttrMap.get("intro")); List goodsInstanceAttrs = new ArrayList(); for(String instanceAttrKey:instanceAttrMap.keySet()) { if(instanceAttrKey!="placeId"&&instanceAttrKey!="npcId"&&instanceAttrKey!="name"&&instanceAttrKey!="intro") { Attr attr = new Attr(); attr.setName(instanceAttrKey); attr.setValue((String)instanceAttrMap.get(instanceAttrKey)); goodsInstanceAttrs.add(attr); } } goodsInstance.setAttrs(goodsInstanceAttrs); if(instanceAttrMap.get("placeId")!=null) { Place place = placeIndex.get(((Map)map.get(String.valueOf(instanceAttrMap.get("placeId")))).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); } } } } /********************************************地点******************************************/ // 周围有什么 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(); Place place = this.placeIndex.get(site); List ableGos = place.getAbleGo(); for(int i=0; i queryPathBySite(String start, String destination){ return null; } /******************************************通过npc获取物品列表**********************************************/ // 通过npcName获取所有物品名称列表 public List 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 queryNpcByClassAndSite(String className, String site){ List npcNames = new ArrayList(); List placeNpcInstances = this.placeIndex.get(site).getNpcInstances(); List npcInstances = this.goodsClassIndex.get(className).getNpcInstances(); NpcInstance npcInstance = new NpcInstance(); 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(); 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