Beispiel #1
0
        private async Task DoWork(PlayerEntity player)
        {
            await _mudProvider.ShowMessage(player.Id, $"你正在{player.Status}。。。");

            WorkTypeEnum workType = WorkTypeEnum.伐木;

            switch (player.Status)
            {
            case PlayerStatusEnum.伐木:
                workType = WorkTypeEnum.伐木;
                break;

            case PlayerStatusEnum.挖矿:
                workType = WorkTypeEnum.挖矿;
                break;

            case PlayerStatusEnum.打猎:
                workType = WorkTypeEnum.打猎;
                break;

            case PlayerStatusEnum.采药:
                workType = WorkTypeEnum.采药;
                break;

            case PlayerStatusEnum.钓鱼:
                workType = WorkTypeEnum.钓鱼;
                break;

            case PlayerStatusEnum.打工:
                workType = WorkTypeEnum.打工;
                break;

            default:
                return;
            }

            var ids = (await _roomItemDropDomainService.GetAll()).Where(x => x.RoomId == player.RoomId).Select(x => x.ItemDropId).ToList();

            var itemDrop = (await _itemDropDomainService.GetAll()).Where(x => ids.Contains(x.Id)).FirstOrDefault(x => x.WorkType == workType);

            if (itemDrop == null)
            {
                return;
            }


            var itemDropRates = (await _itemDropRateDomainService.GetAll()).Where(x => x.ItemDropId == itemDrop.Id).ToList();

            if (itemDropRates?.Count == 0)
            {
                return;
            }

            var random         = new Random();
            int maxWeight      = 100;//掉落总权重
            var itemDropModels = new List <ItemDropRateModel>();

            foreach (var itemDropRate in itemDropRates.OrderBy(x => x.Order))
            {
                if (itemDropRate.Percent < random.Next(0, 100))
                {
                    continue;
                }

                int number = random.Next(Math.Min(itemDropRate.MinNumber, itemDropRate.MaxNumber), itemDropRate.MaxNumber + 1);
                if (number <= 0)
                {
                    continue;
                }

                //掉落
                maxWeight -= itemDropRate.Weight;

                var itemDropModel = new ItemDropRateModel
                {
                    DropType = itemDropRate.DropType,
                    Number   = number,
                    WareId   = itemDropRate.WareId
                };
                itemDropModels.Add(itemDropModel);



                if (maxWeight <= 0)
                {
                    break;
                }
            }

            if (itemDropModels.Count == 0)
            {
                //没有掉落
                return;
            }

            var           playerAttributeChanged = false;
            List <string> dropContents           = new List <string>();

            foreach (var itemDropModel in itemDropModels)
            {
                switch (itemDropModel.DropType)
                {
                case ItemDropTypeEnum.潜能:
                    playerAttributeChanged = true;
                    player.Pot            += itemDropModel.Number;
                    dropContents.Add($"潜能 +{itemDropModel.Number}");
                    break;

                case ItemDropTypeEnum.经验:
                    playerAttributeChanged = true;
                    player.Exp            += itemDropModel.Number;
                    dropContents.Add($"经验 +{itemDropModel.Number}");
                    break;

                case ItemDropTypeEnum.金钱:
                    playerAttributeChanged = true;
                    player.Money          += itemDropModel.Number;
                    dropContents.Add($" +{itemDropModel.Number.ToMoney()}");
                    break;

                case ItemDropTypeEnum.物品:
                    #region MyRegion
                    int wareId = itemDropModel.WareId;
                    int number = itemDropModel.Number;

                    var ware = await _wareDomainService.Get(wareId);

                    if (ware == null)
                    {
                        continue;
                    }

                    dropContents.Add($"{ware.Name} X{number}");

                    var canStack = ware.Category != WareCategoryEnum.武器;

                    if (canStack)
                    {
                        var playerWare = await _playerWareDomainService.Get(x => x.WareId == ware.Id && x.PlayerId == player.Id);

                        if (playerWare == null)
                        {
                            playerWare = new PlayerWareEntity
                            {
                                IsBind   = false,
                                IsTemp   = false,
                                Level    = 1,
                                Number   = number,
                                Damage   = 0,
                                PlayerId = player.Id,
                                Status   = WareStatusEnum.卸下,
                                WareId   = wareId,
                                WareName = ware.Name
                            };
                            await _playerWareDomainService.Add(playerWare);
                        }
                        else
                        {
                            playerWare.Number += number;
                            await _playerWareDomainService.Update(playerWare);
                        }
                    }
                    else
                    {
                        var playerWare = new PlayerWareEntity
                        {
                            IsBind   = false,
                            IsTemp   = false,
                            Level    = 1,
                            Number   = number,
                            Damage   = 0,
                            PlayerId = player.Id,
                            Status   = WareStatusEnum.卸下,
                            WareId   = wareId,
                            WareName = ware.Name
                        };
                        await _playerWareDomainService.Add(playerWare);
                    }
                    #endregion


                    break;
                }
            }

            if (playerAttributeChanged)
            {
                await _bus.RaiseEvent(new PlayerAttributeChangedEvent(player)).ConfigureAwait(false);
            }


            if (dropContents.Count > 0)
            {
                await _mudProvider.ShowMessage(player.Id, $"获得{ string.Join(",", dropContents)   }。");
            }
        }
Beispiel #2
0
        public async Task <List <ItemDropRateEntity> > GetAll(int id)
        {
            var query = await _itemDropRateDomainService.GetAll();

            return(query.Where(x => x.ItemDropId == id).ToList());
        }