-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReforgeHelper.cs
More file actions
814 lines (707 loc) · 26.5 KB
/
ReforgeHelper.cs
File metadata and controls
814 lines (707 loc) · 26.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
using ReforgeHelper.Helper;
using ExileCore2;
using ExileCore2.PoEMemory;
using ExileCore2.PoEMemory.Components;
using ExileCore2.PoEMemory.MemoryObjects;
using System.Drawing;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Numerics;
using System.Threading.Tasks;
using ExileCore2.Shared.Enums;
using System.Windows.Forms;
using System.Threading;
using ExileCore2.Shared.Cache;
namespace ReforgeHelper;
public class ReforgeHelper : BaseSettingsPlugin<ReforgeHelperSettings>
{
private TripletManager _tripletManager;
private Element _lastFoundBench = null;
private Element _reforgeButton = null;
private Element[] _itemSlots = new Element[3];
private DateTime _lastBenchLog = DateTime.MinValue;
private const int LOG_INTERVAL_MS = 1000;
private Random _random = new Random();
private bool _isProcessing = false;
private int _currentTripletIndex = 0;
private CancellationTokenSource _processingCts;
private List<List<TripletData>> _currentTriplets = new();
public ReforgeHelper()
{
Name = "Reforge Helper";
}
public override bool Initialise()
{
RFLogger.Info("Starting initialization...");
try
{
if (GameController == null || Settings == null)
{
RFLogger.Error("GameController or Settings is null!");
return false;
}
_tripletManager = new TripletManager(GameController, Settings);
RFLogger.Info("Successfully initialized");
RFLogger.Initialize(Settings);
return true;
}
catch (Exception ex)
{
RFLogger.Error($"Initialization failed: {ex.Message}");
return false;
}
}
private async Task MoveCursorSmoothly(Vector2 targetPos, CancellationToken cancellationToken = default)
{
var currentPos = Input.ForceMousePosition;
var distance = Vector2.Distance(currentPos, targetPos);
var steps = Math.Clamp((int)(distance / 15), 8, 20);
var totalTime = _random.Next(30, 80);
var stepDelay = totalTime / steps;
for (var i = 0; i < steps; i++)
{
if (cancellationToken.IsCancellationRequested) return;
var t = (i + 1) / (float)steps;
var randomOffset = new Vector2(
((float)_random.NextDouble() * 2.5f) - 1.25f,
((float)_random.NextDouble() * 2.5f) - 1.25f
);
var nextPos = Vector2.Lerp(currentPos, targetPos, t) + randomOffset;
Input.SetCursorPos(nextPos);
await Task.Delay(_random.Next(2, 5), cancellationToken);
}
Input.SetCursorPos(targetPos);
await Task.Delay(25, cancellationToken);
}
private async Task ClickElement(Element element, CancellationToken cancellationToken = default)
{
if (element == null) return;
try
{
var rect = element.GetClientRect();
var center = rect.Center;
var targetPos = new Vector2(
center.X + _random.Next(-3, 4),
center.Y + _random.Next(-3, 4)
);
await MoveCursorSmoothly(targetPos, cancellationToken);
Input.KeyDown(Keys.LButton);
await Task.Delay(_random.Next(15, 30), cancellationToken);
Input.KeyUp(Keys.LButton);
await Task.Delay(_random.Next(50, 100), cancellationToken);
}
catch (Exception ex)
{
LogDebug($"Error clicking element: {ex.Message}");
}
}
private bool IsReforgeBench(Element element)
{
try
{
if (element == null) return false;
if (!element.IsValid || !element.IsVisible) return false;
if (element.PathFromRoot == null) return false;
return element.PathFromRoot.Contains("ReforgingBench");
}
catch (Exception ex)
{
if (Settings.EnableDebug)
{
DebugWindow.LogError($"[ReforgeHelper] Error in IsReforgeBench: {ex.Message}");
}
return false;
}
}
private Element FindReforgeButton(Element bench)
{
try
{
var btn = bench?.Children?.ElementAtOrDefault(3)?.
Children?.ElementAtOrDefault(1)?.
Children?.ElementAtOrDefault(0)?.
Children?.ElementAtOrDefault(0)?.
Children?.ElementAtOrDefault(1);
if (btn != null && btn.IsVisible)
{
var rect = btn.GetClientRect();
LogDebug($"Found reforge button at: {rect.Center.X}, {rect.Center.Y}");
return btn;
}
}
catch (Exception ex)
{
LogDebug($"Error finding reforge button: {ex.Message}");
}
return null;
}
private void UpdateBenchElements(Element bench)
{
try
{
ReforgeBenchHelper.LocateBenchElements(bench, out _itemSlots, out _reforgeButton);
if (Settings.EnableDebug)
{
for (int i = 0; i < _itemSlots.Length; i++)
{
if (_itemSlots[i] != null)
{
var rect = _itemSlots[i].GetClientRect();
RFLogger.Debug($"Item slot {i} found at position: {rect.Center.X}, {rect.Center.Y}");
}
}
if (_reforgeButton != null)
{
var rect = _reforgeButton.GetClientRect();
RFLogger.Debug($"Reforge button found at: {rect.Center.X}, {rect.Center.Y}");
}
}
}
catch (Exception ex)
{
RFLogger.Error($"Error updating bench elements: {ex.Message}");
}
}
public async Task ClickReforgeButton(CancellationToken cancellationToken = default)
{
if (_reforgeButton == null || !_reforgeButton.IsVisible)
{
LogDebug("Reforge button not found or not visible");
return;
}
LogDebug("Clicking reforge button...");
await ClickElement(_reforgeButton, cancellationToken);
}
private void CheckReforgeBench()
{
try
{
if ((DateTime.Now - _lastBenchLog).TotalMilliseconds < LOG_INTERVAL_MS) return;
var bench = FindReforgeBenchElement();
if (bench != null)
{
RFLogger.Debug($"Found bench with path: {bench.PathFromRoot}");
if (bench != _lastFoundBench)
{
_lastFoundBench = bench;
LogDebug($"Found Reforge Bench at address: {bench.Address}");
LogDebug($"Bench Path: {bench.PathFromRoot}");
UpdateBenchElements(bench);
}
}
else if (_lastFoundBench != null)
{
_lastFoundBench = null;
_reforgeButton = null;
Array.Clear(_itemSlots, 0, _itemSlots.Length);
LogDebug("Reforge Bench no longer visible");
}
_lastBenchLog = DateTime.Now;
}
catch (Exception ex)
{
LogDebug($"Error checking reforge bench: {ex.Message}");
}
}
private Element FindReforgeBenchElement()
{
try
{
RFLogger.Debug("Searching for reforge bench...");
var ingameUi = GameController?.Game?.IngameState?.IngameUi;
if (ingameUi == null)
{
RFLogger.Debug("IngameUi is null");
return null;
}
return ReforgeBenchHelper.FindReforgeBench(ingameUi);
}
catch (Exception ex)
{
RFLogger.Error($"Error finding reforge bench: {ex.Message}");
return null;
}
}
private void LogDebug(string message)
{
RFLogger.Debug(message);
}
private Element GetReforgeButton()
{
try
{
// Adjusted path to locate the enabled reforge button
var button = _lastFoundBench?.Children?.ElementAtOrDefault(3)?
.Children?.ElementAtOrDefault(1)?
.Children?.ElementAtOrDefault(0)?
.Children?.ElementAtOrDefault(0);
if (button != null && button.IsVisible)
{
LogDebug("[ReforgeHelper] Enabled reforge button found and visible.");
return button;
}
else
{
LogDebug("[ReforgeHelper] Enabled reforge button not found or not visible.");
return null;
}
}
catch (Exception ex)
{
LogDebug($"[ReforgeHelper] Error locating reforge button: {ex.Message}");
return null;
}
}
private async Task ProcessNextTriplet(CancellationToken ct)
{
try
{
if (_currentTripletIndex >= _currentTriplets.Count) return;
// Make sure bench is still available during processing
if (IsReforgeBenchUiOpen())
{
// Refresh bench reference if needed
if (_lastFoundBench == null || !_lastFoundBench.IsVisible)
{
CheckReforgeBench();
if (_lastFoundBench == null || !_lastFoundBench.IsVisible)
{
LogDebug("Lost reference to reforge bench during processing");
StopProcessing();
return;
}
}
}
else
{
LogDebug("Reforge bench UI closed during processing");
StopProcessing();
return;
}
var triplet = _currentTriplets[_currentTripletIndex];
LogDebug($"Processing triplet {_currentTripletIndex + 1}/{_currentTriplets.Count}");
// Safety check before starting the sequence
if (!IsBenchAndInventoryOpen())
{
LogDebug("Reforge bench or inventory not open, stopping process.");
StopProcessing();
return;
}
// Step 1: Place the triplet into the reforge bench slots
for (int i = 0; i < triplet.Count; i++)
{
if (ct.IsCancellationRequested) return;
var rect = triplet[i].ClientRect;
// Click on the item to move it to the reforge bench
await MoveCursorSmoothly(rect.Center, ct);
Input.KeyDown(Keys.ControlKey);
await Task.Delay(50, ct);
Input.Click(MouseButtons.Left);
await Task.Delay(100, ct);
Input.KeyUp(Keys.ControlKey);
LogDebug($"Moved item {i + 1}/{triplet.Count} of triplet to the reforge bench.");
}
// Step 2: Press the reforge button
var reforgeButton = GetReforgeButton();
if (reforgeButton != null)
{
LogDebug("Clicking the reforge button...");
var reforgeButtonRect = reforgeButton.GetClientRect();
await MoveCursorSmoothly(reforgeButtonRect.Center, ct);
Input.Click(MouseButtons.Left);
await Task.Delay(1000, ct); // Wait for the reforging process to complete
}
else
{
LogDebug("Reforge button not found or not visible.");
StopProcessing();
return;
}
// Step 3: Move the result item to the inventory
await MoveResultItem(ct);
// Step 4: Process the next triplet
_currentTripletIndex++;
if (_currentTripletIndex < _currentTriplets.Count)
{
await Task.Delay(200, ct);
_ = ProcessNextTriplet(ct); // Continue to the next triplet
}
else
{
_isProcessing = false;
LogDebug("Finished processing all triplets.");
}
}
catch (Exception ex)
{
LogDebug($"Error in ProcessNextTriplet: {ex.Message}");
StopProcessing();
}
}
private bool IsBenchAndInventoryOpen()
{
try
{
var ingameUi = GameController?.Game?.IngameState?.IngameUi;
if (ingameUi == null) return false;
var inventoryPanel = ingameUi.InventoryPanel;
if (inventoryPanel == null || !inventoryPanel.IsVisible) return false;
if (_lastFoundBench == null || !_lastFoundBench.IsVisible) return false;
return true;
}
catch (Exception ex)
{
LogDebug($"Error checking UI state: {ex.Message}");
return false;
}
}
private ExileCore2.Shared.RectangleF? GetResultItemRect()
{
try
{
var resultSlot = _lastFoundBench?.Children?.ElementAtOrDefault(3)?
.Children?.ElementAtOrDefault(1)?
.Children?.ElementAtOrDefault(1);
if (resultSlot?.IsVisible == true)
{
var rect = resultSlot.GetClientRect();
RFLogger.Debug($"Found result item at: {rect.Center.X}, {rect.Center.Y}");
return rect;
}
else
{
RFLogger.Debug("Result item slot not found or not visible");
}
}
catch (Exception ex)
{
RFLogger.Error($"Error getting result item rect: {ex.Message}");
}
return null;
}
private async Task MoveResultItem(CancellationToken cancellationToken)
{
var resultRect = GetResultItemRect();
if (resultRect.HasValue)
{
// Move cursor to the result slot
await MoveCursorSmoothly(resultRect.Value.Center, cancellationToken);
// Hold CTRL, click, release CTRL to send the item to the next open inventory slot
Input.KeyDown(Keys.ControlKey);
await Task.Delay(50, cancellationToken); // Small delay to ensure proper input
Input.Click(MouseButtons.Left); // Perform the click
await Task.Delay(50, cancellationToken); // Ensure the action is registered
Input.KeyUp(Keys.ControlKey);
RFLogger.Debug("Result item moved to the next open inventory slot");
}
else
{
RFLogger.Debug("No result item found to move");
}
}
private void StartProcessing()
{
RFLogger.Debug("Attempting to start processing...");
if (_isProcessing)
{
RFLogger.Debug("Already processing, skipping");
return;
}
// Ensure bench is still valid
if (_lastFoundBench == null || !_lastFoundBench.IsVisible)
{
LogDebug("Reforge bench no longer available");
return;
}
_currentTriplets = _tripletManager.FormTriplets();
if (_currentTriplets.Count == 0)
{
LogDebug("No triplets found");
return;
}
_isProcessing = true;
_currentTripletIndex = 0;
_processingCts = new CancellationTokenSource();
_ = ProcessNextTriplet(_processingCts.Token);
LogDebug($"Started processing {_currentTriplets.Count} triplets");
}
private void StopProcessing()
{
if (!_isProcessing) return;
_processingCts?.Cancel();
_isProcessing = false;
LogDebug("Processing stopped");
}
private bool IsInSafeZone()
{
var area = GameController.Area.CurrentArea;
return area?.IsHideout == true || area?.IsTown == true;
}
public override void Tick()
{
try
{
if (!Settings.Enable || !GameController.Window.IsForeground()) return;
if (_tripletManager == null) return;
// Only process in safe zones
if (!IsInSafeZone())
{
_lastFoundBench = null;
return;
}
// We'll check for the bench only when processing or when hotkey is pressed
// No continuous bench checking on every tick
// Process hotkeys
if (Settings.StartReforgeKey.PressedOnce())
{
if (!_isProcessing)
{
// Only check for bench when hotkey is pressed and not already processing
if (IsReforgeBenchUiOpen())
{
CheckReforgeBench();
if (_lastFoundBench?.IsVisible == true)
{
StartProcessing();
}
else
{
LogDebug("Reforge bench not found or not visible. Please open the reforge bench.");
}
}
else
{
LogDebug("Reforge bench UI is not open. Please open the reforge bench first.");
}
}
else
{
StopProcessing();
}
}
if (Settings.EmergencyStopKey.PressedOnce())
{
StopProcessing();
}
}
catch (Exception ex)
{
RFLogger.Error($"Tick error: {ex.Message}");
}
}
public override void Render()
{
try
{
if (!Settings.Enable) return;
if (_tripletManager == null) return;
if (_lastFoundBench != null && _lastFoundBench.IsVisible)
{
if (_reforgeButton != null && _reforgeButton.IsVisible)
{
var rect = _reforgeButton.GetClientRect();
Graphics.DrawFrame(rect, Color.Green, 2);
}
}
if (_isProcessing)
{
var statusText = $"Processing triplet {_currentTripletIndex + 1}/{_currentTriplets.Count}";
var pos = new Vector2(GameController.Window.GetWindowRectangle().Width / 2, 100);
Graphics.DrawText(statusText, pos, Color.Yellow);
}
}
catch (Exception ex)
{
LogDebug($"Render error: {ex.Message}");
}
}
public class TripletData
{
public Entity Entity { get; }
public string BaseName { get; }
public string BaseType { get; }
public int ItemLevel { get; }
public ItemRarity Rarity { get; }
public ExileCore2.Shared.RectangleF ClientRect { get; }
private readonly GameController _gc;
public TripletData(Entity entity, GameController gc, ExileCore2.Shared.RectangleF rect)
{
Entity = entity;
_gc = gc;
ClientRect = rect;
var baseComponent = entity.GetComponent<Base>();
var modsComponent = entity.GetComponent<Mods>();
BaseName = baseComponent?.Name ?? string.Empty;
BaseType = GetBaseType(BaseName);
ItemLevel = modsComponent?.ItemLevel ?? 0;
Rarity = modsComponent?.ItemRarity ?? ItemRarity.Normal;
}
private string GetBaseType(string itemName)
{
// First, try to get the exact subtype
var exactSubtype = ItemSubtypes.GetExactSubtype(itemName);
if (!string.IsNullOrEmpty(exactSubtype))
return exactSubtype;
// Fallback to base category matching
return ItemSubtypes.GetBaseCategory(itemName);
}
}
private bool IsReforgeBenchUiOpen()
{
try
{
var ingameUi = GameController?.Game?.IngameState?.IngameUi;
if (ingameUi == null) return false;
// Loop through visible panels to find reforge UI
foreach (var panel in ingameUi.Children)
{
if (panel?.IsVisible == true &&
panel.PathFromRoot?.Contains("ReforgingBench") == true)
{
return true;
}
}
}
catch (Exception ex)
{
RFLogger.Error($"Error checking reforge UI: {ex.Message}");
}
return false;
}
public class TripletManager
{
private readonly TimeCache<List<TripletData>> _inventoryItems;
private readonly GameController _gameController;
private readonly ReforgeHelperSettings _settings;
public TripletManager(GameController gc, ReforgeHelperSettings settings)
{
_gameController = gc;
_settings = settings;
_inventoryItems = new TimeCache<List<TripletData>>(ScanInventory, 200);
}
private List<TripletData> ScanInventory()
{
var items = new List<TripletData>();
var inventory = _gameController?.Game?.IngameState?.Data?.ServerData?.PlayerInventories[0]?.Inventory;
if (inventory?.InventorySlotItems == null)
{
RFLogger.Debug("Inventory or InventorySlotItems is null.");
return items;
}
var itemCount = inventory.InventorySlotItems.Count;
RFLogger.Debug($"Scanning inventory - Found {itemCount} total items");
foreach (var item in inventory.InventorySlotItems)
{
if (item.Item == null || item.Address == 0) continue;
var baseComp = item.Item.GetComponent<Base>();
var modsComp = item.Item.GetComponent<Mods>();
RFLogger.Debug(
$"Item: {baseComp?.Name} | Level: {modsComp?.ItemLevel} | Rarity: {modsComp?.ItemRarity}"
);
items.Add(new TripletData(item.Item, _gameController, item.GetClientRect()));
}
return items;
}
private List<TripletData> FindBestTriplet(List<TripletData> availableItems)
{
if (availableItems.Count < 3) return null;
// Sort by item level for better triplet selection
var sortedItems = availableItems.OrderBy(x => x.ItemLevel).ToList();
for (int i = 0; i < sortedItems.Count - 2; i++)
{
var triplet = new List<TripletData> { sortedItems[i], sortedItems[i + 1], sortedItems[i + 2] };
var levelDiff = Math.Max(
Math.Max(
Math.Abs(triplet[0].ItemLevel - triplet[1].ItemLevel),
Math.Abs(triplet[1].ItemLevel - triplet[2].ItemLevel)
),
Math.Abs(triplet[0].ItemLevel - triplet[2].ItemLevel)
);
if (levelDiff <= _settings.MaxItemLevelDisparity.Value)
{
return triplet;
}
}
return null;
}
private bool IsValidForReforge(TripletData item)
{
// Add check for unidentified items
var modsComponent = item.Entity.GetComponent<Mods>();
if (modsComponent?.Identified == false)
{
RFLogger.Debug($"Item {item.BaseName} is unidentified - skipping");
return false;
}
// Ensure item has necessary components
var baseComponent = item.Entity.GetComponent<Base>();
if (baseComponent == null || modsComponent == null)
{
DebugWindow.LogMsg($"Item {item.BaseName} missing required components - skipping");
return false;
}
// Check if item is corrupted using the Base component
if (baseComponent.isCorrupted)
{
DebugWindow.LogMsg($"Item {item.BaseName} is corrupted - skipping");
return false;
}
var baseCategory = ItemSubtypes.GetBaseCategory(item.BaseType);
// Category enabled check
bool isCategoryEnabled = baseCategory switch
{
"Soul Core" => _settings.ItemCategories.EnableSoulCores,
"Jewel" => _settings.ItemCategories.EnableJewels,
"Ring" => _settings.ItemCategories.EnableRings,
"Amulet" => _settings.ItemCategories.EnableAmulets,
"Waystone" => _settings.ItemCategories.EnableWaystones,
"Relic" => _settings.ItemCategories.EnableRelics,
"Tablet" => _settings.ItemCategories.EnableTablets,
_ => false
};
if (!isCategoryEnabled)
return false;
// Item level check
if (item.ItemLevel < _settings.MinItemLevel.Value ||
item.ItemLevel > _settings.MaxItemLevel.Value)
return false;
// Rarity check
bool needsMinMagicRarity = baseCategory is "Jewel" or "Ring" or "Amulet" or "Wand";
if (needsMinMagicRarity)
{
return item.Rarity == ItemRarity.Magic ||
item.Rarity == ItemRarity.Rare ||
item.Rarity == ItemRarity.Unique;
}
else
{
return true; // Any rarity allowed for Waystones and Soul Cores
}
}
public List<List<TripletData>> FormTriplets()
{
var triplets = new List<List<TripletData>>();
var items = _inventoryItems.Value
.Where(IsValidForReforge) // Filter items before grouping
.ToList();
RFLogger.Debug($"Forming triplets from {items.Count} valid items");
var itemsByBaseType = items
.GroupBy(x => new { x.BaseType, x.Rarity })
.Where(g => g.Count() >= 3);
foreach (var group in itemsByBaseType)
{
RFLogger.Debug($"Processing group: {group.Key.BaseType} (Rarity: {group.Key.Rarity}) with {group.Count()} items");
var sortedItems = group.OrderBy(x => x.ItemLevel).ToList();
while (sortedItems.Count >= 3)
{
var triplet = sortedItems.Take(3).ToList();
sortedItems.RemoveRange(0, 3); // Remove consumed items
triplets.Add(triplet);
}
}
RFLogger.Debug($"Formed {triplets.Count} triplets");
return triplets;
}
}
}