hxp
2025-06-04 f4a514d5ac952110da846636ecbb9de951eaf3d2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?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);
}