@@ -6,7 +6,6 @@ const MARGONEM_CHARACTER_LIST_URL =
66 "https://public-api.margonem.pl/account/charlist" ;
77const MARGONEM_CHARACTER_LIST_EN_URL =
88 "https://public-api.margonem.com/account/charlist" ;
9- const CHARACTER_LIST_LOG_PREFIX = "[CharacterList]" ;
109
1110export type MargonemCharacter = {
1211 clan ?: number ;
@@ -211,52 +210,6 @@ export const normalizeCharacterList = (
211210 ) ;
212211} ;
213212
214- const logCharacterListDebug = (
215- message : string ,
216- details ?: Record < string , unknown > ,
217- ) => {
218- if ( details ) {
219- console . log ( `${ CHARACTER_LIST_LOG_PREFIX } ${ message } ` , details ) ;
220- return ;
221- }
222-
223- console . log ( `${ CHARACTER_LIST_LOG_PREFIX } ${ message } ` ) ;
224- } ;
225-
226- const summarizeCharacterEntry = ( character : unknown ) => {
227- if ( typeof character !== "object" || character === null ) {
228- return {
229- entryType : typeof character ,
230- isNull : character === null ,
231- } ;
232- }
233-
234- const characterData = character as Record < string , unknown > ;
235-
236- return {
237- keys : Object . keys ( characterData ) ,
238- idType : typeof characterData . id ,
239- iconType : typeof characterData . icon ,
240- lvlType : typeof characterData . lvl ,
241- nickType : typeof characterData . nick ,
242- profType : typeof characterData . prof ,
243- worldType : typeof characterData . world ,
244- idPreview : characterData . id ,
245- lvlPreview : characterData . lvl ,
246- nickPreview : characterData . nick ,
247- profPreview : characterData . prof ,
248- worldPreview : characterData . world ,
249- } ;
250- } ;
251-
252- const getCharacterWorldCounts = ( characters : MargonemCharacter [ ] ) => {
253- return characters . reduce < Record < string , number > > ( ( worldCounts , character ) => {
254- // @ts -expect-error `normalizeCharacterList` guarantees `world` is a string here.
255- worldCounts [ character . world ] = ( worldCounts [ character . world ] ?? 0 ) + 1 ;
256- return worldCounts ;
257- } , { } ) ;
258- } ;
259-
260213const filterCharactersByWorld = (
261214 characters : MargonemCharacter [ ] ,
262215 world : string | undefined ,
@@ -283,28 +236,12 @@ export async function fetchCharacterList({
283236 const margonemEntry = window . localStorage ?. getItem ( "Margonem" ) ;
284237 const accountIdKey = String ( accountId ) ;
285238
286- logCharacterListDebug ( "Fetching character list" , {
287- accountId,
288- world,
289- languageVersion,
290- hasMargonemEntry : Boolean ( margonemEntry ) ,
291- } ) ;
292-
293239 let parsed : unknown = null ;
294240
295241 if ( margonemEntry ) {
296242 try {
297243 parsed = JSON . parse ( margonemEntry ) ;
298244 } catch ( error ) {
299- console . warn (
300- `${ CHARACTER_LIST_LOG_PREFIX } Failed to parse Margonem cache` ,
301- {
302- accountId,
303- world,
304- margonemEntryLength : margonemEntry . length ,
305- error,
306- } ,
307- ) ;
308245 throw error ;
309246 }
310247 }
@@ -321,78 +258,17 @@ export async function fetchCharacterList({
321258 const cached = accountId ? normalizeCharacterList ( rawCachedCharacters ) : [ ] ;
322259 const filteredCached = filterCharactersByWorld ( cached , world ) ;
323260
324- logCharacterListDebug ( "Resolved cached character list" , {
325- accountId,
326- world,
327- hasCharlist : Boolean ( charlist ) ,
328- rawCachedType :
329- rawCachedCharacters === null ? "missing" : typeof rawCachedCharacters ,
330- rawCachedCount : Array . isArray ( rawCachedCharacters )
331- ? rawCachedCharacters . length
332- : null ,
333- normalizedCachedCount : cached . length ,
334- filteredCachedCount : filteredCached . length ,
335- cachedWorldCounts : getCharacterWorldCounts ( cached ) ,
336- } ) ;
337-
338- if (
339- Array . isArray ( rawCachedCharacters ) &&
340- rawCachedCharacters . length > 0 &&
341- cached . length === 0
342- ) {
343- console . warn (
344- `${ CHARACTER_LIST_LOG_PREFIX } Cached character entries were rejected by normalization` ,
345- {
346- accountId,
347- world,
348- firstCachedEntrySummary : summarizeCharacterEntry (
349- rawCachedCharacters [ 0 ] ,
350- ) ,
351- } ,
352- ) ;
353- }
354-
355261 if ( filteredCached . length > 0 ) {
356- logCharacterListDebug ( "Returning cached character list" , {
357- accountId,
358- world,
359- filteredCachedCount : filteredCached . length ,
360- } ) ;
361-
362262 return filteredCached ;
363263 }
364264
365- if ( cached . length > 0 ) {
366- logCharacterListDebug (
367- "Cached character list does not contain entries for current world, falling back to public API" ,
368- {
369- accountId,
370- world,
371- normalizedCachedCount : cached . length ,
372- filteredCachedCount : filteredCached . length ,
373- } ,
374- ) ;
375- }
376-
377265 const hs3 = window . getCookie ?.( "hs3" ) ;
378266 const url =
379267 languageVersion === LanguageVersion . PL
380268 ? MARGONEM_CHARACTER_LIST_URL
381269 : MARGONEM_CHARACTER_LIST_EN_URL ;
382270
383- logCharacterListDebug ( "Cache miss, falling back to public API" , {
384- accountId,
385- world,
386- url,
387- hasHs3 : Boolean ( hs3 ) ,
388- } ) ;
389-
390271 if ( ! hs3 ) {
391- console . warn ( `${ CHARACTER_LIST_LOG_PREFIX } Missing authentication cookie` , {
392- accountId,
393- world,
394- url,
395- } ) ;
396272 throw new Error ( "Missing required authentication cookie" ) ;
397273 }
398274
@@ -406,39 +282,7 @@ export async function fetchCharacterList({
406282 world ,
407283 ) ;
408284
409- logCharacterListDebug ( "Received public API character list" , {
410- accountId,
411- world,
412- rawApiCount : Array . isArray ( response . data ) ? response . data . length : null ,
413- normalizedApiCount : normalizedCharacters . length ,
414- filteredApiCount : filteredCharacters . length ,
415- apiWorldCounts : getCharacterWorldCounts ( normalizedCharacters ) ,
416- } ) ;
417-
418- if (
419- Array . isArray ( response . data ) &&
420- response . data . length > 0 &&
421- normalizedCharacters . length === 0
422- ) {
423- console . warn (
424- `${ CHARACTER_LIST_LOG_PREFIX } API character entries were rejected by normalization` ,
425- {
426- accountId,
427- world,
428- firstApiEntrySummary : summarizeCharacterEntry ( response . data [ 0 ] ) ,
429- } ,
430- ) ;
431- }
432-
433285 if ( ! response . data || response . data . length === 0 ) {
434- console . warn (
435- `${ CHARACTER_LIST_LOG_PREFIX } Empty character list received from API` ,
436- {
437- accountId,
438- world,
439- url,
440- } ,
441- ) ;
442286 throw new Error ( "Empty character list received from API" ) ;
443287 }
444288
0 commit comments