public ItemSellResult SellItem(
            SessionToken token, string userId, Guid itemId, long amount, decimal pricePerItem)
        {
            if (amount <= 0 || pricePerItem <= 0)
            {
                return(new ItemSellResult(ItemTradeState.RequestToLow));
            }

            var character = GetCharacterAsync(token, userId);

            if (character == null)
            {
                return(new ItemSellResult(ItemTradeState.Failed));
            }

            if (!playerManager.AcquiredUserLock(token, character))
            {
                return(new ItemSellResult(ItemTradeState.Failed));
            }

            var itemsToSell = gameData.GetInventoryItems(character.Id, itemId);

            var totalItemCount = itemsToSell.Count > 0 ? itemsToSell.Sum(x => x.Amount.GetValueOrDefault()) : 0;
            var newItemAmount  = totalItemCount - amount;

            if (itemsToSell.Count == 0 || newItemAmount < 0)
            {
                return(new ItemSellResult(ItemTradeState.DoesNotOwn));
            }

            gameData.RemoveRange(itemsToSell);

            if (newItemAmount > 0)
            {
                var mergedInventoryItem = new InventoryItem
                {
                    Id          = Guid.NewGuid(),
                    Amount      = newItemAmount,
                    CharacterId = character.Id,
                    Equipped    = false,
                    ItemId      = itemId,
                };
                gameData.Add(mergedInventoryItem);
            }

            var marketItem = new DataModels.MarketItem
            {
                Id                = Guid.NewGuid(),
                Amount            = amount,
                Created           = DateTime.UtcNow,
                ItemId            = itemId,
                PricePerItem      = pricePerItem,
                SellerCharacterId = character.Id,
            };

            gameData.Add(marketItem);

            return(new ItemSellResult(ItemTradeState.Success));
        }
        public void TestMappingNullableTypes()
        {
            var item = new DataModels.MarketItem();

            item.Flags = 10;
            var result = DataMapper.Map <RavenNest.Models.MarketItem, RavenNest.DataModels.MarketItem>(item);

            Assert.AreEqual(item.Flags.Value, result.Flags);
        }
        public async Task <ItemSellResult> SellItemAsync(
            SessionToken token, string userId, Guid itemId, long amount, decimal pricePerItem)
        {
            if (amount <= 0 || pricePerItem <= 0)
            {
                return(new ItemSellResult(ItemTradeState.RequestToLow));
            }

            using (var db = dbProvider.Get())
            {
                var character = await GetCharacterAsync(db, token, userId);

                if (character == null)
                {
                    return(new ItemSellResult(ItemTradeState.Failed));
                }

                if (!await PlayerManager.AcquiredUserLockAsync(token, db, character))
                {
                    return(new ItemSellResult(ItemTradeState.Failed));
                }

                var itemsToSell = character.InventoryItem
                                  .Where(x => x.ItemId == itemId && !x.Equipped)
                                  .ToList();


                var totalItemCount = itemsToSell.Count > 0 ? itemsToSell.Sum(x => x.Amount.GetValueOrDefault()) : 0;
                var newItemAmount  = totalItemCount - amount;

                if (itemsToSell.Count == 0 || newItemAmount < 0)
                {
                    return(new ItemSellResult(ItemTradeState.DoesNotOwn));
                }

                db.RemoveRange(itemsToSell);

                if (newItemAmount > 0)
                {
                    var mergedInventoryItem = new InventoryItem
                    {
                        Id          = Guid.NewGuid(),
                        Amount      = newItemAmount,
                        CharacterId = character.Id,
                        Equipped    = false,
                        ItemId      = itemId,
                        Character   = character
                    };
                    await db.InventoryItem.AddAsync(mergedInventoryItem);
                }

                var marketItem = new DataModels.MarketItem
                {
                    Id                = Guid.NewGuid(),
                    Amount            = amount,
                    Created           = DateTime.UtcNow,
                    ItemId            = itemId,
                    PricePerItem      = pricePerItem,
                    SellerCharacterId = character.Id,
                    SellerCharacter   = character
                };

                await db.MarketItem.AddAsync(marketItem);

                await db.SaveChangesAsync();
            }

            return(new ItemSellResult(ItemTradeState.Success));
        }
        private async Task BuyMarketItemAsync(
            SessionToken token,
            Guid itemId,
            RavenfallDbContext db,
            Character character,
            DataModels.MarketItem marketItem,
            long amount,
            decimal cost)
        {
            var buyAmount = marketItem.Amount >= amount ? amount : marketItem.Amount;

            if (marketItem.Amount - buyAmount <= 0)
            {
                db.Remove(marketItem);
            }
            else
            {
                marketItem.Amount = buyAmount;
                db.Update(marketItem);
            }

            marketItem.SellerCharacter.Resources.Coins += cost;
            character.Resources.Coins -= cost;

            db.Update(marketItem.SellerCharacter.Resources);
            db.Update(character.Resources);

            var seller = await db.User.FirstOrDefaultAsync(x => x.Id == marketItem.SellerCharacter.UserId);

            var buyer = await db.User.FirstOrDefaultAsync(x => x.Id == character.UserId);

            var inventoryItems =
                await db.InventoryItem
                .Where(x => x.CharacterId == character.Id && !x.Equipped && x.ItemId == itemId)
                .ToListAsync();

            var mergeAmount = buyAmount;

            if (inventoryItems.Count > 0)
            {
                mergeAmount += inventoryItems.Sum(x => x.Amount.GetValueOrDefault());
                db.RemoveRange(inventoryItems);
            }

            var mergedInventoryItem = new InventoryItem
            {
                Id          = Guid.NewGuid(),
                Amount      = mergeAmount,
                CharacterId = character.Id,
                Equipped    = false,
                ItemId      = itemId,
                Character   = character
            };

            await db.InventoryItem.AddAsync(mergedInventoryItem);

            var model = new ItemTradeUpdate
            {
                SellerId = seller?.UserId,
                BuyerId  = buyer?.UserId,
                ItemId   = itemId,
                Amount   = amount,
                Cost     = cost
            };

            var sellerSession = await db.GameSession
                                .OrderByDescending(x => x.Started)
                                .FirstOrDefaultAsync(
                x =>
                x.UserId == marketItem.SellerCharacter.UserIdLock &&
                x.Status == (int)SessionStatus.Active);

            if (sellerSession != null)
            {
                await AddGameEventAsync(db, sellerSession.Id, GameEventType.ItemSell, model);
            }

            await AddGameEventAsync(db, token.SessionId, GameEventType.ItemBuy, model);
        }
        private int BuyMarketItemAsync(
            SessionToken token,
            Guid itemId,
            Character character,
            DataModels.MarketItem marketItem,
            long amount,
            decimal cost)
        {
            var buyAmount = marketItem.Amount >= amount ? amount : marketItem.Amount;

            if (marketItem.Amount == buyAmount)
            {
                gameData.Remove(marketItem);
            }
            else
            {
                marketItem.Amount -= buyAmount;
            }

            var sellerResources = gameData.GetResourcesByCharacterId(marketItem.SellerCharacterId);
            var buyerResources  = gameData.GetResourcesByCharacterId(character.Id);

            sellerResources.Coins += cost;
            buyerResources.Coins  -= cost;

            var sellerCharacter = gameData.GetCharacter(marketItem.SellerCharacterId);
            var seller          = gameData.GetUser(sellerCharacter.UserId);
            var buyer           = gameData.GetUser(character.UserId);

            var inventoryItems = gameData.GetInventoryItems(character.Id, itemId);
            var mergeAmount    = buyAmount;

            if (inventoryItems.Count > 0)
            {
                mergeAmount += inventoryItems.Sum(x => x.Amount.GetValueOrDefault());
                gameData.RemoveRange(inventoryItems);
            }

            var mergedInventoryItem = new InventoryItem
            {
                Id          = Guid.NewGuid(),
                Amount      = mergeAmount,
                CharacterId = character.Id,
                Equipped    = false,
                ItemId      = itemId,
            };

            gameData.Add(mergedInventoryItem);

            var model = new ItemTradeUpdate
            {
                SellerId = seller?.UserId,
                BuyerId  = buyer?.UserId,
                ItemId   = itemId,
                Amount   = amount,
                Cost     = cost
            };

            var sellerSession = gameData.GetUserSession(
                sellerCharacter.UserIdLock.GetValueOrDefault());

            if (sellerSession != null)
            {
                AddGameEvent(sellerSession.Id, GameEventType.ItemSell, model);
            }

            AddGameEvent(token.SessionId, GameEventType.ItemBuy, model);

            return((int)buyAmount);
        }
Beispiel #6
0
        public ItemSellResult SellItem(
            SessionToken token, string userId, Guid itemId, long amount, double pricePerItem)
        {
            //if (i != null && i.Category == (int)DataModels.ItemCategory.StreamerToken)
            //{
            //    return new ItemSellResult(ItemTradeState.Failed);
            //}

            if (amount <= 0 || pricePerItem <= 0)
            {
                return(new ItemSellResult(ItemTradeState.RequestToLow));
            }

            if (pricePerItem >= 1_000_000_000)
            {
                return(new ItemSellResult(ItemTradeState.Failed));
            }

            var character = GetCharacterAsync(token, userId);

            if (character == null)
            {
                return(new ItemSellResult(ItemTradeState.Failed));
            }

            if (!playerManager.AcquiredUserLock(token, character))
            {
                return(new ItemSellResult(ItemTradeState.Failed));
            }

            var    inventory    = inventoryProvider.Get(character.Id);
            var    session      = gameData.GetSession(token.SessionId);
            var    sessionOwner = gameData.GetUser(session.UserId);
            var    item         = gameData.GetItem(itemId);
            string itemTag      = null;

            if (item.Soulbound.GetValueOrDefault())
            {
                return(new ItemSellResult(ItemTradeState.Untradable));
            }

            if (item.Category == (int)DataModels.ItemCategory.StreamerToken)
            {
                itemTag = sessionOwner.UserId;
            }

            var itemToSell = inventory.GetUnequippedItem(itemId, tag: itemTag);

            if (itemToSell.IsNull())
            {
                return(new ItemSellResult(ItemTradeState.DoesNotOwn));
            }

            var totalItemCount = itemToSell.Amount;//itemsToSell.Count > 0 ? itemsToSell.Sum(x => x.Amount.GetValueOrDefault()) : 0;
            var newItemAmount  = totalItemCount - amount;

            if (newItemAmount < 0)
            {
                return(new ItemSellResult(ItemTradeState.DoesNotOwn));
            }

            inventory.RemoveItem(itemToSell, amount);

            var marketItem = new DataModels.MarketItem
            {
                Id                   = Guid.NewGuid(),
                Amount               = amount,
                Created              = DateTime.UtcNow,
                ItemId               = itemId,
                PricePerItem         = pricePerItem,
                SellerCharacterId    = character.Id,
                Tag                  = itemToSell.Tag,
                Flags                = itemToSell.Flags,
                Enchantment          = itemToSell.Enchantment,
                Name                 = itemToSell.Name,
                TransmogrificationId = itemToSell.TransmogrificationId,
                Expires              = DateTime.UtcNow.AddDays(14),
            };

            gameData.Add(marketItem);

            return(new ItemSellResult(ItemTradeState.Success));
        }
Beispiel #7
0
        private int BuyMarketItem(
            SessionToken token,
            Guid itemId,
            Character character,
            DataModels.MarketItem marketItem,
            long amount,
            double pricePerItem)
        {
            // todo(zerratar): Rewrite this!! This is horrible

            var buyAmount      = marketItem.Amount >= amount ? amount : marketItem.Amount;
            var buyerResources = gameData.GetResourcesByCharacterId(character.Id);
            var totalCost      = buyAmount * pricePerItem;

            if (totalCost > buyerResources.Coins)
            {
                return(0);
            }

            if (marketItem.Amount == buyAmount)
            {
                gameData.Remove(marketItem);
            }
            else
            {
                marketItem.Amount -= buyAmount;
            }

            var sellerResources = gameData.GetResourcesByCharacterId(marketItem.SellerCharacterId);

            sellerResources.Coins += totalCost;
            buyerResources.Coins  -= totalCost;

            var sellerCharacter = gameData.GetCharacter(marketItem.SellerCharacterId);
            var seller          = gameData.GetUser(sellerCharacter.UserId);
            var buyer           = gameData.GetUser(character.UserId);

            var inventory = inventoryProvider.Get(character.Id);

            inventory.AddItem(itemId, buyAmount, tag: marketItem.Tag);

            gameData.Add(
                new MarketItemTransaction
            {
                Id                = Guid.NewGuid(),
                Amount            = buyAmount,
                BuyerCharacterId  = character.Id,
                SellerCharacterId = sellerCharacter.Id,
                ItemId            = itemId,
                PricePerItem      = (totalCost / buyAmount),
                TotalPrice        = totalCost,
                Created           = DateTime.UtcNow
            });

            var model = new ItemTradeUpdate
            {
                SellerId = seller?.UserId,
                BuyerId  = buyer?.UserId,
                ItemId   = itemId,
                Amount   = buyAmount,//amount,
                Cost     = totalCost
            };

            var sellerSession = gameData.GetSessionByUserId(
                sellerCharacter.UserIdLock.GetValueOrDefault());

            if (sellerSession != null)
            {
                AddGameEvent(sellerSession.Id, GameEventType.ItemSell, model);
            }

            AddGameEvent(token.SessionId, GameEventType.ItemBuy, model);

            return((int)buyAmount);
        }