void bwDoAllTasks_DoWork(object sender, DoWorkEventArgs e) { DateTime lastAccListRefresh = DateTime.Now.AddDays(-1); var UpdateAccListDelegate = new Action(delegate() { lvAccs.ItemsSource = AccManager.Accounts; }); while (!bwDoAllTasks.CancellationPending) { DateTime dtNow = DateTime.Now; if ((dtNow - lastAccListRefresh).TotalSeconds >= 60) { //lvAccs.Dispatcher.Invoke(DispatcherPriority.Normal, UpdateAccListDelegate); AccManager.UpdateLastRefreshText(); lastAccListRefresh = dtNow; int iLastSync = Properties.Settings.Default.LastSync; int mLastSync = ((int)((Helpers.UnixTime.Now - iLastSync) / 60)); string sLastSync = (Properties.Settings.Default.LastSync == 0 ? "never" : mLastSync + "mins ago");//Sync items with others (Last sync: 1234mins ago) bSync.Dispatcher.Invoke( System.Windows.Threading.DispatcherPriority.Normal, new Action( delegate() { bSync.Content = "Synchronize item list (Last sync: " + sLastSync + ")"; bSync.IsEnabled = (mLastSync >= 2); } ) ); } if (OwnersNeedingSync.Count > 0) { SyncDataHelper sdh = OwnersNeedingSync.Dequeue(); AddLogEntry("[" + sdh.owner + "|" + StaticVariables.LeagueNames[sdh.leagueId] + "] Sync: Uploading stash changes to server.."); byte[] exp = ItemsManagers[sdh.leagueId].ExportBZip2Bytes(sdh.owner); if (HttpImportExport.Export(exp, sdh.owner, sdh.leagueId, sdh.version)) { AddLogEntry("[" + sdh.owner + "|" + StaticVariables.LeagueNames[sdh.leagueId] + "] Sync: Done!"); } else { AddLogEntry("[" + sdh.owner + "|" + StaticVariables.LeagueNames[sdh.leagueId] + "] Sync: Failed!"); } } System.Threading.Thread.Sleep(250); } }
private void bwDownload_DoWork(object sender, DoWorkEventArgs e) { UpdateProgress("Downloading changes index..."); byte[] list = HttpImportExport.GetList(); if (list == null) { return; } List <SyncDataHelper> RemoteData = new List <SyncDataHelper>(); MemoryStream ms = new MemoryStream(list); StreamReader sr = new StreamReader(ms); string line = sr.ReadLine(); if (line != "OK") { line = sr.ReadToEnd(); MessageBox.Show(line); sr.Close(); ms.Close(); return; } while (!sr.EndOfStream) { line = sr.ReadLine(); string[] data = line.Split(' '); SyncDataHelper sdh = new SyncDataHelper(); sdh.owner = data[0]; sdh.leagueId = int.Parse(data[1]); sdh.version = int.Parse(data[2]); RemoteData.Add(sdh); } sr.Close(); ms.Close(); sr.Dispose(); ms.Dispose(); //first we check if server has any outdated info (need to disable on release) for (int iLeague = 0; iLeague < Helpers.StaticVariables.LeagueNames.Length; iLeague++) { foreach (var cm in ItemsManagers[iLeague].OwnerItemsListUpdateTime) { bool found = false; foreach (SyncDataHelper sdh in RemoteData) { if (sdh.owner == cm.Key && sdh.leagueId == iLeague) { if (sdh.version >= cm.Value) { found = true; } break; } } if (!found) //if not found equal or newer, upload it to server { bool itsOwnAcc = false; foreach (PoeAccHandler acc in AccManager.Accounts) { if (acc.UserDisplayName == cm.Key) { itsOwnAcc = true; break; } } if (itsOwnAcc) { byte[] exp = ItemsManagers[iLeague].ExportBZip2Bytes(cm.Key); UpdateProgress("Uploading [" + cm.Key + "|" + StaticVariables.LeagueNames[iLeague] + "] stash..."); HttpImportExport.Export(exp, cm.Key, iLeague, ItemsManagers[iLeague].OwnerItemsListUpdateTime[cm.Key]); } } } } //now check if we don't need any updates foreach (SyncDataHelper sdh in RemoteData) { bool found = false; if (ItemsManagers[sdh.leagueId].OwnerItemsListUpdateTime.ContainsKey(sdh.owner)) { if (ItemsManagers[sdh.leagueId].OwnerItemsListUpdateTime[sdh.owner] >= sdh.version) { found = true; } } if (!found) { ChangesFound = true; UpdateProgress("Downloading [" + sdh.owner + "|" + StaticVariables.LeagueNames[sdh.leagueId] + "]"); byte[] serializedPibo = HttpImportExport.Import(sdh.owner, sdh.leagueId); if (serializedPibo == null) { MWnd.AddLogEntry("[" + sdh.owner + "|" + StaticVariables.LeagueNames[sdh.leagueId] + "] Unable to retrieve the stash. Aborting"); return; } DownloadedSerializedStashes.Add(sdh.owner + "|" + StaticVariables.LeagueNames[sdh.leagueId], serializedPibo); } } SyncSucceeded = true; }
private void bRefreshAccStash_Click(object sender, RoutedEventArgs e) { PoeAccHandler pwr = ((PoeAccHandler)((FrameworkElement)e.OriginalSource).DataContext); bool rechecked = false; repeatStashQuery: if (!pwr.LoggedIn) { if (!pwr.Login()) { AddLogEntry("Can't update stash. Unable to connect or log-in"); return; } else { AccManager.SaveAccounts(); } } string owner = pwr.UserDisplayName; string leagueName = StaticVariables.LeagueNames[(int)ActiveLeagueId]; StashDownloadWaitWindow sdww = new StashDownloadWaitWindow(pwr, leagueName); sdww.Owner = this; sdww.ShowDialog(); string[] jsonStashes = sdww.JsonStash; if (jsonStashes == null) { AddLogEntry("[" + owner + "|" + leagueName + "] Unable to connect. Aborting"); return; } if (jsonStashes.Length == 1 && !rechecked) { if (!pwr.CheckLogin()) { AddLogEntry("[" + owner + "|" + leagueName + "] Error while downloading stashes. Re-loging in to website and trying again."); rechecked = true; goto repeatStashQuery; } } if (jsonStashes.Length == 0 || jsonStashes[0] == null) { AddLogEntry("[" + owner + "|" + leagueName + "] No stashes have been found. Probably this account has no characters in [" + leagueName + "] league."); return; } var stashList = PoeStashParser.DeserializeStashes(jsonStashes, owner); if (stashList == null) { AddLogEntry("[" + owner + "|" + leagueName + "] Error while parsing json stashes. Aborting"); return; } if (stashList.Count == 0) { AddLogEntry("[" + owner + "|" + leagueName + "] No acceptable items have been found in the stash. Aborting"); return; } bool parseSuccess = ItemsManagers[(int)ActiveLeagueId].ParseStashList(stashList); if (parseSuccess && Properties.Settings.Default.SyncKey != "") //save only if changes been made { //ItemManager.ItemsByCatForListView.SortTree(); //Properties.Settings.Default.Items = Serializer<PoeItemsManager>.SerializeToString(ItemManager); //Properties.Settings.Default.Save(); //ItemManager[(int)ActiveLeagueId].Export(owner, "Export\\" + owner + "." + leagueName + ".poe"); SyncDataHelper sdh = new SyncDataHelper(); sdh.owner = owner; sdh.leagueId = (int)ActiveLeagueId; sdh.version = ItemsManagers[(int)ActiveLeagueId].OwnerItemsListUpdateTime[owner]; OwnersNeedingSync.Enqueue(sdh); } AccManager.SaveAccounts(); //save changed acc data like login if needed or last refresh. PrintItemChanges((int)ActiveLeagueId); SaveAllItemsManagers(); }