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