-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMarketWizard.cs
More file actions
680 lines (573 loc) · 28.3 KB
/
Copy pathMarketWizard.cs
File metadata and controls
680 lines (573 loc) · 28.3 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using ExileCore2;
using ExileCore2.PoEMemory.Models;
using ExileCore2.Shared.Helpers;
using ImGuiNET;
using MoreLinq;
using Vector2 = System.Numerics.Vector2;
using Vector4 = System.Numerics.Vector4;
namespace MarketWizard;
public class MarketWizard : BaseSettingsPlugin<MarketWizardSettings>
{
private Func<BaseItemType, double> _getNinjaValue;
private int _spread = 1;
private DateTime _lastDivExRate = DateTime.MinValue;
private double _divExRate = 0;
private DateTime _lastChaosDivRate = DateTime.MinValue;
private double _chaosDivRate = 0;
private Dictionary<string, ItemPriceData> _itemPrices = new();
private Dictionary<string, Dictionary<string, CurrencyRatio>> _currencyRatios = new();
private DateTime _lastRatioUpdate = DateTime.MinValue;
private bool _showDivineInChaos = false;
private class ItemPriceData
{
public DateTime LastUpdate { get; set; }
public double PriceInDivine { get; set; }
public double PriceInExalted { get; set; }
public double PriceInChaos { get; set; }
}
private class CurrencyRatio
{
public DateTime LastUpdate { get; set; }
public double Ratio { get; set; }
public int Volume { get; set; }
}
public override bool Initialise()
{
return true;
}
private void UpdateCurrencyRates(BaseItemType wanted, BaseItemType offered, float ratio)
{
if ((wanted.BaseName == "Divine Orb" && offered.BaseName == "Exalted Orb") ||
(wanted.BaseName == "Exalted Orb" && offered.BaseName == "Divine Orb"))
{
_lastDivExRate = DateTime.Now;
_divExRate = wanted.BaseName == "Divine Orb" ? ratio : 1 / ratio;
}
if ((wanted.BaseName == "Divine Orb" && offered.BaseName == "Chaos Orb") ||
(wanted.BaseName == "Chaos Orb" && offered.BaseName == "Divine Orb"))
{
_lastChaosDivRate = DateTime.Now;
_chaosDivRate = wanted.BaseName == "Divine Orb" ? ratio : 1 / ratio;
}
}
private void UpdateItemPrice(BaseItemType item, BaseItemType currency, float ratio)
{
if (item == null || currency == null) return;
var itemName = item.BaseName;
if (!_itemPrices.ContainsKey(itemName))
{
_itemPrices[itemName] = new ItemPriceData();
}
var priceData = _itemPrices[itemName];
priceData.LastUpdate = DateTime.Now;
switch (currency.BaseName)
{
case "Divine Orb":
priceData.PriceInDivine = ratio;
if (_divExRate > 0)
priceData.PriceInExalted = ratio * _divExRate;
if (_chaosDivRate > 0)
priceData.PriceInChaos = ratio * _chaosDivRate;
break;
case "Exalted Orb":
priceData.PriceInExalted = ratio;
if (_divExRate > 0)
{
priceData.PriceInDivine = ratio / _divExRate;
if (_chaosDivRate > 0)
priceData.PriceInChaos = priceData.PriceInDivine * _chaosDivRate;
}
break;
case "Chaos Orb":
priceData.PriceInChaos = ratio;
if (_chaosDivRate > 0)
{
priceData.PriceInDivine = ratio / _chaosDivRate;
if (_divExRate > 0)
priceData.PriceInExalted = priceData.PriceInDivine * _divExRate;
}
break;
}
}
private void UpdateCurrencyRatio(BaseItemType wanted, BaseItemType offered, float ratio, int volume)
{
if (wanted == null || offered == null) return;
if (!_currencyRatios.ContainsKey(wanted.BaseName))
_currencyRatios[wanted.BaseName] = new Dictionary<string, CurrencyRatio>();
_currencyRatios[wanted.BaseName][offered.BaseName] = new CurrencyRatio
{
LastUpdate = DateTime.Now,
Ratio = ratio,
Volume = volume
};
if (!_currencyRatios.ContainsKey(offered.BaseName))
_currencyRatios[offered.BaseName] = new Dictionary<string, CurrencyRatio>();
_currencyRatios[offered.BaseName][wanted.BaseName] = new CurrencyRatio
{
LastUpdate = DateTime.Now,
Ratio = 1 / ratio,
Volume = volume
};
_lastRatioUpdate = DateTime.Now;
}
private (double profitPercent, string betterCurrency) CalculatePriceProfit(ItemPriceData priceData)
{
if (priceData == null || _divExRate <= 0 || _chaosDivRate <= 0)
return (0, "Unknown");
var divinePrice = priceData.PriceInDivine;
var exaltedPriceInDivine = priceData.PriceInExalted / _divExRate;
var chaosPriceInDivine = priceData.PriceInChaos / _chaosDivRate;
var prices = new[]
{
(Currency: "Divine", Price: divinePrice),
(Currency: "Exalted", Price: exaltedPriceInDivine),
(Currency: "Chaos", Price: chaosPriceInDivine)
};
var minPrice = prices.MinBy(x => x.Price);
var maxPrice = prices.MaxBy(x => x.Price);
var profitPercent = ((maxPrice.Price - minPrice.Price) / minPrice.Price) * 100;
return profitPercent switch
{
> 0 => (Math.Abs(profitPercent), minPrice.Currency),
_ => (0, "Equal")
};
}
private (int profitAmount, string buyCurrency) CalculateCurrencyProfit(string currencyName)
{
if (!_currencyRatios.ContainsKey(currencyName))
return (0, "No data");
var divineRatio = GetRatioOrDefault(currencyName, "Divine Orb");
var exaltedRatio = GetRatioOrDefault(currencyName, "Exalted Orb");
var chaosRatio = GetRatioOrDefault(currencyName, "Chaos Orb");
if (divineRatio <= 0 || exaltedRatio <= 0 || chaosRatio <= 0 ||
_divExRate <= 0 || _chaosDivRate <= 0)
return (0, "No data");
var (profit, buyCurrency) = GetBestProfitCurrency(divineRatio, exaltedRatio, chaosRatio);
return profit < 1 ? (0, "No profit") : ((int)profit, buyCurrency);
}
public override void AreaChange(AreaInstance area)
{
}
public override void Tick()
{
_getNinjaValue = GameController.PluginBridge.GetMethod<Func<BaseItemType, double>>("NinjaPrice.GetBaseItemTypeValue");
}
private double? GetNinjaRatio(BaseItemType wantedItem, BaseItemType offeredItem)
{
return _getNinjaValue?.Invoke(wantedItem) / _getNinjaValue?.Invoke(offeredItem);
}
public override void Render()
{
if (GameController.IngameState.IngameUi.CurrencyExchangePanel is not { IsVisible: true } panel)
{
return;
}
if (panel is { WantedItemType: { } wantedItemType, OfferedItemType: { } offeredItemType })
{
var firstOffer = panel?.OfferedItemStock?
.FirstOrDefault(x => x != null && x.Give != 0 && x.Get != 0);
float? firstOfferedRatio = null;
if (firstOffer != null && firstOffer.Give > 0)
{
firstOfferedRatio = firstOffer.Get / (float)firstOffer.Give;
}
if (firstOfferedRatio.HasValue)
{
UpdateCurrencyRates(wantedItemType, offeredItemType, firstOfferedRatio.Value);
if (wantedItemType.BaseName != "Divine Orb" && wantedItemType.BaseName != "Exalted Orb")
{
if (offeredItemType.BaseName == "Divine Orb" || offeredItemType.BaseName == "Exalted Orb")
{
UpdateItemPrice(wantedItemType, offeredItemType, firstOfferedRatio.Value);
}
}
}
if (firstOffer != null && firstOffer.Give > 0)
{
float ratio = firstOffer.Get / (float)firstOffer.Give;
int volume = firstOffer.ListedCount;
UpdateCurrencyRatio(wantedItemType, offeredItemType, ratio, volume);
}
if (ImGui.Begin("StockWindow"))
{
var offeredStock = panel.OfferedItemStock.Select(x => (x.Give, x.Get, Ratio: x.Get / (float)x.Give, ListedCount: x.ListedCount * x.Give / (float)x.Get))
.Where(x => x.Get != 0 && x.Give != 0 && x.ListedCount > 0).ToList();
var wantedStock = panel.WantedItemStock.Select(x => (x.Give, x.Get, Ratio: x.Give / (float)x.Get, x.ListedCount))
.Where(x => x.Get != 0 && x.Give != 0 && x.ListedCount > 0).ToList();
var leftPoints = offeredStock
.OrderByDescending(x => x.Ratio)
.Aggregate((0f, new List<(float Ratio, float ListedCount)>().AsEnumerable()), (a, x) =>
(x.ListedCount + a.Item1, a.Item2.Append((x.Ratio, x.ListedCount + a.Item1))))
.Item2.Reverse().ToList();
var rightPoints = wantedStock
.OrderBy(x => x.Ratio)
.Aggregate((0, new List<(float Ratio, float ListedCount)>().AsEnumerable()), (a, x) =>
(x.ListedCount + a.Item1, a.Item2.Append((x.Ratio, x.ListedCount + a.Item1))))
.Item2.ToList();
var wantedItemStockRest = panel.WantedItemStock.FirstOrDefault(x => x.Give == 0 && x.Get == 0);
var offeredItemStockRest = panel.OfferedItemStock.FirstOrDefault(x => x.Give == 0 && x.Get == 0);
if (leftPoints.Any() || leftPoints.Any())
{
var trueLeftmostX = leftPoints.Concat(rightPoints).First().Ratio;
var trueRightmostX = leftPoints.Concat(rightPoints).Last().Ratio;
var expansionCoefficient = 0.05f;
var expansionAmount = (trueRightmostX - trueLeftmostX) * expansionCoefficient;
if (rightPoints.Any() && wantedItemStockRest is { ListedCount: > 0 and var restRightListed })
{
rightPoints.Add((trueRightmostX + expansionAmount, restRightListed + rightPoints.Last().ListedCount));
}
if (leftPoints.Any() && offeredItemStockRest is { ListedCount: > 0 and var restLeftListed })
{
leftPoints.Insert(0, (trueLeftmostX - expansionAmount, restLeftListed / leftPoints.First().Ratio + leftPoints.First().ListedCount));
}
var (leftmostX, rightmostX) = (trueLeftmostX - expansionAmount * 2, trueRightmostX + expansionAmount * 2);
var graphSizeX = ImGui.GetContentRegionAvail().X - Settings.GraphPadding.Value * 2;
var graphSizeY = Settings.GraphHeight.Value;
float RatioToU(double ratio) => (float)((ratio - leftmostX) / (rightmostX - leftmostX));
Vector2 UvToXy(Vector2 v) => v * new Vector2(graphSizeX, -graphSizeY) + new Vector2(0, graphSizeY);
Vector2 UvToXyBottom(Vector2 v) => v * new Vector2(graphSizeX, 0) + new Vector2(0, graphSizeY);
var topY = Math.Max(leftPoints.FirstOrDefault().ListedCount, rightPoints.LastOrDefault().ListedCount);
var leftPointsUv = leftPoints.Select(x => new Vector2(RatioToU(x.Ratio), MathF.Log(x.ListedCount + 1) / MathF.Log(topY)))
.Prepend(new Vector2(0, 0))
.ToList();
var rightPointsUv = rightPoints.Select(x => new Vector2(RatioToU(x.Ratio), MathF.Log(x.ListedCount + 1) / MathF.Log(topY)))
.Append(new Vector2(1, 0))
.ToList();
var leftPointXyPairs = leftPointsUv.Pairwise((l, r) => new[]
{
UvToXy(r with { X = l.X }),
UvToXy(r)
}).ToList();
var cursorScreenPos = ImGui.GetCursorScreenPos() + new Vector2(Settings.GraphPadding.Value, 0);
Span<Vector2> leftPointsXy = leftPointXyPairs.SelectMany(x => x)
.Concat(leftPointsUv.AsEnumerable().Reverse().Select(UvToXyBottom))
.Select(x => x + cursorScreenPos)
.ToArray();
var rightPointXyPairs = rightPointsUv.Pairwise((l, r) => new[]
{
UvToXy(l),
UvToXy(l with { X = r.X })
}).ToList();
Span<Vector2> rightPointsXy = rightPointXyPairs.SelectMany(x => x)
.Concat(rightPointsUv.AsEnumerable().Reverse().Select(UvToXyBottom))
.Select(x => x + cursorScreenPos)
.ToArray();
var drawList = ImGui.GetWindowDrawList();
foreach (var pair in leftPointXyPairs)
{
drawList.AddRectFilled(pair[0] + cursorScreenPos, pair[1] with { Y = graphSizeY } + cursorScreenPos,
(Color.Red.ToImguiVec4(60).ToColor()).ToImgui());
}
foreach (var pair in rightPointXyPairs)
{
drawList.AddRectFilled(pair[0] + cursorScreenPos, pair[1] with { Y = graphSizeY } + cursorScreenPos,
(Color.Green.ToImguiVec4(60).ToColor()).ToImgui());
}
drawList.AddPolyline(ref leftPointsXy[0], leftPointsXy.Length, Color.Red.ToImgui(), ImDrawFlags.Closed, 1);
drawList.AddPolyline(ref rightPointsXy[0], rightPointsXy.Length, Color.Green.ToImgui(), ImDrawFlags.Closed, 1);
var lineDict = new Dictionary<int, float>();
var numberSet = new HashSet<string>();
float GetLineHeight(int key)
{
if (lineDict.TryGetValue(key, out var height))
{
return height;
}
height = lineDict.Count * ImGui.GetTextLineHeight() + cursorScreenPos.Y + graphSizeY +
(ImGui.GetTextLineHeightWithSpacing() - ImGui.GetTextLineHeight()) / 2;
lineDict[key] = height;
return height;
}
var leftString = ((double)trueLeftmostX).FormatNumber(2, 0.2);
if (numberSet.Add(leftString))
{
DrawTextMiddle(drawList, leftString, new Vector2(cursorScreenPos.X, GetLineHeight(0)));
}
var rightString = ((double)trueRightmostX).FormatNumber(2, 0.2);
if (numberSet.Add(rightString))
{
DrawTextMiddle(drawList, rightString, new Vector2(cursorScreenPos.X + graphSizeX, GetLineHeight(0)));
}
if (leftPoints.Any())
{
double ratio = leftPoints.Last().Ratio;
var leftMString = ratio.FormatNumber(2, 0.2);
if (numberSet.Add(leftMString))
{
DrawTextMiddle(drawList, leftMString, new Vector2(cursorScreenPos.X + UvToXy(new Vector2(RatioToU(ratio), 0)).X, GetLineHeight(1)));
}
}
if (rightPoints.Any())
{
double ratio = rightPoints.First().Ratio;
var rightMString = ratio.FormatNumber(2, 0.2);
if (numberSet.Add(rightMString))
{
DrawTextMiddle(drawList, rightMString, new Vector2(cursorScreenPos.X + UvToXy(new Vector2(RatioToU(ratio), 0)).X, GetLineHeight(1)));
}
}
if (GetNinjaRatio(wantedItemType, offeredItemType) is { } ninjaRatio)
{
var ninjaU = RatioToU((float)ninjaRatio);
if (ninjaU >= 0 && ninjaU <= 1)
{
var ninjaUvLow = new Vector2(ninjaU, 0);
var ninjaUvHigh = new Vector2(ninjaU, 0.3f);
drawList.AddLine(UvToXy(ninjaUvLow) + cursorScreenPos, UvToXy(ninjaUvHigh) + cursorScreenPos, Color.White.ToImgui(), 3);
var ninjaString = ninjaRatio.FormatNumber(2, 0.2);
if (numberSet.Add(ninjaString))
{
DrawTextMiddle(drawList, ninjaString, new Vector2(cursorScreenPos.X + UvToXy(ninjaUvLow).X, GetLineHeight(2)));
}
}
}
ImGui.Dummy(new Vector2(graphSizeX + Settings.GraphPadding.Value * 2,
graphSizeY + ImGui.GetTextLineHeightWithSpacing() + (lineDict.Count - 1) * ImGui.GetTextLineHeight()));
}
if (ImGui.BeginTable("orderbook", 2))
{
ImGui.TableSetupColumn("Offered item listings");
ImGui.TableSetupColumn("Wanted item listings");
ImGui.TableHeadersRow();
ImGui.TableNextRow();
ImGui.TableNextColumn();
if (ImGui.BeginTable("offeredStock", 2))
{
ImGui.TableSetupColumn("Ratio");
ImGui.TableSetupColumn("Count (in wanted items)");
ImGui.TableHeadersRow();
foreach (var offeredStockItem in offeredStock)
{
ImGui.TableNextRow();
ImGui.TableNextColumn();
ImGui.Text($"{((double)offeredStockItem.Ratio).FormatNumber(2, 0.2)}");
ImGui.TableNextColumn();
ImGui.Text($"{offeredStockItem.ListedCount}");
}
if (offeredItemStockRest is { ListedCount: > 0 })
{
ImGui.TableNextRow();
ImGui.TableNextColumn();
ImGui.Text($"<{((double)offeredStock.Last().Ratio).FormatNumber(2, 0.2)}");
ImGui.TableNextColumn();
ImGui.Text($"{offeredItemStockRest.ListedCount / offeredStock.Last().Ratio:F1}");
}
ImGui.EndTable();
}
ImGui.TableNextColumn();
if (ImGui.BeginTable("wantedStock", 2))
{
ImGui.TableSetupColumn("Ratio");
ImGui.TableSetupColumn("Count");
ImGui.TableHeadersRow();
foreach (var wantedStockItem in wantedStock)
{
ImGui.TableNextRow();
ImGui.TableNextColumn();
ImGui.Text($"{((double)wantedStockItem.Ratio).FormatNumber(2, 0.2)}");
ImGui.TableNextColumn();
ImGui.Text($"{wantedStockItem.ListedCount}");
}
if (wantedItemStockRest is { ListedCount: > 0 })
{
ImGui.TableNextRow();
ImGui.TableNextColumn();
ImGui.Text($">{((double)wantedStock.Last().Ratio).FormatNumber(2, 0.2)}");
ImGui.TableNextColumn();
ImGui.Text($"{wantedItemStockRest.ListedCount}");
}
ImGui.EndTable();
}
ImGui.EndTable();
}
ImGui.Text($"Total {wantedItemType.BaseName} volume: {panel.WantedItemStock.Sum(x => x.ListedCount)}");
ImGui.Text($"Total {offeredItemType.BaseName} volume: {panel.OfferedItemStock.Sum(x => x.ListedCount)}");
ImGui.SliderInt("Spread depth", ref _spread, 1, Settings.MaxSpreadDepth);
ImGui.Text($"Spread: {((wantedStock.SkipWhile((w, i) => wantedStock.Take(i + 1).Sum(ww => ww.ListedCount) < _spread).FirstOrDefault().Ratio /
offeredStock.SkipWhile((o, i) => offeredStock.Take(i + 1).Sum(oo => oo.ListedCount) < _spread).FirstOrDefault().Ratio - 1) * 100) switch {
< 0 => "Unknown",
float.NaN => "Unknown",
var x => x.ToString("F0") + "%%"
}}");
}
}
DrawCurrencyInfo();
}
private void DrawCurrencyInfo()
{
if (!Settings.EnableProfitPanel || !_currencyRatios.Any())
return;
ImGuiWindowFlags windowFlags =
ImGuiWindowFlags.NoScrollWithMouse |
ImGuiWindowFlags.NoScrollbar;
var initialSize = new Vector2(600, 400);
ImGui.SetNextWindowSize(initialSize, ImGuiCond.FirstUseEver);
ImGui.SetNextWindowSizeConstraints(new Vector2(400, 200), new Vector2(float.MaxValue, float.MaxValue));
if (ImGui.Begin("Currency Exchange Profits", windowFlags))
{
if (ImGui.Button("Clear Cache"))
{
ClearCache();
}
ImGui.SameLine();
if (ImGui.Button(_showDivineInChaos ? "Show in Ex" : "Show in Chaos"))
{
_showDivineInChaos = !_showDivineInChaos;
}
ImGui.SameLine();
// Define colors
var warningColor = new Vector4(1.0f, 1.0f, 0.0f, 1.0f); // Yellow
var validColor = new Vector4(0.498f, 0.788f, 0.576f, 1.0f); // #7fc993
// Divine/Ex rate display
ImGui.TextColored(
_divExRate <= 0 ? warningColor : validColor,
$"Divine/Ex rate: {(_divExRate <= 0 ? "Unknown" : _divExRate.ToString("F2"))}");
ImGui.SameLine();
ImGui.Text("|");
ImGui.SameLine();
// Divine/Chaos rate display
ImGui.TextColored(
_chaosDivRate <= 0 ? warningColor : validColor,
$"Divine/Chaos rate: {(_chaosDivRate <= 0 ? "Unknown" : _chaosDivRate.ToString("F2"))}");
ImGui.Separator();
var tableFlags = ImGuiTableFlags.Resizable |
ImGuiTableFlags.Reorderable |
ImGuiTableFlags.Hideable |
ImGuiTableFlags.Sortable |
ImGuiTableFlags.BordersV |
ImGuiTableFlags.ScrollY;
float tableHeight = ImGui.GetContentRegionAvail().Y;
if (ImGui.BeginTable("CurrencyRatios", 5, tableFlags, new Vector2(0, tableHeight)))
{
ImGui.TableSetupScrollFreeze(0, 1);
ImGui.TableSetupColumn("Currency\nName", ImGuiTableColumnFlags.DefaultSort);
ImGui.TableSetupColumn("Divine\nRate");
ImGui.TableSetupColumn("Exalted\nRate");
ImGui.TableSetupColumn("Chaos\nRate");
ImGui.TableSetupColumn("Profit");
ImGui.TableHeadersRow();
foreach (var currency in _currencyRatios.Keys
.Where(k => k != "Divine Orb" && k != "Exalted Orb" && k != "Chaos Orb")
.OrderByDescending(k => CalculateCurrencyProfit(k).profitAmount))
{
var divineRatio = _currencyRatios[currency].GetValueOrDefault("Divine Orb")?.Ratio ?? 0;
var exaltedRatio = _currencyRatios[currency].GetValueOrDefault("Exalted Orb")?.Ratio ?? 0;
var chaosRatio = _currencyRatios[currency].GetValueOrDefault("Chaos Orb")?.Ratio ?? 0;
if (divineRatio <= 0 && exaltedRatio <= 0 && chaosRatio <= 0) continue;
ImGui.TableNextRow();
ImGui.TableNextColumn();
ImGui.TextWrapped(currency);
ImGui.TableNextColumn();
DisplayCurrencyRate(currency, divineRatio);
ImGui.TableNextColumn();
if (exaltedRatio > 0)
{
var divineEquiv = exaltedRatio / _divExRate;
ImGui.Text($"1:{exaltedRatio:F1}\n(1:{divineEquiv:F1} div)");
}
else
{
ImGui.Text("-");
}
ImGui.TableNextColumn();
if (chaosRatio > 0)
{
var divineEquiv = chaosRatio / _chaosDivRate;
ImGui.Text($"1:{chaosRatio:F1}\n(1:{divineEquiv:F1} div)");
}
else
{
ImGui.Text("-");
}
ImGui.TableNextColumn();
var (profit, buyCurrency) = CalculateCurrencyProfit(currency);
if (profit > 0)
{
var color = profit > 5 ? Color.LightGreen : Color.White;
ImGui.TextColored(color.ToImguiVec4(), $"{profit}ex\nBuy with {buyCurrency}");
}
else
{
ImGui.TextColored(Color.Gray.ToImguiVec4(), "No profit");
}
}
ImGui.EndTable();
}
ImGui.End();
}
}
private void ClearCache()
{
var tempDivExRate = _divExRate;
var tempLastDivExRate = _lastDivExRate;
var tempChaosDivRate = _chaosDivRate;
var tempLastChaosDivRate = _lastChaosDivRate;
_currencyRatios.Clear();
_itemPrices.Clear();
_divExRate = tempDivExRate;
_lastDivExRate = tempLastDivExRate;
_chaosDivRate = tempChaosDivRate;
_lastChaosDivRate = tempLastChaosDivRate;
}
private void DrawTextMiddle(ImDrawListPtr drawList, string text, Vector2 position)
{
var textSizeX = ImGui.CalcTextSize(text).X;
drawList.AddText(position - new Vector2(textSizeX / 2, 0), Color.White.ToImgui(), text);
}
private void DisplayCurrencyRate(string currency, double ratio, string prefix = "")
{
if (ratio <= 0)
{
ImGui.Text("-");
return;
}
var (convRate, label) = _showDivineInChaos
? (_chaosDivRate, "c")
: (_divExRate, "ex");
var convertedEquiv = ratio * convRate;
ImGui.Text($"{prefix}1:{ratio:F1}");
ImGui.Text($"(1:{convertedEquiv:F1} {label})");
}
private (double profit, string currency) GetBestProfitCurrency(double divinePrice, double exaltedPrice, double chaosPrice)
{
var prices = new[]
{
("Divine", divinePrice),
("Exalted", exaltedPrice / _divExRate),
("Chaos", chaosPrice / _chaosDivRate)
};
var min = prices.MinBy(x => x.Item2);
var max = prices.MaxBy(x => x.Item2);
var profit = (max.Item2 - min.Item2) * _divExRate;
return profit > 0 ? (profit, min.Item1) : (0, "Equal");
}
private double GetRatioOrDefault(string currency, string target) =>
_currencyRatios.GetValueOrDefault(currency)?.GetValueOrDefault(target)?.Ratio ?? 0;
}
public static class Extensions
{
public static string FormatNumber(this double number, int significantDigits, double maxInvertValue = 0, bool forceDecimals = false)
{
if (double.IsNaN(number))
{
return "NaN";
}
if (number == 0)
{
return "0";
}
if (Math.Abs(number) <= 1e-10)
{
return "~0";
}
if (Math.Abs(number) < maxInvertValue)
{
return $"1/{Math.Round((decimal)(1 / number), 1):#.#}";
}
return Math.Round((decimal)number, significantDigits).ToString($"#,##0.{new string(forceDecimals ? '0' : '#', significantDigits)}");
}
}