openid = $openid; $this->dbconfig = $dbconfig; $this->dbPlayerOpt = new \MongoDB7\MongoDb("Player", $dbconfig); } function onPlayerLogin($syncData) { \Logging\LogInfo('onPlayerLogin: openid=' . $this->openid); $playerData = array(); $where = ['openid' => $this->openid]; $options = [ 'projection' => ['_id' => 0], ]; $result = $this->dbPlayerOpt->query($where, $options); if ($result) { $retData = $result[0]; \Logging\LogInfo('玩家登录: ' . print_r($retData, true)); if ($syncData) { $playerData = $retData; } else { $playerData["openid"] = $this->openid; if (array_key_exists("syncDataTime", $retData)) { $playerData["syncDataTime"] = $retData["syncDataTime"]; } if (array_key_exists("deviceCode", $retData)) { $playerData["deviceCode"] = $retData["deviceCode"]; } } } else { // 插入新数据 $insertData = array( "openid" => $this->openid, "createTime" => date("Y-m-d H:i:s", time()), ); 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 getSyncPlayerData() { $playerData = array(); $where = ['openid' => $this->openid]; $options = [ 'projection' => ['_id' => 0], ]; $result = $this->dbPlayerOpt->query($where, $options); \Logging\LogInfo('获取同步到前端玩家数据: ' . print_r($where, true)); \Logging\LogInfo('result: ' . print_r($result, true)); if ($result) { $playerData = $result[0]; } 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(); } }