/// <summary>
        /// 完成投币任务
        /// </summary>
        public void AddCoinsForVideos()
        {
            int needCoins = GetNeedDonateCoinNum();

            if (needCoins <= 0)
            {
                return;
            }

            //投币前硬币余额
            decimal coinBalance = _coinDomainService.GetCoinBalance();

            _logger.LogInformation("【投币前余额】 : {coinBalance}", coinBalance);

            if (coinBalance <= 0)
            {
                _logger.LogInformation("因硬币余额不足,今日暂不执行投币任务");
                return;
            }

            //余额小于目标投币数,按余额投
            if (coinBalance < needCoins)
            {
                int.TryParse(decimal.Truncate(coinBalance).ToString(), out needCoins);
                _logger.LogInformation("因硬币余额不足,目标投币数调整为: {needCoins}", needCoins);
            }

            int success  = 0;
            int tryCount = 10;

            for (int i = 1; i <= tryCount && success < needCoins; i++)
            {
                _logger.LogDebug("开始尝试第{num}次", i);

                Tuple <string, string> video = TryGetCanDonatedVideo();
                if (video == null)
                {
                    continue;
                }

                _logger.LogInformation("【视频】{title}", video.Item2);

                bool re = DoAddCoinForVideo(video.Item1, 1, _dailyTaskOptions.SelectLike, video.Item2);
                if (re)
                {
                    success++;
                }
            }

            if (success == needCoins)
            {
                _logger.LogInformation("投币任务完成");
            }
            else
            {
                _logger.LogInformation("投币尝试超过10次,已终止");
            }

            _logger.LogInformation("【硬币余额】{coin}", _accountApi.GetCoinBalance().GetAwaiter().GetResult().Data.Money ?? 0);
        }
        /// <summary>
        /// 完成投币任务
        /// </summary>
        public void AddCoinsForVideo()
        {
            int needCoins = GetNeedDonateCoinNum();

            if (needCoins <= 0)
            {
                return;
            }

            //投币前硬币余额
            decimal coinBalance = _coinDomainService.GetCoinBalance();

            _logger.LogInformation("投币前余额为 : {coinBalance}", coinBalance);

            if (coinBalance <= 0)
            {
                _logger.LogInformation("因硬币余额不足,今日暂不执行投币任务");
                return;
            }

            //余额小于目标投币数,按余额投
            if (coinBalance < needCoins)
            {
                int.TryParse(decimal.Truncate(coinBalance).ToString(), out needCoins);
                _logger.LogInformation("因硬币余额不足,目标投币数调整为: {needCoins}", needCoins);
            }

            for (int i = 0; i < needCoins; i++)
            {
                Tuple <string, string> video = TryGetCanDonatedVideo();
                if (video == null)
                {
                    continue;
                }

                _logger.LogDebug("正在为视频“{title}”投币", video.Item2);

                DoAddCoinForVideo(video.Item1, 1, _dailyTaskOptions.SelectLike, video.Item2);
            }

            _logger.LogInformation("投币任务完成,余额为: {money}", _accountApi.GetCoinBalance().Result.Data.Money);
        }
Example #3
0
        /// <summary>
        /// 获取账户硬币余额
        /// </summary>
        /// <returns></returns>
        public decimal GetCoinBalance()
        {
            var response = _accountApi.GetCoinBalance().Result;

            return(response.Data.Money ?? 0);
        }
        /// <summary>
        /// 投币
        /// </summary>
        public void AddCoinsForVideo()
        {
            int needCoins = GetNeedDonateCoins(out int alreadyCoins, out int targetCoins);

            _logger.LogInformation("今日已投{already}枚硬币,目标是投{target}枚硬币", alreadyCoins, targetCoins);

            if (needCoins <= 0)
            {
                _logger.LogInformation("已完成投币任务,今天不需要再投啦");
                return;
            }

            _logger.LogInformation("还需再投{need}枚硬币", needCoins);

            //投币前硬币余额
            var coinBalance = _coinDomainService.GetCoinBalance();

            _logger.LogInformation("投币前余额为 : {coinBalance}", coinBalance);

            if (coinBalance <= 0)
            {
                _logger.LogInformation("因硬币余额不足,今日暂不执行投币任务");
                return;
            }

            //余额小于目标投币数,按余额投
            if (coinBalance < needCoins)
            {
                int.TryParse(decimal.Truncate(coinBalance).ToString(), out needCoins);
                _logger.LogInformation("因硬币余额不足,目标投币数调整为: {needCoins}", needCoins);
            }

            int successCoins = 0;
            int tryCount     = 0;//投币最多操作数 解决csrf校验失败时死循环的问题

            while (successCoins < needCoins)
            {
                tryCount++;

                var video = TryGetNotDonatedVideo();
                if (video == null)
                {
                    continue;
                }

                _logger.LogDebug("正在为“{title}”投币", video.Item2);

                bool isSuccess = AddCoinsForVideo(video.Item1, 1, _dailyTaskOptions.SelectLike, video.Item2);
                if (isSuccess)
                {
                    successCoins++;
                }

                if (tryCount > 10)
                {
                    _logger.LogInformation("尝试投币次数超过10次,投币任务终止");
                    break;
                }
            }

            _logger.LogInformation("投币任务完成,余额为: " + _accountApi.GetCoinBalance().Result.Data.Money);
        }