All files / blacktrigram/src/systems TrigramSystem.ts

94.3% Statements 116/123
75% Branches 18/24
100% Functions 9/9
94.3% Lines 116/123

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1731x     1x 1x 1x   1x 1x   1x 17x 17x         1x 2x 2x 2x 2x 2x   2x     2x 2x   2x 2x         1x 1x 1x 1x   1x 8x 8x 8x 1x 1x 1x 8x   1x 1x         1x 14x 14x 14x 14x 14x 2x 2x 2x 2x 2x 2x   12x 12x 12x 12x 12x 12x   12x 12x     14x 5x 5x 5x 5x 5x 5x   12x 12x 12x 12x 12x 14x         1x 7x 7x 7x 7x 7x 7x 7x 7x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 2x 2x 2x 2x 2x       2x   2x 1x 1x 1x 1x 1x   2x             1x 2x 1x   1x  
import { TrigramStance } from "../types/common";
import { PlayerState } from "./player";
 
import { TRIGRAM_STANCES_ORDER, TrigramTransitionCost } from "./trigram";
import { TrigramCalculator } from "./trigram/TrigramCalculator";
import { PLAYER_ARCHETYPES_DATA } from "./types";
 
export class TrigramSystem {
  private calculator: TrigramCalculator;
 
  constructor() {
    this.calculator = new TrigramCalculator();
  }
 
  /**
   * Check if player can transition to a new stance
   */
  canTransitionTo(
    fromStance: TrigramStance,
    toStance: TrigramStance,
    player: PlayerState
  ): boolean {
    if (fromStance === toStance) return true;
 
    const cost = this.getTransitionCost(fromStance, toStance, player);
 
    // Check if player has sufficient resources
    const hasEnoughKi = player.ki >= cost.ki;
    const hasEnoughStamina = player.stamina >= cost.stamina;
 
    return hasEnoughKi && hasEnoughStamina;
  }
 
  /**
   * Recommend optimal stance against opponent
   */
  recommendStance(player: PlayerState): TrigramStance {
    const from = player.currentStance;
    let best = from;
    let bestScore = Infinity;
 
    for (const to of TRIGRAM_STANCES_ORDER) {
      const costObj: TrigramTransitionCost = this.getTransitionCost(from, to);
      const score = costObj.ki + costObj.stamina + costObj.timeMilliseconds;
      if (score < bestScore) {
        bestScore = score;
        best = to;
      }
    }
 
    return best;
  }
 
  /**
   * Get transition cost between stances
   */
  public getTransitionCost(
    from: TrigramStance,
    to: TrigramStance,
    player?: PlayerState
  ): TrigramTransitionCost {
    if (from === to) {
      return {
        ki: 0,
        stamina: 0,
        timeMilliseconds: 0, // neutral
      };
    }
 
    const difficulty = TrigramCalculator.calculateTransitionDifficulty(
      from,
      to
    );
    const baseCost = 10;
    const baseTime = 500;
 
    let ki = Math.ceil(baseCost * difficulty);
    let stamina = Math.ceil(baseCost * difficulty * 1.5);
 
    // apply archetype stance‐change cost modifier if player provided
    if (player) {
      const archData = PLAYER_ARCHETYPES_DATA[player.archetype];
      const favs = archData.favoredStances || [];
      const mod = favs.includes(to) ? 0.8 : 1.0;
      ki = Math.ceil(ki * mod);
      stamina = Math.ceil(stamina * mod);
    }
 
    return {
      ki,
      stamina,
      timeMilliseconds: Math.ceil(baseTime * difficulty),
    };
  }
 
  /**
   * Calculate stance effectiveness
   */
  calculateStanceEffectiveness(
    attackerStance: TrigramStance,
    defenderStance: TrigramStance
  ): number {
    return this.calculator.calculateStanceEffectiveness(
      attackerStance,
      defenderStance
    );
  }
 
  /**
   * Get stance name in Korean and English
   */
  getStanceName(stance: TrigramStance): { korean: string; english: string } {
    const stanceNames = {
      [TrigramStance.GEON]: { korean: "건", english: "Heaven" },
      [TrigramStance.TAE]: { korean: "태", english: "Lake" },
      [TrigramStance.LI]: { korean: "리", english: "Fire" },
      [TrigramStance.JIN]: { korean: "진", english: "Thunder" },
      [TrigramStance.SON]: { korean: "손", english: "Wind" },
      [TrigramStance.GAM]: { korean: "감", english: "Water" },
      [TrigramStance.GAN]: { korean: "간", english: "Mountain" },
      [TrigramStance.GON]: { korean: "곤", english: "Earth" },
    };
 
    return stanceNames[stance] || { korean: "Unknown", english: "Unknown" };
  }
 
  /**
   * Get current stance data
   */
  getCurrentStanceData(stance: TrigramStance): any {
    const stanceName = this.getStanceName(stance);
    return {
      id: stance,
      name: stanceName,
      korean: stanceName.korean,
      english: stanceName.english,
    };
  }
 
  /**
   * Validate transition with detailed feedback
   */
  validateTransition(
    fromStance: TrigramStance,
    toStance: TrigramStance,
    player: PlayerState
  ): { valid: boolean; reason?: string } {
    if (fromStance === toStance) {
      return { valid: true };
    }
 
    const cost = this.getTransitionCost(fromStance, toStance, player);
 
    if (player.ki < cost.ki) {
      return {
        valid: false,
        reason: `Insufficient Ki: need ${cost.ki}, have ${player.ki}`,
      };
    }
 
    if (player.stamina < cost.stamina) {
      return {
        valid: false,
        reason: `Insufficient Stamina: need ${cost.stamina}, have ${player.stamina}`,
      };
    }
 
    return { valid: true };
  }
}
 
export default TrigramSystem;