support consume point
This commit is contained in:
+26
-6
@@ -1,5 +1,10 @@
|
||||
import PointAPI from "@/api/PointAPI";
|
||||
import type { PointRule } from "@/types/Point";
|
||||
import type { PointAction, PointRule } from "@/types/Point";
|
||||
|
||||
export type SourceValueConfig = {
|
||||
range: [number, number];
|
||||
action: PointAction;
|
||||
};
|
||||
|
||||
/**
|
||||
* 查询一组事件可用的自定义积分范围,并返回所有规则范围的交集。
|
||||
@@ -8,6 +13,16 @@ import type { PointRule } from "@/types/Point";
|
||||
* @returns 可用范围;没有完整有效的 SOURCE_VALUE 规则时返回 undefined
|
||||
*/
|
||||
export async function loadSourceValueRange(eventIdList: string[]): Promise<[number, number] | undefined> {
|
||||
return (await loadSourceValueConfig(eventIdList))?.range;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询登记时自定义积分的数量范围和积分动作。
|
||||
*
|
||||
* @param eventIdList 事件 ID 列表
|
||||
* @returns 所有事件规则的共同配置
|
||||
*/
|
||||
export async function loadSourceValueConfig(eventIdList: string[]): Promise<SourceValueConfig | undefined> {
|
||||
if (!eventIdList.length) {
|
||||
return undefined;
|
||||
}
|
||||
@@ -34,16 +49,21 @@ export async function loadSourceValueRange(eventIdList: string[]): Promise<[numb
|
||||
&& range.max !== undefined
|
||||
&& Number.isSafeInteger(range.min)
|
||||
&& Number.isSafeInteger(range.max)
|
||||
&& 0 < range.min
|
||||
&& 0 < range.max
|
||||
&& range.min !== 0
|
||||
&& range.max !== 0
|
||||
&& (range.min < 0) === (range.max < 0)
|
||||
));
|
||||
if (rangeList.length !== ruleList.length) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const min = Math.max(...rangeList.map(range => range.min), 1);
|
||||
const max = Math.min(...rangeList.map(range => range.max));
|
||||
return min <= max ? [min, max] : undefined;
|
||||
const action = rangeList[0].min < 0 ? "CONSUME" : "EARN";
|
||||
if (rangeList.some(range => (range.min < 0 ? "CONSUME" : "EARN") !== action)) {
|
||||
return undefined;
|
||||
}
|
||||
const min = Math.max(...rangeList.map(range => action === "CONSUME" ? Math.abs(range.max) : range.min), 1);
|
||||
const max = Math.min(...rangeList.map(range => action === "CONSUME" ? Math.abs(range.min) : range.max));
|
||||
return min <= max ? { range: [min, max], action } : undefined;
|
||||
}
|
||||
|
||||
function isSourceValueRule(rule: PointRule): boolean {
|
||||
|
||||
Reference in New Issue
Block a user