public class Entity { protected String id; protected String type; protected Integer width; protected Integer length; protected Integer money; protected Integer supplies; protected Integer population; protected String tech_id; protected String demand_for_happiness; protected String provided_happiness; protected String era; ... } public class ExistEntity { protected String id; protected String cityentity_id; // Entity protected String type; protected Integer x; protected Integer y; … }
import org.json.JSONArray; import org.json.JSONObject; import com.innogames.jmeter.foe.Entity; JSONArray responseData = new JSONArray(prev.getResponseDataAsString()); Map allBuildings = new HashMap(); // Map availableBuildings = new HashMap(); // , () JSONArray buildings = responseData.getJSONObject("responseData").getJSONArray("buildings"); for (int i = 0; i < buildings.length(); i++) { JSONObject building = buildings.getJSONObject(i); String id = building.getString("id"); String type = building.getString("type"); String name = building.getString("name"); // : Integer width = (building.has("width")) ? building.getInt("width") : 0; Integer length = (building.has("length")) ? building.getInt("length") : 0; // : JSONObject requirements = building.getJSONObject("requirements"); Integer money = (requirements.getJSONObject("resources").has("money")) ? requirements.getJSONObject("resources").get("money") : 0; .... // : String min_era = requirements.getString("min_era"); String tech_id = (requirements.has("tech_id") && (!requirements.isNull("tech_id"))) ? requirements.getString("tech_id") : null; Integer provided_happiness = (building.has("provided_happiness") && (!building.isNull("provided_happiness"))) ? building.getInt("provided_happiness") : 0; // Entity e = new Entity(id, type, min_era, width, length, money, supplies, population, tech_id, provided_happiness ); allBuildings.put(e.getId(), e); // , .... if (e.getEraRank() <= userEraRank && tech_researched == true) { availableBuildings.put(e.getId(), e); } } } // - , vars.putObject("availableBuildings", availableBuildings); vars.putObject("allBuildings", allBuildings);
import org.json.JSONArray; import org.json.JSONObject; import com.innogames.jmeter.foe.Entity; import com.innogames.jmeter.foe.ExistEntity; import java.awt.Point; Integer maxBuildingId = 0; JSONArray responseData = new JSONArray(prev.getResponseDataAsString()); Map allBuildings = vars.getObject("allBuildings"); Map cityTerritory = new HashMap(); // // JSONArray entities = unlocked_areas.getJSONObject("responseData").getJSONArray("unlocked_areas"); for (int i = 0; i < unlocked_areas.length(); i++) { Integer x = (unlocked_areas.getJSONObject(i).has("x")) ? unlocked_areas.getJSONObject(i).getInt("x") : 0; Integer y = (unlocked_areas.getJSONObject(i).has("y")) ? unlocked_areas.getJSONObject(i).getInt("y") : 0; Integer width_ = (unlocked_areas.getJSONObject(i).has("width")) ? unlocked_areas.getJSONObject(i).getInt("width") : 0; Integer length_ = (unlocked_areas.getJSONObject(i).has("length")) ? unlocked_areas.getJSONObject(i).getInt("length") : 0; for (Integer xx = x; xx <= x + width_ - 1; xx++) { for (Integer yy = y; yy <= y + length_ - 1; yy++) { p = new Point(xx, yy); cityTerritory.put(p, "0"); } } } // "" JSONArray entities = responseData.getJSONObject("responseData").getJSONArray("buildings"); for (int i = 0; i < entities.length(); i++) { Integer id = entities.getJSONObject(i).getInt("id"); String cityentity_id = entities.getJSONObject(i).getString("cityentity_id"); String type = entities.getJSONObject(i).getString("type"); int x = (entities.getJSONObject(i).has("x")) ? entities.getJSONObject(i).getInt("x") : 0; int y = (entities.getJSONObject(i).has("y")) ? entities.getJSONObject(i).getInt("y") : 0; ExistEntity ee = new ExistEntity(String.valueOf(id), cityentity_id, type, x, y); if (id >= maxBuildingId) maxBuildingId = id; Entity e = allBuildings.get(cityentity_id); for (int xx = x; xx <= x + e.getWidth() - 1; xx++) { for (int yy = y; yy <= y + e.getLength() - 1; yy++) { cityTerritory.put(new Point(xx, yy), e.getType()); } } } // , vars.putObject("cityTerritory", cityTerritory);
... Map cityTerritory = vars.getObject("cityTerritory"); Map availableBuildings = vars.getObject("availableBuildings"); Integer maxBuildingId = Integer.valueOf(vars.get("maxBuildingId")); Iterator cityTerritory = map.entrySet().iterator(); // while (it.hasNext()) Map.Entry entry = (Map.Entry) it.next(); Point key = (Point) entry.getKey(); String value = (String) entry.getValue(); key_x = (int) key.x; key_y = (int) key.y; if (value.equals("0")) { // ( ) if (map.containsKey(new Point(key_x, key_y - 1))) a = map.get(new Point(key_x, key_y - 1)); else a = "-1"; if (map.containsKey(new Point(key_x - 1, key_y - 1))) b = map.get(new Point(key_x - 1, key_y - 1)); else b = "-1"; if (map.containsKey(new Point(key_x + 1, key_y))) c = map.get(new Point(key_x + 1, key_y)); else c = "-1"; if (map.containsKey(new Point(key_x - 1, key_y))) d = map.get(new Point(key_x - 1, key_y)); else d = "-1"; if (map.containsKey(new Point(key_x, key_y + 1))) e = map.get(new Point(key_x, key_y + 1)); else e = "-1"; if (map.containsKey(new Point(key_x - 1, key_y + 1))) f = map.get(new Point(key_x - 1, key_y + 1)); else f = "-1"; if (map.containsKey(new Point(key_x + 1, key_y - 1))) g = map.get(new Point(key_x + 1, key_y - 1)); else g = "-1"; if (map.containsKey(new Point(key_x + 1, key_y - 1))) h = map.get(new Point(key_x + 1, key_y - 1)); else h = "-1"; // ( ) if ((!a.equals("0") && !a.equals("street")) || (!b.equals("0") && !b.equals("street")) || (!d.equals("0") && !d.equals("street")) || (!c.equals("0") && !c.equals("street")) || (!e.equals("0") && !e.equals("street")) || (!f.equals("0") && !f.equals("street")) || (!g.equals("0") && !g.equals("street"))) { // maxBuildingId = maxBuildingId + 1; vars.put("maxBuildingId", String.valueOf(maxBuildingId)); x = String.valueOf(key_x); y = String.valueOf(key_y); ...... } } }
Iterator it = cityTerritory.entrySet().iterator(); Integer checkSizeW = targetBuilding.getWidth() - 1; Integer checkSizeL = targetBuilding.getLength() - 1; // while (it.hasNext()) { Map.Entry entry = (Map.Entry) entries.next(); Point key = (Point) entry.getKey(); String value = (String) entry.getValue(); if (value.equals("0")) { // , // , , 4- : if ((cityTerritory.containsKey(new Point(key.x - 1, key.y - 1)) && cityTerritory.containsKey(new Point(key.x - 1, key.y)) && cityTerritory.containsKey(new Point(key.x, key.y - 1)) && cityTerritory.containsKey(new Point(key.x - 1, key.y + checkSizeL)) && cityTerritory.containsKey(new Point(key.x + checkSizeW, key.y - 1)) && cityTerritory.containsKey(new Point(key.x + checkSizeW, key.y + checkSizeL))) && (cityTerritory.get(new Point(key.x - 1, key.y)).equals("street") || cityTerritory.get(new Point(key.x, key.y - 1)).equals("street") || cityTerritory.get(new Point(key.x - 1, key.y + checkSizeL)).equals("street") || cityTerritory.get(new Point(key.x + checkSizeW, key.y - 1)).equals("street")) ) { boolean isFree = true; // , : for (int W = 0; W <= checkSizeW; W++) { for (int L = 0; L <= checkSizeL; L++) { if (!map.containsKey(new Point(key.x + W, key.y + L))) { sFree = false; } else { if (!map.get(new Point(key.x + W, key.y + L)).equals("0")) { isFree = false; } } } } if (isFree) { // } } } } }
JSONArray responseData = new JSONArray(response); for (int m = 0; m < responseData.length(); m++) { // : if (responseData.getJSONObject(m).getString("requestClass").equals("CityMapService")) { JSONArray city_map_entities = responseData.getJSONObject(m).getJSONArray("responseData"); for (int i = 0; i < city_map_entities.length(); i++) { JSONObject city_map_entity = city_map_entitis.get(i); if (city_map_entity.toString().contains("CityMapEntity")) { Integer id = city_map_entity..getInt("id"); String cityentity_id = city_map_entity..getString("cityentity_id"); String type = city_map_entity..getString("type"); Integer x = (city_map_entity..has("x")) ? city_map_entity..getInt("x") : 0; Integer y = (city_map_entity..has("y")) ? city_map_entity..getInt("y") : 0; Entity e = availableBuildings.get(cityentity_id); if (id >= maxBuildingId) maxBuildingId = id; for (int xx = x; xx <= x + e.getWidth() - 1; xx++) { for (int yy = y; yy <= y + e.getLength() - 1; yy++) { cityTerritory.put(new Point(xx, yy), e.getType()); } } } } // else if (responseData.getJSONObject(m).getString("requestClass").equals("ResourceService") && responseData.getJSONObject(m).getString("requestMethod").equals("getPlayerResources")) { JSONObject resources = responseData.getJSONObject(m).getJSONObject("responseData").getJSONObject("resources"); vars.putObject("resources", resources); Integer money = (resources.has("money")) ? resources.getInt("money") : 0; Integer supplies = (resources.has("supplies")) ? resources.getInt("supplies") : 0; Integer population = (resources.has("population")) ? resources.getInt("population") : 0; Integer strategy_points = (resources.has("strategy_points")) ? resources.getInt("strategy_points") : 0; vars.put("money", String.valueOf(money)); vars.put("supplies", String.valueOf(supplies)); vars.put("population", String.valueOf(population)); vars.put("strategy_points", String.valueOf(strategy_points)); } }
Source: https://habr.com/ru/post/354348/
All Articles