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
83
<?php
 
/**
 * error code 说明.
 * <ul>
 
 *    <li>-41001: encodingAesKey 非法</li>
 *    <li>-41003: aes 解密失败</li>
 *    <li>-41004: 解密后得到的buffer非法</li>
 *    <li>-41005: base64加密失败</li>
 *    <li>-41016: base64解密失败</li>
 * </ul>
 */
class ErrorCode
{
    public static $OK = 0;
    public static $IllegalAesKey = -41001;
    public static $IllegalIv = -41002;
    public static $IllegalBuffer = -41003;
    public static $DecodeBase64Error = -41004;
 
    public static $GameServerFail = -1; // 游戏服务器失败
    public static $ParamErr = -98; // 参数异常
    public static $NoPlayer = -99; // 找不到玩家
    private static $errcodeMsg = null;
    /**
     * 获取错误码对应的错误信息说明
     */
    private static function getErrcodeMsg($errcode)
    {
        if (ErrorCode::$errcodeMsg == null) {
            ErrorCode::$errcodeMsg = array(
                ErrorCode::$OK => "ok",
                ErrorCode::$GameServerFail => "game server fail",
                ErrorCode::$ParamErr => "param error",
                ErrorCode::$NoPlayer => "no player",
            );
        }
        if (array_key_exists($errcode, ErrorCode::$errcodeMsg)) {
            return ErrorCode::$errcodeMsg[$errcode];
        }
        return $errcode . "";
    }
 
    /**
     * 游戏服务器返回成功信息
     * @param array $playerData 通知前端需要更新的玩家数据
     * @param array $optData 通知前端本次操作数据,业务逻辑自行定义
     */
    public static function retOK($playerData = [], $optData = [])
    {
        // gserrcode 游戏服务器处理结果
        $code = ErrorCode::$OK;
        $ret = ["gserrcode" => $code, "gserrmsg" => ErrorCode::getErrcodeMsg($code)];
 
        // 同步更新的玩家数据
        if ($playerData) {
            $ret["playerData"] = $playerData;
        }
 
        // 操作数据
        if ($optData) {
            $ret["optData"] = $optData;
        }
        \Logging\LogInfo('retOK: ' . print_r($ret, true));
        return $ret;
    }
 
    /**
     * 游戏服务器返回错误信息
     * @param mixed $code 错误码
     * @param string $errInfo 错误附加信息,自定义内容,会添加在errmsg后
     */
    public static function retErr($code, $errInfo = "")
    {
        if ($errInfo) {
            $errInfo = " => " . $errInfo;
        }
        $ret = ["gserrcode" => $code, "gserrmsg" => ErrorCode::getErrcodeMsg($code) . $errInfo];
        \Logging\LogInfo('retErr: ' . print_r($ret, true));
        return $ret;
    }
}