RattenheimRechner/src/assets/calculator.js

72 lines
2.4 KiB
JavaScript
Raw Normal View History

2024-07-18 17:51:24 +00:00
const MINIMUM_BASE_AREA = 0.5;
const MINIMUM_AREA_THREE_RATS = 1.8;
const AREA_PER_ADDITIONAL_RAT = 0.2;
const MAXIMUM_FALL_HEIGHT = 0.5;
const MINIMUM_LENGTH = 0.8;
const FAILED_BASE_AREA = "base_area";
const FAILED_OVERALL_AREA = "overall_area";
const FAILED_FALL_HEIGHT = "fall_height";
const FAILED_NUM_RATS = "num_rats";
const FAILED_LENGTH = "length";
const FAIL_CRITERIA = {
[FAILED_BASE_AREA]: `Die Mindestgrundfläche des Käfigs muss ${MINIMUM_BASE_AREA}m² (also z.B. 100x50cm) betragen.`,
[FAILED_OVERALL_AREA]: "Die Gesamtfläche im Käfig ist zu klein.",
[FAILED_FALL_HEIGHT]: `Die mögliche Fallhöhe darf nicht mehr als ${(MAXIMUM_FALL_HEIGHT * 100).toFixed(0)}cm betragen.`,
2024-07-18 17:51:24 +00:00
[FAILED_NUM_RATS]: "Es müssen mindestens 3 Ratten zusammenleben, Paarhaltung ist nicht artgerecht.",
[FAILED_LENGTH]: `Eine Seite des Käfig muss mindestens ${(MINIMUM_LENGTH * 100).toFixed(0)}cm lang sein um Rennen zu ermöglichen.`,
2024-07-18 17:51:24 +00:00
};
class Dimensions {
2024-07-20 06:18:38 +00:00
constructor(width, depth, height) {
2024-07-18 17:51:24 +00:00
this.width = width;
2024-07-20 06:18:38 +00:00
this.depth = depth;
2024-07-18 17:51:24 +00:00
this.height = height;
}
toString() {
2024-07-20 06:18:38 +00:00
return `${this.width}x${this.depth}x${this.height}`;
2024-07-18 17:51:24 +00:00
}
static fromDict(data) {
2024-07-20 06:18:38 +00:00
const { width, depth, height } = data;
return new Dimensions(width, depth, height);
2024-07-18 17:51:24 +00:00
}
}
function overallAreaNeeded(numOfRats) {
if (numOfRats < 3 || numOfRats > 15) {
throw new Error("This formula works only from 3 to 15 rats");
}
return MINIMUM_AREA_THREE_RATS + (numOfRats - 3) * AREA_PER_ADDITIONAL_RAT;
}
function cageCheck(dimensions, numRats, numFullFloors) {
let failedCriteria = {};
if (numRats < 2 || numRats > 15) {
failedCriteria[FAILED_NUM_RATS] = FAIL_CRITERIA[FAILED_NUM_RATS];
}
2024-07-20 06:50:07 +00:00
const baseArea = dimensions.depth * dimensions.width;
2024-07-18 17:51:24 +00:00
if (baseArea < MINIMUM_BASE_AREA) {
failedCriteria[FAILED_BASE_AREA] = FAIL_CRITERIA[FAILED_BASE_AREA];
}
const areaNeeded = overallAreaNeeded(numRats);
if (baseArea * numFullFloors < areaNeeded) {
failedCriteria[FAILED_OVERALL_AREA] = FAIL_CRITERIA[FAILED_OVERALL_AREA];
}
if (dimensions.height / numFullFloors > MAXIMUM_FALL_HEIGHT) {
failedCriteria[FAILED_FALL_HEIGHT] = FAIL_CRITERIA[FAILED_FALL_HEIGHT];
}
2024-07-20 06:50:07 +00:00
if (dimensions.width < MINIMUM_LENGTH && dimensions.depth < MINIMUM_LENGTH) {
2024-07-18 17:51:24 +00:00
failedCriteria[FAILED_LENGTH] = FAIL_CRITERIA[FAILED_LENGTH];
}
return failedCriteria;
}