@@ -159,17 +159,17 @@ export class CharacterOperator {
159159 const battle = battleResult . battles . items . find ( b => b . id . toLowerCase ( ) === this . config . gameAddress . toLowerCase ( ) ) ;
160160
161161 if ( ! battle ) {
162- this . log ( "Battle not found for game address" , this . config . gameAddress ) ;
162+ this . log ( "Battle not found for game address " + this . config . gameAddress ) ;
163163 return false ;
164164 }
165165
166166 if ( battle . winner != null ) {
167- this . log ( "Battle already finished with winner" , battle . winner , " for game address" , this . config . gameAddress ) ;
167+ this . log ( "Battle already finished with winner " + battle . winner + " for game address " + this . config . gameAddress ) ;
168168 return false ;
169169 }
170170
171171 if ( ! battle . gameStartedAt ) {
172- this . log ( "Battle has not started yet" , battle . id ) ;
172+ this . log ( "Battle has not started yet " + battle . id ) ;
173173 return true ;
174174 }
175175
@@ -320,9 +320,17 @@ export class CharacterOperator {
320320 }
321321 }
322322
323- // If no cards can be played, end the turn
324- if ( playableCards . length === 0 ) {
325- this . log ( 'No playable cards with current energy:' , currentEnergy ) ;
323+ // Filter out cards we've already attempted
324+ const untriedPlayableCards = playableCards . filter ( card => ! attemptedCardIndices . has ( card . cardId ) ) ;
325+
326+ // If no cards can be played or all playable cards have been tried, end the turn
327+ if ( playableCards . length === 0 || untriedPlayableCards . length === 0 ) {
328+ this . log ( 'No more playable cards:' , {
329+ currentEnergy,
330+ playableCardsCount : playableCards . length ,
331+ untriedCount : untriedPlayableCards . length ,
332+ attemptedCount : attemptedCardIndices . size
333+ } ) ;
326334
327335 // End the turn
328336 const endTurnData = encodeFunctionData ( {
@@ -362,8 +370,8 @@ export class CharacterOperator {
362370 break ;
363371 }
364372
365- // Randomly select a card from the playable cards
366- const selectedCard = playableCards [ Math . floor ( Math . random ( ) * playableCards . length ) ] ;
373+ // Randomly select a card from the untried playable cards
374+ const selectedCard = untriedPlayableCards [ Math . floor ( Math . random ( ) * untriedPlayableCards . length ) ] ;
367375 const playableCardId = selectedCard . cardId ;
368376 const playableHandIndex = selectedCard . handIndex ;
369377 const energyCost = selectedCard . energyCost ;
@@ -437,13 +445,15 @@ export class CharacterOperator {
437445 this . lastActionTime = Date . now ( ) ;
438446 } catch ( error : any ) {
439447 if ( error . message ?. includes ( 'CardNotInHandError' ) ) {
440- // This should never happen with proper tracking
441- this . error ( 'Unexpected CardNotInHandError - this indicates a bug in the card tracking logic ' , {
448+ // This can happen if there's a race condition or state mismatch
449+ this . error ( 'CardNotInHandError - marking card as attempted and continuing ' , {
442450 playableHandIndex,
443451 playableCardId,
444452 attemptedIndices : Array . from ( attemptedCardIndices )
445453 } ) ;
446- break ; // Exit to prevent further issues
454+ // Continue to next iteration instead of breaking - this allows us to try other cards
455+ // The attempted card is already marked, so we won't try it again
456+ continue ;
447457 } else if ( error . message ?. includes ( 'GameHasNotStartedError' ) ) {
448458 this . log ( 'Game has ended, stopping card play' ) ;
449459 break ;
0 commit comments