<?php
|
include_once '/Common/CommFunc.php';
|
include_once "/Common/Logging.php";
|
include_once "/db/DBOper.php";
|
include_once "/Common/ServerOPS.php";
|
include_once "/Common/Commtox7.php";
|
include_once "/serverrep/report.php";
|
|
\Logging\CreateLogging("task.minuteLoop.php");
|
|
$curyweek = date("W", time());
|
$curDate = getdate();
|
$dateStr = $curDate['year'] . "-" . $curDate['mon'] . "-" . $curDate['mday'];
|
$hourStr = $dateStr . " " . $curDate['hours'];
|
$minuteStr = $hourStr . ":" . $curDate['minutes'];
|
\Logging\LogInfo("curweek:" . $curyweek . " curDate:" . $dateStr . " curHour:" . $hourStr . " curMinute:" . $minuteStr);
|
|
$eventKey = 'serverLoopInfo';
|
if (!\DBOper\FindOne("ServerEvent", array("Key" => $eventKey), $ret, null, false)) {
|
exit;
|
}
|
$serverLoopInfo = $ret["Value"] ? $ret["Value"] : array();
|
\Logging\LogInfo("dbValue:" . json_encode($serverLoopInfo));
|
|
if ($serverLoopInfo['yweek'] != $curyweek) {
|
OnWeek($curyweek);
|
$serverLoopInfo['yweek'] = $curyweek;
|
}
|
|
if ($serverLoopInfo['date'] != $dateStr) {
|
OnDay($dateStr);
|
$serverLoopInfo['date'] = $dateStr;
|
}
|
|
if ($serverLoopInfo['hour'] != $hourStr) {
|
OnHour($hourStr);
|
$serverLoopInfo['hour'] = $hourStr;
|
}
|
|
if ($serverLoopInfo['minute'] != $minuteStr) {
|
OnMinute($minuteStr);
|
$serverLoopInfo['minute'] = $minuteStr;
|
}
|
|
\DBOper\Update("ServerEvent", array("Key" => $eventKey), array("Value" => $serverLoopInfo), false, true);
|
exit;
|
|
function OnWeek($yweek)
|
{
|
\Logging\LogInfo("Todo OnWeek: " . $yweek);
|
}
|
|
function OnDay($dateStr)
|
{
|
\Logging\LogInfo("Todo OnDay: " . $dateStr);
|
}
|
|
function OnHour($hourStr)
|
{
|
\Logging\LogInfo("Todo OnHour: " . $hourStr);
|
\Report\OnExportRep();
|
}
|
|
function OnMinute($minuteStr)
|
{
|
\Logging\LogInfo("Todo OnMinute: " . $minuteStr);
|
checkAutoOpenServer();
|
checkPreOpenServer();
|
checkOpenServerTime();
|
}
|
|
//==========================================================================================================
|
/**
|
* 检查自动服务器信息,每分钟处理一次
|
*/
|
function checkAutoOpenServer()
|
{
|
$curDate = getdate();
|
$dateStr = $curDate['year'] . "-" . $curDate['mon'] . "-" . $curDate['mday'];
|
\Logging\LogInfo("=== Do checkAutoOpenServer ===");
|
// 设定时间范围 如21-24点 不会自动开服
|
CommFunc\GetConfig("ServerInfo", "AutoOpenServerLimitTimeRange", $AutoOpenServerLimitTimeRange);
|
$limitTimeList = json_decode($AutoOpenServerLimitTimeRange);
|
\Logging\LogInfo("AutoOpenServerLimitTimeRange" . $AutoOpenServerLimitTimeRange);
|
$curTime = strtotime(date("Y-m-d H:i:s", time()));
|
for ($i = 0; $i < count($limitTimeList); $i++) {
|
$limitTimeFrom = strtotime(date("Y-m-d H:i:s", strtotime($dateStr . " " . $limitTimeList[$i][0] . ":00")));
|
$limitTimeTo = strtotime(date("Y-m-d H:i:s", strtotime($dateStr . " " . $limitTimeList[$i][1] . ":00")));
|
// 跨天了
|
if ($limitTimeFrom > $limitTimeTo) {
|
$limitTimeTo += 3600 * 24;
|
}
|
\Logging\LogInfo("limitTimeRange: " . $limitTimeFrom . " ~ " . $limitTimeTo . " curTime:" . $curTime);
|
if ($curTime >= $limitTimeFrom && $curTime <= $limitTimeTo) {
|
\Logging\LogInfo("time is not allow to auto open server. curTime:" . $curTime);
|
return;
|
}
|
}
|
|
// 找出所有预开服服务器
|
\DBOper\Find("GameServers", array(
|
"RunningStatus" => \ServerOPS\ServerRunningStatus::PreOpen,
|
"OpenServerWay" => 1,
|
"RelationServerOK" => 0,
|
), $serverList);
|
|
if (!$serverList) {
|
\Logging\LogInfo("Can not found preopen and auto open servers by RelationServer.");
|
return;
|
}
|
|
// 达到条件后多少分钟后入口开放,分钟,需要预留清档缓冲时间,需大于提前清档时间,目前是提前15分钟清档
|
$setStartDate = \CommFunc\TimeToStrDateTime(strtotime("+20 minutes"));
|
\Logging\LogInfo("setStartDate." . $setStartDate);
|
|
$serverCreateRoleInfo = array();
|
\DBOper\Find("GameServerInfo", array(), $serverInfoList);
|
if (isset($serverInfoList)) {
|
foreach ($serverInfoList as $serverInfo) {
|
$Channel = $serverInfo["Channel"];
|
$ServerID = $serverInfo["ServerID"];
|
$key = $Channel . "_" . $ServerID;
|
$serverCreateRoleInfo[$key] = $serverInfo["CreateRoleCount"];
|
}
|
}
|
// \Logging\LogInfo("serverCreateRoleInfo" . print_r($serverCreateRoleInfo, true));
|
|
foreach ($serverList as $serverInfo) {
|
$Channel = $serverInfo["Channel"];
|
$ServerID = $serverInfo["ServerID"];
|
$RelationServerID = $serverInfo["RelationServerID"];
|
$RelationCreateRoles = $serverInfo["RelationCreateRoles"];
|
|
if (!$RelationServerID || !$RelationCreateRoles) {
|
\Logging\LogError("RelationServer option error. Channel:" . $Channel . " ServerID:" . $ServerID .
|
" RelationServerID:" . $RelationServerID . " RelationCreateRoles:" . $RelationCreateRoles);
|
continue;
|
}
|
|
$createRoleCount = 0;
|
$key = $Channel . "_" . $RelationServerID;
|
if (array_key_exists($key, $serverCreateRoleInfo)) {
|
$createRoleCount = $serverCreateRoleInfo[$key];
|
}
|
if ($createRoleCount < $RelationCreateRoles) {
|
\Logging\LogInfo("Channel:" . $Channel . " ServerID:" . $ServerID . " RelationServerID:" . $RelationServerID .
|
" createRoleCount:" . $createRoleCount . " < " . $RelationCreateRoles);
|
continue;
|
}
|
|
$find = array("Channel" => $Channel, "ServerID" => $ServerID);
|
$set = array(
|
"RelationServerOK" => 1,
|
"StartDate" => $setStartDate,
|
);
|
|
if (\DBOper\Update("GameServers", $find, $set)) {
|
\Logging\LogInfo("set server auto open time ok. setStartDate:" . $setStartDate .
|
" Channel:" . $Channel . " ServerID:" . $ServerID . " RelationServerID:" . $RelationServerID . " RelationCreateRoles:" . $RelationCreateRoles);
|
} else {
|
\Logging\LogError("set server auto open time error. setStartDate:" . $setStartDate . print_r($serverInfo, true));
|
}
|
}
|
}
|
|
/**
|
* 检查预开服服务器信息,每分钟处理一次
|
*/
|
function checkPreOpenServer()
|
{
|
\Logging\LogInfo("=== Do checkPreOpenServer ===");
|
// 找出所有预开服服务器
|
\DBOper\Find("GameServers", array(
|
"RunningStatus" => \ServerOPS\ServerRunningStatus::PreOpen
|
), $serverList);
|
|
if (!$serverList) {
|
\Logging\LogInfo("Can not found preopen and internalopen servers.");
|
return;
|
}
|
|
$x7notifyserverInfo = array();
|
$refrshJsonBranchArray = array();
|
|
$curTime = strtotime(date("Y-m-d H:i:s", time()));
|
foreach ($serverList as $serverInfo) {
|
$Channel = $serverInfo["Channel"];
|
$ServerID = $serverInfo["ServerID"];
|
$OpenServerWay = $serverInfo["OpenServerWay"];
|
if ($OpenServerWay == 1) {
|
if ($serverInfo["RelationServerOK"] != 1) {
|
\Logging\LogInfo("根据创角人数自动开服的还未达到条件,不处理入口开放. Channel:" . $Channel . " ServerID:" . $ServerID);
|
continue;
|
}
|
}
|
$StartDateStr = $serverInfo["StartDate"];
|
if (!$StartDateStr) {
|
\Logging\LogError("not StartDate." . print_r($serverInfo, true));
|
continue;
|
}
|
$startTime = strtotime(date("Y-m-d H:i:s", strtotime($StartDateStr)));
|
if ($curTime < $startTime) {
|
\Logging\LogInfo("time has not arrived. Channel:" . $Channel . " ServerID:" . $ServerID . " StartDate:" . $StartDateStr);
|
continue;
|
}
|
if ($serverInfo["ClearState"] != 1) {
|
\Logging\LogError("game server clearState error." . print_r($serverInfo, true));
|
continue;
|
}
|
|
// 进一步检查最新服务器开服状态是否正常
|
$checkUrl = "http://127.0.0.1/serverops/checkOpenServer.php";
|
$retStr = \CommFunc\DoGet($checkUrl, array("channel" => $Channel, "serverID" => $ServerID));
|
$checkRet = json_decode($retStr, true);
|
if (!isset($checkRet) || !array_key_exists("errMsg", $checkRet) || $checkRet["errMsg"] != "OK") {
|
\Logging\LogError("checkOpenServer error. Channel:" . $Channel . " ServerID:" . $ServerID . " ret:" . print_r($checkRet, true));
|
continue;
|
}
|
$JsonBranch = $serverInfo["JsonBranch"];
|
|
$find = array("Channel" => $Channel, "ServerID" => $ServerID);
|
$set = array(
|
"RunningStatus" => \ServerOPS\ServerRunningStatus::Light,
|
"Statue" => \ServerOPS\ServerStatue::Open,
|
"Recommend" => 1, // 新开放入口服默认推荐
|
);
|
if (\DBOper\Update("GameServers", $find, $set)) {
|
\Logging\LogInfo("set server open ok." . print_r($serverInfo, true));
|
// 其他服设置为不推荐
|
\DBOper\Update("GameServers", array(
|
"Channel" => $Channel,
|
"ServerID" => array('$nin' => array($ServerID))
|
), array("Recommend" => 0), false, false, true);
|
// 通知sp服务器开放
|
\CommFunc\DoPost("http://127.0.0.1/serverops/hy/report_server.php", array("channel" => $Channel, "server_id" => $ServerID));
|
} else {
|
\Logging\LogError("set server open error." . print_r($serverInfo, true));
|
}
|
|
$refreshBranch = array("Channel" => $Channel, "JsonBranch" => $JsonBranch);
|
if (!in_array($refreshBranch, $refrshJsonBranchArray)) {
|
array_push($refrshJsonBranchArray, $refreshBranch);
|
}
|
|
if (\CommFunc\GetConfig("x7", "JsonBranch_" . $Channel, $branch) && $branch == $JsonBranch) {
|
if (!array_key_exists($Channel, $x7notifyserverInfo)) {
|
$x7notifyserverInfo[$Channel] = array();
|
}
|
$x7notifyserverList = $x7notifyserverInfo[$Channel];
|
array_push($x7notifyserverList, $serverInfo);
|
$x7notifyserverInfo[$Channel] = $x7notifyserverList;
|
}
|
}
|
|
\Logging\LogInfo("refrshJsonBranchArray." . print_r($refrshJsonBranchArray, true));
|
foreach ($refrshJsonBranchArray as $refreshBranch) {
|
\ServerOPS\RefreshCommonServerlistJson($refreshBranch["Channel"], $refreshBranch["JsonBranch"], "System");
|
}
|
|
foreach ($x7notifyserverInfo as $Channel => $x7notifyserverList) {
|
x7_common_serverNotify($Channel, $x7notifyserverList);
|
}
|
}
|
|
function x7_common_serverNotify($appid, $notifyserverList)
|
{
|
\Logging\LogInfo("x7_common_serverNotify " . $appid . " notifyserverList:" . print_r($notifyserverList, true));
|
if (
|
!\CommFunc\GetConfig("x7", "serverNotifyUrl", $serverNotifyUrl)
|
|| !\CommFunc\GetConfig("x7", "Appkey_" . $appid, $appkey)
|
|| !\CommFunc\GetConfig("x7", "Pubkey_" . $appid, $pubkey)
|
|| !\CommFunc\GetConfig("sign", "Prikey_" . $appid, $prikey)
|
) {
|
return;
|
}
|
|
// bizParams字段 类型 必选 说明
|
// serverList Server[] 是 开服数据
|
//
|
// Server字段 类型 必选 说明
|
// serverId String 是 区服ID(区服编号)
|
// serverTime String 是 开服时间,格式使用ISO8601规范,示例:2022-06-29T15:08:44+0800
|
// serverName String 否 区服名称,可为空
|
// apiServer String 否 api区服,如不为空,角色信息查询等接口会优先使用此值作为serverId进行调用查询
|
|
$serverList = array();
|
foreach ($notifyserverList as $serverInfo) {
|
array_push(
|
$serverList,
|
array(
|
"serverId" => $serverInfo["ServerID"] . "",
|
"serverTime" => \CommFunc\StrDateTimeFormatConver($serverInfo["StartDate"], DATE_ISO8601),
|
"serverName" => $serverInfo["ServerName"] . "",
|
"apiServer" => "",
|
)
|
);
|
}
|
|
$apiMethod = "common.serverNotify";
|
$bizParams = array(
|
"serverList" => $serverList
|
);
|
send_common_to_x7($serverNotifyUrl, $bizParams, $appkey, $prikey, $pubkey, $apiMethod);
|
}
|
|
function checkOpenServerTime()
|
{
|
\Logging\LogInfo("=== Do checkOpenServerTime ===");
|
// 找出所有未清档服务器
|
\DBOper\Find("GameServers", array("ClearState" => 0), $serverList);
|
|
if (!$serverList) {
|
return;
|
}
|
|
$aheadClearMin = 15; // 提前清档时间,分钟,0点~0点x分钟,统一0点清档,不然可能导致清档后0点又过天的问题;x=提前清档时间
|
$aheadSyncMin = 15; // 相对清档时间提前同步设置清档时间,分钟
|
|
$curTime = strtotime(date("Y-m-d H:i:s", time()));
|
foreach ($serverList as $serverInfo) {
|
$Channel = $serverInfo["Channel"];
|
$ServerID = $serverInfo["ServerID"];
|
$OpenServerWay = $serverInfo["OpenServerWay"];
|
if ($OpenServerWay == 1) {
|
if ($serverInfo["RelationServerOK"] != 1) {
|
\Logging\LogInfo("根据创角人数自动开服的还未达到条件,不处理清档同步. Channel:" . $Channel . " ServerID:" . $ServerID);
|
continue;
|
}
|
}
|
$StartDate = $serverInfo["StartDate"];
|
if ($serverInfo["ClearState"] == 1 || !$StartDate) {
|
continue;
|
}
|
|
$startTime = \CommFunc\StrDateTimeToTime($StartDate);
|
$zeroTimeA = \CommFunc\StrDateTimeToTime(substr($StartDate, 0, 10) . " 00:00:00");
|
$zeroTimeB = \CommFunc\StrDateTimeToTime(substr($StartDate, 0, 10) . " 00:" . $aheadClearMin . ":00");
|
if ($startTime >= $zeroTimeA && $startTime <= $zeroTimeB) {
|
// 0点~0点x分之间默认0点清档
|
$clearOpenServerDate = substr($StartDate, 0, 10) . " 00:00:00";
|
// \Logging\LogInfo("特殊开服时间点默认0点清档 ServerID:" . $ServerID . " StartDate:" . $StartDate . " clearOpenServerDate:" . $clearOpenServerDate);
|
$clearOpenServerTime = \CommFunc\StrDateTimeToTime($clearOpenServerDate);
|
} else {
|
$clearOpenServerTime = strtotime("-" . $aheadClearMin . " minutes", \CommFunc\StrDateTimeToTime($StartDate)); # 提前清档时间
|
$clearOpenServerDate = \CommFunc\TimeToStrDateTime($clearOpenServerTime);
|
// \Logging\LogInfo("开服清档时间 ServerID:" . $ServerID . " StartDate:" . $StartDate . " clearOpenServerDate:" . $clearOpenServerDate);
|
}
|
|
$syncTime = strtotime("-" . $aheadSyncMin . " minutes", $clearOpenServerTime); # 提前开始同步清档时间
|
if ($curTime < $syncTime) {
|
// \Logging\LogInfo("未到同步清档时间 ServerID:" . $ServerID . " StartDate:" . $StartDate . " clearOpenServerDate:" . $clearOpenServerDate .
|
// " syncTime:" . \CommFunc\TimeToStrDateTime($syncTime));
|
continue;
|
}
|
|
$packData = array(
|
"OpenServerDate" => substr($clearOpenServerDate, 0, 10),
|
"OpenServerHour" => substr($clearOpenServerDate, 11, 2),
|
"OpenServerMinute" => substr($clearOpenServerDate, 14, 2),
|
);
|
$serverIDArray = array($ServerID);
|
|
\Logging\LogInfo("同步清档时间 Channel:" . $Channel . " ServerID:" . $ServerID . " StartDate:" . $StartDate
|
. " clearOpenServerDate:" . $clearOpenServerDate . " packData:" . print_r($packData, true));
|
|
$retList = \CommFunc\SendGMTToGameServers(True, "GMT_SetOpenServerTime", $packData, $Channel, $serverIDArray);
|
\Logging\LogInfo("retList:" . print_r($retList, true));
|
if (!is_array($retList)) {
|
continue;
|
}
|
$ret = json_decode($retList[$ServerID], true);
|
if (!$ret || !array_key_exists("ResultType", $ret) || $ret["ResultType"] != 0) {
|
\Logging\LogError("同步清档失败 ServerID:" . $ServerID . " ResultMsg:" . $ret["ResultMsg"]);
|
continue;
|
}
|
if (!DBOper\Update("GameServers", array("Channel" => $Channel, "ServerID" => $ServerID), array("ClearState" => 1))) {
|
continue;
|
}
|
\Logging\LogInfo("GMT_SetOpenServerTime OK. ServerID:" . $ServerID . " packData:" . print_r($packData, true));
|
}
|
}
|