All files / web/src/utils skillConfiguration.ts

97.91% Statements 235/240
90% Branches 45/50
100% Functions 4/4
97.91% Lines 235/240

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 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 2411x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 39x 15x 15x 39x 1x 1x 1x 39x 1x 1x 1x 13x 13x 13x 13x 52x 7x 7x 52x 4x 4x 4x 52x 1x 1x 1x 13x 13x 13x 13x 117x 1x 1x 117x       117x 3x 3x 3x 13x 13x 13x 13x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 36x 36x 2x 36x 1x 34x 6x 33x 27x 27x 6x 6x 6x 24x 24x   24x 2x 24x 2x 22x 20x 20x 6x 6x 6x 54x 54x 2x 54x   52x 1x 52x 51x 51x 6x 6x 6x 6x  
import type { SkillSet } from '../types/tutorial'
 
export type SkillMode = 'off' | 'allowed' | 'target' | 'forbidden'
 
export interface SkillConfiguration {
  basic: {
    directAddition: SkillMode
    heavenBead: SkillMode
    simpleCombinations: SkillMode
  }
  fiveComplements: {
    '4=5-1': SkillMode
    '3=5-2': SkillMode
    '2=5-3': SkillMode
    '1=5-4': SkillMode
  }
  tenComplements: {
    '9=10-1': SkillMode
    '8=10-2': SkillMode
    '7=10-3': SkillMode
    '6=10-4': SkillMode
    '5=10-5': SkillMode
    '4=10-6': SkillMode
    '3=10-7': SkillMode
    '2=10-8': SkillMode
    '1=10-9': SkillMode
  }
}
 
// Helper functions for new skill configuration
export function createDefaultSkillConfiguration(): SkillConfiguration {
  return {
    basic: {
      directAddition: 'allowed',
      heavenBead: 'off',
      simpleCombinations: 'off',
    },
    fiveComplements: {
      '4=5-1': 'off',
      '3=5-2': 'off',
      '2=5-3': 'off',
      '1=5-4': 'off',
    },
    tenComplements: {
      '9=10-1': 'off',
      '8=10-2': 'off',
      '7=10-3': 'off',
      '6=10-4': 'off',
      '5=10-5': 'off',
      '4=10-6': 'off',
      '3=10-7': 'off',
      '2=10-8': 'off',
      '1=10-9': 'off',
    },
  }
}
 
export function createBasicAllowedConfiguration(): SkillConfiguration {
  return {
    basic: {
      directAddition: 'allowed',
      heavenBead: 'allowed',
      simpleCombinations: 'off',
    },
    fiveComplements: {
      '4=5-1': 'off',
      '3=5-2': 'off',
      '2=5-3': 'off',
      '1=5-4': 'off',
    },
    tenComplements: {
      '9=10-1': 'off',
      '8=10-2': 'off',
      '7=10-3': 'off',
      '6=10-4': 'off',
      '5=10-5': 'off',
      '4=10-6': 'off',
      '3=10-7': 'off',
      '2=10-8': 'off',
      '1=10-9': 'off',
    },
  }
}
 
// Convert between old and new formats
export function skillConfigurationToSkillSets(config: SkillConfiguration): {
  required: SkillSet
  target: Partial<SkillSet>
  forbidden: Partial<SkillSet>
} {
  const required: SkillSet = {
    basic: {
      directAddition: false,
      heavenBead: false,
      simpleCombinations: false,
      directSubtraction: false,
      heavenBeadSubtraction: false,
      simpleCombinationsSub: false,
    },
    fiveComplements: {
      '4=5-1': false,
      '3=5-2': false,
      '2=5-3': false,
      '1=5-4': false,
    },
    tenComplements: {
      '9=10-1': false,
      '8=10-2': false,
      '7=10-3': false,
      '6=10-4': false,
      '5=10-5': false,
      '4=10-6': false,
      '3=10-7': false,
      '2=10-8': false,
      '1=10-9': false,
    },
    fiveComplementsSub: {
      '-4=-5+1': false,
      '-3=-5+2': false,
      '-2=-5+3': false,
      '-1=-5+4': false,
    },
    tenComplementsSub: {
      '-9=+1-10': false,
      '-8=+2-10': false,
      '-7=+3-10': false,
      '-6=+4-10': false,
      '-5=+5-10': false,
      '-4=+6-10': false,
      '-3=+7-10': false,
      '-2=+8-10': false,
      '-1=+9-10': false,
    },
    advanced: {
      cascadingCarry: false,
      cascadingBorrow: false,
    },
  }
 
  const target: Partial<SkillSet> = {}
  const forbidden: Partial<SkillSet> = {}
 
  // Basic skills
  Object.entries(config.basic).forEach(([skill, mode]) => {
    if (mode === 'allowed' || mode === 'target') {
      required.basic[skill as keyof typeof required.basic] = true
    }
    if (mode === 'target') {
      if (!target.basic) target.basic = {} as any
      target.basic![skill as keyof typeof required.basic] = true
    }
    if (mode === 'forbidden') {
      if (!forbidden.basic) forbidden.basic = {} as any
      forbidden.basic![skill as keyof typeof required.basic] = true
    }
  })
 
  // Five complements
  Object.entries(config.fiveComplements).forEach(([skill, mode]) => {
    if (mode === 'allowed' || mode === 'target') {
      required.fiveComplements[skill as keyof typeof required.fiveComplements] = true
    }
    if (mode === 'target') {
      if (!target.fiveComplements) target.fiveComplements = {} as any
      target.fiveComplements![skill as keyof typeof required.fiveComplements] = true
    }
    if (mode === 'forbidden') {
      if (!forbidden.fiveComplements) forbidden.fiveComplements = {} as any
      forbidden.fiveComplements![skill as keyof typeof required.fiveComplements] = true
    }
  })
 
  // Ten complements
  Object.entries(config.tenComplements).forEach(([skill, mode]) => {
    if (mode === 'allowed' || mode === 'target') {
      required.tenComplements[skill as keyof typeof required.tenComplements] = true
    }
    if (mode === 'target') {
      if (!target.tenComplements) target.tenComplements = {} as any
      target.tenComplements![skill as keyof typeof required.tenComplements] = true
    }
    if (mode === 'forbidden') {
      if (!forbidden.tenComplements) forbidden.tenComplements = {} as any
      forbidden.tenComplements![skill as keyof typeof required.tenComplements] = true
    }
  })
 
  return { required, target, forbidden }
}
 
// Convert from old format to new format
export function skillSetsToConfiguration(
  required: SkillSet,
  target?: Partial<SkillSet>,
  forbidden?: Partial<SkillSet>
): SkillConfiguration {
  const config = createDefaultSkillConfiguration()
 
  // Process each skill category
  Object.entries(required.basic).forEach(([skill, isRequired]) => {
    const skillKey = skill as keyof typeof config.basic
    if (forbidden?.basic?.[skillKey]) {
      config.basic[skillKey] = 'forbidden'
    } else if (target?.basic?.[skillKey]) {
      config.basic[skillKey] = 'target'
    } else if (isRequired) {
      config.basic[skillKey] = 'allowed'
    } else {
      config.basic[skillKey] = 'off'
    }
  })
 
  Object.entries(required.fiveComplements).forEach(([skill, isRequired]) => {
    const skillKey = skill as keyof typeof config.fiveComplements
    if (forbidden?.fiveComplements?.[skillKey]) {
      config.fiveComplements[skillKey] = 'forbidden'
    } else if (target?.fiveComplements?.[skillKey]) {
      config.fiveComplements[skillKey] = 'target'
    } else if (isRequired) {
      config.fiveComplements[skillKey] = 'allowed'
    } else {
      config.fiveComplements[skillKey] = 'off'
    }
  })
 
  Object.entries(required.tenComplements).forEach(([skill, isRequired]) => {
    const skillKey = skill as keyof typeof config.tenComplements
    if (forbidden?.tenComplements?.[skillKey]) {
      config.tenComplements[skillKey] = 'forbidden'
    } else if (target?.tenComplements?.[skillKey]) {
      config.tenComplements[skillKey] = 'target'
    } else if (isRequired) {
      config.tenComplements[skillKey] = 'allowed'
    } else {
      config.tenComplements[skillKey] = 'off'
    }
  })
 
  return config
}