Beispiel #1
0
        public SerializedPlayerItems Serialize()
        {
            if (_settings.GetPersistent().BuddySyncUserIdV2 != 0)
            {
                SerializedPlayerItems s = GetItems();
                s.Description = _settings.GetPersistent().BuddySyncDescription;
                s.UserId      = _settings.GetPersistent().BuddySyncUserIdV2 ?? 0L;
                s.UUID        = RuntimeSettings.Uuid;
                return(s);
            }

            return(null);
        }
Beispiel #2
0
        public SerializedPlayerItems Serialize()
        {
            if ((long)Properties.Settings.Default.BuddySyncUserIdV2 != 0)
            {
                SerializedPlayerItems s = GetItems();
                s.Description = Properties.Settings.Default.BuddySyncDescription;
                s.UserId      = (long)Properties.Settings.Default.BuddySyncUserIdV2;
                s.UUID        = GlobalSettings.Uuid;
                return(s);
            }

            return(null);
        }
Beispiel #3
0
        /// <summary>
        /// Deserialize and store items retrieved from BuddySync
        /// </summary>
        /// <param name="data"></param>
        public void DeserializeAndSave(SerializedPlayerItems data)
        {
            if (string.IsNullOrEmpty(data.Items))
            {
                Logger.Debug("Either data or items is null, or no items received. Skipping buddy items update");
                return;
            }

            if (data.Verification != BuddyVerificationId)
            {
                Logger.Warn($"Received verification id \"{data.Verification}\", expected 0x{BuddyVerificationId}. Buddy most likely has an older version.");
                return;
            }

            // Make sure we don't store an empty buddy description, would get interpreted as an owned item :o
            if (string.IsNullOrEmpty(data.Description))
            {
                data.Description = $"Buddy {data.UserId}";
            }
            else if (data.Description.Length < 3)
            {
                data.Description = $"Buddy {data.Description}";
            }

            Logger.Debug($"Deserializing items for {data.UserId}");
            var preFilteredBuddyItems = JsonConvert.DeserializeObject <List <JsonBuddyItem> >(data.Items);
            var buddyItems            = preFilteredBuddyItems.Where(m => m.Id > 0).ToList();

            if (buddyItems.Count != preFilteredBuddyItems.Count)
            {
                Logger.Warn($"{preFilteredBuddyItems.Count - buddyItems.Count} items were filtered out due to a version missmatch");
            }



            Logger.Debug($"Storing {buddyItems.Count} items for {data.UserId}");
            _buddyItemDao.SetItems(data.UserId, data.Description, buddyItems);

            Logger.Debug($"Updating item rarity for {data.UserId}");
            var items = _buddyItemDao.ListItemsWithMissingRarity();

            _buddyItemDao.UpdateRarity(items);

            Logger.Debug($"Updating item names for {data.UserId}");
            _buddyItemDao.UpdateNames(_buddyItemDao.ListItemsWithMissingName());

            Logger.Debug($"Updating item level requirements for {data.UserId}");
            _buddyItemDao.UpdateLevelRequirements(_buddyItemDao.ListItemsWithMissingLevelRequirement());

            Logger.Debug($"Buddy sync complete for {data.UserId}");
        }
Beispiel #4
0
        /// <summary>
        /// Upload item data to buddies
        /// Will return false if still on cooldown, or item count is the same as before.
        /// (Will not update if adding one item, and removing another)
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool UploadBuddyData(SerializedPlayerItems data)
        {
            if (!string.IsNullOrEmpty(data.Items) && data.UserId > 0)
            {
                if (data.Items.Length != _lastBuddyUpdateLength)
                {
                    _lastBuddyUpdateLength = data.Items.Length;

                    string json = JsonConvert.SerializeObject(data);

                    var compressedJson        = GzipCompressionHandler.CompressAndConvertToBase64(json);
                    var encodedCompressedJson = HttpUtility.UrlEncode(compressedJson);

                    /*var uncompressedSize = Encoding.ASCII.GetByteCount(json);
                     * var compressedSize = Encoding.ASCII.GetByteCount(compressedJson);
                     * var compressedEncodedSize = Encoding.ASCII.GetByteCount(encodedCompressedJson);
                     *
                     * Logger.Debug($"Transmitting buddy items, {uncompressedSize} before compression, {compressedSize} after compression and {compressedEncodedSize} after URL encoding.");*/
                    var result = UploadBuddyData($"json={encodedCompressedJson}", UrlBuddyUpload);

                    // Access denied, we'll need a new user id it seems.
                    // Something must be wrong.
                    if (result != null && result.Status == 401)
                    {
                        Properties.Settings.Default.BuddySyncUserIdV2 = 0;
                        Properties.Settings.Default.Save();
                        CreateUserId();
                    }

                    return(result != null && result.Success);
                }
                else
                {
                    Logger.Info("Buddy items not updated, no new changes detected.");
                    return(true);
                }
            }
            else
            {
                Logger.Info("Tried to upload buddy data, but either userid or items were null");
            }

            return(false);
        }
Beispiel #5
0
        /// <summary>
        /// Upload item data to buddies
        /// Will return false if still on cooldown, or item count is the same as before.
        /// (Will not update if adding one item, and removing another)
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool UploadBuddyData(SerializedPlayerItems data)
        {
            if (!string.IsNullOrEmpty(data.Items) && data.UserId > 0)
            {
                if (data.Items.Length != _lastBuddyUpdateLength)
                {
                    _lastBuddyUpdateLength = data.Items.Length;

                    string json = JsonConvert.SerializeObject(data);

                    var compressedJson        = GzipCompressionHandler.CompressAndConvertToBase64(json);
                    var encodedCompressedJson = HttpUtility.UrlEncode(compressedJson);
                    var result = UploadBuddyData($"json={encodedCompressedJson}", UrlBuddyUpload);

                    // Access denied, we'll need a new user id it seems.
                    // Something must be wrong.
                    if (result != null && result.Status == 401)
                    {
                        _settings.GetPersistent().BuddySyncUserIdV2 = 0;
                        CreateUserId();
                    }

                    return(result != null && result.Success);
                }
                else
                {
                    Logger.Info("Buddy items not updated, no new changes detected.");
                    return(true);
                }
            }
            else
            {
                Logger.Info("Tried to upload buddy data, but either userid or items were null");
            }

            return(false);
        }
        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            if (Thread.CurrentThread.Name == null)
            {
                Thread.CurrentThread.Name = "BuddyBackground";
            }

            BackgroundWorker worker       = sender as BackgroundWorker;
            Serializer       serializer   = new Serializer(_buddyItemDao, _playerItemDao, _settings);
            Synchronizer     synchronizer = new Synchronizer(_settings);


            worker?.ReportProgress(ProgressSetUid, synchronizer.CreateUserId());

            while (!worker.CancellationPending)
            {
                try {
                    Thread.Sleep(10);

                    if (_cooldown.IsReady)
                    {
                        // Sync up, if UI thread has sent us any data.
                        SerializedPlayerItems element = serializer.Serialize();
                        if (element != null)
                        {
                            if (!synchronizer.UploadBuddyData(element))
                            {
                                Logger.Warn("Buddy items upload failed");
                            }
                        }
                        else
                        {
                            Logger.Warn("Buddy Items upload NULL!?");
                        }



                        // Sync down, and pass data to UI thread
                        List <SerializedPlayerItems> syncdownData = synchronizer.DownloadBuddyItems(_subscribers);
                        if (syncdownData != null && syncdownData.Count > 0)
                        {
                            Logger.InfoFormat("Downloaded itemdata for {0} buddies.", syncdownData.Count);

                            var deserializer = new Serializer(_buddyItemDao, _playerItemDao, _settings);
                            foreach (var item in syncdownData)
                            {
                                if (item.UserId > 0)
                                {
                                    deserializer.DeserializeAndSave(item);
                                }
                            }

                            worker.ReportProgress(ProgressStoreBuddydata);
                        }

                        _cooldown.Reset();
                    }
                }
                catch (NullReferenceException ex) {
                    Logger.Info("The following exception is logged, but can safely be ignored:");
                    Logger.Warn(ex.Message);
                    Logger.Warn(ex.StackTrace);
                }
                catch (Exception ex) {
                    Logger.Error(ex.Message);
                    Logger.Error(ex.StackTrace);
                    ExceptionReporter.ReportException(ex);
                }
            }
        }