Exemplo n.º 1
0
        /// <summary>
        /// 计算采矿设备可采集数额
        /// </summary>
        /// <param name="equipment"></param>
        /// <param name="checkStatus"></param>
        /// <returns></returns>
        public decimal MemberMiningEquipmentCalculate(MemberMiningEquipment equipment, bool checkStatus = true)
        {
            var status = GetMemberMiningEquipmentStatus(equipment.LastCollectAt);

            if (checkStatus && status != MemberMiningEquipmentStatus.CanCollected)
            {
                throw new PlatformException(ErrorCode.MiningEquipmentCollectError);
            }
            var collectTerm = MiddleTier.BusinessConfig.MiningEquipmentCollectDay;

            switch (status)
            {
            case MemberMiningEquipmentStatus.InUse:
                var days = (DateTime.Now.Date - equipment.LastCollectAt.Date).Days;
                collectTerm = days % 5;
                break;

            case MemberMiningEquipmentStatus.CanCollected:
                break;

            case MemberMiningEquipmentStatus.Expired:
            case MemberMiningEquipmentStatus.Lock:
                collectTerm = 0;
                break;
            }
            var     amount     = equipment.PurchasePrice;
            decimal percentage = 0;
            //获取设备变动记录
            var changeRecords = DataBase.GetList <MiningEquipmentChangeRecord>(e => e.Type == equipment.Type);

            for (int i = 1; i <= collectTerm; i++)
            {
                var day = DateTime.Now.AddDays(0 - i);
//				if (day.DayOfWeek == DayOfWeek.Sunday)
//					continue;
                var changeRecord = changeRecords.Where(e => e.CreatedAt.Date <= day).OrderByDescending(e => e.Id).FirstOrDefault();
                percentage += (changeRecord?.Percentage ?? equipment.Type.GetMiningEquipment().Percentage);
            }
            return(amount * percentage / 100);
        }
Exemplo n.º 2
0
 public void UpdateMemberMiningEquipment(MemberMiningEquipment memberMiningEquipment)
 {
     DataBase.Update(memberMiningEquipment);
 }
Exemplo n.º 3
0
        public void MemberBuyMiningEquipment(string userName, MiningEquipmentType type)
        {
            //数据校验
            var member = MiddleTier.MemberManager.GetMember(userName);

            if (member == null)
            {
                throw new PlatformException(ErrorCode.UserNameIsNotExisted);
            }
            var miningEquipment = DataBase.Get <MiningEquipment>(e => e.Type == type);

            if (miningEquipment == null)
            {
                throw new PlatformException(ErrorCode.ErrorId);
            }
            if (member.SilverBalance < miningEquipment.Price)
            {
                throw new PlatformException(ErrorCode.AccountBalanceNotEnough);
            }
            //11、可以设置购买劳工和矿机的上限(比如劳工设置最多可以买10个,半自动矿机最多可以买5个),这个数字后台可以调整。
            var count      = DataBase.Count <MemberMiningEquipment>(e => e.MemberUserName == userName && e.Type == type);
            var limitCount = type == MiningEquipmentType.Miner
                                ? MiddleTier.BusinessConfig.MemberMiningEquipmentType1Limit
                                : (
                type == MiningEquipmentType.GoldFactory
                                                ? MiddleTier.BusinessConfig.MemberMiningEquipmentType2Limit
                                                : MiddleTier.BusinessConfig.MemberMiningEquipmentType3Limit);

            if (count >= limitCount)
            {
                throw new PlatformException(ErrorCode.EquipmentCountLimit);
            }
            ISession     session      = DataBase.Session;
            ITransaction iTransaction = session.BeginTransaction(IsolationLevel.ReadCommitted);

            try
            {
                var diamondAmount = type == MiningEquipmentType.Miner
                                        ? MiddleTier.BusinessConfig.PartnerBuyEquipmentType1RewardDiamondAmount
                                        : (
                    type == MiningEquipmentType.GoldFactory
                                                        ? MiddleTier.BusinessConfig.PartnerBuyEquipmentType2RewardDiamondAmount
                                                        : MiddleTier.BusinessConfig.PartnerBuyEquipmentType3RewardDiamondAmount);
                //扣除银余额
                member.SilverBalance -= miningEquipment.Price;
                if (member.Status == MemberStatus.Actived)
                {
                    member.Status = MemberStatus.InUse;
                }
                if (member.ExpiredAt < DateTime.Now.AddDays(miningEquipment.ExpirationDay + 20))
                {
                    member.ExpiredAt = DateTime.Now.AddDays(miningEquipment.ExpirationDay + 20);
                }
                member.DiamondBalance += diamondAmount;
                DataBase.Update(member, session);
//				DataBase.ExecuteBySql($"UPDATE Members set SilverBalance = SilverBalance-{miningEquipment.Price}  Where UserName='******';");
                //新增用户挖矿设备
                var memberMiningEquipment = new MemberMiningEquipment
                {
                    MemberUserName = userName,
                    LastCollectAt  = DateTime.Now,
                    OverDueTime    = 0,
                    Percentage     = miningEquipment.Percentage,
                    PurchasePrice  = miningEquipment.Price,
                    Type           = miningEquipment.Type,
                    ValidityTerm   = DateTime.Now.AddDays(miningEquipment.ExpirationDay),
                    Status         = MemberMiningEquipmentStatus.InUse
                };
                DataBase.Save(memberMiningEquipment, session);
//				2.用户购买矿工后,给她的直接上级奖励矿工价值的25 %(这25 % 中30 % 是金,70 % 是狂渣),她的间接推荐人(上级的上级)拿矿工价值的10 %。
//				限制条件:每种类型矿工的前30个,第31个开始就不给上面的奖励了。
                if (DataBase.Count <MemberMiningEquipment>(e => e.MemberUserName == member.UserName && e.Type == type, session) <= 30)
                {
                    if (member.RecommendUserName.IsNotNullOrEmpty())
                    {
                        var fRecommendUser = DataBase.Get <Member>(e => e.UserName == member.RecommendUserName, session);
                        if (fRecommendUser != null)
                        {
                            //Todo 收益记录
                            var fAmount = miningEquipment.Price / 4;
                            fRecommendUser.GoldBalance        += fAmount * 0.3m;
                            fRecommendUser.FeedBackAmount     += fAmount;
                            fRecommendUser.FeedBackGoldAmount += fAmount * 0.3m;
                            fRecommendUser.SlagBalance        += fAmount * 0.7m;
                            fRecommendUser.FeedBackSlagAmount += fAmount * 0.7m;
                            DataBase.Update(fRecommendUser, session);
                            var fMemberIncomeRecord = new MemberIncomeRecord
                            {
                                Amount                  = fAmount,
                                GoldAmount              = fAmount * 0.3m,
                                SlagAmount              = fAmount * 0.7m,
                                MemberUserName          = fRecommendUser.UserName,
                                Type                    = MemberIncomeType.InviteFeedback,
                                ReferenceMemberUsername = userName
                            };
                            DataBase.Save(fMemberIncomeRecord, session);
                            var ffMemberIncomeRecord = new MemberIncomeRecord
                            {
                                Amount                  = diamondAmount,
                                MemberUserName          = userName,
                                DiamondAmount           = diamondAmount,
                                Type                    = MemberIncomeType.EquipmentBuyFeedback,
                                ReferenceMemberUsername = userName
                            };
                            DataBase.Save(ffMemberIncomeRecord, session);
                            if (fRecommendUser.RecommendUserName.IsNotNullOrEmpty())
                            {
                                var sRecommendUser = DataBase.Get <Member>(e => e.UserName == fRecommendUser.RecommendUserName, session);
                                if (sRecommendUser != null)
                                {
                                    var sRecommendUserRefineEquipmentCount =
                                        DataBase.Count <SlagRefiningEquipment>(e => e.MemberUserName == sRecommendUser.UserName);
                                    if (sRecommendUserRefineEquipmentCount >= 4)
                                    {
                                        var sAmount = miningEquipment.Price / 10;
                                        sRecommendUser.GoldBalance        += sAmount * 0.3m;
                                        sRecommendUser.FeedBackAmount     += sAmount;
                                        sRecommendUser.FeedBackGoldAmount += sAmount * 0.3m;
                                        sRecommendUser.SlagBalance        += sAmount * 0.7m;
                                        sRecommendUser.FeedBackSlagAmount += sAmount * 0.7m;
                                        DataBase.Update(sRecommendUser, session);
                                        var sMemberIncomeRecord = new MemberIncomeRecord
                                        {
                                            Amount                  = sAmount,
                                            GoldAmount              = sAmount,
                                            MemberUserName          = sRecommendUser.UserName,
                                            Type                    = MemberIncomeType.EquipmentBuyFeedback,
                                            ReferenceMemberUsername = userName
                                        };
                                        DataBase.Save(sMemberIncomeRecord, session);
                                    }
                                }
                            }
                        }
                    }
                    //给邀请人发放奖励-矿渣
                    //增加直接推荐奖励,推荐的玩家每购买一台矿机,推荐人都有矿机价值的10%奖励,累计到矿渣!
//					var inviters = GetInviters(member, 2, session);
//					inviters.ForEach(inviter =>
//					{
//						inviter.SlagBalance +=
//							memberMiningEquipment.PurchasePrice / 100 * MiddleTier.BusinessConfig.EquipmentBuyRewardPercentage;
//						//6、激活成功送钻石一个(给推荐人),每增加一个矿工送一个钻石,选金厂送5个,冶炼厂送10个!钻石以后有用! (这个数字做成可配的)
//						inviter.DiamondBalance += type == MiningEquipmentType.Type1
//							? MiddleTier.BusinessConfig.PartnerBuyEquipmentType1RewardDiamondAmount
//							: (
//								type == MiningEquipmentType.Type2
//									? MiddleTier.BusinessConfig.PartnerBuyEquipmentType2RewardDiamondAmount
//									: MiddleTier.BusinessConfig.PartnerBuyEquipmentType3RewardDiamondAmount);
//						DataBase.Update(inviter, session);
//					});
                }
                iTransaction.Commit();
            }
            catch (Exception ex)
            {
                iTransaction.Rollback();
                MiddleTier.LogManager.Error(ex);
                if (ex is PlatformException platformException)
                {
                    throw platformException;
                }
                throw new PlatformException(ErrorCode.SystemError);
            }
            finally
            {
                session.Close();
            }
        }