<?php

namespace loyalsoft;

/**
 *  玩家竞技场数据
 * @version
 *          2.0.0 改自原PVP数据(内容基本全换). 2020.5.7 -- gwang
 *          1.0.0 Created at 2017-7-4. by --gwang
 * @author gwang (mail@wanggangzero.cn)
 * @copyright ? 2017-7-4, SJZ LoyalSoft Corporation & gwang. All rights reserved.
 */
class Info_UserPVP extends Object_ext {

    /**
     * @var array[string] 每日/当天已挑战对手记录(有想法设定尽量不重复当天已经刷到过的对手)
     */
    public $dailyMatchRecord = array();

    /**
     * @var object 防守阵容 
     */
    public $defTeam = array("0", "0", "0");

    /**
     * @var int 竞技币
     */
    public $pvpCoins = 0;

    /**
     * @var int 竞技场挑战票 
     */
    public $fightTicket = 0;

    /**
     * @var 下次刷新时间
     */
    public $fightTicketRefreshTs = 0;

    /**
     * @var int 每天有3次免费挑战次数,每天8天重置 
     */
    public $freeFightTickets = 0;

    /**
     * @var int 挑战记录最后拉取时间戳(晚于此时间戳的记录为新记录,显示小红点)
     */
    public $lastCheckDefLog_ts = 0;

    /**
     * @var type 当前竞技商店内道具列表(子结构可以记录售罄状态)
     */
    public $curShopItems;

    /**
     * @var int 商店下次刷新的时间戳(24小时自动刷新, 提前刷新扣钻)
     */
    public $shopRefreshTs = 0;

    /**
     * @var 今日未发放奖励(tsDay < today =>发放奖励并同步到today)
     */
    public $haventReward_tsDay = 0;

    /**
     * @var 赛季未发放奖励(seasonId < curSeason =>发放奖励并同步到curSeason)
     */
    public $haventReward_season = 0;

    /**
     * @var int 总胜利场次
     */
    public $totalWin = 0;

    public function refreshDailyData() {
        // 刷新免费挑战次数
        if (tsDay($this->fightTicketRefreshTs) <= tsDay()) {                    # 刷新时间<=今天,该刷新了
            $this->freeFightTickets = 3;                                        # 重置为3, 此处的3可以改为读取配置数据
            $this->fightTicketRefreshTs = now() + 86400;                        # 刷新时间设置为明天 
        }
    }

    /**
     * 发送上榜奖励(昨天的奖励,以及上一赛季的奖励)
     * @param type $zoneid
     * @param type $uid
     * @param type $seasonId
     */
    public function sendRewardEmail($zoneid, $uid, $seasonId) {
        $key = MemKey_GameRun::Game_PVPScoreByZoneSeason_zset($zoneid, $seasonId); # 积分总榜
        if ($this->haventReward_season >= 0 && $this->haventReward_season < $seasonId) { # 尚未发放上赛季奖励
            $haventKey = MemKey_GameRun::Game_PVPScoreByZoneSeason_zset($zoneid, $this->haventReward_season);
            // todo:发放上赛季奖励邮件
            $rank = PVPProc::_getRank_by_uid($uid, $haventKey);                    # 查询上赛季排名
            if ($rank <= PVPProc::pvpMaxRank) {                                    # 防御未上榜
                foreach (GameConfig::pvp_rankreward() as $cfg) {
                    isEditor() and $cfg = new \sm_pvp_rankreward();
                    if ($rank >= $cfg->minRank && $rank <= $cfg->maxRank) {     # 找到对应的名次段
                        EmailProc::SendPvpRankReward_Season($zoneid, $uid, $rank); # 发放奖励邮件(竞技币)
                    }
                }
                UserProc::updateUserInfo();
            }
        }
        $this->haventReward_season = $seasonId;                                  # 更新待发奖赛季
        if ($this->haventReward_tsDay < tsDay() - 7) {
            $this->haventReward_tsDay = tsDay() - 7;
        }
        for ($this->haventReward_tsDay; $this->haventReward_tsDay < tsDay(); $this->haventReward_tsDay++) {
//        if ($this->haventReward_tsDay == tsDay() - 1) {                          # 尚未发放昨天奖励
            $haventKey_day = MemKey_GameRun::Game_PVPScoreByZone_zset_Day($zoneid, $this->haventReward_tsDay);
            if (!gMem()->exists($haventKey_day) && $this->haventReward_tsDay == tsDay() - 1) { # 昨天的积分记录不存在
                gMem()->zcopy($key, $haventKey_day);                            # 复制一份当前积分作为昨天的截止积分榜
            } else {
                // 不是昨天登录的, 且没有对应的数据 就不再复制当前数据了.直接未上榜处理
            }
            $rank = PVPProc::_getRank_by_uid($uid, $haventKey_day);                # 查询上一天排名
            if ($rank <= PVPProc::pvpMaxRank) {                                    # 防御未上榜
                foreach (GameConfig::pvp_rankreward() as $cfg) {
                    isEditor() and $cfg = new \sm_pvp_rankreward();
                    if ($rank >= $cfg->minRank && $rank <= $cfg->maxRank) {     # 找到对应的名次段
                        EmailProc::SendPvpRankReward_Lastday($zoneid, $uid, $rank); # 发放奖励邮件(竞技币)
                    }
                }
                UserProc::updateUserInfo();
            }
//        }
        }
        $this->haventReward_tsDay = tsDay();                                     # 更新待发放奖励日期
    }

    public function __construct($arg = null) {
        parent::__construct($arg);
        if (null == $this->curShopItems) {
            $this->curShopItems = GameConfig::pvp_shop();
        }
        if ($this->defTeam[0] <= 0) {
            $one = 1;
            if (isset(req()->userInfo)) {
                $teamCfg = JsonUtil::decode(req()->userInfo->game->heroTeamConfig);
                $teams = $teamCfg->teamDic;
                if (isset($teams->$one->heros)) {
                    $this->defTeam = array_slice($teams->$one->heros, 0, 3);
                }
            }
        }
    }

}
