-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEZVendorCore.cs
930 lines (832 loc) · 34.1 KB
/
EZVendorCore.cs
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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using ExileCore;
using ExileCore.PoEMemory;
using ExileCore.PoEMemory.Components;
using ExileCore.PoEMemory.Elements.InventoryElements;
using ExileCore.PoEMemory.MemoryObjects;
using ExileCore.Shared;
using ExileCore.Shared.Enums;
using ExileCore.Shared.Helpers;
using EZVendor.Item;
using EZVendor.Item.DivCards;
using EZVendor.Item.Filters;
using EZVendor.Item.Ninja;
using ImGuiNET;
#pragma warning disable CS0618
// ReSharper disable ConstantConditionalAccessQualifier
namespace EZVendor
{
internal class MyItem
{
internal long? InitialInvItemItemAddress { get; }
internal NormalInventoryItem InvItem { get; }
public MyItem(NormalInventoryItem invItem)
{
InvItem = invItem;
InitialInvItemItemAddress = InvItem?.Item?.Address;
}
}
public class EZVendorCore : BaseSettingsPlugin<EZVendorSettings>
{
private const int MaxIDTimes = 2;
private const int MaxVendorTimes = 3;
private const int MaxSameBases = 4;
private const string MainCoroutineName = "EZV_Main";
private const string TwoStoneBase1 = @"Metadata/Items/Rings/Ring12";
private const string TwoStoneBase2 = @"Metadata/Items/Rings/Ring13";
private const string TwoStoneBase3 = @"Metadata/Items/Rings/Ring14";
private IItemFactory _itemFactory;
private INinjaProvider _ninja;
private IDivCardsProvider _divCardsProvider;
private int Latency => GameController?.IngameState?.ServerData?.Latency ?? 50;
private bool _init;
private void Init()
{
Input.RegisterKey(Keys.LControlKey);
Input.RegisterKey(Keys.LShiftKey);
Input.RegisterKey(Settings.MainHotkey2);
Input.RegisterKey(Settings.CopyStatsHotkey2);
Input.RegisterKey(Settings.StopHotkey2);
_ninja = new NinjaUniqueProvider(
Settings.Unique0LChaosCutoff2,
Settings.Unique6LChaosCutoff2,
DirectoryFullName,
Settings.LeagueNameArchnemesis);
_divCardsProvider = new LocalDivCardsProvider(
Settings.LimitedUsername,
Settings.FilterName,
DirectoryFullName
);
_itemFactory = new ItemFactory(
GameController,
_ninja,
_divCardsProvider,
Settings.BypassBrokenItemMods21052022,
Settings.VendorInfluenced2,
Settings.VendorFractured,
Settings.SellRares6L
);
_init = true;
}
public override void DrawSettings()
{
try
{
if (!_init)
{
ImGui.Text($"Loading...");
return;
}
ImGui.Text($"Welcome to EZV {Assembly.GetExecutingAssembly().GetName().Version}");
try
{
var cheapDivCards = _divCardsProvider?.GetSellDivCardsList()?.Count;
var zeroLinks = _ninja?.GetCheap0LUniques()?.Count();
var sixLinks = _ninja?.GetCheap6LUniques()?.Count();
ImGui.Text(cheapDivCards > 0
? $"Loaded {cheapDivCards} div cards to sell"
: "CANT LOAD LOOT FILTER. Check offline filter name");
ImGui.Text(zeroLinks > 0
? $"Loaded {zeroLinks} <6L cheap uniques"
: "CANT LOAD <6L UNIQUE LIST");
ImGui.Text($"Loaded {sixLinks} =6L cheap uniques");
}
catch (Exception)
{
ImGui.Text($"EXCEPTION DURING LOADING FILTERS. DO NO USE !!!");
}
ImGui.InputText("League name", ref Settings.LeagueNameArchnemesis, 255);
base.DrawSettings();
ImGui.InputText("Poe username", ref Settings.LimitedUsername, 255);
ImGui.InputText("Filter name", ref Settings.FilterName, 255);
if (ImGui.Button("Delete ninja cache (after you change settings)"))
{
File.Delete(Path.Combine(DirectoryFullName, "ninja0L.json"));
File.Delete(Path.Combine(DirectoryFullName, "ninja6L.json"));
RestartHud();
}
}
catch
{
// ignored
}
}
private static void RestartHud()
{
var exeDirectory = AppDomain.CurrentDomain.BaseDirectory;
var exeFiles = Directory.GetFiles(exeDirectory, "*.exe");
foreach (var exeFile in exeFiles)
{
var fileName = Path.GetFileName(exeFile);
var processes = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(fileName));
if (processes.Length > 0)
{
// Select the first running process and use its filename for restart
Process.Start("cmd.exe", $"/c taskkill /im \"{fileName}\" & timeout 3 & taskkill /f /im \"{fileName}\" & timeout 1 & pushd \"{exeDirectory}\" & start \"\" \"{fileName}\"");
return;
}
}
}
public override void ReceiveEvent(string eventId, object args)
{
if (!Settings.Enable.Value || !_init) return;
switch (eventId)
{
case "start_ezv":
StartMainCoroutine();
break;
}
}
#region Tracking item under cursor
private readonly Stopwatch _cursorStuckWithGarbageTimer = new Stopwatch();
private bool IsCursorWithItem()
{
if (GameController?.Game?.IsPreGame == true) return false;
try
{
var playerInventories = GameController
?.Game
?.IngameState
?.ServerData
?.PlayerInventories;
var cursorItems =
(from playerInventory in playerInventories
select playerInventory?.Inventory
into inventory
where inventory?.InventType == InventoryTypeE.Cursor
select inventory)
.FirstOrDefault();
if (cursorItems?.Items?.Count != 1) return false;
var cursorItem = cursorItems?.Items?[0];
return !string.IsNullOrEmpty(cursorItem?.Path);
}
catch (Exception) // ok
{
return false;
}
}
private void UpdateCursorStuckWithGarbageTimer()
{
if (IsCursorWithItem())
_cursorStuckWithGarbageTimer.Start();
else if (_cursorStuckWithGarbageTimer.IsRunning)
_cursorStuckWithGarbageTimer.Reset();
}
#endregion
public override Job Tick()
{
#region Lazy init
if (!_init && GameController.InGame)
{
var leagueName = GameController?.IngameState?.ServerData?.League;
if (leagueName?.Length is >= 4 and <= 64) Settings.LeagueNameArchnemesis = leagueName;
Init();
}
#endregion
#region Stop if we have stuck item under cursor
UpdateCursorStuckWithGarbageTimer();
if (_cursorStuckWithGarbageTimer.ElapsedMilliseconds > 5000)
{
Core.ParallelRunner?.FindByName(MainCoroutineName)?.Done(true);
return null;
}
#endregion
#region Start main routine
if (Settings.MainHotkey2.PressedOnce())
{
StartMainCoroutine();
}
if (Settings.StopHotkey2.PressedOnce() &&
Core.ParallelRunner?.FindByName(MainCoroutineName)?.Done(true) == true)
{
LogMessage("[EZV] aborted");
PublishEvent("ezv_finished", null);
}
#endregion
#region Debug item mods
if (Settings.CopyStatsHotkey2.PressedOnce())
{
var invItem = GetInventoryItem(GameController?.IngameState?.UIHoverElement?.Address);
var itemComponent = invItem.Item.GetComponent<Mods>();
var stats = itemComponent.UniqueName;
try
{
var itemClass = GameController.Files.BaseItemTypes.Translate(invItem.Item.Path).ClassName;
stats += " [" + itemClass + "] ";
}
catch (Exception e)
{
LogMessage(e.StackTrace, 20);
}
stats += Environment.NewLine;
stats += "Item mods: " + Environment.NewLine;
try
{
foreach (var itemMod in itemComponent.ItemMods)
stats += "name: [" + itemMod.Name + "] " +
"group: [" + itemMod.Group + "] " +
"values: " + itemMod.Value1 + " " + itemMod.Value2 +
Environment.NewLine;
}
catch (Exception e)
{
LogMessage(e.StackTrace, 20);
}
try
{
if (itemComponent.ItemRarity == ItemRarity.Unique)
stats += $"Internal name: {itemComponent?.UniqueName} " + Environment.NewLine;
}
catch (Exception e)
{
LogMessage(e.StackTrace, 20);
}
stats += "Human mods (this one is often broken in PoeHUD): " + Environment.NewLine;
try
{
foreach (var humanStat in itemComponent.HumanImpStats) stats += humanStat + Environment.NewLine;
}
catch (Exception e)
{
LogMessage(e.StackTrace, 20);
}
try
{
foreach (var humanStat in itemComponent.HumanStats) stats += humanStat + Environment.NewLine;
}
catch (Exception e)
{
LogMessage(e.StackTrace, 20);
}
try
{
foreach (var humanStat in itemComponent.HumanCraftedStats) stats += humanStat + Environment.NewLine;
}
catch (Exception e)
{
LogMessage(e.StackTrace, 20);
}
ImGui.SetClipboardText(stats);
LogMessage("[EZV] Saved item stats to clipboard. Send it to me if plugin misses it", 20);
}
#endregion
return null;
}
private void StartMainCoroutine()
{
if (Core.ParallelRunner.FindByName(MainCoroutineName) != null) return;
Core.ParallelRunner?.Run(new Coroutine(MainRoutine(), this, MainCoroutineName));
LogMessage("[EZV] started");
PublishEvent("ezv_started", null);
}
private IEnumerator MainRoutine()
{
#region Open inventory to update items
if (Settings.DebugLog2) LogMessage($"[EZV] Detected {GetInventoryItems()?.Count()} items in inventory", 15);
if (GetInventoryItems()?.Count() is null or <= 1)
{
if (Settings.DebugLog2) LogMessage("[EZV] OpenInventory");
yield return OpenInventory();
if (Settings.DebugLog2) LogMessage("[EZV] WaitUntilItemsUpdated");
yield return WaitUntilItemsUpdated();
if (Settings.DebugLog2) LogMessage($"[EZV] Detected {GetInventoryItems()?.Count()} items in inventory", 15);
}
#endregion
// If we need to check fractured mod this item need to be mouse over
if (!Settings.VendorFractured)
{
GetUnverifiedItems(out var mouseOverList);
if (mouseOverList.Any())
{
if (Settings.DebugLog2) LogMessage("[EZV] OpenNPCTrade");
yield return OpenNPCTrade();
if (Settings.DebugLog2) LogMessage("[EZV] WaitForOpenInventory");
yield return WaitForOpenInventory();
if (Settings.DebugLog2) LogMessage("[EZV] RefreshItemCache");
yield return RefreshCache(mouseOverList);
}
}
for (var iteration = 0; iteration < (Settings.AutoClickAcceptButton2 ? MaxIDTimes : 1); iteration++)
{
GetUnidentifiedItems(out var unidList);
if (!unidList.Any()) continue;
if (Settings.DebugLog2) LogMessage("[EZV] OpenNPCTrade");
yield return OpenNPCTrade();
if (Settings.DebugLog2) LogMessage("[EZV] WaitForOpenInventory");
yield return WaitForOpenInventory();
if (Settings.DebugLog2) LogMessage("[EZV] Unid");
yield return DoUnid(unidList);
}
for (var iteration = 0; iteration < (Settings.AutoClickAcceptButton2 ? MaxVendorTimes : 1); iteration++)
{
#region AnalyzeIdentifiedItems
IList<MyItem> vendorList;
IDictionary<string, int> vendorBases;
try
{
if (Settings.DebugLog2) LogMessage("[EZV] AnalyzeIdentifiedItems");
AnalyzeIdentifiedItems(out vendorList, out vendorBases);
}
catch (Exception e)
{
LogError($"[EZV] AnalyzeIdentifiedItems " + e.StackTrace, 30);
yield break;
}
#endregion
#region RemoveBadVendorRecipes
try
{
if (Settings.DebugLog2) LogMessage("[EZV] RemoveBadVendorRecipes");
RemoveBadVendorRecipes(vendorList, vendorBases);
}
catch (Exception e)
{
LogError($"[EZV] RemoveBadVendorRecipes " + e.StackTrace, 30);
}
#endregion
if (!vendorList.Any()) continue;
if (Settings.DebugLog2) LogMessage("[EZV] OpenNPCTrade");
yield return OpenNPCTrade();
if (Settings.DebugLog2) LogMessage("[EZV] WaitForOpenInventory");
yield return WaitForOpenInventory();
if (Settings.DebugLog2) LogMessage("[EZV] VendorGarbage");
yield return DoVendorGarbage(vendorList);
if (Settings.DebugLog2) LogMessage("[EZV] ClickSellWindowAcceptButton");
yield return ClickSellWindowAcceptButton();
if (Settings.DebugLog2) LogMessage("[EZV] WaitForClosedInventory");
yield return WaitForSellWindowClosed();
}
if (IsInventoryOpened()) yield return CloseAllUi();
// Make sure keys are not stuck
Input.KeyUp(Keys.LShiftKey);
Input.KeyUp(Keys.LControlKey);
LogMessage("[EZV] finished");
PublishEvent("ezv_finished", null);
}
private IEnumerator OpenInventory()
{
yield return Input.KeyPress(Settings.OpenInventoryStopHotkey);
yield return new WaitTime(100 + Latency);
}
private IEnumerator CloseAllUi()
{
yield return Input.KeyPress(Settings.CloseAllUiHotkey);
yield return new WaitTime(100 + Latency);
}
private IEnumerator WaitForOpenInventory(int timeoutMs = 5000)
{
yield return WaitUntil(() => IsInventoryOpened() && IsSellWindowOpened(), timeoutMs);
}
private IEnumerator WaitUntilItemsUpdated(int timeoutMs = 5000)
{
yield return WaitUntil(() => IsInventoryOpened() && IsItemsUpdated(), timeoutMs);
}
private IEnumerator WaitForSellWindowClosed(int timeoutMs = 5000)
{
yield return WaitUntil(() => !IsSellWindowOpened(), timeoutMs);
}
private IEnumerator WaitUntil(Func<bool> condition, int timeoutMs = 5000)
{
const int waitTickRate = 100;
MinMax(waitTickRate, ref timeoutMs, 10000);
for (var i = 0; i < timeoutMs / waitTickRate; i++)
{
if (condition())
{
if (Settings.DebugLog2) LogMessage($"[EZV] Finished waiting for {condition.Method.Name}");
yield return new WaitTime(100 + Latency);
yield break;
}
if (Settings.DebugLog2) LogMessage($"[EZV] Waiting for {condition.Method.Name}");
yield return new WaitTime(waitTickRate);
}
}
private static void MinMax(int min, ref int value, int max)
{
value = Math.Max(min, value);
value = Math.Min(value, max);
}
/// <summary>
/// AnalyzeIdentifiedItems
/// </summary>
/// <param name="vendorList"></param>
/// <param name="pathCount"></param>
private void AnalyzeIdentifiedItems(
out IList<MyItem> vendorList,
out IDictionary<string, int> pathCount)
{
vendorList = new List<MyItem>();
pathCount = new Dictionary<string, int>();
try
{
foreach (var invItem in GetInventoryItems())
{
try
{
if (invItem.Item.Rarity == MonsterRarity.Error ||
!invItem.Item.HasComponent<Base>() ||
_itemFactory.Evaluate(invItem) == Actions.Vendor)
{
vendorList.Add(new MyItem(invItem));
var key = invItem.Item.Path;
if (pathCount.ContainsKey(key))
pathCount[key]++;
else
pathCount.Add(key, 1);
}
}
catch (Exception e)
{
LogMessage($"[EZV] Found krangled item. Selling " + e.StackTrace, 30);
vendorList.Add(new MyItem(invItem));
}
}
}
catch (Exception)
{
// ignored
}
}
private void GetUnidentifiedItems(
out IList<MyItem> unidList)
{
try
{
var query =
from invItem in GetInventoryItems()
let item = invItem.Item
let baseComponent = item.HasComponent<Base>() ? item.GetComponent<Base>() : null
where baseComponent?.isCorrupted != true // skip krangled
let modsComponent = item.HasComponent<Mods>() ? item.GetComponent<Mods>() : null
where item.Type == EntityType.Error || modsComponent?.Identified == false
let socketsComponent = item.HasComponent<Sockets>() ? item.GetComponent<Sockets>() : null
let sixSockets = socketsComponent?.NumberOfSockets == 6
let sixLinks = socketsComponent?.LargestLinkSize == 6
where !sixSockets || sixLinks // skip 6S
select new MyItem(invItem);
unidList = query.ToList();
}
catch (Exception)
{
unidList = new List<MyItem>();
}
}
/// <summary>
/// Some items need to be mouse over to get all the info
/// </summary>
/// <param name="mouseOverList"></param>
private void GetUnverifiedItems(
out IList<MyItem> mouseOverList)
{
try
{
var query =
from invItem in GetInventoryItems()
let item = invItem.Item
let modsComponent = item.HasComponent<Mods>() ? item.GetComponent<Mods>() : null
where item.Type == EntityType.Error || modsComponent?.ItemRarity is ItemRarity.Magic or ItemRarity.Rare
select new MyItem(invItem);
mouseOverList = query.ToList();
}
catch (Exception)
{
mouseOverList = new List<MyItem>();
}
}
private IEnumerator RefreshCache(IList<MyItem> itemList)
{
if (!itemList.Any()) yield break;
LogMessage($"[EZV] Want to refresh cache for {itemList.Count} items");
yield return ClickAll(itemList, 1, Keys.None, MouseButtons.Right);
}
/// <summary>
/// Removes bad vendor recipes like 5 same bases
/// </summary>
/// <param name="vendorList"></param>
/// <param name="basesCount"></param>
/// <returns>true - removed something</returns>
// ReSharper disable once UnusedMethodReturnValue.Local
private bool RemoveBadVendorRecipes(
IList<MyItem> vendorList,
IDictionary<string, int> basesCount)
{
var removedSomething = false;
try
{
// 5 to 1 recipe
foreach (var key in basesCount.Keys)
{
var value = basesCount[key];
if (value <= MaxSameBases) continue;
removedSomething |= Remove(vendorList, key, value - MaxSameBases);
}
}
catch (Exception e)
{
LogError($"[EZV] RemoveBadVendorRecipes #1 " + e.StackTrace, 30);
}
// prismatic ring
try
{
for (var i = 0; i < 10; i++)
{
var count = 0;
var r1 = vendorList.Count(myItem => myItem?.InvItem?.Item?.Path == TwoStoneBase1);
if (r1 > 0) count++;
var r2 = vendorList.Count(myItem => myItem?.InvItem?.Item?.Path == TwoStoneBase2);
if (r2 > 0) count++;
var r3 = vendorList.Count(myItem => myItem?.InvItem?.Item?.Path == TwoStoneBase3);
if (r3 > 0) count++;
if (count == 3)
{
var key = TwoStoneBase3;
if (r1 <= r2 && r1 <= r3) key = TwoStoneBase1;
else if (r2 <= r1 && r2 <= r3) key = TwoStoneBase2;
removedSomething |= Remove(vendorList, key);
}
}
}
catch (Exception e)
{
LogError($"[EZV] RemoveBadVendorRecipes #2 " + e.StackTrace, 30);
}
// transmute + 2 amulets
try
{
if (vendorList.Count(myItem => myItem?.InvItem?.Item?.Path.Contains("Amulets") == true) >= 2)
{
removedSomething |= Remove(vendorList, @"Metadata/Items/Currency/CurrencyUpgradeToMagic");
}
}
catch (Exception e)
{
LogError($"[EZV] RemoveBadVendorRecipes #3 " + e.StackTrace, 30);
}
try
{
// weapon + whetstone
if (vendorList.Count(myItem => myItem?.InvItem?.Item?.Path.Contains("Weapons") == true) >= 1)
{
removedSomething |= Remove(vendorList, @"Metadata/Items/Currency/CurrencyWeaponQuality");
}
}
catch (Exception e)
{
LogError($"[EZV] RemoveBadVendorRecipes #4 " + e.StackTrace, 30);
}
return removedSomething;
}
private static bool Remove(
IList<MyItem> list,
string key,
int n = int.MaxValue)
{
var removedSomething = false;
var removed = 0;
for (var i = list.Count - 1; i >= 0; i--)
{
if (list[i].InvItem.Item.Path == key)
{
list.RemoveAt(i);
removed++;
removedSomething = true;
}
if (removed == n) break;
}
return removedSomething;
}
/// <summary>
/// VendorGarbage
/// </summary>
/// <param name="itemList"></param>
/// <returns></returns>
private IEnumerator DoVendorGarbage(IList<MyItem> itemList)
{
if (!itemList.Any()) yield break;
LogMessage($"[EZV] Want to sell {itemList.Count} items");
yield return ClickAll(itemList, 3, Keys.LControlKey, MouseButtons.Left);
Input.KeyUp(Keys.LControlKey);
}
private IEnumerator DoUnid(IList<MyItem> itemList)
{
if (!itemList.Any()) yield break;
LogMessage($"[EZV] Want to unid {itemList.Count} items");
var scrollOfWisdom = GetInventoryItem("Metadata/Items/Currency/CurrencyIdentification");
if (scrollOfWisdom == null)
{
LogMessage($"[EZV] No ID scrolls", 20);
yield break;
}
Input.KeyDown(Keys.LShiftKey);
yield return ClickItem(scrollOfWisdom, MouseButtons.Right);
yield return ClickAll(itemList, 3, Keys.LShiftKey, MouseButtons.Left);
Input.KeyUp(Keys.LShiftKey);
}
/// <summary>
/// Click all items
/// </summary>
/// <param name="itemList"></param>
/// <param name="iterations"></param>
/// <param name="keyToHold"></param>
/// <param name="mouseButton"></param>
/// <returns></returns>
private IEnumerator ClickAll(
IList<MyItem> itemList,
int iterations,
Keys keyToHold,
MouseButtons mouseButton)
{
if (!itemList.Any()) yield break;
Input.KeyDown(keyToHold);
for (var step = 1; step <= iterations; step++)
{
for (var i = itemList.Count - 1; i >= 0; i--)
{
if (!IsInventoryOpened() || !IsSellWindowOpened()) break;
var invItem = itemList?[i].InvItem;
if (invItem?.Item?.Type == EntityType.Error)
{
LogError($"[EZV] Selling krangled item", 30);
yield return ClickItem(invItem, mouseButton);
}
if (GetServerItem(itemList?[i].InitialInvItemItemAddress) == null)
{
itemList.RemoveAt(i);
continue;
}
yield return ClickItem(invItem, mouseButton);
}
if (!itemList.Any()) break;
yield return new WaitTime(100 + Latency);
}
Input.KeyUp(keyToHold);
}
/// <summary>
/// Click single item with UI hover logic
/// </summary>
/// <param name="invItem"></param>
/// <param name="mouseButton"></param>
/// <returns></returns>
private IEnumerator ClickItem(Element invItem, MouseButtons mouseButton)
{
LogMessage($"[EZV] ClickItem ", 30);
for (var j = 0; j < 10; j++) // timeout = 10 x DelayAfterMouseMove
{
if (!invItem.GetClientRectCache.Intersects(GetPlayerInventory().GetClientRectCache)) yield break;
Input.SetCursorPos(invItem.GetClientRect().ClickRandomNum());
yield return new WaitTime(Settings.Delay1AfterMouseMove4 + Latency);
if (GameController.IngameState.UIHoverElement.Address == invItem.Address)
{
Input.Click(mouseButton);
yield return new WaitTime(Settings.Delay2AfterClick4 + Latency);
break;
}
}
}
/// <summary>
/// OpenNPCTrade
/// </summary>
/// <returns></returns>
private IEnumerator OpenNPCTrade()
{
if (IsInventoryOpened() && IsSellWindowOpened()) yield break;
const string liliPath = @"Metadata/NPC/Epilogue/LillyHideout";
if (!Settings.AutoOpenTrade2) yield break;
var npc = GameController
?.Game
?.IngameState
?.IngameUi
?.ItemsOnGroundLabels
?.FirstOrDefault(labelOnGround =>
labelOnGround?.Label?.IsVisibleLocal == true &&
labelOnGround?.ItemOnGround?.Metadata == liliPath);
if (npc == null)
{
LogMessage("[EZV] Cant find NPC LILI nearby", 20);
yield break;
}
Input.KeyDown(Keys.LControlKey);
Input.SetCursorPos(npc.Label.GetClientRectCache.ClickRandomNum());
yield return new WaitTime(100 + Latency);
Input.Click(MouseButtons.Left);
yield return new WaitTime(100 + Latency);
Input.KeyUp(Keys.LControlKey);
}
/// <summary>
/// ClickSellWindowAcceptButton
/// </summary>
/// <returns></returns>
private IEnumerator ClickSellWindowAcceptButton()
{
var btn = GetSellWindowUi()?.GetChildAtIndex(4)?.GetChildAtIndex(5)?.GetChildAtIndex(0);
if (btn == null) yield break;
Input.SetCursorPos(btn.GetClientRectCache.ClickRandomNum());
yield return new WaitTime(100 + Latency);
if (!Settings.AutoClickAcceptButton2) yield break;
Input.Click(MouseButtons.Left);
yield return new WaitTime(100 + Latency);
}
private bool IsItemsUpdated() => GetInventoryItems()?.Count() > 0;
private bool IsInventoryOpened() =>
GameController
?.IngameState
?.IngameUi
?.InventoryPanel
?.IsVisible == true;
private IEnumerable<Element> GetVisibleUi()
{
var elements = GameController?.IngameState?.IngameUi?.Children?.Where(
x =>
x is {Address: > 0, IsValid: true, IsVisible: true, IsVisibleLocal: true});
return elements ?? [];
}
private Element GetSellWindowUi()
{
#region HUD check
try
{
var uiElement = GameController?.IngameState?.IngameUi?.SellWindowHideout;
if (uiElement?.IsValid == true &&
uiElement?.Width > 0 &&
uiElement?.Height > 0 &&
uiElement?.ChildCount == 5 &&
uiElement?.AcceptButton != null &&
uiElement?.CancelButton != null)
return uiElement;
}
catch (Exception)
{
// ignored
}
try
{
var uiElement = GameController?.IngameState?.IngameUi?.SellWindow;
if (uiElement?.IsValid == true &&
uiElement?.Width > 0 &&
uiElement?.Height > 0 &&
uiElement?.ChildCount == 5 &&
uiElement?.AcceptButton != null &&
uiElement?.CancelButton != null)
return uiElement;
}
catch (Exception)
{
// ignored
}
#endregion
#region Alternative check
try
{
var uiElement = GetVisibleUi()?.FirstOrDefault(
x =>
x?.ChildCount == 5 &&
(x?.GetChildAtIndex(0)?.ChildCount == 3 &&
x?.GetChildAtIndex(1)?.ChildCount == 1 &&
x?.GetChildAtIndex(3)?.ChildCount == 0) ||
x?.GetChildAtIndex(4)?.GetChildAtIndex(3)?.Text == @"Your Offer");
return uiElement;
}
catch (Exception)
{
return new Element();
}
#endregion
}
private bool IsSellWindowOpened() =>
GetSellWindowUi()?.IsVisible == true;
private NormalInventoryItem GetInventoryItem(long? address) =>
GetInventoryItems()
?.FirstOrDefault(item =>
item?.Address == address);
private NormalInventoryItem GetInventoryItem(string path) =>
GetInventoryItems()
?.FirstOrDefault(item =>
item?.Item?.Path == path);
private Inventory GetPlayerInventory() =>
GameController
?.IngameState
?.IngameUi
?.InventoryPanel?[InventoryIndex.PlayerInventory];
private IEnumerable<NormalInventoryItem> GetInventoryItems() =>
GetPlayerInventory()
?.VisibleInventoryItems;
private Entity GetServerItem(long? address) =>
GetServerItems()
?.FirstOrDefault(item =>
item?.Address == address);
private IEnumerable<Entity> GetServerItems() =>
GameController
?.IngameState
?.ServerData
?.PlayerInventories?[0]
?.Inventory
?.Items;
}
}