@@ -18,6 +18,9 @@ public static class ApiAdapter
1818 private static StashTabPropsList PropsList { get ; set ; }
1919 public static bool FetchError { get ; set ; }
2020 public static bool FetchingDone { get ; set ; }
21+
22+ public static List < StashTab > StashTabList { get ; private set ; }
23+
2124 public static async Task < bool > GenerateUri ( )
2225 {
2326 FetchError = false ;
@@ -31,7 +34,7 @@ public static async Task<bool> GenerateUri()
3134
3235 if ( await GetProps ( accName , league ) && ! FetchError )
3336 {
34- GenerateStashTabs ( ) ;
37+ StashTabList = PropsList . tabs . ConvertAll ( x => new StashTab ( x . n , x . i ) ) ;
3538 GenerateStashtabUris ( accName , league ) ;
3639 return true ;
3740 }
@@ -44,70 +47,16 @@ public static async Task<bool> GenerateUri()
4447 return false ;
4548 }
4649
47- private static void GenerateStashTabs ( )
48- {
49- var ret = new List < StashTab > ( ) ;
50-
51- // mode = ID
52- if ( Properties . Settings . Default . StashtabMode == 0 )
53- {
54- StashTabList . GetStashTabIndices ( ) ;
55- if ( PropsList != null )
56- {
57- foreach ( var p in PropsList . tabs )
58- {
59- for ( int i = StashTabList . StashTabIndices . Count - 1 ; i > - 1 ; i -- )
60- {
61- if ( StashTabList . StashTabIndices [ i ] == p . i )
62- {
63- StashTabList . StashTabIndices . RemoveAt ( i ) ;
64- ret . Add ( new StashTab ( p . n , p . i ) ) ;
65- }
66- }
67- }
68- StashTabList . StashTabs = ret ;
69- GetAllTabNames ( ) ;
70- }
71- }
72- // mode = Name
73- else
74- {
75- if ( PropsList != null )
76- {
77- string stashName = Properties . Settings . Default . StashTabName ;
78- foreach ( var p in PropsList . tabs )
79- {
80- if ( p . n . StartsWith ( stashName ) )
81- {
82- ret . Add ( new StashTab ( p . n , p . i ) ) ;
83- }
84- }
85- StashTabList . StashTabs = ret ;
86- }
87- }
88- Trace . WriteLine ( StashTabList . StashTabs . Count , "stash tab count" ) ;
89- }
90-
9150 private static void GenerateStashtabUris ( string accName , string league )
9251 {
93- foreach ( var i in StashTabList . StashTabs )
52+ if ( StashTabList is null )
9453 {
95- string stashTab = i . TabIndex . ToString ( ) ;
96- i . StashTabUri = new Uri ( $ "https://www.pathofexile.com/character-window/get-stash-items?accountName={ accName } &tabIndex={ stashTab } &league={ league } " ) ;
54+ return ;
9755 }
98- }
9956
100- private static void GetAllTabNames ( )
101- {
102- foreach ( var s in StashTabList . StashTabs )
57+ for ( int i = 0 ; i < StashTabList . Count ; i ++ )
10358 {
104- foreach ( var p in PropsList . tabs )
105- {
106- if ( s . TabIndex == p . i )
107- {
108- s . TabName = p . n ;
109- }
110- }
59+ StashTabList [ i ] . StashTabUri = new Uri ( $ "https://www.pathofexile.com/character-window/get-stash-items?accountName={ accName } &tabIndex={ i } &league={ league } " ) ;
11160 }
11261 }
11362
@@ -193,7 +142,7 @@ public static async Task<bool> GetItems()
193142 return false ;
194143 }
195144 // check rate limit
196- if ( RateLimit . RateLimitState [ 0 ] >= RateLimit . MaximumRequests - StashTabList . StashTabs . Count - 4 )
145+ if ( RateLimit . RateLimitState [ 0 ] >= RateLimit . MaximumRequests - 5 )
197146 {
198147 RateLimit . RateLimitExceeded = true ;
199148 return false ;
@@ -209,42 +158,42 @@ public static async Task<bool> GetItems()
209158 {
210159 // add user agent
211160 client . DefaultRequestHeaders . Add ( "User-Agent" , $ "EnhancePoEApp/v{ Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version } " ) ;
212- foreach ( var i in StashTabList . StashTabs )
161+
162+ // check rate limit ban
163+ if ( RateLimit . CheckForBan ( ) )
213164 {
214- // check rate limit ban
215- if ( RateLimit . CheckForBan ( ) )
165+ return false ;
166+ }
167+
168+ var stashTab = MainWindow . Instance . SelectedStashTab ;
169+ if ( ! usedUris . Contains ( stashTab . StashTabUri ) )
170+ {
171+ cookieContainer . Add ( stashTab . StashTabUri , new Cookie ( "POESESSID" , sessionId ) ) ;
172+ using var res = await client . GetAsync ( stashTab . StashTabUri ) ;
173+ usedUris . Add ( stashTab . StashTabUri ) ;
174+ if ( res . IsSuccessStatusCode )
216175 {
217- return false ;
176+ using var content = res . Content ;
177+ // get new rate limit values
178+ string rateLimit = res . Headers . GetValues ( "X-Rate-Limit-Account" ) . FirstOrDefault ( ) ;
179+ string rateLimitState = res . Headers . GetValues ( "X-Rate-Limit-Account-State" ) . FirstOrDefault ( ) ;
180+ string responseTime = res . Headers . GetValues ( "Date" ) . FirstOrDefault ( ) ;
181+ RateLimit . DeserializeRateLimits ( rateLimit , rateLimitState ) ;
182+ RateLimit . DeserializeResponseSeconds ( responseTime ) ;
183+
184+ // deserialize response
185+ string resContent = await content . ReadAsStringAsync ( ) ;
186+ var deserializedContent = JsonSerializer . Deserialize < ItemList > ( resContent ) ;
187+ stashTab . ItemList = deserializedContent . items ;
188+ stashTab . Quad = deserializedContent . quadLayout ;
189+
190+ stashTab . CleanItemList ( ) ;
218191 }
219- if ( ! usedUris . Contains ( i . StashTabUri ) )
192+ else
220193 {
221- cookieContainer . Add ( i . StashTabUri , new Cookie ( "POESESSID" , sessionId ) ) ;
222- using var res = await client . GetAsync ( i . StashTabUri ) ;
223- usedUris . Add ( i . StashTabUri ) ;
224- if ( res . IsSuccessStatusCode )
225- {
226- using var content = res . Content ;
227- // get new rate limit values
228- string rateLimit = res . Headers . GetValues ( "X-Rate-Limit-Account" ) . FirstOrDefault ( ) ;
229- string rateLimitState = res . Headers . GetValues ( "X-Rate-Limit-Account-State" ) . FirstOrDefault ( ) ;
230- string responseTime = res . Headers . GetValues ( "Date" ) . FirstOrDefault ( ) ;
231- RateLimit . DeserializeRateLimits ( rateLimit , rateLimitState ) ;
232- RateLimit . DeserializeResponseSeconds ( responseTime ) ;
233-
234- // deserialize response
235- string resContent = await content . ReadAsStringAsync ( ) ;
236- var deserializedContent = JsonSerializer . Deserialize < ItemList > ( resContent ) ;
237- i . ItemList = deserializedContent . items ;
238- i . Quad = deserializedContent . quadLayout ;
239-
240- i . CleanItemList ( ) ;
241- }
242- else
243- {
244- FetchError = true ;
245- _ = MessageBox . Show ( res . ReasonPhrase , "Error fetching data" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
246- return false ;
247- }
194+ FetchError = true ;
195+ _ = MessageBox . Show ( res . ReasonPhrase , "Error fetching data" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
196+ return false ;
248197 }
249198 }
250199 }
0 commit comments