50 lines
1.7 KiB
JavaScript
50 lines
1.7 KiB
JavaScript
// utils/dogStep.js — 体型档位 → 步幅 → 狗步(docs/05 R3)
|
||
// 狗步 = round(distanceMeters / stride);stride 由体型档位决定,体重做 ±15% 校准。
|
||
// 与 cloudfunctions/dogManage/index.js 的同名常量保持一致。
|
||
|
||
const SIZE_LABELS = [
|
||
{ key: 'xs', label: '超小型' },
|
||
{ key: 's', label: '小型/短腿' },
|
||
{ key: 'm', label: '中型' },
|
||
{ key: 'l', label: '大型' },
|
||
{ key: 'xl', label: '超大型' },
|
||
];
|
||
|
||
const BASE_STRIDE = { xs: 0.20, s: 0.25, m: 0.35, l: 0.45, xl: 0.55 }; // 米/步
|
||
const STD_WEIGHT = { xs: 3, s: 7, m: 15, l: 28, xl: 45 }; // 档位标准体重 kg
|
||
|
||
// 犬种 → 体型档位;未列出的(中华田园犬/串串/其他)返回 null,需用户手动选
|
||
const BREED_SIZE = {
|
||
柯基: 's', 比熊: 'xs', 泰迪: 's', 柴犬: 'm', 金毛: 'l',
|
||
拉布拉多: 'l', 哈士奇: 'l', 边境牧羊犬: 'm', 萨摩耶: 'l', 法斗: 's',
|
||
};
|
||
|
||
function sizeOfBreed(breed) {
|
||
return BREED_SIZE[breed] || null;
|
||
}
|
||
|
||
// 由体型档位(+可选体重)算步幅
|
||
function computeStride(sizeLevel, weight) {
|
||
const base = BASE_STRIDE[sizeLevel];
|
||
if (!base) return { baseStride: null, stride: null };
|
||
let stride = base;
|
||
if (weight) {
|
||
const std = STD_WEIGHT[sizeLevel];
|
||
let f = 1 + (Number(weight) - std) * 0.005;
|
||
f = Math.max(0.85, Math.min(1.15, f)); // 封顶 ±15%
|
||
stride = +(base * f).toFixed(3);
|
||
}
|
||
return { baseStride: base, stride };
|
||
}
|
||
|
||
// 距离(米) + 步幅 → 狗步;无步幅返回 null
|
||
function dogStepsFromMeters(meters, stride) {
|
||
if (!stride || meters == null) return null;
|
||
return Math.round(meters / stride);
|
||
}
|
||
|
||
module.exports = {
|
||
SIZE_LABELS, BASE_STRIDE, STD_WEIGHT, BREED_SIZE,
|
||
sizeOfBreed, computeStride, dogStepsFromMeters,
|
||
};
|