예제 #1
0
        public Equipment Generate(IEnumerable <Item> items)
        {
            var eq = new Equipment();

            foreach (var item in items)
            {
                if (_shopItemDataStore.TryGetValue(item.Index, out ShopItem shopItem))
                {
                    //do something
                }
            }
            return(eq);
        }
        public async Task Process(INetworkMessage message, GameNetworkContext context)
        {
            if (await context.FaultyState(GameState.Online))
            {
                return;
            }
            if (message is ShopPurchaseRequest shopRequest)
            {
                int goldCost = 0, apCost = 0, charmCost = 0, newCharacters = 0;
                var result = ValidatePurchase(context, shopRequest, ref goldCost, ref apCost, ref charmCost, ref newCharacters);
                if (result == ShopPurchaseResponse.ShopPurchaseResult.Success)
                {
                    if (!context.HasGold(goldCost))
                    {
                        result = ShopPurchaseResponse.ShopPurchaseResult.NeedMoreGold;
                    }
                    else if (!context.HasAp(apCost))
                    {
                        result = ShopPurchaseResponse.ShopPurchaseResult.NeedMoreAp;
                    }
                    else if (!context.HasCharm(charmCost))
                    {
                        result = ShopPurchaseResponse.ShopPurchaseResult.NotEnoughCharmPoints;
                    }
                    if (newCharacters > 0)
                    {
                        await using (var uow = _unitOfWorkFactory.Create())
                        {
                            var currentCount = await uow.Characters.Where(p => p.AccountId == context.Character.AccountId).CountAsync();

                            if (newCharacters + currentCount > MaxCharacters)
                            {
                                result = ShopPurchaseResponse.ShopPurchaseResult.CharacterLimit;
                            }
                        }
                    }
                }

                var newItems = new List <Item>();
                if (result == ShopPurchaseResponse.ShopPurchaseResult.Success)
                {
                    if (!await context.AddCurrency(-goldCost, -apCost, -charmCost))
                    {
                        result = ShopPurchaseResponse.ShopPurchaseResult.CashInfoFailed;
                    }
                    else
                    {
                        await using (var uow = _unitOfWorkFactory.Create())
                        {
                            foreach (var purchaseItem in shopRequest.Items)
                            {
                                if (_shopItemDataStore.TryGetValue(purchaseItem.Index, out ShopItem shopItem))
                                {
                                    if (shopItem.GoldBack > 0)
                                    {
                                        await context.AddGold(shopItem.GoldBack);
                                    }
                                    if (shopItem.Item1 != 0) // set
                                    {
                                        int characterId = context.Character.Id;
                                        if (!shopItem.Hero.IsStrict(HeroType.All))
                                        {
                                            var character = await _characterBuilder.Create(context.Character.AccountId, shopItem.Hero);

                                            characterId = character.Id;
                                        }

                                        foreach (var index in shopItem.Items)
                                        {
                                            var item = new Item()
                                            {
                                                CharacterId  = characterId,
                                                CategoryType = ShopCategoryType.Parts,
                                                Index        = index,
                                                UseType      = shopItem.UseType,
                                                Quantity     = 1,
                                            };
                                            uow.Items.Add(item);
                                            if (characterId == context.Character.Id)
                                            {
                                                newItems.Add(item);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        var item = context.Items.FirstOrDefault(p => p.Index == shopItem.Item0);
                                        if (item == null)
                                        {
                                            item = new Item()
                                            {
                                                CharacterId  = context.Character.Id,
                                                CategoryType = shopItem.CategoryType,
                                                Index        = shopItem.Item0,
                                                UseType      = shopItem.UseType
                                            };
                                            uow.Items.Add(item);
                                            context.Items.Add(item);
                                        }
                                        else
                                        {
                                            uow.Attach(item);
                                        }
                                        if (purchaseItem.Option == 0)
                                        {
                                            item.Quantity += shopItem.Use0 == 0 ? 1 : shopItem.Use0;
                                        }
                                        else if (purchaseItem.Option == 1)
                                        {
                                            item.Quantity += shopItem.Use1;
                                        }
                                        else if (purchaseItem.Option == 2)
                                        {
                                            item.Quantity += shopItem.Use2;
                                        }
                                        if (shopItem.UseType == ItemUseType.Time)
                                        {
                                            item.ExpirationDate = DateTime.UtcNow.AddDays(item.Quantity);
                                        }
                                    }
                                }
                            }
                            await uow.CommitAsync();
                        }
                    }
                }

                await context.SendAsync(new ShopPurchaseResponse()
                {
                    Result = result,
                    Items  = newItems
                });

                if (result == ShopPurchaseResponse.ShopPurchaseResult.Success)
                {
                    await context.SendAsync(new SynchronizeCurrencyResponse()
                    {
                        Ap   = context.Ap,
                        Gold = context.Gold
                    });
                }
            }
        }