step-sequence-generator
Version:
A step sequence generator for figure skating programs
106 lines (105 loc) • 4.35 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractSequenceGenerator = void 0;
const MovementExtendedFactory_1 = require("../movement/MovementExtendedFactory");
const random_generator_1 = require("../../utils/random-generator");
const rb_movement_percentage_1 = require("../../shared/constants/rb-percentage/rb-movement-percentage");
const extractors_1 = require("../roulette/weight-calculator/extractors");
const weight_key_creators_1 = require("../roulette/number-generator/weight-key-creators");
class AbstractSequenceGenerator {
constructor(data) {
const { library, context, counter, chanceRatioMapGenerator, roulette, tracker, filterStrategy, compassArc, } = data;
this.stepSequence = [];
this.library = library;
this.context = context;
this.counter = counter;
this.chanceRatioMapGenerator = chanceRatioMapGenerator;
this.roulette = roulette;
this.tracker = tracker;
this.filterStrategy = filterStrategy;
this.compassArc = compassArc;
}
generateMovement(distanceFactor, filterStrategy) {
const currentLibrary = this.getCurrentLibrary(filterStrategy);
const chanceRatioMap = this.chanceRatioMapGenerator.getChanceRatioMap({
movements: currentLibrary.movements,
rbPercentage: rb_movement_percentage_1.RB_MOVEMENTS_PERCENTAGE,
});
const newMovement = this.chooseMovement(currentLibrary.movements, chanceRatioMap);
const extendedMovement = this.extendMovement(newMovement);
extendedMovement.coordinates = this.getCoordinates(newMovement, distanceFactor);
return extendedMovement;
}
extendMovement(movement) {
return MovementExtendedFactory_1.MovementExtendedFactory.createMovementExtended({
movement,
coordinates: { coordinates: null },
threeTurnsBlockInfo: { threeTurnsBlockInfo: null },
});
}
chooseMovement(movements, chanceRatioMap) {
const movementIndex = this.roulette.spinWheel({
selection: movements,
chanceRatioMap,
itemKeyExtractor: extractors_1.movementKeyExtractor,
weightKeyCreator: weight_key_creators_1.movementWeightKeyCreator,
});
return movements[movementIndex];
}
getCurrentLibrary(filterStrategy) {
return filterStrategy.filter(this.library, this.context);
}
getFilterStrategy(filterStrategyName) {
const strategy = this.filterStrategy.get(filterStrategyName);
if (!strategy)
throw new Error(`Filter strategy with name ${filterStrategyName} not found`);
return strategy;
}
reset() {
this.resetSequence();
this.counter.reset();
this.context.resetCurrentStep();
}
resetSequence() {
this.stepSequence = [];
}
update(movementExtended) {
this.contextUpdate(movementExtended);
this.counterUpdate(movementExtended);
this.stepSequenceUpdate(movementExtended);
}
contextUpdate(movementExtended) {
this.context.currentStep = movementExtended;
}
counterUpdate(movementExtended) {
this.counter.update(movementExtended);
}
stepSequenceUpdate(movement) {
this.stepSequence.push(movement);
}
getCoordinates(newMovement, distanceFactor) {
const currentCoordinates = this.context.endCoordinate || this.tracker.getStartCoordinates();
const vector = this.context.vector;
const currentAcrVectorIndex = this.compassArc.getArcVectorIndex({
transitionDirection: this.context.currentDirection,
leg: this.context.currentLeg,
edge: this.context.currentEdge,
});
const coordinates = this.tracker.getNextPosition({
currentVectorKey: vector,
currentCoordinates,
distance: newMovement.distance * distanceFactor,
currentAcrVectorIndex,
});
return {
vector: coordinates.vector,
start: currentCoordinates,
end: coordinates.coordinates,
};
}
getRandomIndex(max) {
const min = 0;
return (0, random_generator_1.randomGenerator)(min, max);
}
}
exports.AbstractSequenceGenerator = AbstractSequenceGenerator;