@Override
 public void updateState(int id, int state, int type) {
   WorldServer worldServer = this.get(id);
   if (type == 0) { // 0为更新运行状态
     CacheService.getWorldServerById(id).setState(state);
     worldServer.setState(state);
   } else if (type == 1) { // 1为更新部署状态
     CacheService.getWorldServerById(id).setIsDeploy(state);
     worldServer.setIsDeploy(state);
   }
   this.update(worldServer);
 }
 /** 检测是否有关联 */
 public boolean checkIsRelate(int worldId) {
   boolean checkResult = false;
   for (DispatchServer dispatchServer : CacheService.getDispatchServerAllList()) {
     if (dispatchServer.getWorldId() == worldId) {
       checkResult = true;
     }
   }
   return checkResult;
 }
 public static void main(String[] args) {
   Server server = CacheService.getServerById(2);
   UploadDataToServer uploadDataToServer = new UploadDataToServer();
   uploadDataToServer.exce(
       CommanConstant.STATE_UPLOAD_ALL,
       server,
       "D://logs//gunsoul_report",
       "/data/apps/modeltest/");
 }
 /** 迁移当前机子的数据到其他机子 */
 public String updateCfgToOtherServer(ChangeServerVo changeServerVo) {
   String result = "success";
   try {
     WorldServer worldServer = CacheService.getWorldServerById(changeServerVo.getId());
     changeServerVo.setRemark("修改" + worldServer.getName() + "数据库配置中");
     if (changeServerVo.getServerId() == worldServer.getServerId()) {
       changeServerVo.setRemark("服务已在当前服务器,请确认目标服务器是否正确");
       result = "服务已在当前服务器,请确认目标服务器是否正确";
       return result;
     }
     int serverId = changeServerVo.getServerId(); // 迁移目标serverId
     Server server = CacheService.getServerById(serverId);
     String publicip = server.getServerName().trim(); // 目标机子公共ip
     // 获取排好序的world列表(port倒序)
     List<WorldServer> worldServerList = worldServerDao.getWorldServerListByServerId(serverId);
     if (worldServerList != null && worldServerList.size() > 0) {
       WorldServer worldServerTemp = worldServerList.get(0); // 获取port最大的WorldServer
       // 判断目标机子上是否已经存在改端口
       if (worldServerDao.get(
               new String[] {"port", "serverId"},
               new Object[] {worldServerTemp.getPort() + 1, serverId})
           != null) {
         result = "world存在相同的port,生成失败";
         System.out.println("world存在相同的port,生成失败");
         return result;
       }
       worldServer.setPort(worldServerTemp.getPort() + 1);
       worldServer.setAdminport(worldServerTemp.getAdminport() + 1);
       worldServer.setHttpport(worldServerTemp.getHttpport() + 1);
     } else { // 如果该机子没有记录,则设置初设值
       worldServer.setPort(6860);
       worldServer.setAdminport(28960);
       worldServer.setHttpport(6660);
     }
     worldServer.setServerId(serverId);
     worldServer.setAccountId(CacheService.getAccountServerByServerId(serverId).getId());
     worldServer.setPublicip(publicip);
     this.saveOrUpdate(worldServer);
     CacheService.initWorldServer();
   } catch (Exception e) {
     e.printStackTrace();
   }
   return result;
 }
 /** 根据worldIds获取所对应的ACCOUNT中未开启的accountId */
 public StringBuilder getAccountState(String worldIds) {
   StringBuilder sb = new StringBuilder();
   try {
     String[] worldIdArray = worldIds.split(",");
     for (int i = 0; i < worldIdArray.length; i++) {
       AccountServer accountServer =
           CacheService.getAccountServerById(
               CacheService.getWorldServerById(Integer.valueOf(worldIdArray[i])).getAccountId());
       // 检测ACCOUNT服务是否开启
       if (accountServer.getState() == CommanConstant.STATE_STOP) {
         sb.append(accountServer.getId()).append(",");
       }
     }
   } catch (Exception e) {
     sb.append(e.getMessage());
     e.printStackTrace();
   }
   return sb;
 }
 /** 同步配置信息 */
 @Override
 public void synchronizeConfig(String worldIds) {
   String[] ids = worldIds.split(",");
   for (int i = 0; i < ids.length; i++) {
     Map<String, String> configMap =
         SshxcuteUtils.getConfigMap(
             CacheService.getWorldServerById(Integer.valueOf(ids[i])),
             Global.CONFIG_WORLD_PROPERTIES);
     this.updateWorldByMap(Integer.valueOf(ids[i]), configMap);
   }
 }
 /** 开启world服务 */
 public String startServer(BeanInterface beanInterface) throws Exception {
   System.out.println("开启world服务");
   StringBuffer stringBuffer = new StringBuffer();
   WorldServer worldServer = (WorldServer) beanInterface;
   if (worldServer.getIsDeploy() == CommanConstant.STATE_START) {
     int accountState = CacheService.getAccountServerById(worldServer.getAccountId()).getState();
     if (accountState == CommanConstant.STATE_START) {
       String startResult = SshxcuteUtils.startEachServer(worldServer, Global.WORLD_PID_FILE);
       stringBuffer.append(startResult);
     } else {
       stringBuffer.append("ID为:" + worldServer.getAccountId() + "ACCOUNT服务未启动,请先启动ACCOUNT!");
     }
   } else {
     stringBuffer.append("ID为:" + worldServer.getId() + "worldServer未部署");
   }
   return stringBuffer.toString();
 }
 /**
  * 關閉world
  *
  * @param batchOpearVo 操作vo
  * @param dispatchServerIds 已開啟的dispatch服務Id
  * @param worldId worldID
  */
 public boolean closeWorld(
     BatchOpearVo batchOpearVo,
     WorldServer worldServer,
     List<Integer> dispatchServerIds,
     ServerService serverService)
     throws Exception {
   boolean closeResult = false;
   batchOpearVo.setRemark(worldServer.getName() + "正在关闭");
   WorldServerService worldServerService = Application.getBean(WorldServerService.class);
   Server server = serverService.get(worldServer.getServerId());
   // worldServer已关闭的情况
   if (worldServer.getState() == CommanConstant.STATE_STOP) {
     boolean result =
         SshxcuteUtils.progressIsExistNew(server, worldServer.getPath(), Global.WORLD_PID_FILE);
     if (!result) {
       return true;
     }
   }
   if (dispatchServerIds != null && dispatchServerIds.size() > 0) {
     // System.out.println("=====dispatchServerIds=====" + dispatchServerIds.toArray().toString());
     boolean dispatchCloseIsSuccess =
         worldServerService.dispatchCloseIsSuccess(
             dispatchServerIds, worldServer, server, CommanConstant.STATE_STOP);
     // 检测Dispatch服务是否关闭完成,若未完成则不关闭world服务
     if (!dispatchCloseIsSuccess) {
       batchOpearVo.isFail("分发服关闭未完成,请关闭再试");
       return closeResult;
     }
   }
   Object[] killResult = SshxcuteUtils.killExec(server, worldServer.getPath());
   if (killResult != null
       && killResult.length > 0
       && (Integer) killResult[0] != CommanConstant.RESULT_TRUE_STATE) {
     batchOpearVo.isFail(worldServer.getName() + "world关闭出错");
     return closeResult;
   }
   worldServer.setState(CommanConstant.STATE_STOP);
   worldServerService.update(worldServer);
   CacheService.getWorldServerById(worldServer.getId()).setState(CommanConstant.STATE_STOP);
   batchOpearVo.isSUCCESS(worldServer.getName() + "world关闭成功");
   closeResult = true;
   return closeResult;
 }
 public void saveOrUpdateWorld(WorldServer worldServers) {
   try {
     WorldServer worldServer = null;
     if (worldServers.getId() == null) {
       worldServer = new WorldServer();
     } else {
       worldServer = this.get(worldServers.getId());
     }
     worldServer.setGameId(worldServers.getGameId());
     worldServer.setServerId(worldServers.getServerId());
     worldServer.setAccountId(worldServers.getAccountId());
     worldServer.setModelPath(worldServers.getModelPath());
     worldServer.setUpdatePath(worldServers.getUpdatePath());
     worldServer.setName(worldServers.getName());
     worldServer.setLocalip(worldServers.getLocalip());
     worldServer.setPort(worldServers.getPort());
     worldServer.setAdminip(worldServers.getAdminip());
     worldServer.setAdminport(worldServers.getAdminport());
     worldServer.setHttpip(worldServers.getHttpip());
     worldServer.setPublicip(worldServers.getPublicip());
     worldServer.setHttpport(worldServers.getHttpport());
     worldServer.setAuthip(worldServers.getAuthip());
     worldServer.setAuthport(worldServers.getAuthport());
     worldServer.setServerpassword(worldServers.getServerpassword());
     worldServer.setAreaid(worldServers.getAreaid());
     worldServer.setMachinecode(worldServers.getMachinecode());
     worldServer.setServerType(worldServers.getServerType());
     worldServer.setDid(worldServers.getDid());
     worldServer.setPath(worldServers.getPath());
     worldServer.setState(worldServers.getState());
     worldServer.setIsDeploy(worldServers.getIsDeploy());
     worldServer.setOrders(worldServers.getOrders());
     this.saveOrUpdate(worldServer);
     CacheService.initWorldServer();
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
 /**
  * 获取运行结果信息
  *
  * @author:LKJ
  */
 public String getStdout(Server server, WorldServer worldServer, HttpSession session)
     throws Exception {
   boolean successResult = false;
   boolean exceptionResult = false;
   String result = "";
   Object[] stdResult = SshxcuteUtils.showStdout(server, worldServer.getPath());
   String successStr = Global.WORLD_SUCCESS_RESULT;
   String exceptionStr = "Exception";
   if (stdResult != null && stdResult.length > 0 && stdResult[1] != null) {
     successResult = CommonUtil.Pattern(successStr, stdResult[1].toString());
     exceptionResult = CommonUtil.Pattern(exceptionStr, stdResult[1].toString());
   }
   if (exceptionResult) {
     result = "false";
     SystemLogService.worldServerLog(
         session, ":" + worldServer.getId() + "," + stdResult[1].toString());
   } else if (successResult) {
     result = "true";
     CacheService.getWorldServerById(worldServer.getId()).setState(CommanConstant.STATE_START);
     worldServer.setState(CommanConstant.STATE_START);
     Application.getBean(WorldServerService.class).update(worldServer);
   }
   return result;
 }
 /**
  * 获取通配信息
  *
  * @param worldServerModel
  * @return
  */
 @Override
 public String getWildcard(int worldId) {
   StringBuffer sb = new StringBuffer();
   WorldServer worldServer = CacheService.getWorldServerById(worldId);
   if (worldServer != null && worldServer.getId() > 0) {
     sb.append(worldServer.getLocalip())
         .append(",")
         .append(worldServer.getPort())
         .append(",")
         .append(worldServer.getAdminip())
         .append(",")
         .append(worldServer.getAdminport())
         .append(",")
         .append(worldServer.getHttpip())
         .append(",")
         .append(worldServer.getPublicip())
         .append(",")
         .append(worldServer.getHttpport())
         .append(",")
         .append(worldServer.getAuthip())
         .append(",")
         .append(worldServer.getAuthport())
         .append(",")
         .append(worldServer.getAreaid())
         .append(",")
         .append(worldServer.getMachinecode())
         .append(",")
         .append(worldServer.getServerType())
         .append(",")
         .append(worldServer.getBattleip())
         .append(",")
         .append(worldServer.getBattleport())
         .append(",");
   }
   return sb.toString();
 }
 @Override
 public void initCache() {
   CacheService.initWorldServer();
 }
 /** 生成worldServer对象 */
 public String createWorldServer(WorldServer worldServers, String mergeIds) {
   String createResult = "";
   try {
     String cnType = Application.getConfig("system", "cnType"); // 分区前缀类型
     int serverId = worldServers.getServerId(); // 服务器Id(哪台机子)
     Server server = CacheService.getServerById(serverId);
     int machinecode = worldServers.getMachinecode(); // 分区号
     String publicip = server.getServerName().trim(); // 公网ip
     String battleip = worldServers.getBattleip(); // 对战服ip
     int battleport = worldServers.getBattleport(); // 对战服端口
     String areaId = cnType + "_" + machinecode; // 加前缀的分编号,如“CN_1”
     int serverType = worldServers.getServerType();
     String path = getWorldPath(worldServers, cnType);
     String name = getWorldName(worldServers, cnType);
     List<WorldServer> worldServerListCheck =
         worldServerDao.getWorldServerByServerTypeAndAreaId(serverType, machinecode);
     if (worldServerListCheck != null && worldServerListCheck.size() > 0) {
       createResult = "存在相同類型和分區號的記錄";
       return createResult;
     }
     WorldServer worldServer = null;
     List<WorldServer> worldServerList =
         worldServerDao.getWorldServerListByServerId(worldServers.getServerId());
     if (worldServerList != null && worldServerList.size() > 0) {
       worldServer = new WorldServer();
       WorldServer worldServerTemp = worldServerList.get(0);
       BeanUtils.copyProperties(worldServer, worldServerTemp);
       if (worldServerDao.get(
               new String[] {"port", "serverId"},
               new Object[] {worldServerTemp.getPort() + 1, serverId})
           != null) {
         createResult = "存在相同的port,生成失败";
         return createResult;
       }
       worldServer.setId(null);
       worldServer.setPort(worldServerTemp.getPort() + 1);
       worldServer.setAdminport(worldServerTemp.getAdminport() + 1);
       worldServer.setHttpport(worldServerTemp.getHttpport() + 1);
       worldServer.setAuthport(worldServerTemp.getAuthport());
       worldServer.setUpdateTime(null);
       worldServer.setState(0);
       worldServer.setIsDeploy(1);
       worldServer.setMergeIds("");
     } else {
       worldServer = this.getDefaultWorld();
     }
     worldServer.setServerId(serverId);
     worldServer.setAccountId(CacheService.getAccountServerByServerId(serverId).getId());
     worldServer.setName(name);
     worldServer.setPublicip(publicip);
     worldServer.setAreaid(areaId);
     worldServer.setMachinecode(machinecode);
     worldServer.setBattleport(battleport);
     worldServer.setBattleip(battleip);
     worldServer.setPath(path);
     worldServer.setServerType(worldServers.getServerType());
     worldServer.setOrders(this.getMaxOrderByType(serverType, worldServers.getOrders()) + 1);
     if (worldServers.getIsDeploy() != null) {
       worldServer.setIsDeploy(worldServers.getIsDeploy());
     }
     // 合服區
     if (!StringUtils.isEmpty(mergeIds)) {
       // String mergeIds = "";
       StringBuilder sb = new StringBuilder();
       String[] mergeIdArray = mergeIds.split(",");
       // if (mergeIdArray.length == 2) {
       for (String id : mergeIdArray) {
         if (!StringUtils.isNumeric(id)) {
           createResult = "合服id串格式錯誤";
           return createResult;
         }
         String mergeId = cnType + "_" + id;
         sb.append(mergeId).append(",");
       }
       mergeIds = sb.substring(0, sb.lastIndexOf(","));
       // } else if (mergeIdArray.length == 1) {
       // mergeIds = cnType + "_" + mergeIdArray[0];
       // } else {
       // createResult = "名稱格式錯誤";
       // return createResult;
       // }
       worldServer.setMergeIds(mergeIds);
     }
     this.saveOrUpdate(worldServer);
     CacheService.initWorldServer();
     createResult = "true";
   } catch (Exception e) {
     e.printStackTrace();
     createResult = "遇到未知错误";
   }
   return createResult;
 }
 public static void upload(String serverId, String localPath, String toPath) {
   UploadDataToServer uploadDataToServer = new UploadDataToServer();
   Server server = CacheService.getServerById(Integer.valueOf(serverId));
   uploadDataToServer.exce(CommanConstant.STATE_UPLOAD_ALL, server, localPath, toPath);
 }