hxp
2025-06-09 6c3f6335c70859ded94a1ad8d218acb0ac34239c
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
<?php
include_once "/Common/Logging.php";
include_once "/Common/CommFunc.php";
include_once "/Account/User.php";
include_once '/db/RedisOper.php';
 
\Logging\CreateLogging("chatmonitorserver2.php");
\Logging\LogInfo("_POST:" . print_r($_POST, true));
$spid = $_POST["spid"];
 
$interfaceConfig = parse_ini_file("/InterfaceConfig.php", true);
$GameName = $interfaceConfig["ServerInfo"]["GameName"];
$CacheChatCount = $interfaceConfig["Chatmonitor"]["CacheChatCount"];
 
$redisKey = \RedisOper\GetFCRedisKey($GameName, "Chatmonitor", $spid);
 
$retArray = array();
if ($_POST["AccountID"]) {
    $chat["Time"];
    $doCountMax = 12; // 执行取数据次数
    $perCountSleep = 5; // 每次执行后休眠x秒
    $sleepTime = $doCountMax * $perCountSleep;
    if ($sleepTime >= 30) {
        set_time_limit($sleepTime + 30); // 限制执行时长秒 30秒 + 休眠时长
    }
 
    // 直到取到有新数据
    $doCount = 0;
    while ($doCount < $doCountMax && count($retArray) <= 0) {
        $doCount += 1;
        if ($doCount > 1) {
            sleep($perCountSleep);
        }
        // 取最后一条之后的数据
        $startIndex = 0;
        $stopIndex = -1;
        $perGetCount = 10; // 每次一次性取几条数据
        $loopCount = $CacheChatCount / $perGetCount;
        // \Logging\LogInfo("doCount:" . $doCount . " CacheChatCount :" . $CacheChatCount . " loopCount:" . $loopCount);
        $isEnd = false;
        while (!$isEnd && $loopCount >= 0) {
            $loopCount -= 1;
            $startIndex -= $perGetCount;
            // \Logging\LogInfo("i :" . $i . " index:" . $startIndex . " ~ " . $stopIndex);
            // 从后往前读
            if (!\RedisOper\ListRange($redisKey, $ret, $startIndex, $stopIndex)) {
                break;
            }
            if (isset($ret) && count($ret) > 0) {
                // \Logging\LogInfo("retCount:" . count($ret));
                // 倒序遍历
                for ($j = count($ret) - 1; $j >= 0; $j--) {
                    $chat = $ret[$j];
                    if ($chat["Time"] < $_POST["Time"] && $_POST["Time"] && $_POST["Time"] != "undefined") {
                        $isEnd = true;
                        break;
                    }
                    if ($chat["AccountID"] == $_POST['AccountID'] && $chat["Time"] == $_POST['Time'] && $chat["Content"] == $_POST['Content']) {
                        $isEnd = true;
                        break;
                    }
                    array_splice($retArray, 0, 0, array($chat));
                    // \Logging\LogInfo("array_splice count:" . count($retArray) . print_r($chat, true));
                }
            } else {
                $isEnd = true;
            }
            $stopIndex = $startIndex - 1;
        }
    }
} else {
    // 全部
    if (!\RedisOper\ListRange($redisKey, $retArray, 0, -1)) {
        exit;
    }
}
if (!isset($retArray) || count($retArray) <= 0) {
    exit;
}
 
$msg = array("MsgType" => "CHAT", "Msg" => $retArray);
echo json_encode($msg);