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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 | 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 1x | // Pedagogically-grounded difficulty profiles
// Maps regrouping frequency + scaffolding to teaching progression
//
// **ARCHITECTURE NOTE: Discrete Ordered Progressions**
// This system uses explicit ordered arrays for both dimensions to eliminate
// quantization and cycling issues. Each dimension has a discrete progression
// from easiest to hardest, and make harder/easier simply navigate these arrays.
import type { DisplayRules } from './displayRules'
/**
* SCAFFOLDING_PROGRESSION: Ordered array of scaffolding configurations
* Index 0 = maximum scaffolding (easiest)
* Index N = no scaffolding (hardest)
*
* Each step removes or makes conditional one type of scaffolding aid
*/
export const SCAFFOLDING_PROGRESSION: DisplayRules[] = [
// Level 0: Maximum scaffolding - everything always visible
{
carryBoxes: 'always',
answerBoxes: 'always',
placeValueColors: 'always',
tenFrames: 'always',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'always',
borrowingHints: 'always',
},
// Level 1: Carry boxes become conditional
{
carryBoxes: 'whenRegrouping',
answerBoxes: 'always',
placeValueColors: 'always',
tenFrames: 'always',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping',
borrowingHints: 'always',
},
// Level 2: Place value colors become conditional
{
carryBoxes: 'whenRegrouping',
answerBoxes: 'always',
placeValueColors: 'whenRegrouping',
tenFrames: 'always',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping',
borrowingHints: 'whenRegrouping',
},
// Level 3: Answer boxes become conditional
{
carryBoxes: 'whenRegrouping',
answerBoxes: 'whenRegrouping',
placeValueColors: 'whenRegrouping',
tenFrames: 'always',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping',
borrowingHints: 'whenRegrouping',
},
// Level 4: Multiple helpers become more conditional
{
carryBoxes: 'whenRegrouping',
answerBoxes: 'whenMultipleRegroups',
placeValueColors: 'whenRegrouping',
tenFrames: 'always',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping',
borrowingHints: 'whenMultipleRegroups',
},
// Level 5: More helpers get more conditional
{
carryBoxes: 'whenMultipleRegroups',
answerBoxes: 'whenMultipleRegroups',
placeValueColors: 'whenMultipleRegroups',
tenFrames: 'always',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping',
borrowingHints: 'whenMultipleRegroups',
},
// Level 6: Ten frames become conditional (only when regrouping)
{
carryBoxes: 'whenMultipleRegroups',
answerBoxes: 'whenMultipleRegroups',
placeValueColors: 'whenMultipleRegroups',
tenFrames: 'whenRegrouping',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping',
borrowingHints: 'whenMultipleRegroups',
},
// Level 7: Ten frames become more conditional
{
carryBoxes: 'whenMultipleRegroups',
answerBoxes: 'whenMultipleRegroups',
placeValueColors: 'whenMultipleRegroups',
tenFrames: 'whenMultipleRegroups',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping',
borrowingHints: 'never',
},
// Level 8: Carry boxes removed
{
carryBoxes: 'never',
answerBoxes: 'whenMultipleRegroups',
placeValueColors: 'whenMultipleRegroups',
tenFrames: 'whenMultipleRegroups',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping',
borrowingHints: 'never',
},
// Level 9: Answer boxes removed
{
carryBoxes: 'never',
answerBoxes: 'never',
placeValueColors: 'whenMultipleRegroups',
tenFrames: 'whenMultipleRegroups',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping',
borrowingHints: 'never',
},
// Level 10: Ten frames removed
{
carryBoxes: 'never',
answerBoxes: 'never',
placeValueColors: 'whenMultipleRegroups',
tenFrames: 'never',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping',
borrowingHints: 'never',
},
// Level 11: Place value colors only for large numbers
{
carryBoxes: 'never',
answerBoxes: 'never',
placeValueColors: 'when3PlusDigits',
tenFrames: 'never',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping',
borrowingHints: 'never',
},
// Level 12: Minimal scaffolding - place value colors removed
{
carryBoxes: 'never',
answerBoxes: 'never',
placeValueColors: 'never',
tenFrames: 'never',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'never',
borrowingHints: 'never',
},
]
/**
* REGROUPING_PROGRESSION: Ordered array of regrouping configurations
* Index 0 = no regrouping (easiest)
* Index N = maximum regrouping (hardest)
*/
export const REGROUPING_PROGRESSION: Array<{
pAnyStart: number
pAllStart: number
}> = [
{ pAnyStart: 0.0, pAllStart: 0.0 }, // 0: No regrouping
{ pAnyStart: 0.15, pAllStart: 0.0 }, // 1: Minimal
{ pAnyStart: 0.25, pAllStart: 0.0 }, // 2: Light (Beginner/Early Learner)
{ pAnyStart: 0.35, pAllStart: 0.0 }, // 3:
{ pAnyStart: 0.45, pAllStart: 0.0 }, // 4:
{ pAnyStart: 0.54, pAllStart: 0.0 }, // 5:
{ pAnyStart: 0.6, pAllStart: 0.1 }, // 6:
{ pAnyStart: 0.68, pAllStart: 0.15 }, // 7:
{ pAnyStart: 0.75, pAllStart: 0.2 }, // 8:
{ pAnyStart: 0.75, pAllStart: 0.25 }, // 9: Intermediate
{ pAnyStart: 0.8, pAllStart: 0.3 }, // 10:
{ pAnyStart: 0.85, pAllStart: 0.38 }, // 11:
{ pAnyStart: 0.9, pAllStart: 0.45 }, // 12:
{ pAnyStart: 0.9, pAllStart: 0.5 }, // 13: Advanced/Expert
{ pAnyStart: 0.93, pAllStart: 0.58 }, // 14:
{ pAnyStart: 0.96, pAllStart: 0.67 }, // 15:
{ pAnyStart: 0.98, pAllStart: 0.78 }, // 16:
{ pAnyStart: 1.0, pAllStart: 0.9 }, // 17:
{ pAnyStart: 1.0, pAllStart: 1.0 }, // 18: Maximum regrouping
]
/**
* Find the closest scaffolding index for given display rules
*/
export function findScaffoldingIndex(rules: DisplayRules): number {
// Try exact match first
for (let i = 0; i < SCAFFOLDING_PROGRESSION.length; i++) {
if (JSON.stringify(SCAFFOLDING_PROGRESSION[i]) === JSON.stringify(rules)) {
return i
}
}
// No exact match - find closest by counting matching rules
let bestIndex = 0
let bestMatchCount = 0
for (let i = 0; i < SCAFFOLDING_PROGRESSION.length; i++) {
const level = SCAFFOLDING_PROGRESSION[i]
let matchCount = 0
if (level.carryBoxes === rules.carryBoxes) matchCount++
if (level.answerBoxes === rules.answerBoxes) matchCount++
if (level.placeValueColors === rules.placeValueColors) matchCount++
if (level.tenFrames === rules.tenFrames) matchCount++
if (matchCount > bestMatchCount) {
bestMatchCount = matchCount
bestIndex = i
}
}
return bestIndex
}
/**
* Find the closest regrouping index for given probabilities
*/
export function findRegroupingIndex(pAnyStart: number, pAllStart: number): number {
let bestIndex = 0
let bestDistance = Infinity
for (let i = 0; i < REGROUPING_PROGRESSION.length; i++) {
const level = REGROUPING_PROGRESSION[i]
const distance = Math.sqrt(
(level.pAnyStart - pAnyStart) ** 2 + (level.pAllStart - pAllStart) ** 2
)
if (distance < bestDistance) {
bestDistance = distance
bestIndex = i
}
}
return bestIndex
}
/**
* Describe what changed between two scaffolding levels
* @param fromRules - Previous display rules
* @param toRules - New display rules
* @param direction - Whether scaffolding was 'added' or 'reduced'
* @param operator - Current worksheet operator (filters out irrelevant scaffolds)
*/
function describeScaffoldingChange(
fromRules: DisplayRules,
toRules: DisplayRules,
direction: 'added' | 'reduced',
operator?: 'addition' | 'subtraction' | 'mixed'
): string {
const changes: string[] = []
const ruleNames: Record<keyof DisplayRules, string> = {
carryBoxes: 'carry boxes',
answerBoxes: 'answer boxes',
placeValueColors: 'place value colors',
tenFrames: 'ten frames',
problemNumbers: 'problem numbers',
cellBorders: 'cell borders',
borrowNotation: 'borrow notation',
borrowingHints: 'borrowing hints',
}
// Operator-specific scaffolds to filter
const additionOnlyScaffolds: Array<keyof DisplayRules> = ['carryBoxes', 'tenFrames']
const subtractionOnlyScaffolds: Array<keyof DisplayRules> = ['borrowNotation', 'borrowingHints']
for (const key of Object.keys(ruleNames) as Array<keyof DisplayRules>) {
if (fromRules[key] !== toRules[key]) {
// Filter out operator-specific scaffolds when not relevant
if (operator === 'addition' && subtractionOnlyScaffolds.includes(key)) {
continue // Skip subtraction scaffolds for addition-only worksheets
}
if (operator === 'subtraction' && additionOnlyScaffolds.includes(key)) {
continue // Skip addition scaffolds for subtraction-only worksheets
}
// For 'mixed' or undefined operator, show all scaffolds
changes.push(ruleNames[key])
}
}
if (changes.length === 0) return 'Adjust difficulty'
if (changes.length === 1) {
return direction === 'added' ? `Add ${changes[0]}` : `Reduce ${changes[0]}`
}
return direction === 'added' ? `Add ${changes.join(', ')}` : `Reduce ${changes.join(', ')}`
}
// =============================================================================
// PEDAGOGICAL CONSTRAINTS: Valid 2D Difficulty Space
// =============================================================================
/**
* Get valid scaffolding range for a given regrouping index
*
* Pedagogical principle: As regrouping increases (problems get harder),
* scaffolding should decrease (student gains independence).
*
* This creates a diagonal band of valid states, preventing nonsensical
* combinations like:
* - High regrouping + High scaffolding (overscaffolding)
* - Low regrouping + Low scaffolding (pointless - nothing to scaffold)
*/
function getValidScaffoldingRange(regroupingIdx: number): {
min: number
max: number
} {
// No/minimal regrouping (0-2): Need scaffolding to learn structure
if (regroupingIdx <= 2) {
return { min: 0, max: 4 }
}
// Light regrouping (3-5): Still need significant scaffolding
if (regroupingIdx <= 5) {
return { min: 0, max: 6 }
}
// Light-medium regrouping (6-9): Transitional phase
if (regroupingIdx <= 9) {
return { min: 1, max: 8 }
}
// Medium-high regrouping (10-12): Reducing scaffolding, wider range
if (regroupingIdx <= 12) {
return { min: 3, max: 11 }
}
// High regrouping (13-15): Can work independently, allow full range
if (regroupingIdx <= 15) {
return { min: 4, max: 12 }
}
// Very high regrouping (16-18): Should prefer independence
return { min: 6, max: 12 }
}
/**
* Check if a regrouping/scaffolding combination is pedagogically valid
*/
function isValidCombination(regroupingIdx: number, scaffoldingIdx: number): boolean {
const range = getValidScaffoldingRange(regroupingIdx)
return scaffoldingIdx >= range.min && scaffoldingIdx <= range.max
}
/**
* Clamp scaffolding index to valid range for given regrouping index
*/
function clampScaffoldingToValidRange(regroupingIdx: number, scaffoldingIdx: number): number {
const range = getValidScaffoldingRange(regroupingIdx)
return Math.max(range.min, Math.min(range.max, scaffoldingIdx))
}
/**
* Find nearest valid state to a given regrouping/scaffolding combination
* If the combination is invalid, adjusts scaffolding to fit the valid range
*/
export function findNearestValidState(
regroupingIdx: number,
scaffoldingIdx: number
): { regroupingIdx: number; scaffoldingIdx: number } {
// Clamp indices to valid progression ranges
const clampedRegrouping = Math.max(0, Math.min(REGROUPING_PROGRESSION.length - 1, regroupingIdx))
const clampedScaffolding = clampScaffoldingToValidRange(clampedRegrouping, scaffoldingIdx)
return {
regroupingIdx: clampedRegrouping,
scaffoldingIdx: clampedScaffolding,
}
}
export interface DifficultyProfile {
name: string
label: string
description: string
regrouping: {
pAllStart: number
pAnyStart: number
}
displayRules: DisplayRules
}
/**
* Pre-defined difficulty profiles that map to pedagogical progression
* Each profile balances problem complexity (regrouping) with scaffolding support
*/
export const DIFFICULTY_PROFILES: Record<string, DifficultyProfile> = {
beginner: {
name: 'beginner',
label: 'Beginner',
description:
'Full scaffolding with no regrouping. Focus on learning the structure of addition.',
regrouping: { pAllStart: 0, pAnyStart: 0 },
displayRules: {
carryBoxes: 'always', // Show structure even when not needed
answerBoxes: 'always', // Guide digit placement
placeValueColors: 'always', // Reinforce place value concept
tenFrames: 'always', // Visual aid for understanding place value
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'always', // Subtraction: show scratch work
borrowingHints: 'always', // Subtraction: show visual hints
},
},
earlyLearner: {
name: 'earlyLearner',
label: 'Early Learner',
description: 'Scaffolds appear when needed. Introduces occasional regrouping.',
regrouping: { pAllStart: 0, pAnyStart: 0.25 },
displayRules: {
carryBoxes: 'whenRegrouping', // Show scaffold only when needed
answerBoxes: 'always', // Still guide placement
placeValueColors: 'always', // Reinforce concepts
tenFrames: 'whenRegrouping', // Visual aid for new concept
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping', // Subtraction: show when borrowing
borrowingHints: 'always', // Subtraction: keep hints visible
},
},
practice: {
name: 'practice',
label: 'Practice',
description:
'High scaffolding with frequent regrouping. Master regrouping WITH support before training wheels come off.',
regrouping: { pAllStart: 0.25, pAnyStart: 0.75 },
displayRules: {
carryBoxes: 'whenRegrouping', // Show when regrouping happens
answerBoxes: 'always', // Keep guiding placement during intensive practice
placeValueColors: 'always', // Keep visual support during intensive practice
tenFrames: 'whenRegrouping', // Visual aid for regrouping
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping', // Subtraction: show when borrowing
borrowingHints: 'whenRegrouping', // Subtraction: show hints during practice
},
},
intermediate: {
name: 'intermediate',
label: 'Intermediate',
description: 'Reduced scaffolding with regular regrouping practice.',
regrouping: { pAllStart: 0.25, pAnyStart: 0.75 },
displayRules: {
carryBoxes: 'whenRegrouping', // Still helpful for regrouping
answerBoxes: 'whenMultipleRegroups', // Only for complex problems
placeValueColors: 'whenRegrouping', // Only when it matters
tenFrames: 'whenRegrouping', // Concrete aid when needed
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping', // Subtraction: show when borrowing
borrowingHints: 'whenMultipleRegroups', // Subtraction: only for complex problems
},
},
advanced: {
name: 'advanced',
label: 'Advanced',
description: 'Minimal scaffolding with frequent complex regrouping.',
regrouping: { pAllStart: 0.5, pAnyStart: 0.9 },
displayRules: {
carryBoxes: 'never', // Should internalize concept
answerBoxes: 'never', // Should know alignment
placeValueColors: 'when3PlusDigits', // Only for larger numbers
tenFrames: 'never', // Beyond concrete representations
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping', // Subtraction: still helpful
borrowingHints: 'never', // Subtraction: no hints
},
},
expert: {
name: 'expert',
label: 'Expert',
description: 'No scaffolding. Frequent complex regrouping for mastery.',
regrouping: { pAllStart: 0.5, pAnyStart: 0.9 },
displayRules: {
carryBoxes: 'never',
answerBoxes: 'never',
placeValueColors: 'never',
tenFrames: 'never',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'never', // Subtraction: no scaffolding
borrowingHints: 'never', // Subtraction: no hints
},
},
}
/**
* Ordered progression of difficulty levels
* PEDAGOGICAL NOTE: "practice" phase is critical - students master regrouping
* WITH scaffolding before we remove support (intermediate/advanced/expert)
*/
export const DIFFICULTY_PROGRESSION = [
'beginner',
'earlyLearner',
'practice', // NEW: High regrouping + high scaffolding
'intermediate',
'advanced',
'expert',
] as const
export type DifficultyLevel = (typeof DIFFICULTY_PROGRESSION)[number]
// =============================================================================
// 2D DIFFICULTY SYSTEM: Regrouping Intensity × Scaffolding Level
// =============================================================================
/**
* Calculate regrouping intensity on 0-10 scale
* Maps probability settings to single dimension
*/
export function calculateRegroupingIntensity(pAnyStart: number, pAllStart: number): number {
// pAnyStart (occasional regrouping) contributes 70% of score
// pAllStart (compound regrouping) contributes 30% of score
// This reflects pedagogical importance: frequency matters more than compound complexity
return pAnyStart * 7 + pAllStart * 3
}
/**
* Reverse mapping: Convert regrouping intensity back to probabilities
* Uses pedagogical progression: introduce frequency first, then compound
*/
function intensityToRegrouping(intensity: number): {
pAnyStart: number
pAllStart: number
} {
// Below 5: Focus on pAnyStart, keep pAllStart minimal
if (intensity <= 5) {
return {
pAnyStart: Math.min(intensity / 7, 1),
pAllStart: 0,
}
}
// Above 5: pAnyStart near max, start increasing pAllStart
const excessIntensity = intensity - 5
return {
pAnyStart: Math.min(5 / 7 + excessIntensity / 14, 1),
pAllStart: Math.min(excessIntensity / 10, 1),
}
}
/**
* Calculate scaffolding level on 0-10 scale
* Higher number = LESS scaffolding = HARDER
*
* Each rule contributes based on:
* - 'always' = 0 pts (max scaffolding)
* - 'whenRegrouping' = 2 pts (conditional)
* - 'whenMultipleRegroups' = 5 pts (sparse)
* - 'when3PlusDigits' = 7 pts (rare)
* - 'never' = 10 pts (no scaffolding)
*
* Special handling for tenFrames:
* - When regrouping intensity is very low (<4), tenFrames is not pedagogically
* relevant (you don't need ten-frames if there's no/minimal regrouping).
* In this case, tenFrames is excluded from the calculation to prevent
* oscillation between 'whenRegrouping' and 'never'.
*/
export function calculateScaffoldingLevel(
rules: DisplayRules,
regroupingIntensity?: number
): number {
const ruleScores: Record<string, number> = {
auto: 8, // Default to 'whenRegrouping' level for auto mode
always: 10, // Maximum scaffolding (easiest)
whenRegrouping: 8,
whenMultipleRegroups: 5,
when3PlusDigits: 3,
never: 0, // No scaffolding (hardest)
}
const weights = {
carryBoxes: 1.5, // Most pedagogically important
answerBoxes: 1.5, // Very important for alignment
placeValueColors: 1.0, // Helpful but less critical
tenFrames: 1.0, // Concrete visual aid (contextual - see above)
}
// Determine if tenFrames should be included in calculation
// When regrouping is minimal, tenFrames isn't pedagogically relevant
const includeTenFrames = regroupingIntensity === undefined || regroupingIntensity >= 4
const weightedScores = [
ruleScores[rules.carryBoxes] * weights.carryBoxes,
ruleScores[rules.answerBoxes] * weights.answerBoxes,
ruleScores[rules.placeValueColors] * weights.placeValueColors,
...(includeTenFrames ? [ruleScores[rules.tenFrames] * weights.tenFrames] : []),
]
// Recalculate total weight excluding tenFrames if not included
const totalWeight = includeTenFrames
? Object.values(weights).reduce((a, b) => a + b, 0)
: Object.values(weights).reduce((a, b) => a + b, 0) - weights.tenFrames
const weightedAverage = weightedScores.reduce((a, b) => a + b, 0) / totalWeight
return Math.min(10, Math.max(0, weightedAverage))
}
/**
* Calculate overall difficulty on 0-10 scale for single-bar UI
* Combines regrouping intensity and scaffolding level
*/
export function calculateOverallDifficulty(
pAnyStart: number,
pAllStart: number,
displayRules: DisplayRules
): number {
const regrouping = calculateRegroupingIntensity(pAnyStart, pAllStart)
const scaffolding = calculateScaffoldingLevel(displayRules, regrouping)
console.log('[calculateOverallDifficulty]', {
pAnyStart,
pAllStart,
regrouping,
scaffolding,
displayRules,
})
// Pedagogical path: start at (0, 10) [no regrouping, all scaffolding]
// end at (10, 0) [all regrouping, no scaffolding]
// Use Euclidean distance to measure progress along this diagonal path
const startPoint = { regrouping: 0, scaffolding: 10 }
const endPoint = { regrouping: 10, scaffolding: 0 }
// Distance from start to current position
const distFromStart = Math.sqrt(
(regrouping - startPoint.regrouping) ** 2 + (scaffolding - startPoint.scaffolding) ** 2
)
// Distance from end to current position
const distFromEnd = Math.sqrt(
(regrouping - endPoint.regrouping) ** 2 + (scaffolding - endPoint.scaffolding) ** 2
)
// Progress ratio: 0 at start, 1 at end
const progress = distFromStart / (distFromStart + distFromEnd)
console.log('[calculateOverallDifficulty] distances:', {
distFromStart,
distFromEnd,
progress,
result: progress * 10,
})
// Scale to 0-10 range and ensure no NaN
const result = progress * 10
return Number.isNaN(result) ? 0 : result
}
/**
* Reverse mapping: Convert scaffolding level to display rules
* Uses pedagogical progression: reduce critical scaffolds last
*/
function levelToScaffoldingRules(level: number): DisplayRules {
// Level 0-2: Full scaffolding
if (level <= 2) {
return {
carryBoxes: 'always',
answerBoxes: 'always',
placeValueColors: 'always',
tenFrames: level < 1 ? 'never' : 'whenRegrouping', // Ten-frames only when regrouping introduced
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'always',
borrowingHints: 'always',
}
}
// Level 2-4: Transition to conditional
if (level <= 4) {
return {
carryBoxes: 'whenRegrouping',
answerBoxes: 'always',
placeValueColors: 'always',
tenFrames: 'whenRegrouping',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping',
borrowingHints: 'always',
}
}
// Level 4-6: Reduce non-critical scaffolds
if (level <= 6) {
return {
carryBoxes: 'whenRegrouping',
answerBoxes: level < 5.5 ? 'always' : 'whenMultipleRegroups',
placeValueColors: 'whenRegrouping',
tenFrames: 'whenRegrouping',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping',
borrowingHints: level < 5.5 ? 'whenRegrouping' : 'whenMultipleRegroups',
}
}
// Level 6-8: Remove critical scaffolds
if (level <= 8) {
return {
carryBoxes: level < 7 ? 'whenRegrouping' : 'never',
answerBoxes: 'never',
placeValueColors: level < 7.5 ? 'whenRegrouping' : 'when3PlusDigits',
tenFrames: 'never',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping',
borrowingHints: 'never',
}
}
// Level 8-10: Minimal to no scaffolding
return {
carryBoxes: 'never',
answerBoxes: 'never',
placeValueColors: level < 9 ? 'when3PlusDigits' : 'never',
tenFrames: 'never',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: level < 9 ? 'whenRegrouping' : 'never',
borrowingHints: 'never',
}
}
/**
* Calculate Euclidean distance between two points in difficulty space
*/
function difficultyDistance(reg1: number, scaf1: number, reg2: number, scaf2: number): number {
return Math.sqrt((reg1 - reg2) ** 2 + (scaf1 - scaf2) ** 2)
}
/**
* Find nearest preset profile to current state
* Returns profile and distance
*/
export function findNearestPreset(
currentRegrouping: number,
currentScaffolding: number,
direction: 'harder' | 'easier' | 'any'
): { profile: DifficultyProfile; distance: number } | null {
const candidates = DIFFICULTY_PROGRESSION.map((name) => {
const profile = DIFFICULTY_PROFILES[name]
const regrouping = calculateRegroupingIntensity(
profile.regrouping.pAnyStart,
profile.regrouping.pAllStart
)
const scaffolding = calculateScaffoldingLevel(profile.displayRules, regrouping)
const distance = difficultyDistance(
currentRegrouping,
currentScaffolding,
regrouping,
scaffolding
)
// Calculate if this preset is harder or easier
// Harder = not easier in ANY dimension AND harder in AT LEAST ONE dimension
// This prevents selecting the current preset as "next" when already at a preset
const isHarder =
regrouping >= currentRegrouping &&
scaffolding >= currentScaffolding &&
(regrouping > currentRegrouping || scaffolding > currentScaffolding)
// Easier = not harder in ANY dimension AND easier in AT LEAST ONE dimension
const isEasier =
regrouping <= currentRegrouping &&
scaffolding <= currentScaffolding &&
(regrouping < currentRegrouping || scaffolding < currentScaffolding)
return { profile, distance, regrouping, scaffolding, isHarder, isEasier }
})
// Filter by direction
const filtered = candidates.filter((c) => {
if (direction === 'any') return true
if (direction === 'harder') return c.isHarder
if (direction === 'easier') return c.isEasier
return false
})
if (filtered.length === 0) return null
// Find closest
const nearest = filtered.reduce((a, b) => (a.distance < b.distance ? a : b))
return { profile: nearest.profile, distance: nearest.distance }
}
export type DifficultyMode = 'both' | 'challenge' | 'support'
/**
* Make worksheet harder using discrete progression indices with pedagogical constraints
*
* Algorithm:
* 1. Find current indices in discrete progressions
* 2. Calculate position in 2D difficulty space
* 3. Find nearest harder preset profile
* 4. Try to increment dimension with larger gap toward preset (or specific dimension if mode specified)
* 5. Ensure resulting state respects pedagogical constraints
*
* @param mode - 'both' (default smart diagonal), 'challenge' (regrouping only), 'support' (scaffolding only)
* @param operator - Current worksheet operator (filters scaffolding descriptions)
*/
export function makeHarder(
currentState: {
pAnyStart: number
pAllStart: number
displayRules: DisplayRules
},
mode: DifficultyMode = 'both',
operator?: 'addition' | 'subtraction' | 'mixed'
): {
pAnyStart: number
pAllStart: number
displayRules: DisplayRules
difficultyProfile?: string
changeDescription: string
} {
// Find current indices in discrete progressions
const currentRegroupingIdx = findRegroupingIndex(currentState.pAnyStart, currentState.pAllStart)
const currentScaffoldingIdx = findScaffoldingIndex(currentState.displayRules)
// Ensure current state is valid (adjust if needed)
const validCurrent = findNearestValidState(currentRegroupingIdx, currentScaffoldingIdx)
let newRegroupingIdx = validCurrent.regroupingIdx
let newScaffoldingIdx = validCurrent.scaffoldingIdx
// Check if at maximum
if (
newRegroupingIdx >= REGROUPING_PROGRESSION.length - 1 &&
newScaffoldingIdx >= SCAFFOLDING_PROGRESSION.length - 1
) {
return {
...currentState,
changeDescription: 'Already at maximum difficulty',
}
}
// Calculate current position in 2D difficulty space
const currentRegrouping = calculateRegroupingIntensity(
currentState.pAnyStart,
currentState.pAllStart
)
const currentScaffolding = calculateScaffoldingLevel(currentState.displayRules, currentRegrouping)
// Try to move based on mode
let moved = false
if (mode === 'challenge') {
// Only increase regrouping (more complex problems)
if (newRegroupingIdx < REGROUPING_PROGRESSION.length - 1) {
const testRegroupingIdx = newRegroupingIdx + 1
const adjustedScaffoldingIdx = clampScaffoldingToValidRange(
testRegroupingIdx,
newScaffoldingIdx
)
newRegroupingIdx = testRegroupingIdx
newScaffoldingIdx = adjustedScaffoldingIdx
moved = true
}
} else if (mode === 'support') {
// Only reduce scaffolding (remove help)
if (
newScaffoldingIdx < SCAFFOLDING_PROGRESSION.length - 1 &&
isValidCombination(newRegroupingIdx, newScaffoldingIdx + 1)
) {
newScaffoldingIdx++
moved = true
}
} else {
// mode === 'both': Smart diagonal navigation toward preset
// Find nearest harder preset to guide direction
const nearestPreset = findNearestPreset(currentRegrouping, currentScaffolding, 'harder')
if (nearestPreset) {
// Calculate target position from preset
const targetRegrouping = calculateRegroupingIntensity(
nearestPreset.profile.regrouping.pAnyStart,
nearestPreset.profile.regrouping.pAllStart
)
const targetScaffolding = calculateScaffoldingLevel(
nearestPreset.profile.displayRules,
targetRegrouping
)
// Calculate gaps in both dimensions
const regroupingGap = targetRegrouping - currentRegrouping
const scaffoldingGap = targetScaffolding - currentScaffolding
// Try dimension with larger gap first
if (
Math.abs(regroupingGap) > Math.abs(scaffoldingGap) &&
newRegroupingIdx < REGROUPING_PROGRESSION.length - 1
) {
// Try increasing regrouping
const testRegroupingIdx = newRegroupingIdx + 1
const adjustedScaffoldingIdx = clampScaffoldingToValidRange(
testRegroupingIdx,
newScaffoldingIdx
)
newRegroupingIdx = testRegroupingIdx
newScaffoldingIdx = adjustedScaffoldingIdx
moved = true
} else if (newScaffoldingIdx < SCAFFOLDING_PROGRESSION.length - 1) {
// Try increasing scaffolding (reducing help)
const testScaffoldingIdx = newScaffoldingIdx + 1
if (isValidCombination(newRegroupingIdx, testScaffoldingIdx)) {
newScaffoldingIdx = testScaffoldingIdx
moved = true
}
}
}
// Fallback: If we couldn't move toward preset, try any valid harder move
// Per spec: makeHarder should increase complexity (regrouping) first, then reduce support (scaffolding)
if (!moved) {
// Try increasing regrouping (complexity) first
if (newRegroupingIdx < REGROUPING_PROGRESSION.length - 1) {
newRegroupingIdx++
newScaffoldingIdx = clampScaffoldingToValidRange(newRegroupingIdx, newScaffoldingIdx)
moved = true
}
// Otherwise try increasing scaffolding (removing help)
else if (
newScaffoldingIdx < SCAFFOLDING_PROGRESSION.length - 1 &&
isValidCombination(newRegroupingIdx, newScaffoldingIdx + 1)
) {
newScaffoldingIdx++
moved = true
}
}
}
if (!moved) {
return {
...currentState,
changeDescription: 'Already at maximum difficulty',
}
}
// Get new values from progressions
const newRegrouping = REGROUPING_PROGRESSION[newRegroupingIdx]
const newRules = SCAFFOLDING_PROGRESSION[newScaffoldingIdx]
// Generate description
let description = ''
if (
newRegroupingIdx > validCurrent.regroupingIdx &&
newScaffoldingIdx > validCurrent.scaffoldingIdx
) {
const scaffoldingChange = describeScaffoldingChange(
currentState.displayRules,
newRules,
'reduced',
operator
)
description = `Increase regrouping to ${Math.round(newRegrouping.pAnyStart * 100)}% + ${scaffoldingChange.toLowerCase()}`
} else if (newRegroupingIdx > validCurrent.regroupingIdx) {
description = `Increase regrouping to ${Math.round(newRegrouping.pAnyStart * 100)}%`
if (newScaffoldingIdx !== validCurrent.scaffoldingIdx) {
const scaffoldingChange = describeScaffoldingChange(
currentState.displayRules,
newRules,
newScaffoldingIdx < validCurrent.scaffoldingIdx ? 'added' : 'reduced',
operator
)
description += ` (auto-adjust: ${scaffoldingChange.toLowerCase()})`
}
} else if (newScaffoldingIdx > validCurrent.scaffoldingIdx) {
description = describeScaffoldingChange(
currentState.displayRules,
newRules,
'reduced',
operator
)
}
// Check if result matches a preset
const matchedProfile = getProfileFromConfig(
newRegrouping.pAllStart,
newRegrouping.pAnyStart,
newRules
)
return {
pAnyStart: newRegrouping.pAnyStart,
pAllStart: newRegrouping.pAllStart,
displayRules: newRules,
difficultyProfile: matchedProfile !== 'custom' ? matchedProfile : undefined,
changeDescription: description,
}
}
/**
* Make worksheet easier using discrete progression indices with pedagogical constraints
*
* Algorithm (inverse of makeHarder):
* 1. Find current indices in discrete progressions
* 2. Calculate position in 2D difficulty space
* 3. Find nearest easier preset profile
* 4. Try to decrement dimension with larger gap toward preset (or specific dimension if mode specified)
* 5. Ensure resulting state respects pedagogical constraints
*
* @param mode - 'both' (default smart diagonal), 'challenge' (regrouping only), 'support' (scaffolding only)
* @param operator - Current worksheet operator (filters scaffolding descriptions)
*/
export function makeEasier(
currentState: {
pAnyStart: number
pAllStart: number
displayRules: DisplayRules
},
mode: DifficultyMode = 'both',
operator?: 'addition' | 'subtraction' | 'mixed'
): {
pAnyStart: number
pAllStart: number
displayRules: DisplayRules
difficultyProfile?: string
changeDescription: string
} {
// Find current indices in discrete progressions
const currentRegroupingIdx = findRegroupingIndex(currentState.pAnyStart, currentState.pAllStart)
const currentScaffoldingIdx = findScaffoldingIndex(currentState.displayRules)
// Ensure current state is valid (adjust if needed)
const validCurrent = findNearestValidState(currentRegroupingIdx, currentScaffoldingIdx)
let newRegroupingIdx = validCurrent.regroupingIdx
let newScaffoldingIdx = validCurrent.scaffoldingIdx
// Check if at minimum
if (newRegroupingIdx === 0 && newScaffoldingIdx === 0) {
return {
...currentState,
changeDescription: 'Already at minimum difficulty',
}
}
// Calculate current position in 2D difficulty space
const currentRegrouping = calculateRegroupingIntensity(
currentState.pAnyStart,
currentState.pAllStart
)
const currentScaffolding = calculateScaffoldingLevel(currentState.displayRules, currentRegrouping)
// Try to move based on mode
let moved = false
if (mode === 'challenge') {
// Only decrease regrouping (simpler problems)
if (newRegroupingIdx > 0) {
const testRegroupingIdx = newRegroupingIdx - 1
const adjustedScaffoldingIdx = clampScaffoldingToValidRange(
testRegroupingIdx,
newScaffoldingIdx
)
newRegroupingIdx = testRegroupingIdx
newScaffoldingIdx = adjustedScaffoldingIdx
moved = true
}
} else if (mode === 'support') {
// Only increase scaffolding (add help)
if (newScaffoldingIdx > 0 && isValidCombination(newRegroupingIdx, newScaffoldingIdx - 1)) {
newScaffoldingIdx--
moved = true
}
} else {
// mode === 'both': Smart diagonal navigation toward preset
// Find nearest easier preset to guide direction
const nearestPreset = findNearestPreset(currentRegrouping, currentScaffolding, 'easier')
if (nearestPreset) {
// Calculate target position from preset
const targetRegrouping = calculateRegroupingIntensity(
nearestPreset.profile.regrouping.pAnyStart,
nearestPreset.profile.regrouping.pAllStart
)
const targetScaffolding = calculateScaffoldingLevel(
nearestPreset.profile.displayRules,
targetRegrouping
)
// Calculate gaps in both dimensions
const regroupingGap = targetRegrouping - currentRegrouping
const scaffoldingGap = targetScaffolding - currentScaffolding
// Try dimension with larger gap first
if (Math.abs(regroupingGap) > Math.abs(scaffoldingGap) && newRegroupingIdx > 0) {
// Try decreasing regrouping
const testRegroupingIdx = newRegroupingIdx - 1
const adjustedScaffoldingIdx = clampScaffoldingToValidRange(
testRegroupingIdx,
newScaffoldingIdx
)
newRegroupingIdx = testRegroupingIdx
newScaffoldingIdx = adjustedScaffoldingIdx
moved = true
} else if (newScaffoldingIdx > 0) {
// Try decreasing scaffolding (adding help)
const testScaffoldingIdx = newScaffoldingIdx - 1
if (isValidCombination(newRegroupingIdx, testScaffoldingIdx)) {
newScaffoldingIdx = testScaffoldingIdx
moved = true
}
}
}
// Fallback: If we couldn't move toward preset, try any valid easier move
// Per spec: makeEasier should add support (scaffolding) first, then reduce complexity (regrouping)
if (!moved) {
// Try decreasing scaffolding (adding help) first
if (newScaffoldingIdx > 0 && isValidCombination(newRegroupingIdx, newScaffoldingIdx - 1)) {
newScaffoldingIdx--
moved = true
}
// Otherwise try decreasing regrouping (reducing complexity)
else if (newRegroupingIdx > 0) {
const testRegroupingIdx = newRegroupingIdx - 1
newRegroupingIdx = testRegroupingIdx
newScaffoldingIdx = clampScaffoldingToValidRange(testRegroupingIdx, newScaffoldingIdx)
moved = true
}
}
}
if (!moved) {
return {
...currentState,
changeDescription: 'Already at minimum difficulty',
}
}
// Get new values from progressions
const newRegrouping = REGROUPING_PROGRESSION[newRegroupingIdx]
const newRules = SCAFFOLDING_PROGRESSION[newScaffoldingIdx]
// Generate description
let description = ''
if (
newRegroupingIdx < validCurrent.regroupingIdx &&
newScaffoldingIdx < validCurrent.scaffoldingIdx
) {
const scaffoldingChange = describeScaffoldingChange(
currentState.displayRules,
newRules,
'added',
operator
)
description = `Reduce regrouping to ${Math.round(newRegrouping.pAnyStart * 100)}% + ${scaffoldingChange.toLowerCase()}`
} else if (newRegroupingIdx < validCurrent.regroupingIdx) {
description = `Reduce regrouping to ${Math.round(newRegrouping.pAnyStart * 100)}%`
if (newScaffoldingIdx !== validCurrent.scaffoldingIdx) {
const scaffoldingChange = describeScaffoldingChange(
currentState.displayRules,
newRules,
newScaffoldingIdx < validCurrent.scaffoldingIdx ? 'added' : 'reduced',
operator
)
description += ` (auto-adjust: ${scaffoldingChange.toLowerCase()})`
}
} else if (newScaffoldingIdx < validCurrent.scaffoldingIdx) {
description = describeScaffoldingChange(currentState.displayRules, newRules, 'added', operator)
}
// Check if result matches a preset
const matchedProfile = getProfileFromConfig(
newRegrouping.pAllStart,
newRegrouping.pAnyStart,
newRules
)
return {
pAnyStart: newRegrouping.pAnyStart,
pAllStart: newRegrouping.pAllStart,
displayRules: newRules,
difficultyProfile: matchedProfile !== 'custom' ? matchedProfile : undefined,
changeDescription: description,
}
}
/**
* Match config to known profile or return 'custom'
*/
export function getProfileFromConfig(
pAllStart: number,
pAnyStart: number,
displayRules?: DisplayRules
): string {
if (!displayRules) return 'custom'
for (const profile of Object.values(DIFFICULTY_PROFILES)) {
const regroupMatch =
Math.abs(profile.regrouping.pAllStart - pAllStart) < 0.05 &&
Math.abs(profile.regrouping.pAnyStart - pAnyStart) < 0.05
const rulesMatch = JSON.stringify(profile.displayRules) === JSON.stringify(displayRules)
if (regroupMatch && rulesMatch) {
return profile.name
}
}
return 'custom'
}
|