| <?php | 
| include_once "../../../Common/Logging.php"; | 
| include_once '../../../Common/MongoDB7.php'; | 
|   | 
| ignore_user_abort(); //关掉浏览器,PHP脚本也可以继续执行. | 
| set_time_limit(0); // 通过set_time_limit(0)可以让程序无限制的执行下去 | 
| \Logging\CreateLogging("serverLoop.php"); | 
|   | 
| $interval = 0; | 
| do { | 
|   | 
|     $run = include 'serverLoopSwitch.php'; | 
|     if (!$run) die('process abort'); | 
|   | 
|     $curTime = strtotime(date("Y-m-d H:i:s", time())); | 
|     $nextMinute = strtotime(date("Y-m-d H:i", strtotime("+1 minutes"))); // 下个整分 | 
|   | 
|     $interval = $nextMinute - $curTime; | 
|     // \Logging\LogInfo("curTime: " . $curTime); | 
|     // \Logging\LogInfo("nextMinute: " . $nextMinute); | 
|     // \Logging\LogInfo("interval: " . $interval); | 
|   | 
|     sleep($interval); | 
|   | 
|     DoServerLoop(); | 
|   | 
|     // $run = include 'serverLoopSwitch.php'; | 
|     // if (!$run) die('process abort'); | 
| } while (true); | 
|   | 
| function DoServerLoop() | 
| { | 
|     $curyweek = date("W", time()); | 
|     $curDate = getdate(); | 
|     $dateStr = $curDate['year'] . "-" . $curDate['mon'] . "-" . $curDate['mday']; | 
|     $hourStr = $dateStr . " " . $curDate['hours']; | 
|     $minuteStr = $hourStr . ":" . $curDate['minutes']; | 
|     // \Logging\LogInfo("-------- todo server loop logic -------- "); | 
|     // \Logging\LogInfo("  dateStr: " . $dateStr); | 
|     // \Logging\LogInfo("  hourStr: " . $hourStr); | 
|     // \Logging\LogInfo("minuteStr: " . $minuteStr); | 
|   | 
|     $gamename = 'mandown'; | 
|     $gameconfig = parse_ini_file("config/" . $gamename . ".ini", true); | 
|   | 
|     $eventKey = 'serverLoopInfo'; | 
|     $insert = false; | 
|     $isUpdate = false; | 
|     $serverLoopInfo = []; | 
|     $dbOpt = new \MongoDB7\MongoDb("ServerEvent", $gameconfig["db"]); | 
|     $where = ['key' => $eventKey]; | 
|     $result = $dbOpt->query($where); | 
|     if ($result) { | 
|         $serverLoopInfo = $result[0]['value']; | 
|     } else { | 
|         $insert = true; | 
|         $serverLoopInfo = ['yweek' => 0, 'date' => '', 'hour' => '', 'minute' => '']; | 
|     } | 
|   | 
|     if ($serverLoopInfo['yweek'] != $curyweek) { | 
|         OnWeek($curyweek, $gameconfig["db"]); | 
|         $isUpdate = true; | 
|         $serverLoopInfo['yweek'] = $curyweek; | 
|     } | 
|   | 
|     if ($serverLoopInfo['date'] != $dateStr) { | 
|         OnDay($dateStr, $gameconfig["db"]); | 
|         $isUpdate = true; | 
|         $serverLoopInfo['date'] = $dateStr; | 
|     } | 
|   | 
|     if ($serverLoopInfo['hour'] != $hourStr) { | 
|         OnHour($hourStr, $gameconfig["db"]); | 
|         $isUpdate = true; | 
|         $serverLoopInfo['hour'] = $hourStr; | 
|     } | 
|   | 
|     if ($serverLoopInfo['minute'] != $minuteStr) { | 
|         OnMinute($minuteStr, $gameconfig["db"]); | 
|         $isUpdate = true; | 
|         $serverLoopInfo['minute'] = $minuteStr; | 
|     } | 
|   | 
|     if ($isUpdate) { | 
|         if ($insert) { | 
|             //\Logging\LogInfo("insert serverLoopInfo: " . print_r($serverLoopInfo, true)); | 
|             $dbOpt->insert(['key' => $eventKey, 'value' => $serverLoopInfo]); | 
|         } else { | 
|             //\Logging\LogInfo("update serverLoopInfo: " . print_r($serverLoopInfo, true)); | 
|             $dbOpt->update(['key' => $eventKey], ['value' => $serverLoopInfo]); | 
|         } | 
|     } | 
| } | 
|   | 
| function OnWeek($yweek, $dbconfig) | 
| { | 
|     \Logging\LogInfo("Todo OnWeek: " . $yweek); | 
|     ResetWeekScore($dbconfig); | 
| } | 
|   | 
| function OnDay($dateStr, $dbconfig) | 
| { | 
|     \Logging\LogInfo("Todo OnDay: " . $dateStr); | 
| } | 
|   | 
| function OnHour($hourStr, $dbconfig) | 
| { | 
|     \Logging\LogInfo("Todo OnHour: " . $hourStr); | 
| } | 
|   | 
| function OnMinute($minuteStr, $dbconfig) | 
| { | 
|     \Logging\LogInfo("Todo OnMinute: " . $minuteStr); | 
| } | 
|   | 
| function ResetWeekScore($dbconfig) | 
| { | 
|     $dbOpt = new \MongoDB7\MongoDb("Player", $dbconfig); | 
|     $modCount = $dbOpt->update([], ['weekScore' => 0]); | 
|     \Logging\LogInfo("ResetWeekScore modCount: " . $modCount); | 
| } |