step-sequence-generator
Version:
A step sequence generator for figure skating programs
135 lines (134 loc) • 5.67 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StepCounter = void 0;
const turn_absolute_name_enum_1 = require("../../shared/enums/turn-absolute-name.enum");
const movement_enums_1 = require("../../shared/enums/movement-enums");
class StepCounter {
constructor() {
this.lastStep = null;
this.turns = this.initTurns();
this.rotations = this.initRotations();
this.distance = 0;
this.threeTurnsBlock = this.initThreeTurnsBlock();
}
update(currentMovement) {
const turnAbsoluteName = currentMovement.absoluteName;
if (this.conditionIsMovementDifficult(currentMovement)) {
this.increaseTurnsDifficultAll();
if (this.conditionToIncreaseDifficultOrigin(turnAbsoluteName)) {
this.increaseDifficultOrigin(turnAbsoluteName, this.getCurrentDifficultOriginAmount(turnAbsoluteName));
}
}
if (this.conditionToIncreaseRotations(currentMovement)) {
this.increaseRotations(currentMovement, this.lastStepRotationDegree);
}
this.updateLastStep(currentMovement);
}
increaseThreeTurnsBlockAmount() {
this.threeTurnsBlock.blockAmount++;
}
// todo delete
updateThreeTurnsBlockOrigin(turnName) {
const { turns } = this.threeTurnsBlock;
const currentCount = turns.get(turnName);
const oneTypeTurnMaxAmount = this.getOneTypeTurnMaxAmount();
if (currentCount !== undefined && currentCount < oneTypeTurnMaxAmount) {
turns.set(turnName, currentCount + 1);
}
}
get unusedDifficultTurns() {
const { turns } = this.threeTurnsBlock;
const oneTypeTurnMaxAmount = this.getOneTypeTurnMaxAmount();
return Array.from(turns.entries())
.filter(([, amount]) => amount < oneTypeTurnMaxAmount)
.map(([name]) => name);
}
// todo delete
getOneTypeTurnMaxAmount() {
const values = Array.from(this.threeTurnsBlock.turns.values());
return values.includes(2) ? 1 : 2;
}
// todo delete
get threeTurnsBlockAmount() {
return this.threeTurnsBlock.blockAmount;
}
get difficultTurnsAllAmount() {
return this.turns.difficultAll || 0;
}
get difficultTurnsOriginAmount() {
return Array.from(this.turns.difficultOrigin.values()).reduce((a, b) => a + b, 0);
}
get rotationAmount() {
return {
clockwise: this.rotations.get(movement_enums_1.RotationDirectionString.CLOCKWISE) || 0,
counterclockwise: this.rotations.get(movement_enums_1.RotationDirectionString.COUNTERCLOCKWISE) || 0,
};
}
conditionToIncreaseRotations(currentMovement) {
return (currentMovement.rotationDegree >= movement_enums_1.RotationDegree.DEGREE_360 ||
(currentMovement.rotationDegree > movement_enums_1.RotationDegree.DEGREES_0 &&
currentMovement.rotationDirection === this.lastStep?.rotationDirection));
}
increaseRotations(currentMovement, lastStepRotationDegree) {
this.rotations.set(this.mappingRotationDirection(currentMovement.rotationDirection), currentMovement.rotationDegree + lastStepRotationDegree);
}
get lastStepRotationDegree() {
return this.lastStep?.rotationDegree || 0;
}
mappingRotationDirection(direction) {
switch (direction) {
case movement_enums_1.RotationDirection.COUNTERCLOCKWISE:
return movement_enums_1.RotationDirectionString.COUNTERCLOCKWISE;
case movement_enums_1.RotationDirection.CLOCKWISE:
return movement_enums_1.RotationDirectionString.CLOCKWISE;
case movement_enums_1.RotationDirection.NONE:
return movement_enums_1.RotationDirectionString.NONE;
default:
throw new Error('from mappingRotationDirection: Unrecognized RotationDirection');
}
}
conditionIsMovementDifficult(currentMovement) {
return currentMovement.isDifficult;
}
conditionToIncreaseDifficultOrigin(absoluteName) {
const currentAmount = this.turns.difficultOrigin.get(absoluteName) || 0;
return currentAmount < 2;
}
increaseTurnsDifficultAll() {
this.turns.difficultAll += 1;
}
increaseDifficultOrigin(absoluteName, currentDifficultOriginAmount) {
this.turns.difficultOrigin.set(absoluteName, currentDifficultOriginAmount + 1);
}
getCurrentDifficultOriginAmount(absoluteName) {
return this.turns.difficultOrigin.get(absoluteName) || 0;
}
updateLastStep(movement) {
this.lastStep = movement;
}
reset() {
this.lastStep = null;
this.turns = this.initTurns();
this.rotations = this.initRotations();
this.distance = 0;
this.threeTurnsBlock = this.initThreeTurnsBlock();
}
initTurns() {
return {
difficultAll: 0,
difficultOrigin: new Map(Object.values(turn_absolute_name_enum_1.TurnAbsoluteName).map((key) => [key, 0])),
};
}
// todo delete
initThreeTurnsBlock() {
const keys = Object.values(turn_absolute_name_enum_1.TurnAbsoluteName).filter((name) => ![turn_absolute_name_enum_1.TurnAbsoluteName.CHOCTAW, turn_absolute_name_enum_1.TurnAbsoluteName.UNKNOWN].includes(name));
return {
blockAmount: 0,
turns: new Map(keys.map((key) => [key, 0])),
};
}
initRotations() {
return new Map(Object.values(movement_enums_1.RotationDirectionString).map((key) => [key, 0]));
}
}
exports.StepCounter = StepCounter;