-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstants.ts
More file actions
103 lines (87 loc) · 2.77 KB
/
constants.ts
File metadata and controls
103 lines (87 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { AuctionConfig, Founder } from './types';
type RawSetup = {
startPrice?: number;
floorPrice?: number;
decrementAmount?: number;
dropIntervalMs?: number;
participantCount?: number;
participants?: Founder[];
};
declare global {
interface Window {
AUCTION_SETUP?: RawSetup;
}
}
const FALLBACK_PARTICIPANTS: Founder[] = [
{ id: '1', name: 'EF', color: 'bg-rose-500' },
{ id: '2', name: 'EG', color: 'bg-indigo-500' },
{ id: '3', name: 'AG', color: 'bg-emerald-500' },
];
const DEFAULTS = {
startPrice: 20000,
floorPrice: 1000,
decrementAmount: 1000,
dropIntervalMs: 10000,
participantCount: 3,
};
const COLORS = [
'bg-rose-500',
'bg-indigo-500',
'bg-emerald-500',
'bg-cyan-500',
'bg-amber-500',
'bg-violet-500',
'bg-orange-500',
'bg-pink-500',
];
const n = (value: unknown, fallback: number) => {
const parsed = Number(value);
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
};
export function buildParticipants(desiredCount: number, source?: Founder[]): Founder[] {
const provided = Array.isArray(source) ? source : [];
const normalized = provided
.map((item, idx) => {
const id = String(item?.id ?? idx + 1);
const name = String(item?.name ?? `P${idx + 1}`).slice(0, 10).toUpperCase();
const color = String(item?.color ?? COLORS[idx % COLORS.length]);
if (!id || !name) return null;
return { id, name, color } as Founder;
})
.filter((x): x is Founder => Boolean(x));
const target = Math.max(1, Math.floor(desiredCount));
if (normalized.length >= target) {
return normalized.slice(0, target);
}
const seed = normalized.length > 0 ? normalized : FALLBACK_PARTICIPANTS;
const result: Founder[] = [...normalized];
while (result.length < target) {
const i = result.length;
const sourceFounder = seed[i % seed.length];
result.push({
id: String(i + 1),
name: sourceFounder?.name ? `${sourceFounder.name}${i + 1}`.slice(0, 10) : `P${i + 1}`,
color: COLORS[i % COLORS.length],
});
}
return result;
}
const raw: RawSetup = window.AUCTION_SETUP ?? {};
const participantCount = Math.max(
1,
Math.floor(
Number.isFinite(Number(raw.participantCount))
? Number(raw.participantCount)
: (Array.isArray(raw.participants) && raw.participants.length > 0
? raw.participants.length
: DEFAULTS.participantCount),
),
);
export const INITIAL_CONFIG: AuctionConfig = {
startPrice: n(raw.startPrice, DEFAULTS.startPrice),
floorPrice: n(raw.floorPrice, DEFAULTS.floorPrice),
decrementAmount: n(raw.decrementAmount, DEFAULTS.decrementAmount),
dropIntervalMs: n(raw.dropIntervalMs, DEFAULTS.dropIntervalMs),
participantCount,
};
export const INITIAL_FOUNDERS = buildParticipants(participantCount, raw.participants);