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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
include_once '../../../Common/MongoDB7.php';
include_once "errorCode.php";
 
// 游戏玩家
class GamePlayer
{
 
    private $openid;
    private $dbconfig;
    private $dbPlayerOpt;
 
    public function __construct($openid, $dbconfig)
    {
        $this->openid = $openid;
        $this->dbconfig = $dbconfig;
        $this->dbPlayerOpt = new \MongoDB7\MongoDb("Player", $dbconfig);
    }
 
    function onPlayerLogin()
    {
        \Logging\LogInfo('onPlayerLogin: openid=' . $this->openid);
        $playerData = array();
        $where = ['openid' => $this->openid];
        $options = [
            'projection' => ['_id' => 0],
        ];
        $result = $this->dbPlayerOpt->query($where, $options);
        if ($result) {
            $playerData = $result[0];
            \Logging\LogInfo('玩家登录: ' . print_r($playerData, true));
        } else {
            // 插入新数据
            $insertData = array(
                "openid" => $this->openid,
                "nickName" => "",
                "gold" => 0,
                "coin" => 0,
                "weekScore" => 0,
                "highestScore" => 0,
                "missionNum" => 0,
            );
            if ($this->dbPlayerOpt->insert($insertData)) {
                $playerData = $insertData;
                $playerData["isNew"] = true;
                \Logging\LogInfo('新增登录玩家成功: ' . print_r($playerData, true));
            } else {
                \Logging\LogInfo('新增登录玩家失败: ' . print_r($playerData, true));
            }
        }
        return $playerData;
    }
 
    function saveSessionkey($session_key)
    {
        $dbOpt = new \MongoDB7\MongoDb("session", $this->dbconfig);
        $where = ['openid' => $this->openid];
        $result = $dbOpt->query($where);
        if ($result) {
            $update = array("session_key" => $session_key);
            $modCount = $dbOpt->update($where, $update);
            \Logging\LogInfo('update session_key: ' . $modCount);
        } else {
            // 插入新数据
            $insertData = array("openid" => $this->openid, "session_key" => $session_key);
            if ($dbOpt->insert($insertData)) {
                \Logging\LogInfo('insert session_key ok!: ');
            } else {
                \Logging\LogInfo('insert session_key fail!');
            }
        }
    }
 
    /**增加钻石 */
    function AddGold($addGold)
    {
        $addGold = (int) $addGold;
        $where = ['openid' => $this->openid];
        $options = [
            'projection' => ['_id' => 0, 'gold' => 1],
        ];
        $result = $this->dbPlayerOpt->query($where, $options);
        if (!$result) {
            return ErrorCode::retErr(ErrorCode::$NoPlayer, "openid=" . $this->openid);
        }
        $curGold = $result[0]["gold"];
 
        $update = ['gold' => $curGold + $addGold];
        $modCount = $this->dbPlayerOpt->update($where, $update);
        if ($modCount) {
            return ErrorCode::retOK($update, ['addGold' => $addGold]);
        }
        return ErrorCode::retOK();
    }
 
    /**增加金币 */
    function AddCoin($value)
    {
        $value = (int) $value;
    }
 
    /**设置货币 */
    function SetMoney($type, $value)
    {
        $value = (int) $value;
        $where = ['openid' => $this->openid];
        $options = [
            'projection' => ['_id' => 0, 'gold' => 1, 'coin' => 1],
        ];
        $result = $this->dbPlayerOpt->query($where, $options);
        if (!$result) {
            return ErrorCode::retErr(ErrorCode::$NoPlayer, "openid=" . $this->openid);
        }
 
        $update = [];
        if ($type == "1") {
            $update = ["gold" => $value];
        } else if ($type == "3") {
            $update = ["coin" => $value];
        }
        $modCount = $this->dbPlayerOpt->update($where, $update);
        if ($modCount) {
            return ErrorCode::retOK($update);
        }
        return ErrorCode::retOK();
    }
 
    /**更新字段值 */
    function UpdateFieldValue($fieldName, $value)
    {
        $where = ['openid' => $this->openid];
        $options = [
            'projection' => ['_id' => 0, 'openid' => 1],
        ];
        $result = $this->dbPlayerOpt->query($where, $options);
        if (!$result) {
            return ErrorCode::retErr(ErrorCode::$NoPlayer, "openid=" . $this->openid);
        }
 
        $update = [$fieldName => $value];
        $modCount = $this->dbPlayerOpt->update($where, $update);
        if ($modCount) {
            return ErrorCode::retOK($update);
        }
        return ErrorCode::retOK();
    }
}