<?php

namespace loyalsoft;

/**
 * 玩家基本信息
 * @author gwang(wanggangzero@qq.com)
 */
class Info_UserBase extends Object_ext {

    /**
     * 第一次登录时间
     * @var int
     */
    public $firstLogin;

    /**
     * 上次登录时间
     * @var int
     */
    public $lastLogin;

    /**
     * 上次活动时间
     * @var int
     */
    public $lastSaveTs;

    /**
     * @var int 当前等级
     */
    public $level;

    /**
     * @var string 昵称
     */
    public $name;

    /**
     * @var string 头像
     */
    public $headImg;

    /**
     * @var int 头像框Id
     */
    public $imgBorderId = 0;

    /**
     * @var string 形象(龙骨)
     */
    public $img;

    /**
     * @var int 金币
     */
    public $gold;

    /**
     * 钻石
     * @var int
     */
    public $cash;

    /**
     * 额外体力值
     * @var int
     */
    public $tili = 0;

    /**
     * @var int 体力时间戳
     */
    public $tili_ts = 0;

    /**
     * 当前经验
     */
    public $xp = 0;

    /**
     * 升级经验
     * @var type
     */
    public $maxXp = 0;

    /**
     * @var int 历史充值总金额(单位:分)
     */
    public $charge_amt = 0;

    /**
     * 新解锁的头像框
     * @var type
     */
    #[ArrayType]
    public $headImgFrameList = array();

    /**
     * 片头动画
     * @var type
     */
    public $animation = 0;

    /**
     * 首充奖励是否领取过首充奖励是否领取过
     * @var type
     */
    public $fRechargePriceReceived = 0;

    /**
     * 洗练石
     * @var type
     */
    public $xilianStone = 0;

    public function initialize() {
//        my_Assert(GameConfig::primordial_data(), "找不到账号初始化数据");         # 防御
        $this->gold = glc()->Init_Player_gold;
        $this->cash = glc()->Init_Player_cash;
        $this->xp = 0;
            
        $this->maxXp = GameConfig::player_level_getItem(2)->xp_need;

        $this->level = 1;
        $this->name = '';

        $this->img = "";

        $heroMo = GameConfig::hero_getItem(1);
        $this->headImg = $heroMo->img;
    }

    // <editor-fold defaultstate="collapsed" desc="  方法  ">
    //

    /**
     * 给玩家增加体力
     * @param int $amt
     */
    function Add_tili($amt) {
        my_Assert($amt >= 0, "体力amt小于0");
        //ActiveProc::ChangeTili($amt);
        TaskProc::OnAccumulateTiliNum($amt);
        for ($i = 0; $i < $amt; $i++) {
            if ((now() - $this->tili_ts) / glc()->tili_RecoverTS < glc()->tili_RecoverrMax) {
                $this->tili_ts -= glc()->tili_RecoverTS;
            } else {
                $this->tili++;
            }
        }
    }

    /**
     * 检查并修改体力ts的最小值.
     */
    function Reset_tilits() {
        $min = now() - glc()->tili_RecoverTS * glc()->tili_RecoverrMax;
        if ($this->tili_ts < $min) {
            $this->tili_ts = $min;
        }
    }

    /**
     * 扣除玩家体力
     * @param int $amt
     * @return bool 成功与否
     */
    function Consume_tili($amt) {
        if ($amt >= 0) {
            $cpt = glc()->tili_RecoverTS;
            if ($this->tili > 0) {
                if ($this->tili - $amt >= 0) {
                    $this->tili -= $amt;
                    return true;
                } else {
                    $amt -= $this->tili;
                    $this->tili = 0;
                    $this->tili_ts += $amt * $cpt;
                    return true;
                }
            } else if ($this->tili_ts + $amt * $cpt < now()) {
                $this->tili_ts += $amt * $cpt;
                return true;
            }
        }
        return false;
    }

    /**
     * @return int 计算当前可用体力值
     */
    function CurTili() {
        $rec = (now() - $this->tili_ts) / glc()->tili_RecoverTS;
        $rec = glc()->tili_RecoverrMax < $rec ? glc()->tili_RecoverrMax : $rec;
        $t = $this->tili + $rec;
        return $t;
    }

    /**
     * 用户获得金币
     * @param int $amt
     */
    function Add_Gold($amt, $mask = 0) {
        my_Assert($amt >= 0, "参数为负");
        TaskProc::OnAccumulateGoldNum($amt);
        $this->gold += $amt;
    }

    /**
     * 扣除玩家金币
     * @param int $amt
     * @return boolean true:成功, false:金币不足
     */
    function Consume_Gold($amt) {
        if ($amt > 0) {
            if ($this->gold - $amt >= 0) {
                $this->gold -= $amt;
                return true;
            }
        }
        return false;
    }

    /**
     * 增加用户钻石
     * @param type $amt
     */
    function Add_Cash($amt) {
        my_Assert($amt >= 0, "amt值为负");
        $this->cash += $amt;
    }

    /**
     * 扣除玩家钻石
     * @param int $amt
     * @return bool 成功与否
     */
    function Consume_Cash($amt) {
        if ($amt >= 0) {
            if ($this->cash - $amt >= 0) {
                $this->cash -= $amt;
                return true;
            }
        }

        TaskProc::OnConsumeCashNum($amt);
        return false;
    }

    /**
     * 用户获得经验值
     * @param int $amt
     */
    function Add_Exp($amt) {
        my_Assert($amt >= 0, "amt值为负");
        $cfgLVs = GameConfig::player_level();

        $this->xp += $amt;
        $initLevel = $curLevel = $this->level;
        $nextLevel = $curLevel + 1;
        while ($this->xp >= $cfgLVs->$nextLevel->xp_need) {                     # 超过升级所需经验
            if ($this->level < glc()->Game_MaxPlayerLevel) {                    # 如果未到达最大等级
                $this->level++;
                $this->xp -= $cfgLVs->$nextLevel->xp_need;
                $curLevel = $this->level;
                $nextLevel = $curLevel + 1;
                my_Assert(CommUtil::isPropertyExists($cfgLVs, $nextLevel), ErrCode::err_const_no); // "取英雄升级常量数据失败." . $nextLevel . "级");
                $this->maxXp = $cfgLVs->$nextLevel->xp_need;
//                StatProc::UserLevel($nowlv);                                  # 等级统计
            } else {                                                            # 如果已到达最大等级则仅补齐缺失的经验即可
                $this->xp = $this->maxXp;                                       # 经验不能超过最大值
                break;
            }
        }
    }

    //
// </editor-fold>
}
