public async Task RemoveAsync(ulong userId, InventoryData data) { var dbInventory = await GetAsync(userId); var dbStatistics = await DB.Statistics.GetAsync(userId); var inventory = new RiftInventory { UserId = userId }; var stat = new StatisticData(); await using var context = new RiftContext(); var inventoryEntry = context.Attach(inventory); if (data.Coins.HasValue) { var coinsBefore = dbInventory.Coins; var value = Math.Min(data.Coins.Value, coinsBefore); if (value > uint.MinValue) { OnCoinsSpent?.Invoke(null, new CoinsSpentEventArgs(userId, value)); inventory.Coins = coinsBefore - value; if (uint.MaxValue - dbStatistics.CoinsSpent < value) { stat.CoinsSpent = uint.MaxValue; } else { stat.CoinsSpent = dbStatistics.CoinsSpent + value; } inventoryEntry.Property(x => x.Coins).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s coin(s): ({coinsBefore.ToString()} => {inventory.Coins.ToString()})"); } } if (data.Tokens.HasValue) { var tokensBefore = dbInventory.Tokens; var value = Math.Min(data.Tokens.Value, tokensBefore); if (value > uint.MinValue) { inventory.Tokens = tokensBefore - value; if (uint.MaxValue - dbStatistics.TokensSpent < value) { stat.TokensSpent = uint.MaxValue; } else { stat.TokensSpent = dbStatistics.TokensSpent + value; } inventoryEntry.Property(x => x.Tokens).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s token(s): ({tokensBefore.ToString()} => {inventory.Tokens.ToString()})"); } } if (data.Essence.HasValue) { var essenceBefore = dbInventory.Essence; var value = Math.Min(data.Essence.Value, essenceBefore); if (value > uint.MinValue) { inventory.Chests = essenceBefore - value; if (uint.MaxValue - dbStatistics.ChestsOpened < value) { stat.ChestsOpened = uint.MaxValue; } else { stat.ChestsOpened = dbStatistics.ChestsOpened + value; } inventoryEntry.Property(x => x.Essence).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s essence: ({essenceBefore.ToString()} => {inventory.Essence.ToString()})"); } } if (data.Chests.HasValue) { var chestsBefore = dbInventory.Chests; var value = Math.Min(data.Chests.Value, chestsBefore); if (value > uint.MinValue) { inventory.Chests = chestsBefore - value; if (uint.MaxValue - dbStatistics.ChestsOpened < value) { stat.ChestsOpened = uint.MaxValue; } else { stat.ChestsOpened = dbStatistics.ChestsOpened + value; } inventoryEntry.Property(x => x.Chests).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s chest(s): ({chestsBefore.ToString()} => {inventory.Chests.ToString()})"); } } if (data.Spheres.HasValue) { var spheresBefore = dbInventory.Spheres; var value = Math.Min(data.Spheres.Value, spheresBefore); if (value > uint.MinValue) { inventory.Spheres = spheresBefore - value; if (uint.MaxValue - dbStatistics.SpheresOpened < value) { stat.SpheresOpened = uint.MaxValue; } else { stat.SpheresOpened = dbStatistics.SpheresOpened + value; } inventoryEntry.Property(x => x.Spheres).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s sphere(s): ({spheresBefore.ToString()} => {inventory.Spheres.ToString()})"); } } if (data.Capsules.HasValue) { var capsulesBefore = dbInventory.Capsules; var value = Math.Min(data.Capsules.Value, capsulesBefore); if (value > uint.MinValue) { inventory.Capsules = capsulesBefore - value; if (uint.MaxValue - dbStatistics.CapsulesOpened < value) { stat.CapsulesOpened = uint.MaxValue; } else { stat.CapsulesOpened = dbStatistics.CapsulesOpened + value; } inventoryEntry.Property(x => x.Capsules).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s capsule(s): ({capsulesBefore.ToString()} => {inventory.Capsules.ToString()})"); } } if (data.Tickets.HasValue) { var ticketsBefore = dbInventory.Tickets; var value = Math.Min(data.Tickets.Value, ticketsBefore); if (value > uint.MinValue) { inventory.Tickets = ticketsBefore - value; if (uint.MaxValue - dbStatistics.TicketsSpent < value) { stat.TicketsSpent = uint.MaxValue; } else { stat.TicketsSpent = dbStatistics.TicketsSpent + value; } inventoryEntry.Property(x => x.Tickets).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s usual ticket(s): ({ticketsBefore.ToString()} => {inventory.Tickets.ToString()})"); } } if (data.DoubleExps.HasValue) { var doubleExpsBefore = dbInventory.BonusDoubleExp; var value = Math.Min(data.DoubleExps.Value, doubleExpsBefore); if (value > uint.MinValue) { inventory.BonusDoubleExp = doubleExpsBefore - value; if (uint.MaxValue - dbStatistics.DoubleExpsActivated < value) { stat.DoubleExpsActivated = uint.MaxValue; } else { stat.DoubleExpsActivated = dbStatistics.DoubleExpsActivated + value; } inventoryEntry.Property(x => x.BonusDoubleExp).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s doubleExp(s): ({doubleExpsBefore.ToString()} => {inventory.BonusDoubleExp.ToString()})"); } } if (data.BotRespects.HasValue) { var respectsBefore = dbInventory.BonusBotRespect; var value = Math.Min(data.BotRespects.Value, respectsBefore); if (value > uint.MinValue) { inventory.BonusBotRespect = respectsBefore - value; if (uint.MaxValue - dbStatistics.BotRespectsActivated < value) { stat.BotRespectsActivated = uint.MaxValue; } else { stat.BotRespectsActivated = dbStatistics.BotRespectsActivated + value; } inventoryEntry.Property(x => x.BonusBotRespect).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s respect(s): ({respectsBefore.ToString()} => {inventory.BonusBotRespect.ToString()})"); } } if (data.Rewinds.HasValue) { var rewindsBefore = dbInventory.BonusRewind; var value = Math.Min(data.Rewinds.Value, rewindsBefore); if (value > uint.MinValue) { inventory.BonusRewind = rewindsBefore - value; if (uint.MaxValue - dbStatistics.RewindsActivated < value) { stat.RewindsActivated = uint.MaxValue; } else { stat.RewindsActivated = dbStatistics.RewindsActivated + value; } inventoryEntry.Property(x => x.BonusRewind).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s rewind(s): ({rewindsBefore.ToString()} => {inventory.BonusRewind.ToString()})"); } } await context.SaveChangesAsync(); }
public async Task AddAsync(ulong userId, InventoryData data) { var dbInventory = await GetAsync(userId); var inventory = new RiftInventory { UserId = userId }; var stat = new StatisticData(); await using var context = new RiftContext(); var inventoryEntry = context.Attach(inventory); if (data.Coins.HasValue) { var coinsBefore = dbInventory.Coins; var value = data.Coins.Value; OnCoinsReceived?.Invoke(null, new CoinsReceivedEventArgs(userId, value)); if (uint.MaxValue - coinsBefore < value) { inventory.Coins = uint.MaxValue; } else { inventory.Coins = coinsBefore + value; } stat.CoinsEarned = inventory.Coins - coinsBefore; inventoryEntry.Property(x => x.Coins).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s coin(s): ({coinsBefore.ToString()} => {inventory.Coins.ToString()})"); } if (data.Tokens.HasValue) { var tokensBefore = dbInventory.Tokens; var value = data.Tokens.Value; if (uint.MaxValue - tokensBefore < value) { inventory.Tokens = uint.MaxValue; } else { inventory.Tokens = tokensBefore + value; } stat.TokensEarned = inventory.Tokens - tokensBefore; inventoryEntry.Property(x => x.Tokens).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s token(s): ({tokensBefore.ToString()} => {inventory.Tokens.ToString()})"); } if (data.Essence.HasValue) { var essenceBefore = dbInventory.Essence; var value = data.Essence.Value; if (uint.MaxValue - essenceBefore < value) { inventory.Essence = uint.MaxValue; } else { inventory.Essence = essenceBefore + value; } stat.EssenceEarned = inventory.Essence - essenceBefore; inventoryEntry.Property(x => x.Essence).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s essence: ({essenceBefore.ToString()} => {inventory.Essence.ToString()})"); } if (data.Chests.HasValue) { var chestsBefore = dbInventory.Chests; var value = data.Chests.Value; if (uint.MaxValue - chestsBefore < value) { inventory.Chests = uint.MaxValue; } else { inventory.Chests = chestsBefore + value; } stat.ChestsEarned = inventory.Chests - chestsBefore; inventoryEntry.Property(x => x.Chests).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s chest(s): ({chestsBefore.ToString()} => {inventory.Chests.ToString()})"); } if (data.Spheres.HasValue) { var spheresBefore = dbInventory.Spheres; var value = data.Spheres.Value; if (uint.MaxValue - spheresBefore < value) { inventory.Spheres = uint.MaxValue; } else { inventory.Spheres = spheresBefore + value; } stat.SpheresEarned = inventory.Spheres - spheresBefore; inventoryEntry.Property(x => x.Spheres).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s sphere(s): ({spheresBefore.ToString()} => {inventory.Spheres.ToString()})"); } if (data.Capsules.HasValue) { var capsulesBefore = dbInventory.Capsules; var value = data.Capsules.Value; if (uint.MaxValue - capsulesBefore < value) { inventory.Capsules = uint.MaxValue; } else { inventory.Capsules = capsulesBefore + value; } stat.CapsulesEarned = inventory.Capsules - capsulesBefore; inventoryEntry.Property(x => x.Capsules).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s capsule(s): ({capsulesBefore.ToString()} => {inventory.Capsules.ToString()})"); } if (data.Tickets.HasValue) { var ticketsBefore = dbInventory.Tickets; var value = data.Tickets.Value; if (uint.MaxValue - ticketsBefore < value) { inventory.Tickets = uint.MaxValue; } else { inventory.Tickets = ticketsBefore + value; } stat.TicketsEarned = inventory.Tickets - ticketsBefore; inventoryEntry.Property(x => x.Tickets).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s ticket(s): ({ticketsBefore.ToString()} => {inventory.Tickets.ToString()})"); } if (data.DoubleExps.HasValue) { var doubleExpsBefore = dbInventory.BonusDoubleExp; var value = data.DoubleExps.Value; if (uint.MaxValue - doubleExpsBefore < value) { inventory.BonusDoubleExp = uint.MaxValue; } else { inventory.BonusDoubleExp = doubleExpsBefore + value; } stat.DoubleExpsEarned = inventory.BonusDoubleExp - doubleExpsBefore; inventoryEntry.Property(x => x.BonusDoubleExp).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s doubleExp(s): ({doubleExpsBefore.ToString()} => {inventory.BonusDoubleExp.ToString()})"); } if (data.BotRespects.HasValue) { var respectsBefore = dbInventory.BonusBotRespect; var value = data.BotRespects.Value; if (uint.MaxValue - respectsBefore < value) { inventory.BonusBotRespect = uint.MaxValue; } else { inventory.BonusBotRespect = respectsBefore + value; } stat.BotRespectsEarned = inventory.BonusBotRespect - respectsBefore; inventoryEntry.Property(x => x.BonusBotRespect).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s respect(s): ({respectsBefore.ToString()} => {inventory.BonusBotRespect.ToString()})"); } if (data.Rewinds.HasValue) { var rewindsBefore = dbInventory.BonusRewind; var value = data.Rewinds.Value; if (uint.MaxValue - rewindsBefore < value) { inventory.BonusRewind = uint.MaxValue; } else { inventory.BonusRewind = rewindsBefore + value; } stat.RewindsEarned = inventory.BonusRewind - rewindsBefore; inventoryEntry.Property(x => x.BonusRewind).IsModified = true; RiftBot.Log.Information( $"Modified {userId.ToString()}'s rewind(s): ({rewindsBefore.ToString()} => {inventory.BonusRewind.ToString()})"); } await context.SaveChangesAsync(); await DB.Statistics.AddAsync(userId, stat); }
public async Task AddAsync(ulong userId, StatisticData data) { if (!await EnsureExistsAsync(userId)) { throw new DatabaseException(nameof(AddAsync)); } var dbStatistics = await GetAsync(userId); var stat = new RiftStatistics { UserId = userId }; await using var context = new RiftContext(); var entry = context.Attach(stat); if (data.CoinsEarned.HasValue) { var before = dbStatistics.CoinsEarned; var value = data.CoinsEarned.Value; if (uint.MaxValue - before < value) { stat.CoinsEarned = uint.MaxValue; } else { stat.CoinsEarned = before + value; } entry.Property(x => x.CoinsEarned).IsModified = true; } if (data.CoinsSpent.HasValue) { var before = dbStatistics.CoinsSpent; var value = data.CoinsSpent.Value; if (uint.MaxValue - before < value) { stat.CoinsSpent = uint.MaxValue; } else { stat.CoinsSpent = before + value; } entry.Property(x => x.CoinsSpent).IsModified = true; } if (data.TokensEarned.HasValue) { var before = dbStatistics.TokensEarned; var value = data.TokensEarned.Value; if (uint.MaxValue - before < value) { stat.TokensEarned = uint.MaxValue; } else { stat.TokensEarned = before + value; } entry.Property(x => x.TokensEarned).IsModified = true; } if (data.TokensSpent.HasValue) { var before = dbStatistics.TokensSpent; var value = data.TokensSpent.Value; if (uint.MaxValue - before < value) { stat.TokensSpent = uint.MaxValue; } else { stat.TokensSpent = before + value; } entry.Property(x => x.TokensSpent).IsModified = true; } if (data.EssenceEarned.HasValue) { var before = dbStatistics.EssenceEarned; var value = data.EssenceEarned.Value; if (uint.MaxValue - before < value) { stat.EssenceEarned = uint.MaxValue; } else { stat.EssenceEarned = before + value; } entry.Property(x => x.EssenceEarned).IsModified = true; } if (data.EssenceSpent.HasValue) { var before = dbStatistics.EssenceSpent; var value = data.EssenceSpent.Value; if (uint.MaxValue - before < value) { stat.EssenceSpent = uint.MaxValue; } else { stat.EssenceSpent = before + value; } entry.Property(x => x.EssenceSpent).IsModified = true; } if (data.ChestsEarned.HasValue) { var before = dbStatistics.ChestsEarned; var value = data.ChestsEarned.Value; if (uint.MaxValue - before < value) { stat.ChestsEarned = uint.MaxValue; } else { stat.ChestsEarned = before + value; } entry.Property(x => x.ChestsEarned).IsModified = true; } if (data.ChestsOpened.HasValue) { var before = dbStatistics.ChestsOpened; var value = data.ChestsOpened.Value; if (uint.MaxValue - before < value) { stat.ChestsOpened = uint.MaxValue; } else { stat.ChestsOpened = before + value; } entry.Property(x => x.ChestsOpened).IsModified = true; } if (data.SpheresEarned.HasValue) { var before = dbStatistics.SpheresEarned; var value = data.SpheresEarned.Value; if (uint.MaxValue - before < value) { stat.SpheresEarned = uint.MaxValue; } else { stat.SpheresEarned = before + value; } entry.Property(x => x.SpheresEarned).IsModified = true; } if (data.SpheresOpened.HasValue) { var before = dbStatistics.SpheresOpened; var value = data.SpheresOpened.Value; if (uint.MaxValue - before < value) { stat.SpheresOpened = uint.MaxValue; } else { stat.SpheresOpened = before + value; } entry.Property(x => x.SpheresOpened).IsModified = true; } if (data.CapsulesEarned.HasValue) { var before = dbStatistics.CapsulesEarned; var value = data.CapsulesEarned.Value; if (uint.MaxValue - before < value) { stat.CapsulesEarned = uint.MaxValue; } else { stat.CapsulesEarned = before + value; } entry.Property(x => x.CapsulesEarned).IsModified = true; } if (data.CapsulesOpened.HasValue) { var before = dbStatistics.CapsulesOpened; var value = data.CapsulesOpened.Value; if (uint.MaxValue - before < value) { stat.CapsulesOpened = uint.MaxValue; } else { stat.CapsulesOpened = before + value; } entry.Property(x => x.CapsulesOpened).IsModified = true; } if (data.TicketsEarned.HasValue) { var before = dbStatistics.TicketsEarned; var value = data.TicketsEarned.Value; if (uint.MaxValue - before < value) { stat.TicketsEarned = uint.MaxValue; } else { stat.TicketsEarned = before + value; } entry.Property(x => x.TicketsEarned).IsModified = true; } if (data.TicketsSpent.HasValue) { var before = dbStatistics.TicketsSpent; var value = data.TicketsSpent.Value; if (uint.MaxValue - before < value) { stat.TicketsSpent = uint.MaxValue; } else { stat.TicketsSpent = before + value; } entry.Property(x => x.TicketsSpent).IsModified = true; } if (data.DoubleExpsEarned.HasValue) { var before = dbStatistics.DoubleExpsEarned; var value = data.DoubleExpsEarned.Value; if (uint.MaxValue - before < value) { stat.DoubleExpsEarned = uint.MaxValue; } else { stat.DoubleExpsEarned = before + value; } entry.Property(x => x.DoubleExpsEarned).IsModified = true; } if (data.DoubleExpsActivated.HasValue) { var before = dbStatistics.DoubleExpsActivated; var value = data.DoubleExpsActivated.Value; if (uint.MaxValue - before < value) { stat.DoubleExpsActivated = uint.MaxValue; } else { stat.DoubleExpsActivated = before + value; } entry.Property(x => x.DoubleExpsActivated).IsModified = true; } if (data.BotRespectsEarned.HasValue) { var before = dbStatistics.BotRespectsEarned; var value = data.BotRespectsEarned.Value; if (uint.MaxValue - before < value) { stat.BotRespectsEarned = uint.MaxValue; } else { stat.BotRespectsEarned = before + value; } entry.Property(x => x.BotRespectsEarned).IsModified = true; } if (data.BotRespectsActivated.HasValue) { var before = dbStatistics.BotRespectsActivated; var value = data.BotRespectsActivated.Value; if (uint.MaxValue - before < value) { stat.BotRespectsActivated = uint.MaxValue; } else { stat.BotRespectsActivated = before + value; } entry.Property(x => x.BotRespectsActivated).IsModified = true; } if (data.RewindsEarned.HasValue) { var before = dbStatistics.RewindsEarned; var value = data.RewindsEarned.Value; if (uint.MaxValue - before < value) { stat.RewindsEarned = uint.MaxValue; } else { stat.RewindsEarned = before + value; } entry.Property(x => x.RewindsEarned).IsModified = true; } if (data.RewindsActivated.HasValue) { var before = dbStatistics.RewindsActivated; var value = data.RewindsActivated.Value; if (uint.MaxValue - before < value) { stat.RewindsActivated = uint.MaxValue; } else { stat.RewindsActivated = before + value; } entry.Property(x => x.RewindsActivated).IsModified = true; } if (data.GiftsSent.HasValue) { var before = dbStatistics.GiftsSent; var value = data.GiftsSent.Value; if (uint.MaxValue - before < value) { stat.GiftsSent = uint.MaxValue; } else { stat.GiftsSent = before + value; } entry.Property(x => x.GiftsSent).IsModified = true; } if (data.GiftsReceived.HasValue) { var before = dbStatistics.GiftsReceived; var value = data.GiftsReceived.Value; if (uint.MaxValue - before < value) { stat.GiftsReceived = uint.MaxValue; } else { stat.GiftsReceived = before + value; } entry.Property(x => x.GiftsReceived).IsModified = true; } if (data.MessagesSent.HasValue) { var before = dbStatistics.MessagesSent; var value = data.MessagesSent.Value; if (uint.MaxValue - before < value) { stat.MessagesSent = uint.MaxValue; } else { stat.MessagesSent = before + value; } entry.Property(x => x.MessagesSent).IsModified = true; } if (data.BragsDone.HasValue) { var before = dbStatistics.BragsDone; var value = data.BragsDone.Value; if (uint.MaxValue - before < value) { stat.BragsDone = uint.MaxValue; } else { stat.BragsDone = before + value; } entry.Property(x => x.BragsDone).IsModified = true; } if (data.PurchasedItems.HasValue) { var before = dbStatistics.PurchasedItems; var value = data.PurchasedItems.Value; if (uint.MaxValue - before < value) { stat.PurchasedItems = uint.MaxValue; } else { stat.PurchasedItems = before + value; } entry.Property(x => x.PurchasedItems).IsModified = true; } if (data.VoiceUptime.HasValue) { var before = TimeSpan.FromMinutes(dbStatistics.VoiceUptimeMinutes); var value = data.VoiceUptime.Value; if (TimeSpan.MaxValue - before < value) { stat.VoiceUptimeMinutes = (uint)TimeSpan.MaxValue.TotalMinutes; } else { stat.VoiceUptimeMinutes = (uint)(before + value).TotalMinutes; } entry.Property(x => x.VoiceUptimeMinutes).IsModified = true; } await context.SaveChangesAsync(); }