| <?php | 
|   | 
| namespace ServerOPS; | 
|   | 
| include_once "/Common/Logging.php"; | 
| include_once '/Common/CommFunc.php'; | 
| include_once "/db/DBOper.php"; | 
| include_once "/Account/User.php"; | 
| include_once "/Account/userlogcomm.php"; | 
|   | 
| /**服务器开放状态 */ | 
| class ServerStatue | 
| { | 
|     const Open = 1; | 
|     const InternalOpen = 2; | 
|     const NotOpen = 3; | 
|     static $StatueName = array( | 
|         ServerStatue::Open => "正式开放", | 
|         ServerStatue::InternalOpen => "内部开放", | 
|         ServerStatue::NotOpen => "不开放", | 
|     ); | 
| } | 
|   | 
| /**服务器运行状态 */ | 
| class ServerRunningStatus | 
| { | 
|     const Upkeep = 0; | 
|     const Light = 1; | 
|     const Busy = 2; | 
|     const Full = 3; | 
|     const PreOpen = 4; | 
|     static $RunningStatusName = array( | 
|         ServerRunningStatus::Light => "流畅", | 
|         ServerRunningStatus::Busy => "繁忙", | 
|         ServerRunningStatus::Full => "爆满", | 
|         ServerRunningStatus::PreOpen => "预开服", | 
|         ServerRunningStatus::Upkeep => "维护", | 
|     ); | 
| } | 
|   | 
| /** | 
|  * 刷新后台用的渠道服务器列表ini文件,兼容旧版本格式,采用合并数据的方法 | 
|  */ | 
| function RefreshServerListIni($channel) | 
| { | 
|     if (!\DBOper\Find( | 
|         "GameServers", | 
|         array("Channel" => $channel), | 
|         $serverList, | 
|         array( | 
|             "ServerID" => 1, | 
|             "ServerName" => 1, | 
|             "RegionDomain" => 1, | 
|             "MainServerID" => 1 | 
|         ), | 
|         array("ServerID" => 1) | 
|     )) { | 
|         return; | 
|     } | 
|     if (!isset($serverList)) { | 
|         return; | 
|     } | 
|   | 
|     $GameServerWebPort = "80"; | 
|     if (\CommFunc\Has_option("ServerInfo", "GameServerWebPort_" . $channel)) { | 
|         \CommFunc\GetConfig("ServerInfo", "GameServerWebPort_" . $channel, $GameServerWebPort, "80"); | 
|     } | 
|   | 
|     \Logging\LogInfo("RefreshServerListIni Channel:" . $channel . " GameServerWebPort:" . $GameServerWebPort); | 
|   | 
|     $channelServerIDArray = array(); | 
|     $channelServerIniArray = array(); | 
|     $mixServerMap = array(); // 合服映射表 {mainServerID:[serverID, ...], ...} | 
|     foreach ($serverList as $serverInfo) { | 
|         $ServerID = $serverInfo["ServerID"]; | 
|         $ServerName = $serverInfo["ServerName"]; | 
|         $RegionDomain = $serverInfo["RegionDomain"]; | 
|         $MainServerID = $serverInfo["MainServerID"]; | 
|   | 
|         if ($GameServerWebPort == "80") { | 
|             $PageUrl = "http://" . $RegionDomain . "/Server/Tool.php"; | 
|         } else { | 
|             $PageUrl = "http://" . $RegionDomain . ":" . $GameServerWebPort . "/Server/Tool.php"; | 
|         } | 
|         $optinArray = array( | 
|             "Page" => $PageUrl, | 
|             "ServerID" => $ServerID | 
|         ); | 
|         if ($MainServerID > 0) { | 
|             $optinArray["MainServer"] = $MainServerID; | 
|         } | 
|         if (array_key_exists($ServerName, $channelServerIniArray)) { | 
|             $channelServerIniArray[$ServerName . "_" . $ServerID] = $optinArray; | 
|         } else { | 
|             $channelServerIniArray[$ServerName] = $optinArray; | 
|         } | 
|         array_push($channelServerIDArray, $ServerID); | 
|   | 
|         // 更新合服映射 | 
|         $mixMainServerID = $MainServerID > 0 ? $MainServerID : $ServerID; | 
|         if (!isset($mixServerMap[$mixMainServerID])) { | 
|             $mixServerMap[$mixMainServerID] = array(); | 
|         } | 
|         $mixServerIDList = $mixServerMap[$mixMainServerID]; | 
|         array_push($mixServerIDList, $ServerID); | 
|         $mixServerMap[$mixMainServerID] = $mixServerIDList; | 
|     } | 
|   | 
|     // 保存合服服务器映射表 | 
|     file_put_contents(GetServerMix($channel), json_encode($mixServerMap)); | 
|   | 
|     $serverIniFile = dirname(__FILE__) . "\\..\\Account\\Server\\Server_" . $channel . ".ini"; | 
|     // echo '$serverIniFile:', $serverIniFile, "<br/>"; | 
|     if (file_exists($serverIniFile)) { | 
|         $serverIniArray = parse_ini_file($serverIniFile, true); | 
|     } else { | 
|         $serverIniArray = array(); | 
|     } | 
|   | 
|     // [chengxukf] | 
|     // Page=http://vm-chengxukf/Server/Tool.php | 
|     // [mobile-hzr1] | 
|     // Page=http://mobile-hzr/Server/Tool.php | 
|     // ServerID=23 | 
|     // [mobile-hzr2] | 
|     // Page=http://mobile-hzr/Server/Tool.php | 
|     // ServerID=24 | 
|     // MainServer=23 | 
|     // 一定有 Page ,不一定有 ServerID 、 MainServer,如跨服可能只有Page,非合服的没有MainServer | 
|   | 
|     // 由于服务器名可能改名,这里去除旧ini中已经重复存在的ServerID | 
|     foreach ($serverIniArray as $serverName => $serverInfo) { | 
|         if (array_key_exists("ServerID", $serverInfo) && in_array($serverInfo["ServerID"], $channelServerIDArray)) { | 
|             // \Logging\LogInfo("ServerID 已存在,移除: " . $serverInfo["ServerID"] . " serverName:" . $serverName); | 
|             unset($serverIniArray[$serverName]); | 
|         } | 
|     } | 
|     // 合并覆盖前者 | 
|     $serverIniArrayUpd = array_merge($serverIniArray, $channelServerIniArray); | 
|   | 
|     $f = fopen($serverIniFile, 'w'); | 
|     // 保存ini | 
|     foreach ($serverIniArrayUpd as $serverName => $serverInfo) { | 
|         fwrite($f, "[" . $serverName . "]\r\n"); | 
|         fwrite($f, "Page=" . $serverInfo["Page"] . "\r\n"); | 
|         if ($serverInfo["ServerID"] > 0) { | 
|             fwrite($f, "ServerID=" . $serverInfo["ServerID"] . "\r\n"); | 
|         } | 
|         if ($serverInfo["MainServer"] > 0) { | 
|             fwrite($f, "MainServer=" . $serverInfo["MainServer"] . "\r\n"); | 
|         } | 
|         fwrite($f, "\r\n"); | 
|     } | 
|     fclose($f); | 
| } | 
|   | 
| /** | 
|  * 刷新常规服务器列表json文件,仅导出正常开放的服务器,内部开放及未开放的不导出 | 
|  *    | 
|  *  导出格式 | 
|  *  { | 
|  *     "common": [ | 
|  *        { | 
|  *           "group_title": "分组名", | 
|  *           "group_list": [ | 
|  *              { | 
|  *                "login_port": 80, | 
|  *                "region_domain": "bt6test.secondworld.net.cn", | 
|  *                "name": "服务器名称", | 
|  *                "running_status": 1, | 
|  *                "game_port": 19006, | 
|  *                "is_recommend": 0, | 
|  *                "region_flag": 98, | 
|  *                "statue": 1, | 
|  *                "start_date": "2022-03-04 15:00:00" | 
|  *              }, | 
|  *              ... | 
|  *            ] | 
|  *         } | 
|  *       ... | 
|  *      ] | 
|  *  } | 
|  */ | 
| function RefreshCommonServerlistJson($Channel, $JsonBranch = null, $UserAccount = "") | 
| { | 
|     $groupJsonFile = GetServerGroup($Channel); | 
|     $groupArray = json_decode(\CommFunc\GetFileContents($groupJsonFile, "[]"), true); | 
|     if (!$groupArray) { | 
|         \Logging\LogError("can not found server group. Channel:" . $Channel); | 
|         return; | 
|     } | 
|   | 
|     if (!\CommFunc\GetConfig("ServerInfo", "PlayerCenterRoot", $PlayerCenterRoot)) { | 
|         return; | 
|     } | 
|   | 
|     $allBranchGroup = array(); | 
|     if ($JsonBranch != null) { | 
|         \DBOper\Find("GameServers", array("Channel" => $Channel, "JsonBranch" => $JsonBranch), $serverList); | 
|     } else { | 
|         \DBOper\Find("GameServers", array("Channel" => $Channel), $serverList); | 
|     } | 
|     \Logging\LogInfo("find db server count:" . count($serverList) . " Channel:" . $Channel . " JsonBranch:" . $JsonBranch); | 
|   | 
|     foreach ($serverList as $serverInfo) { | 
|         if ($serverInfo["Statue"] != ServerStatue::Open) { | 
|             // \Logging\LogInfo("非开放状态 ServerID:" . $serverInfo["ServerID"]); | 
|             continue; | 
|         } | 
|   | 
|         $JsonBranch = $serverInfo["JsonBranch"]; | 
|         if (!array_key_exists($JsonBranch, $allBranchGroup)) { | 
|             $commGroup = array(); | 
|             foreach ($groupArray as $groupInfo) { | 
|                 array_push($commGroup, array( | 
|                     "group_title" => $groupInfo["GroupTitle"], | 
|                     "group_list" => array(), | 
|                     "ServerIDList" => $groupInfo["ServerIDList"] | 
|                 )); | 
|             } | 
|             $allBranchGroup[$JsonBranch] = $commGroup; | 
|         } | 
|         $commGroup = $allBranchGroup[$JsonBranch]; | 
|   | 
|         $serverJson = array( | 
|             "login_port" => $serverInfo["LoginPort"], | 
|             "region_domain" => $serverInfo["RegionDomain"], | 
|             "name" => $serverInfo["ServerName"], | 
|             "running_status" => $serverInfo["RunningStatus"], | 
|             "game_port" => $serverInfo["GamePort"], | 
|             "is_recommend" => $serverInfo["Recommend"], | 
|             "region_flag" => $serverInfo["ServerID"], | 
|             "statue" => $serverInfo["Statue"], | 
|             "start_date" => $serverInfo["StartDate"], | 
|         ); | 
|   | 
|         $serverID = $serverInfo["ServerID"]; | 
|         foreach ($commGroup as $key => $groupInfo) { | 
|             $ServerIDList = $groupInfo["ServerIDList"]; | 
|             foreach ($ServerIDList as $idRange) { | 
|                 if ( | 
|                     is_array($idRange) && count($idRange) == 2 | 
|                     && ($serverID >= $idRange[0] && $serverID <= $idRange[1]) | 
|                 ) { | 
|                     // 直接访问 group_list 实际地址进行push | 
|                     array_push($allBranchGroup[$JsonBranch][$key]["group_list"], $serverJson); | 
|                     break; | 
|                 } | 
|             } | 
|         } | 
|     } | 
|   | 
|     // \Logging\LogInfo("allBranchGroup:" . print_r($allBranchGroup, true)); | 
|     $JsonBranchList = array(); | 
|     $refreshJsonFileArray = array(); | 
|     foreach ($allBranchGroup as $JsonBranch => &$commGroup) { | 
|         foreach ($commGroup as $key => &$groupInfo) { | 
|             unset($groupInfo["ServerIDList"]); | 
|             // $commGroup[$key] = $groupInfo; | 
|             if (count($groupInfo["group_list"]) == 0) { | 
|                 unset($commGroup[$key]); | 
|             } | 
|         } | 
|         // $allBranchGroup[$JsonBranch] = $commGroup; | 
|         if (count($commGroup) > 0) { | 
|             $saveCommon = array(); | 
|             foreach ($commGroup as $value) { | 
|                 array_push($saveCommon, $value); | 
|             } | 
|             $saveArray = array("common" => $saveCommon); | 
|             $saveJsonFile =  $PlayerCenterRoot . "/Common/serverjson/" . GetCommonServerlistJsonFileName($Channel, $JsonBranch); | 
|             // file_put_contents($saveJsonFile, \CommFunc\MyJson_encode($saveArray)); | 
|             file_put_contents($saveJsonFile, json_encode($saveArray)); //选服文件暂不存utf8 | 
|             array_push($refreshJsonFileArray, $saveJsonFile); | 
|             \Logging\LogInfo("RefreshCommonServerlistJson Channel:" . $Channel . " JsonBranch:" . $JsonBranch . " saveJsonFile:" . $saveJsonFile); | 
|             array_push($JsonBranchList, $JsonBranch); | 
|         } | 
|     } | 
|     if (count($refreshJsonFileArray) > 0) { | 
|         AddSuccessLog($Channel, $UserAccount, \User\Permission::P_OPSRefreshServer, array("JsonBranchList" => $JsonBranchList)); | 
|     } | 
|     RefreshServerListIni($Channel); | 
|     return $refreshJsonFileArray; | 
| } | 
|   | 
| function GetServerGroup($Channel) | 
| { | 
|     if (!\CommFunc\GetConfig("ServerInfo", "PlayerCenterRoot", $PlayerCenterRoot)) { | 
|         return ""; | 
|     } | 
|     return $PlayerCenterRoot . "/Common/servergroup/" . $Channel . ".json"; | 
| } | 
|   | 
| // 合服映射表 | 
| function GetServerMix($Channel) | 
| { | 
|     if (!\CommFunc\GetConfig("ServerInfo", "PlayerCenterRoot", $PlayerCenterRoot)) { | 
|         return ""; | 
|     } | 
|     return $PlayerCenterRoot . "/Common/servermix/MixServerMap_" . $Channel . ".json"; | 
| } | 
|   | 
| function GetCommonServerlistJsonFileName($Channel, $JsonBranch) | 
| { | 
|     $gameName = \CommFunc\GetGameName(); | 
|     if ($JsonBranch == 0) { | 
|         return $gameName . "_" . $Channel . ".json"; | 
|     } | 
|     return $gameName . "_" . $Channel . "_" . $JsonBranch . ".json"; | 
| } | 
|   | 
| function GetAllCommonServerlistJsonFile($Channel) | 
| { | 
|     if (!\CommFunc\GetConfig("ServerInfo", "PlayerCenterRoot", $PlayerCenterRoot)) { | 
|         return; | 
|     } | 
|     $gameName = \CommFunc\GetGameName(); | 
|     $jsonPath = $PlayerCenterRoot . "/Common/serverjson/"; | 
|     $jsonFiles = array(); | 
|     foreach (glob($jsonPath . $gameName . "_" . $Channel . "*.json") as $value) { | 
|         array_push($jsonFiles, str_replace($jsonPath, "", $value)); | 
|     } | 
|     return $jsonFiles; | 
| } | 
|   | 
| // 白名单列表 | 
| function GetWhiteList() | 
| { | 
|     return json_decode(\CommFunc\GetFileContents(dirname(__FILE__) . "/whitelist.json", "[]"), true); | 
| } |