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
<?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);
}