コード例 #1
0
        private Campaign_ShakingLotteryGiftPool GetGiftPool(Guid campaignId, Guid?periodId)
        {
            Campaign_ShakingLotteryGiftPool pool = null;

            if (_giftPool.ContainsKey(campaignId) == false)
            {
                lock (_giftPoolLockObj)
                {
                    if (_giftPool.ContainsKey(campaignId) == false)
                    {
                        pool = new Campaign_ShakingLotteryGiftPool(campaignId, periodId);
                        _giftPool.Add(campaignId, pool);
                    }
                }
            }

            if (pool == null)
            {
                pool = _giftPool[campaignId];
            }

            if (pool.PeriodId != periodId)
            {
                lock (_giftPoolLockObj)
                {
                    if (pool.PeriodId != periodId)
                    {
                        pool = new Campaign_ShakingLotteryGiftPool(campaignId, periodId);
                        _giftPool[campaignId] = pool;
                    }
                }
            }

            if ((DateTime.Now - pool.CreatedTime).TotalMinutes > 5)
            {
                lock (pool)
                {
                    if ((DateTime.Now - pool.CreatedTime).TotalMinutes > 5)
                    {
                        pool.LoadGiftList();
                    }
                }
            }

            return(pool);
        }
コード例 #2
0
        /// <summary>
        /// 摇奖
        /// </summary>
        /// <param name="campaignId"></param>
        /// <param name="memberId"></param>
        /// <param name="domain"></param>
        /// <returns></returns>
        public NormalResult <Campaign_ShakingLotteryGiftEntity> Shake(Guid campaignId, Guid?periodId, Guid memberId, DomainContext domain)
        {
            NormalResult <Campaign_ShakingLotteryGiftEntity> result = new NormalResult <Campaign_ShakingLotteryGiftEntity>();

            CampaignEntity campaign = _campaignManager.GetCampaign(campaignId);

            if (campaign == null)
            {
                result.Message = "指定的活动不存在。";
                result.Success = false;
                return(result);
            }

            //状态是否进行中
            switch (campaign.Status)
            {
            case EnumCampaignStatus.Preparatory:
                result.Message = "活动尚未开始。";
                result.Success = false;
                return(result);

            case EnumCampaignStatus.End:
                result.Message = "活动已结束。";
                result.Success = false;
                return(result);
            }

            Campaign_ShakingLotteryEntity shakingLottery = Get(campaignId);

            if (shakingLottery == null)
            {
                result.Message = "指定的活动不存在。";
                result.Success = false;
                return(result);
            }

            if (shakingLottery.Mode == EnumCampaign_ShakingLotteryMode.Period)
            {
                if (shakingLottery.Period.HasValue == false)
                {
                    result.Message = "摇奖尚未开始~~";
                    result.Success = false;
                    return(result);
                }

                //直接参与最新一期
                periodId = shakingLottery.Period;
            }
            else
            {
                if (shakingLottery.Started == false)
                {
                    result.Message = "摇奖尚未开始~~";
                    result.Success = false;
                    return(result);
                }
            }

            //判断是否摇过了
            int playedTimes = GetMemberPlayedTimes(campaignId, periodId, memberId);

            if (playedTimes >= shakingLottery.ChanceTimes)
            {
                if (shakingLottery.Mode == EnumCampaign_ShakingLotteryMode.Period)
                {
                    result.Message = "您已经用本轮完全部参与机会~";
                }
                else
                {
                    result.Message = "您已经用完全部参与机会~";
                }

                result.Success = false;
                return(result);
            }
            //////

            Campaign_ShakingLotteryLogEntity log = new Campaign_ShakingLotteryLogEntity();

            log.CampaignId = campaignId;
            log.Period     = periodId;
            log.Domain     = domain.Domain.Id;
            log.AppId      = domain.AppId;
            log.Member     = memberId;
            log.Time       = DateTime.Now;

            //判断是否已经中奖了
            if (GetMemberObtainedGiftList(campaignId, memberId).Count > 0)
            {
                //同一个摇奖活动,只允许中一个奖,但是还有次数或新的周期开始时
                //还是允许他继续摇,假装还能参与
                //result.Message = "您已中奖~";
                //扣掉摇奖机会
                log.Win = false;
                _campaignManager.DataBase.Insert(log);

                result.Message = "遗憾您没有摇中,请再接再厉~";
                result.Success = false;
                return(result);
            }

            Campaign_ShakingLotteryGiftPool pool = GetGiftPool(campaignId, periodId);

            if (pool == null)
            {
                _campaignManager.Log.Write("没有取到 GiftPool",
                                           String.Format("campaignId:{0},periodId:{1}", campaignId, periodId), TraceEventType.Error);
                result.Message = "遗憾您没有摇中,请再接再厉~~";
                result.Success = false;
                return(result);
            }

            Campaign_ShakingLotteryGiftEntity gift = pool.GetGift();

            if (gift == null)
            {
                result.Message = "遗憾您没有摇中,请再接再厉!~";
                result.Success = false;
                return(result);
            }

            log.Gift = gift.Id;

            if (gift.IsGift == false || gift.Stock <= 0)
            {
                log.Win = false;
            }
            else
            {
                //尝试扣减库存
                List <CommandParameter> parameterList = new List <CommandParameter>();
                parameterList.Add(new CommandParameter("@giftId", gift.Id));

                DataSet dsResult =
                    _campaignManager.DataBase.ExecuteDataSet(CommandType.StoredProcedure, "DecrementShakingLotteryGiftStock",
                                                             parameterList, new string[] { "result" });

                result.Reason = int.Parse(dsResult.Tables[0].Rows[0]["Result"].ToString());
                if (result.Reason == 0)
                {
                    log.Win = true;
                }
                else
                {
                    log.Win = false;
                }

                if (result.Reason != 2)
                {
                    gift.Stock = int.Parse(dsResult.Tables[0].Rows[0]["Stock"].ToString());
                }
            }

            _campaignManager.DataBase.Insert(log);

            if (log.Win)
            {
                result.Data    = gift;
                result.Success = true;
                return(result);
            }
            else
            {
                result.Message = "遗憾您没有摇中,请再接再厉~";
                result.Success = false;
                return(result);
            }
        }