/** * Saves all city/district mappings. * * @return This CityManager. */ public CityManager saveCityDistrictMappings() { File mappingFile = Mafiacraft.getOrCreateSubFile("geo", "city_district_mappings.yml"); YamlConfiguration conf = new YamlConfiguration(); for (Entry<City, List<District>> mapping : cityDistrictMap.entrySet()) { City city = mapping.getKey(); List<District> dists = mapping.getValue(); List<String> distStrs = new ArrayList<String>(); for (District district : dists) { distStrs.add(district.getUid()); } conf.set(Integer.toString(city.getId()), dists); } try { conf.save(mappingFile); } catch (IOException ex) { MLogger.log( Level.SEVERE, "The city/district mapping file could not be written for some odd reason!", ex); } return this; }
/** * Founds a city. * * @param player * @param name * @param center * @return */ public City foundCity(MPlayer player, String name, District center) { // Make city City city = new City(getNextCityId()); city.setName(name); city.setMayor(player.getName()); city.attachNewDistrict(center); center.setType(DistrictType.GOVERNMENT); insertCity(city); center.getWorld().setCapital(city); return city; }
/** * Disbands a city and wipes it off of the map. Forever. * * @param city * @return */ public CityManager disbandCity(City city) { for (District district : city.getDistricts()) { district.detachFromCity(); } MWorld cw = city.getCityWorld(); City capital = cw.getCapital(); if (capital.equals(city)) { cw.removeCapital(); } return this; }
/** * Saves all districts currently loaded to files. * * @return This CityManager. */ public CityManager saveDistricts() { File districtFile = Mafiacraft.getOrCreateSubFile("geo", "districts.yml"); YamlConfiguration conf = new YamlConfiguration(); for (District district : getDistrictList()) { conf.set(district.getUid(), district); } try { conf.save(districtFile); } catch (IOException ex) { MLogger.log(Level.SEVERE, "The district file could not be written for some odd reason!", ex); } return this; }
/** * Detaches the district from its city. * * @param district The district to detach from * @return This CityManager. */ public CityManager detachDistrict(District district) { City city = district.getCity(); districtCityMap.remove(district); if (city != null) { getActualCityDistricts(city).remove(district); } return this; }
/** * Inserts the given district. * * @param district The district to insert. * @return This CityManager. */ public CityManager insertDistrict(District district) { MWorld world = district.getWorld(); getDistrictMap(world).put(district.getId(), district); registerLandOwner(district); return this; }
// 参数为车站ID // 所有相关的记录都将删除. ?????????????? public boolean deleteStationInfo(String stationName) { SQLRequestMessage msg = new SQLRequestMessage(); msg.setCommandMode(Constants.MODE_CS_SYN_CLIENT); // 从列车区段表District中获取车站ID等于stationID的所有区段的ID List<String> districtIDList = new ArrayList<String>(); msg.setCommandType(Constants.TYPE_CLIENT_SQLQUERY); msg.setDataBean("District"); String sqlStr = "select * from District where District_startstationname='" + stationName + "' or District_endstationname='" + stationName + "'"; msg.setSql(sqlStr); String paprams = "null"; msg.setParams(paprams); String listString = synClientSupport.sqlMessageSend(msg); if (listString == null) { // 此情况不会出现 districtIDList = null; } else { List<District> list = JsonUtil.getList4Json(listString, District.class); if (list.size() <= 0) districtIDList = null; else { for (int i = 0; i < list.size(); i++) { District data = (District) list.get(i); districtIDList.add(data.getDistrict_name()); } } } // 从列车表Train中获取车站ID等于stationID的所有车次的ID List<String> trainIDList = new ArrayList<String>(); msg.setDataBean("Train"); sqlStr = "select * from Train where Train_startstationname='" + stationName + "' or Train_endstationname='" + stationName + "'"; msg.setSql(sqlStr); paprams = "null"; msg.setParams(paprams); listString = synClientSupport.sqlMessageSend(msg); if (listString == null) { // 此情况不会出现 trainIDList = null; } else { List<Train> list = JsonUtil.getList4Json(listString, Train.class); if (list.size() <= 0) trainIDList = null; else { for (int i = 0; i < list.size(); i++) { Train data = (Train) list.get(i); trainIDList.add(data.getTrain_name()); } } } // 批量删除 // 涉及到的表:Station, District Train StationDistrictRelation TrainDistrictRelation int districtNumber = 0; int trainNumber = 0; if (districtIDList != null) districtNumber = districtIDList.size(); if (trainIDList != null) trainNumber = trainIDList.size(); String[] sqlArray = new String[5 + districtNumber * 2 + trainNumber]; int index = 0; sqlArray[index++] = "delete from Station where Station_name='" + stationName + "'"; // number 1 sqlArray[index++] = "delete from StationDistrictRelation where Station_name='" + stationName + "'"; // number 1 sqlArray[index++] = "delete from Plan where Station_name='" + stationName + "' or Prestation_name='" + stationName + "'"; // number 1 if (districtIDList != null) { sqlArray[index++] = "delete from District where District_startstationname='" + stationName + "' or District_endstationname='" + stationName + "'"; // number 1 for (int i = 0; i < districtNumber; i++) { // number districtNumber*2 sqlArray[index++] = "delete from StationDistrictRelation where District_name='" + districtIDList.get(i) + "'"; sqlArray[index++] = "delete from TrainDistrictRelation where District_name='" + districtIDList.get(i) + "'"; } } if (trainIDList != null) { sqlArray[index++] = "delete from Train where Train_startstationname='" + stationName + "' or Train_endstationname='" + stationName + "'"; // number 1 for (int i = 0; i < trainNumber; i++) { // number trainNumber sqlArray[index++] = "delete from TrainDistrictRelation where Train_name='" + trainIDList.get(i) + "'"; } } msg.setCommandType(Constants.TYPE_CLIENT_SQLBATCHDELETE); sqlStr = JsonUtil.array2json(sqlArray); msg.setSql(sqlStr); paprams = "null"; msg.setParams(paprams); listString = synClientSupport.sqlMessageSend(msg); // 同步通信 if (listString == null) { return false; } else { return true; } }
static int loadDist(int whseKount, int distWhseKount) { int k = 0; int t = 0; try { now = new java.util.Date(); if (outputFiles == true) { out = new PrintWriter(new FileOutputStream(fileLocation + "district.csv")); System.out.println("\nWriting District file to: " + fileLocation + "district.csv"); } District district = new District(); t = (whseKount * distWhseKount); System.out.println("\nStart District Data for " + t + " Dists @ " + now + " ..."); for (int w = 1; w <= whseKount; w++) { for (int d = 1; d <= distWhseKount; d++) { district.d_id = d; district.d_w_id = w; district.d_ytd = 30000; // random within [0.0000 .. 0.2000] district.d_tax = (float) ((jTPCCUtil.randomNumber(0, 2000, gen)) / 10000.0); district.d_next_o_id = 3001; district.d_name = jTPCCUtil.randomStr(jTPCCUtil.randomNumber(6, 10, gen)); district.d_street_1 = jTPCCUtil.randomStr(jTPCCUtil.randomNumber(10, 20, gen)); district.d_street_2 = jTPCCUtil.randomStr(jTPCCUtil.randomNumber(10, 20, gen)); district.d_city = jTPCCUtil.randomStr(jTPCCUtil.randomNumber(10, 20, gen)); district.d_state = jTPCCUtil.randomStr(3).toUpperCase(); district.d_zip = "123456789"; k++; if (outputFiles == false) { distPrepStmt.setLong(1, district.d_id); distPrepStmt.setLong(2, district.d_w_id); distPrepStmt.setDouble(3, district.d_ytd); distPrepStmt.setDouble(4, district.d_tax); distPrepStmt.setLong(5, district.d_next_o_id); distPrepStmt.setString(6, district.d_name); distPrepStmt.setString(7, district.d_street_1); distPrepStmt.setString(8, district.d_street_2); distPrepStmt.setString(9, district.d_city); distPrepStmt.setString(10, district.d_state); distPrepStmt.setString(11, district.d_zip); distPrepStmt.executeUpdate(); } else { String str = ""; str = str + district.d_id + ","; str = str + district.d_w_id + ","; str = str + district.d_ytd + ","; str = str + district.d_tax + ","; str = str + district.d_next_o_id + ","; str = str + district.d_name + ","; str = str + district.d_street_1 + ","; str = str + district.d_street_2 + ","; str = str + district.d_city + ","; str = str + district.d_state + ","; str = str + district.d_zip; out.println(str); } } // end for [d] } // end for [w] long tmpTime = new java.util.Date().getTime(); String etStr = " Elasped Time(ms): " + ((tmpTime - lastTimeMS) / 1000.000) + " "; System.out.println(etStr.substring(0, 30) + " Writing record " + k + " of " + t); lastTimeMS = tmpTime; transCommit(); now = new java.util.Date(); System.out.println("End District Load @ " + now); } catch (SQLException se) { System.out.println(se.getMessage()); transRollback(); } catch (Exception e) { e.printStackTrace(); transRollback(); } return (k); } // end loadDist()