Exemplo n.º 1
0
        private static async Task EndSessionAsync(RavenfallDbContext db, GameSession session)
        {
            var characters = await db.Character
                             .Where(x => x.UserIdLock == session.UserId && x.LastUsed != null && x.LastUsed >= session.Started)
                             .OrderByDescending(x => x.LastUsed)
                             .ToListAsync();

            foreach (var character in characters)
            {
                character.UserIdLock = null;
                db.Update(character);
            }

            session.Status  = (int)SessionStatus.Inactive;
            session.Stopped = DateTime.UtcNow;
            db.Update(session);
            await db.SaveChangesAsync();
        }
Exemplo n.º 2
0
        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);
        }