147 lines
7.2 KiB
TypeScript
147 lines
7.2 KiB
TypeScript
// Der Vorschlag für eine Runde (Konzept, „Regeln für den Vorschlag“) — rein,
|
|
// ohne Datenbank, damit jede Regel einzeln getestet ist. Regelbasiert und
|
|
// nachvollziehbar: Jeder Slot trägt einen Grund.
|
|
//
|
|
// 0. „Mein Gericht“ jeder Person zuerst (26.11.30): kein Veto streicht es; wer
|
|
// in der Vorrunde leer ausging, kommt bei zu wenig Slots zuerst dran.
|
|
// 1. Alle Wünsche in die Slots, Favoriten der Runde zuerst.
|
|
// 2. Vetos, kein Gericht der Vorrunde, nicht dieselbe Beilage zweimal hintereinander.
|
|
// 3. Freie Slots mit „beliebt und lange nicht gehabt“ füllen.
|
|
// 4. Nach Frische sortieren: Fisch, Fleisch, frisches Gemüse zuerst.
|
|
// 5. Kosten: höchstens ein teures Gericht je vier Slots (einstellbar); Wünsche
|
|
// zählen mit, werden aber nie gestrichen. Günstiges hat beim Auffüllen einen
|
|
// kleinen Vorsprung — „günstig und beliebt“ kommt nach „beliebt und lange nicht gehabt“.
|
|
|
|
export type Candidate = {
|
|
id: string;
|
|
title: string;
|
|
base: string | null;
|
|
isTrial: boolean;
|
|
shareUp: number | null; // Anteil Daumen hoch, null ohne Bewertung
|
|
lastCooked: string | null; // ISO-Datum
|
|
freshness: number; // 3 Fisch/Fleisch, 2 frisches Gemüse, 0 haltbar
|
|
priceClass?: "cheap" | "normal" | "expensive" | null; // aus lib/pricing.ts, null ohne Preis
|
|
};
|
|
|
|
export type WishInput = { dishId: string; memberId: string; memberName: string; isVeto: boolean; wantsToCook: boolean; isMine?: boolean };
|
|
|
|
export type Reason = { k: "wish"; n: string[] } | { k: "mine"; n: string[] } | { k: "popular" } | { k: "longAgo" } | { k: "cheap" } | { k: "pool" } | { k: "locked" } | { k: "manual" };
|
|
|
|
export type Planned = { dishId: string; reason: Reason; cookMemberId: string | null };
|
|
|
|
const DAY = 24 * 60 * 60 * 1000;
|
|
const NO_BASE = new Set([null, "none"]);
|
|
|
|
function score(c: Candidate, now: number): number {
|
|
// Beliebtheit zählt mehr als Abstand; nie gekocht gilt als „mittel lange her“,
|
|
// damit Unbekanntes Bewährtes nicht verdrängt.
|
|
const liked = c.shareUp ?? 0.6;
|
|
const days = c.lastCooked ? (now - Date.parse(c.lastCooked)) / DAY : 45;
|
|
const recent = days < 14 ? 1 : 0; // was gerade erst auf dem Tisch stand, wartet
|
|
const cheap = c.priceClass === "cheap" ? 0.25 : 0;
|
|
return 1.5 * liked + Math.min(days, 90) / 90 - recent + cheap;
|
|
}
|
|
|
|
/** Zwei gleiche Beilagen hintereinander auseinanderziehen, wo es geht. */
|
|
export function spreadBases<T>(items: T[], baseOf: (t: T) => string | null, fixed: (t: T) => boolean = () => false): T[] {
|
|
const out = [...items];
|
|
for (let i = 1; i < out.length; i++) {
|
|
const b = baseOf(out[i]!);
|
|
if (NO_BASE.has(b) || b !== baseOf(out[i - 1]!) || fixed(out[i]!)) continue;
|
|
// Ersten späteren Eintrag suchen, der an beide Nachbarn passt.
|
|
for (let j = i + 1; j < out.length; j++) {
|
|
const c = baseOf(out[j]!);
|
|
if (fixed(out[j]!) || c === b) continue;
|
|
const next = out[i + 1];
|
|
if (j !== i + 1 && next && !NO_BASE.has(c) && baseOf(next) === c) continue;
|
|
[out[i], out[j]] = [out[j]!, out[i]!];
|
|
break;
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
export function suggest(input: {
|
|
slots: number;
|
|
candidates: Candidate[];
|
|
wishes: WishInput[];
|
|
previousRound: Set<string>;
|
|
keep?: string[]; // schon gesperrte Gerichte: zählen mit, kommen nicht doppelt
|
|
expensivePerSlots?: number; // ein teures Gericht je so vielen Slots
|
|
owed?: Set<string>; // Personen, deren „Mein Gericht“ in der Vorrunde nicht in den Plan kam
|
|
now?: number;
|
|
}): Planned[] {
|
|
const now = input.now ?? Date.now();
|
|
const byId = new Map(input.candidates.map((c) => [c.id, c]));
|
|
const protectedDishes = new Set(input.wishes.filter((w) => w.isMine && !w.isVeto).map((w) => w.dishId));
|
|
const vetoed = new Set(input.wishes.filter((w) => w.isVeto && !protectedDishes.has(w.dishId)).map((w) => w.dishId));
|
|
const taken = new Set(input.keep ?? []);
|
|
const free = input.slots - taken.size;
|
|
if (free <= 0) return [];
|
|
|
|
const picked: (Planned & { c: Candidate })[] = [];
|
|
const cookFor = (id: string) => input.wishes.find((w) => w.dishId === id && !w.isVeto && w.wantsToCook)?.memberId ?? null;
|
|
|
|
// 0. „Mein Gericht“: Wer leer ausging zuerst, sonst in Reihenfolge des Markierens.
|
|
const owed = input.owed ?? new Set<string>();
|
|
const mineBy = new Map<string, WishInput[]>();
|
|
for (const w of input.wishes) if (w.isMine && !w.isVeto && byId.has(w.dishId)) mineBy.set(w.dishId, [...(mineBy.get(w.dishId) ?? []), w]);
|
|
const mineOrder = [...mineBy]
|
|
.filter(([id]) => !taken.has(id))
|
|
.map(([id, ws], i) => ({ id, ws, i, first: ws.some((w) => owed.has(w.memberId)) ? 0 : 1 }))
|
|
.sort((a, b) => a.first - b.first || a.i - b.i);
|
|
for (const { id, ws } of mineOrder) {
|
|
if (picked.length >= free) break;
|
|
picked.push({ dishId: id, c: byId.get(id)!, reason: { k: "mine", n: ws.map((w) => w.memberName) }, cookMemberId: cookFor(id) });
|
|
taken.add(id);
|
|
}
|
|
|
|
// 1. Wünsche, nach Zahl der Wünschenden, bei Gleichstand in Reihenfolge des Wünschens.
|
|
const wished = new Map<string, WishInput[]>();
|
|
for (const w of input.wishes) if (!w.isVeto && byId.has(w.dishId)) wished.set(w.dishId, [...(wished.get(w.dishId) ?? []), w]);
|
|
const wishOrder = [...wished].filter(([id]) => !vetoed.has(id) && !taken.has(id)).sort((a, b) => b[1].length - a[1].length);
|
|
|
|
for (const [id, ws] of wishOrder) {
|
|
if (picked.length >= free) break;
|
|
picked.push({
|
|
dishId: id,
|
|
c: byId.get(id)!,
|
|
reason: { k: "wish", n: ws.map((w) => w.memberName) },
|
|
cookMemberId: ws.find((w) => w.wantsToCook)?.memberId ?? null,
|
|
});
|
|
taken.add(id);
|
|
}
|
|
|
|
// 5. Wie viele teure Gerichte noch passen (gesperrte und Wünsche zählen mit).
|
|
const allowed = Math.max(1, Math.floor(input.slots / Math.max(1, input.expensivePerSlots ?? 4)));
|
|
let expensive = [...taken].filter((id) => byId.get(id)?.priceClass === "expensive").length;
|
|
|
|
// 3. Auffüllen aus dem Pool: beliebt und lange nicht gehabt; nichts aus der Vorrunde, nichts mit Veto.
|
|
const pool = input.candidates
|
|
.filter((c) => !c.isTrial && !taken.has(c.id) && !vetoed.has(c.id) && !input.previousRound.has(c.id))
|
|
.sort((a, b) => score(b, now) - score(a, now) || a.title.localeCompare(b.title));
|
|
for (const c of pool) {
|
|
if (picked.length >= free) break;
|
|
if (c.priceClass === "expensive") {
|
|
if (expensive >= allowed) continue;
|
|
expensive++;
|
|
}
|
|
const longAgo = !c.lastCooked || now - Date.parse(c.lastCooked) > 28 * DAY;
|
|
const reason: Reason =
|
|
(c.shareUp ?? 0) >= 0.75 ? { k: "popular" } : longAgo ? { k: "longAgo" } : c.priceClass === "cheap" ? { k: "cheap" } : { k: "pool" };
|
|
picked.push({ dishId: c.id, c, reason, cookMemberId: null });
|
|
taken.add(c.id);
|
|
}
|
|
|
|
// 4. Frisches zuerst (stabil), dann gleiche Beilagen auseinander.
|
|
const fresh = picked.map((p, i) => ({ p, i })).sort((a, b) => b.p.c.freshness - a.p.c.freshness || a.i - b.i).map((x) => x.p);
|
|
return spreadBases(fresh, (p) => p.c.base).map(({ dishId, reason, cookMemberId }) => ({ dishId, reason, cookMemberId }));
|
|
}
|
|
|
|
/** Frische eines Gerichts aus den Kategorien seiner Zutaten. */
|
|
export function freshnessOf(categories: string[]): number {
|
|
if (categories.some((c) => ["fish", "meat", "poultry"].includes(c))) return 3;
|
|
if (categories.some((c) => ["vegetables", "herbs", "fruit", "chilled"].includes(c))) return 2;
|
|
return 0;
|
|
}
|