public void TestDeleteItem() { int n = new Random().Next(); var item = GetBasicItem(n); dao.Save(item); item.Id.Should().Be.GreaterThan(0); Executing.This(() => { dao.Remove(item); }).Should().NotThrow(); }
private void Download() { var result = Post($"token={_token}&lastUpdate={Properties.Settings.Default.LastOnlineBackup}", DownloadUrl); if (string.IsNullOrEmpty(result)) { Logger.Warn("Error downloading items from remote backup server"); _nextDownload = DateTime.UtcNow.AddMinutes(30); return; } var obj = JsonConvert.DeserializeObject <PlayerItemBackupDownload>(result); if (obj.Success) { var existingItems = _playerItemDao.ListAll() .Where(m => m.OnlineId.HasValue) .Select(m => m.OnlineId.Value) .ToList(); var newItems = obj.Items.Where(onlineItem => !existingItems.Contains(onlineItem.OnlineId)).ToList(); foreach (var simplified in newItems) { var item = Map(simplified); // TODO: Get timestamp from server [on upload] _playerItemDao.Save(item); } foreach (var deleted in obj.Deleted) { var item = _playerItemDao.GetByOnlineId(deleted); if (item != null) { _playerItemDao.Remove(item); } } Logger.Info($"Added {obj.Items.Count} items after online sync"); Logger.Info($"Removed {obj.Deleted.Count} items after online sync"); // TODO: This is very low for every-day usage, and very high for frequent pc switch usage _nextDownload = DateTime.UtcNow.AddHours(1); if (obj.Items.Count > 0) { Properties.Settings.Default.LastOnlineBackup = obj.Items.Max(m => m.ModifiedDate); Properties.Settings.Default.Save(); } } else { HandleAuthenticationIssues(obj.ErrorCode); Logger.Warn($"Could not synchronize items from online backup. Error code {obj.ErrorCode}"); } }
public new void Remove(PlayerItem obj) { ThreadExecuter.Execute( () => repo.Remove(obj) ); }
/// <summary> /// Loot all the items stored on page X, and store them to the local database /// </summary> /// <param name="filename"></param> private string EmptyPageX(string filename) { Logger.InfoFormat("Looting {0}", filename); var pCrypto = new GDCryptoDataBuffer(DataBuffer.ReadBytesFromDisk(filename)); var stash = new Stash(); if (stash.Read(pCrypto)) { var lootFromIndex = GetStashToLootFrom(stash); var isHardcore = GlobalPaths.IsHardcore(filename); #if DEBUG if (stash.Tabs.Count < 5) { while (stash.Tabs.Count < 5) { stash.Tabs.Add(new StashTab()); } SafelyWriteStash(filename, stash); Logger.Info("Upgraded stash to 5 pages."); return(string.Empty); } #endif // Update the internal listing of unlooted items (in other stash tabs) var unlootedLocal = new List <Item>(); for (var idx = 0; idx < stash.Tabs.Count; idx++) { if (idx != lootFromIndex) { unlootedLocal.AddRange(stash.Tabs[idx].Items); } } Interlocked.Exchange(ref _unlootedItems, new ConcurrentBag <Item>(unlootedLocal)); StashUpdated?.Invoke(null, null); if (stash.Tabs.Count < 2) { Logger.WarnFormat($"File \"{filename}\" only contains {stash.Tabs.Count} pages. IA requires at least 2 stash pages to function properly."); return(GlobalSettings.Language.GetTag("iatag_not_enough_stash", filename, stash.Tabs.Count)); } if (stash.Tabs.Count < lootFromIndex + 1) { Logger.Warn($"You have configured IA to loot from stash {lootFromIndex + 1} but you only have {stash.Tabs.Count} pages"); return(GlobalSettings.Language.GetTag("iatag_invalid_loot_stash_number", lootFromIndex + 1, stash.Tabs.Count)); } if (stash.Tabs[lootFromIndex].Items.Count > 0) { _hasLootedItemsOnceThisSession = true; // Grab the items and clear the tab var items = stash.Tabs[lootFromIndex].Items .Where(m => m.StackCount <= 1) .Where(m => !_playerItemDao.Exists(Map(m, stash.ModLabel, isHardcore, stash.IsExpansion1))) .ToList(); var notLootedDueToStackSize = stash.Tabs[lootFromIndex].Items.Where(m => m.StackCount > 1).ToList(); if (notLootedDueToStackSize.Count > 0) { _setFeedback("Warning", GlobalSettings.Language.GetTag("iatag_feedback_stacked_not_looted", notLootedDueToStackSize.Count)); } var notLootedDueToDuplicate = stash.Tabs[lootFromIndex].Items.ToList(); notLootedDueToDuplicate.RemoveAll(m => items.Contains(m) || m.StackCount > 1); if (notLootedDueToDuplicate.Count > 0) { _setFeedback("Warning", GlobalSettings.Language.GetTag("iatag_feedback_duplicates_not_looted", notLootedDueToDuplicate.Count)); } stash.Tabs[lootFromIndex].Items.RemoveAll(e => items.Any(m => m.Equals(e))); var storedItems = StoreItemsToDatabase(items, stash.ModLabel, isHardcore, stash.IsExpansion1); var message = GlobalSettings.Language.GetTag("iatag_looted_from_stash", items.Count, lootFromIndex + 1); if (storedItems != null) { Logger.Info(message); if (!SafelyWriteStash(filename, stash)) { _playerItemDao.Remove(storedItems); } } _performedLootCallback(); return(message); } Logger.Info($"Looting of stash {lootFromIndex + 1} halted, no items available."); return(GlobalSettings.Language.GetTag("iatag_feedback_no_items_to_loot")); } Logger.Error("Could not load stash file."); Logger.Error("An update from the developer is most likely required."); return(string.Empty); }
/// <summary> /// Transfer item request from sub control /// MUST BE CALLED ON SQL THREAD /// </summary> public void TransferItem(object ignored, EventArgs _args) { StashTransferEventArgs args = _args as StashTransferEventArgs; Logger.Debug($"Item transfer requested, arguments: {args}"); if (CanTransfer()) { List <PlayerItem> items = GetItemsForTransfer(args); if (items?.Count > 0) { string file = GetTransferFile(); if (string.IsNullOrEmpty(file)) { Logger.Warn("Could not find transfer file, transfer aborted."); return; } Logger.Debug($"Found {items.Count} items to transfer"); var result = TransferItems(file, items, (int)args.Count); Logger.InfoFormat("Successfully deposited {0} out of {1} items", result.NumItemsTransferred, result.NumItemsRequested); try { var message = string.Format(GlobalSettings.Language.GetTag("iatag_stash3_success"), result.NumItemsTransferred, result.NumItemsRequested); _setFeedback(message); _browser.ShowMessage(message, "Success"); } catch (FormatException ex) { Logger.Warn(ex.Message); Logger.Warn(ex.StackTrace); Logger.Warn("Language pack error, iatag_stash3_success is of invalid format."); } if (result.NumItemsTransferred == 0) { _setTooltip(GlobalSettings.Language.GetTag("iatag_stash3_failure")); _browser.ShowMessage(GlobalSettings.Language.GetTag("iatag_stash3_failure"), "Warning"); } // Lets do this last, to make sure feedback reaches the user as fast as possible _searchWindow.UpdateListview(); } else { Logger.Warn("Could not find any items for the requested transfer"); } } else if (GlobalSettings.StashStatus == StashAvailability.OPEN) { // "InstaTransfer" is an experimental feature if (Properties.Settings.Default.InstaTransfer) // disabled for now { List <PlayerItem> items = GetItemsForTransfer(args); int numDepositedItems = 0; int numItemsToTransfer = items?.Count ?? 0; if (items == null) { Logger.Info("Item previously deposited (ghost item)"); _browser.ShowMessage("Item previously deposited (ghost item)", "Warning"); _searchWindow.UpdateListviewDelayed(); } else { foreach (var item in items) { if (TransferToOpenStash(item)) { _dao.Remove(item); numDepositedItems++; } } var message = string.Format(GlobalSettings.Language.GetTag("iatag_stash3_success"), numDepositedItems, numItemsToTransfer); _setFeedback(message); _browser.ShowMessage(message, "Success"); Logger.InfoFormat("Successfully deposited {0} out of {1} items", numDepositedItems, numItemsToTransfer); if (numDepositedItems > 0) { _searchWindow.UpdateListviewDelayed(); } } } else { var message = GlobalSettings.Language.GetTag("iatag_deposit_stash_open"); _setFeedback(message); _setTooltip(message); _browser.ShowMessage(message, "Warning"); } } else if (GlobalSettings.StashStatus == StashAvailability.SORTED) { _setFeedback(GlobalSettings.Language.GetTag("iatag_deposit_stash_sorted")); _browser.ShowMessage(GlobalSettings.Language.GetTag("iatag_deposit_stash_sorted"), "Warning"); } else if (GlobalSettings.StashStatus == StashAvailability.UNKNOWN) { _setFeedback(GlobalSettings.Language.GetTag("iatag_deposit_stash_unknown_feedback")); _setTooltip(GlobalSettings.Language.GetTag("iatag_deposit_stash_unknown_tooltip")); _browser.ShowMessage(GlobalSettings.Language.GetTag("iatag_deposit_stash_unknown_feedback"), "Warning"); } }
/// <summary> /// Loot all the items stored on page X, and store them to the local database /// </summary> /// <param name="filename"></param> private string EmptyPageX(string filename) { Logger.InfoFormat("Looting {0}", filename); GDCryptoDataBuffer pCrypto = new GDCryptoDataBuffer(DataBuffer.ReadBytesFromDisk(filename)); Stash stash = new Stash(); if (stash.Read(pCrypto)) { int lootFromIndex; if (Settings.Default.StashToLootFrom == 0) { lootFromIndex = stash.Tabs.Count - 1; } else { lootFromIndex = Settings.Default.StashToLootFrom - 1; } bool isHardcore = GlobalPaths.IsHardcore(filename); #if DEBUG if (stash.Tabs.Count < 5) { while (stash.Tabs.Count < 5) { stash.Tabs.Add(new StashTab()); } SafelyWriteStash(filename, stash); Logger.Info("Upgraded stash to 5 pages."); return(string.Empty); } #endif // Update the internal listing of unlooted items (in other stash tabs) List <Item> unlootedLocal = new List <Item>(); for (var idx = 0; idx < stash.Tabs.Count; idx++) { if (idx != lootFromIndex) { unlootedLocal.AddRange(stash.Tabs[idx].Items); } } Interlocked.Exchange(ref _unlootedItems, new ConcurrentBag <Item>(unlootedLocal)); StashUpdated?.Invoke(null, null); if (stash.Tabs.Count < 2) { Logger.WarnFormat( "File \"{0}\" only contains {1} pages, must have at least 2 pages to function properly.", filename, stash.Tabs.Count); return ($"File \"{filename}\" only contains {stash.Tabs.Count} pages, must have at least 2 pages to function properly."); } if (stash.Tabs.Count < lootFromIndex + 1) { var message = $"You have configured IA to loot from {lootFromIndex + 1} but you only have {stash.Tabs.Count} pages"; Logger.Warn(message); return(message); } if (stash.Tabs[lootFromIndex].Items.Count > 0) { //_hasLootedItemsOnceThisSession = true; // Grab the items and clear the tab List <Item> items = new List <Item>(stash.Tabs[lootFromIndex].Items); stash.Tabs[lootFromIndex].Items.Clear(); List <PlayerItem> storedItems = StoreItemsToDatabase(items, stash.ModLabel, isHardcore, stash.IsExpansion1); if (storedItems != null) { Logger.Info($"Looted {items.Count} items from stash {lootFromIndex + 1}"); if (!SafelyWriteStash(filename, stash)) { // TODO: Delete from DB _playerItemDao.Remove(storedItems); } } _performedLootCallback(); return($"Looted {items.Count} items from stash {lootFromIndex + 1}"); } Logger.Info($"Looting of stash {lootFromIndex + 1} halted, no items available."); return(GlobalSettings.Language.GetTag("iatag_feedback_no_items_to_loot")); } Logger.Error("Could not load stash file."); Logger.Error("An update from the developer is most likely required."); return(string.Empty); }