fix(抽奖): 概率按 weight/1000 显示与录入(支持小数%,总权重100000=100%)
- prize-grid: 概率预览/合计/卡片改用 weightToPercent(weight/1000), 合计校验对齐 FULL_TOTAL_WEIGHT=100000;修正原来把原始 weight 当 % 显示(如 49901%) - prize-form: 概率输入按百分比(0-100,step 0.001),提交 ×1000 存 weight,回填 ÷1000 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -14,9 +14,16 @@ import PrizeForm from "./prize-form";
|
||||
|
||||
const DEFAULT_GRID_SIZE = 9;
|
||||
const CENTER_SLOT = 4;
|
||||
const PERCENT_DECIMALS = 1;
|
||||
const REQUIRED_TOTAL_PERCENT = 100;
|
||||
const CENTS_PER_YUAN = 100;
|
||||
// 概率单位约定:weight 1000 = 1%,总权重 100000 = 100.000%(支持 3 位小数概率)。
|
||||
const WEIGHT_PER_PERCENT = 1000;
|
||||
const FULL_TOTAL_WEIGHT = 100 * WEIGHT_PER_PERCENT;
|
||||
|
||||
// weightToPercent 把原始 weight 转成百分比字符串(最多 3 位小数,去尾零)。
|
||||
function weightToPercent(weight: number): string {
|
||||
const pct = weight / WEIGHT_PER_PERCENT;
|
||||
return `${Number.parseFloat(pct.toFixed(3))}`;
|
||||
}
|
||||
|
||||
interface PrizeGridProps {
|
||||
activity: API.LotteryActivity;
|
||||
@@ -110,7 +117,7 @@ export default function PrizeGrid({ activity }: PrizeGridProps) {
|
||||
[randomPrizes]
|
||||
);
|
||||
const hasFallback = prizes.some((prize) => prize.is_fallback);
|
||||
const totalIsValid = totalWeight === REQUIRED_TOTAL_PERCENT;
|
||||
const totalIsValid = totalWeight === FULL_TOTAL_WEIGHT;
|
||||
const gridSize = activity.grid_size || DEFAULT_GRID_SIZE;
|
||||
const slots = Array.from({ length: gridSize }, (_, index) => index);
|
||||
|
||||
@@ -137,7 +144,8 @@ export default function PrizeGrid({ activity }: PrizeGridProps) {
|
||||
</p>
|
||||
{randomPrizes.length > 0 && (
|
||||
<Badge variant={totalIsValid ? "secondary" : "destructive"}>
|
||||
{t("prize.probabilityTotal", "Total")}: {totalWeight}%
|
||||
{t("prize.probabilityTotal", "Total")}:{" "}
|
||||
{weightToPercent(totalWeight)}%
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
@@ -145,9 +153,7 @@ export default function PrizeGrid({ activity }: PrizeGridProps) {
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{randomPrizes.map((prize) => (
|
||||
<Badge key={prize.id} variant="secondary">
|
||||
{prize.name}:{" "}
|
||||
{((prize.weight / totalWeight) * 100).toFixed(PERCENT_DECIMALS)}
|
||||
%
|
||||
{prize.name}: {weightToPercent(prize.weight)}%
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
@@ -170,7 +176,7 @@ export default function PrizeGrid({ activity }: PrizeGridProps) {
|
||||
{t(
|
||||
"prize.totalNot100Hint",
|
||||
"The drawable prizes currently total {{total}}%. Adjust each prize's win probability so they sum to exactly 100%.",
|
||||
{ total: totalWeight }
|
||||
{ total: weightToPercent(totalWeight) }
|
||||
)}
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
@@ -244,8 +250,10 @@ export default function PrizeGrid({ activity }: PrizeGridProps) {
|
||||
})()}
|
||||
<span className="text-muted-foreground text-xs">
|
||||
{t("prize.weightShort", "Prob")}:{" "}
|
||||
{prize.is_fallback ? "—" : `${prize.weight}%`} ·{" "}
|
||||
{t("prize.stockShort", "Stock")}:{" "}
|
||||
{prize.is_fallback
|
||||
? "—"
|
||||
: `${weightToPercent(prize.weight)}%`}{" "}
|
||||
· {t("prize.stockShort", "Stock")}:{" "}
|
||||
{prize.total_stock === null ? "∞" : prize.total_stock}
|
||||
</span>
|
||||
<div className="flex flex-wrap justify-center gap-1">
|
||||
|
||||
Reference in New Issue
Block a user