public async Task <bool> ProcessDataAsync(string content, string contentId, string direction) { var itemToProcess = new ItemToProcess { ContentId = contentId, Direction = direction, Size = content.Length, Text = content }; var currenResult = _comparisonRepository.GetResultByContentId(contentId); var itemOnDb = await _itemToProcessRepository.GetDataFromDbById(contentId); //If no item in the DB create new Process if (currenResult == null) { SaveNewContentToDb(content, contentId, itemToProcess); return(true); } //Process if already in DB if ((itemOnDb.Direction == "right" && direction == "left") || (itemOnDb.Direction == "left" && direction == "right")) { return(ProcessContentAlreadyOnDb(content, contentId, direction, itemToProcess, itemOnDb)); } return(false); }
public void ShouldUpdateObjectWithProperStatusAndCompareValues() { Random rnd = new Random(); var id = rnd.Next(); var mockComparisonRepository = new Mock <IProcessResultRepository>(); mockComparisonRepository .Setup(x => x.UpdateResultByContentId(It.IsAny <ProcessResult>(), It.IsAny <string>())) .Returns((ProcessResult processResult, string contentId) => processResult); mockComparisonRepository .Setup(x => x.GetResultByContentId(It.IsAny <string>())) .Returns((string contentId) => { return(new ProcessResult { ProcessResultId = id, ContentId = "teste" }); }); var itemToProcess = new ItemToProcess { Hash = "70A4B9F4707D258F559F91615297A3EC", Size = 200 }; var mockDiffenceSearchService = new Mock <IDiffenceSearchService>(); var comparisonService = new ComparisonService(mockComparisonRepository.Object, mockDiffenceSearchService.Object); var comparisonResult = comparisonService.SaveProcessResult(itemToProcess, itemToProcess); Assert.Equal(id, comparisonResult.ProcessResultId); Assert.Equal(StatusEnum.DONE, comparisonResult.status); Assert.Equal("teste", comparisonResult.ContentId); Assert.True(comparisonResult.IsEqual); Assert.True(comparisonResult.IsEqualSize); }
public async Task SaveDataToDB(ItemToProcess itemToProcess) { using (IDbConnection conn = _DbManager.CreateConnection()) { string insertQuery = @"INSERT INTO ItemToProcess ([ContentId], [Direction], [Size],[Hash],[Text]) VALUES ( @ContentId, @Direction, @Size, @Hash,@Text)"; conn.Open(); var result = conn.Execute(insertQuery, itemToProcess); var identity = await conn.InsertAsync(itemToProcess); } }
private void SaveNewContentToDb(string content, string contentId, ItemToProcess itemToProcess) { //Save the Id in the data base and start the processing of new item async _comparisonService.CreateNewComparison(contentId); new Thread(() => { itemToProcess.Hash = _hashService.CreateHash(content); _comparisonService.CreateNewComparison(contentId); _itemToProcessRepository.SaveDataToDB(itemToProcess); _comparisonService.UpdateComparisonToProcessing(contentId, StatusEnum.PROCESSED_FIRST); }).Start(); }
public ProcessResult SaveProcessResult(ItemToProcess itemToProcessRight, ItemToProcess itemToProcessLeft) { var processResult = _comparisonRepository.GetResultByContentId(itemToProcessRight.ContentId); processResult.IsEqual = itemToProcessRight.Hash == itemToProcessLeft.Hash; processResult.IsEqualSize = itemToProcessRight.Size == itemToProcessLeft.Size; //if same size find the difference if (processResult.IsEqualSize) { var diff = _diffenceSearchService.GetDifferences(itemToProcessRight.Text, itemToProcessLeft.Text, itemToProcessRight.Size); processResult.Differences = diff; } processResult.status = StatusEnum.DONE; return(_comparisonRepository.UpdateResultByContentId(processResult, itemToProcessRight.ContentId)); }
private bool ProcessContentAlreadyOnDb(string content, string contentId, string direction, ItemToProcess itemToProcess, ItemToProcess itemOnDb) { var currenResult = _comparisonRepository.GetResultByContentId(contentId); //If item already done returns if new start thread if (currenResult.status == StatusEnum.NEW || currenResult.status == StatusEnum.PROCESSED_FIRST) { _comparisonService.UpdateComparisonToProcessing(contentId, StatusEnum.PROCESSED_SECOND_STARTED); new Thread(() => { //wait while the previuos value is processed while (currenResult.status != StatusEnum.PROCESSED_FIRST) { currenResult = _comparisonRepository.GetResultByContentId(contentId); Thread.Sleep(100); } //if same size calculate the hash to compare contents if (itemToProcess.Size == itemOnDb.Size) { itemToProcess.Hash = _hashService.CreateHash(content); } else { itemToProcess.Hash = ""; } if (itemOnDb.Direction == "right" && direction == "left") { _comparisonService.SaveProcessResult(itemOnDb, itemToProcess); _comparisonService.UpdateComparisonToProcessing(contentId, StatusEnum.DONE); } if (itemOnDb.Direction == "left" && direction == "right") { _comparisonService.SaveProcessResult(itemToProcess, itemOnDb); _comparisonService.UpdateComparisonToProcessing(contentId, StatusEnum.DONE); } }).Start(); return(true); } return(false); }
bool DoAutoPack() { bool waitingForIds = false; // Get all of our side pack information and put them in the correct order int[] packs = new int[CoreManager.Current.WorldFilter[CoreManager.Current.CharacterFilter.Id].Values(LongValueKey.PackSlots) + 1]; // Main pack packs[0] = CoreManager.Current.CharacterFilter.Id; // Load the side pack information foreach (WorldObject obj in CoreManager.Current.WorldFilter.GetByContainer(CoreManager.Current.CharacterFilter.Id)) { if (obj.ObjectClass != ObjectClass.Container) continue; packs[obj.Values(LongValueKey.Slot) + 1] = obj.Id; } // Process our inventory Collection<ItemToProcess> itemsToProcess = new Collection<ItemToProcess>(); foreach (WorldObject item in CoreManager.Current.WorldFilter.GetInventory()) { if (blackLitedItems.Contains(item.Id)) continue; // If the item is equipped or wielded, don't process it. if (item.Values(LongValueKey.EquippedSlots, 0) > 0 || item.Values(LongValueKey.Slot, -1) == -1) continue; // If the item is a container or a foci, don't process it. if (item.ObjectClass == ObjectClass.Container || item.ObjectClass == ObjectClass.Foci) continue; // Convert the item into a VT GameItemInfo object uTank2.LootPlugins.GameItemInfo itemInfo = uTank2.PluginCore.PC.FWorldTracker_GetWithID(item.Id); if (itemInfo == null) { // This happens all the time for aetheria that has been converted continue; } if (((VTClassic.LootCore)lootProfile).DoesPotentialItemNeedID(itemInfo)) { waitingForIds = true; continue; } uTank2.LootPlugins.LootAction result = ((VTClassic.LootCore)lootProfile).GetLootDecision(itemInfo); if (!result.IsKeepUpTo) continue; // Separate the Data1 int result into a byte[]. Meaning, turn 123456 into { 1, 2, 3, 4, 5, 6 } char[] packNumbersAsChar = result.Data1.ToString().ToCharArray(); byte[] packNumbers = new byte[packNumbersAsChar.Length]; for (int i = 0 ; i < packNumbersAsChar.Length ; i++) packNumbers[i] = byte.Parse(packNumbersAsChar[i].ToString()); // If this item is already in its primary pack, we don't need to queue it up. if (item.Container == packs[packNumbers[0]]) continue; int[] targetPackIds = new int[packNumbers.Length]; for (int i = 0 ; i < packNumbers.Length ; i++) targetPackIds[i] = packs[packNumbers[i]]; ItemToProcess itemToProcess = new ItemToProcess(item.Id, targetPackIds); itemsToProcess.Add(itemToProcess); } // Lets go through our list and see if any items are in their primary target pack for (int i = itemsToProcess.Count - 1 ; i >= 0 ; i--) { ItemToProcess itemToProcess = itemsToProcess[i]; WorldObject item = CoreManager.Current.WorldFilter[itemToProcess.Id]; if (item == null) { itemsToProcess.RemoveAt(i); continue; } if (item.Container == itemToProcess.TargetPackIds[0]) itemsToProcess.RemoveAt(i); } Collection<int> itemsToSkip = new Collection<int>(); // Lets see if we can find an item that can be moved to its target pack for (int packIndex = 0 ; packIndex < 10 ; packIndex++) { for (int i = itemsToProcess.Count - 1 ; i >= 0 ; i--) { ItemToProcess itemToProcess = itemsToProcess[i]; WorldObject item = CoreManager.Current.WorldFilter[itemToProcess.Id]; if (itemToProcess.TargetPackIds.Length <= packIndex) continue; if (itemsToSkip.Contains(item.Id)) continue; // Check to see if this item is already in the target pack if (item.Container == itemToProcess.TargetPackIds[packIndex]) { itemsToSkip.Add(item.Id); continue; } // Check to see that the target is even a pack. WorldObject target = CoreManager.Current.WorldFilter[itemToProcess.TargetPackIds[packIndex]]; if (target == null || (target.ObjectClass != ObjectClass.Container && target.ObjectClass != ObjectClass.Player)) continue; if (Util.GetFreePackSlots(itemToProcess.TargetPackIds[packIndex]) > 0) { if (currentWorkingId != item.Id) { currentWorkingId = item.Id; currentWorkingIdFirstAttempt = DateTime.Now; } else { if (DateTime.Now - currentWorkingIdFirstAttempt > TimeSpan.FromSeconds(10)) { Debug.WriteToChat("Blacklisting item: " + item.Id + ", " + item.Name); blackLitedItems.Add(item.Id); return true; } } CoreManager.Current.Actions.MoveItem(item.Id, itemToProcess.TargetPackIds[packIndex], 0, true); return true; } } } if (waitingForIds) return true; return false; }
bool DoAutoPack() { bool waitingForIds = false; // Get all of our side pack information and put them in the correct order int[] packs = new int[CoreManager.Current.WorldFilter[CoreManager.Current.CharacterFilter.Id].Values(LongValueKey.PackSlots) + 1]; // Main pack packs[0] = CoreManager.Current.CharacterFilter.Id; // Load the side pack information foreach (WorldObject obj in CoreManager.Current.WorldFilter.GetByContainer(CoreManager.Current.CharacterFilter.Id)) { if (obj.ObjectClass != ObjectClass.Container) { continue; } packs[obj.Values(LongValueKey.Slot) + 1] = obj.Id; } // Process our inventory Collection <ItemToProcess> itemsToProcess = new Collection <ItemToProcess>(); foreach (WorldObject item in CoreManager.Current.WorldFilter.GetInventory()) { if (blackLitedItems.Contains(item.Id)) { continue; } // If the item is equipped or wielded, don't process it. if (item.Values(LongValueKey.EquippedSlots, 0) > 0 || item.Values(LongValueKey.Slot, -1) == -1) { continue; } // If the item is a container or a foci, don't process it. if (item.ObjectClass == ObjectClass.Container || item.ObjectClass == ObjectClass.Foci) { continue; } // Convert the item into a VT GameItemInfo object uTank2.LootPlugins.GameItemInfo itemInfo = uTank2.PluginCore.PC.FWorldTracker_GetWithID(item.Id); if (itemInfo == null) { // This happens all the time for aetheria that has been converted continue; } if (((VTClassic.LootCore)lootProfile).DoesPotentialItemNeedID(itemInfo)) { waitingForIds = true; continue; } uTank2.LootPlugins.LootAction result = ((VTClassic.LootCore)lootProfile).GetLootDecision(itemInfo); if (!result.IsKeepUpTo) { continue; } // Separate the Data1 int result into a byte[]. Meaning, turn 123456 into { 1, 2, 3, 4, 5, 6 } char[] packNumbersAsChar = result.Data1.ToString().ToCharArray(); byte[] packNumbers = new byte[packNumbersAsChar.Length]; for (int i = 0; i < packNumbersAsChar.Length; i++) { packNumbers[i] = byte.Parse(packNumbersAsChar[i].ToString()); } // If this item is already in its primary pack, we don't need to queue it up. if (item.Container == packs[packNumbers[0]]) { continue; } int[] targetPackIds = new int[packNumbers.Length]; for (int i = 0; i < packNumbers.Length; i++) { targetPackIds[i] = packs[packNumbers[i]]; } ItemToProcess itemToProcess = new ItemToProcess(item.Id, targetPackIds); itemsToProcess.Add(itemToProcess); } // Lets go through our list and see if any items are in their primary target pack for (int i = itemsToProcess.Count - 1; i >= 0; i--) { ItemToProcess itemToProcess = itemsToProcess[i]; WorldObject item = CoreManager.Current.WorldFilter[itemToProcess.Id]; if (item == null) { itemsToProcess.RemoveAt(i); continue; } if (item.Container == itemToProcess.TargetPackIds[0]) { itemsToProcess.RemoveAt(i); } } Collection <int> itemsToSkip = new Collection <int>(); // Lets see if we can find an item that can be moved to its target pack for (int packIndex = 0; packIndex < 10; packIndex++) { for (int i = itemsToProcess.Count - 1; i >= 0; i--) { ItemToProcess itemToProcess = itemsToProcess[i]; WorldObject item = CoreManager.Current.WorldFilter[itemToProcess.Id]; if (itemToProcess.TargetPackIds.Length <= packIndex) { continue; } if (itemsToSkip.Contains(item.Id)) { continue; } // Check to see if this item is already in the target pack if (item.Container == itemToProcess.TargetPackIds[packIndex]) { itemsToSkip.Add(item.Id); continue; } // Check to see that the target is even a pack. WorldObject target = CoreManager.Current.WorldFilter[itemToProcess.TargetPackIds[packIndex]]; if (target == null || (target.ObjectClass != ObjectClass.Container && target.ObjectClass != ObjectClass.Player)) { continue; } if (Util.GetFreePackSlots(itemToProcess.TargetPackIds[packIndex]) > 0) { if (currentWorkingId != item.Id) { currentWorkingId = item.Id; currentWorkingIdFirstAttempt = DateTime.UtcNow; } else { if (DateTime.UtcNow - currentWorkingIdFirstAttempt > TimeSpan.FromSeconds(10)) { Debug.WriteToChat("Blacklisting item: " + item.Id + ", " + item.Name); blackLitedItems.Add(item.Id); return(true); } } CoreManager.Current.Actions.MoveItem(item.Id, itemToProcess.TargetPackIds[packIndex], 0, true); return(true); } } } if (waitingForIds) { return(true); } return(false); }