first commit
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 1m4s
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 1m4s
This commit is contained in:
commit
6fc0a01c56
51 changed files with 11319 additions and 0 deletions
157
shared/bubble-physics.js
Normal file
157
shared/bubble-physics.js
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
import Matter from "matter-js";
|
||||
|
||||
const { Bodies, Body, Composite, Engine, Sleeping } = Matter;
|
||||
export const BUBBLE_RADIUS = 9.5;
|
||||
export const DRAW_BUBBLE_RADIUS = 8.4;
|
||||
export const VESSEL_RADIUS = 142;
|
||||
const packedPositions = [];
|
||||
for (let row = -9; row <= 9; row++)
|
||||
for (let col = -9; col <= 9; col++) {
|
||||
const x = 160 + (col + (row % 2) * 0.5) * 19.1;
|
||||
const y = 160 + (row * 19.1 * Math.sqrt(3)) / 2;
|
||||
if (Math.hypot(x - 160, y - 160) <= VESSEL_RADIUS - BUBBLE_RADIUS)
|
||||
packedPositions.push({ x, y });
|
||||
}
|
||||
packedPositions.sort(
|
||||
(a, b) => b.y - a.y || Math.abs(a.x - 160) - Math.abs(b.x - 160),
|
||||
);
|
||||
export const MAX_BUBBLES = packedPositions.length;
|
||||
export function bubbleCount(value, goal, enabled) {
|
||||
const ratio = Math.max(0, Math.min(value / (enabled ? goal : 2500), 1));
|
||||
return ratio > 0 ? Math.max(1, Math.round(ratio * MAX_BUBBLES)) : 0;
|
||||
}
|
||||
|
||||
// Project Earth's gravity onto the screen, then account for portrait/landscape.
|
||||
// Alpha is a compass heading and does not affect gravity.
|
||||
export function screenGravity(beta, gamma, screenAngle = 0) {
|
||||
const b = (beta * Math.PI) / 180;
|
||||
const g = (gamma * Math.PI) / 180;
|
||||
const angle = (screenAngle * Math.PI) / 180;
|
||||
const x = Math.sin(g) * Math.cos(b);
|
||||
const y = Math.sin(b);
|
||||
if (Math.hypot(x, y) < 0.12) return { x: 0, y: 0.18 };
|
||||
return {
|
||||
x: x * Math.cos(angle) + y * Math.sin(angle),
|
||||
y: -x * Math.sin(angle) + y * Math.cos(angle),
|
||||
};
|
||||
}
|
||||
|
||||
export function createBubbleWorld(initialCount = 0, random = Math.random) {
|
||||
const engine = Engine.create({
|
||||
enableSleeping: true,
|
||||
positionIterations: 24,
|
||||
velocityIterations: 8,
|
||||
});
|
||||
engine.gravity.scale = 0.00035;
|
||||
const balls = [];
|
||||
let target = initialCount;
|
||||
let spawnTime = 0;
|
||||
const walls = Array.from({ length: 72 }, (_, i) => {
|
||||
const angle = (i / 72) * Math.PI * 2;
|
||||
const radius = VESSEL_RADIUS + 9;
|
||||
return Bodies.rectangle(
|
||||
160 + Math.cos(angle) * radius,
|
||||
160 + Math.sin(angle) * radius,
|
||||
(2 * Math.PI * radius) / 72 + 2,
|
||||
18,
|
||||
{
|
||||
isStatic: true,
|
||||
angle: angle + Math.PI / 2,
|
||||
friction: 0.05,
|
||||
restitution: 0.2,
|
||||
},
|
||||
);
|
||||
});
|
||||
Composite.add(engine.world, walls);
|
||||
function add(x, y) {
|
||||
const ball = Bodies.polygon(x, y, 24, BUBBLE_RADIUS, {
|
||||
restitution: 0.34,
|
||||
friction: 0.035,
|
||||
frictionStatic: 0.15,
|
||||
frictionAir: 0.018,
|
||||
sleepThreshold: 45,
|
||||
});
|
||||
ball.circleRadius = BUBBLE_RADIUS;
|
||||
balls.push(ball);
|
||||
Composite.add(engine.world, ball);
|
||||
return ball;
|
||||
}
|
||||
function wake() {
|
||||
for (const ball of balls) Sleeping.set(ball, false);
|
||||
}
|
||||
function seed(count) {
|
||||
for (const ball of balls) Composite.remove(engine.world, ball);
|
||||
balls.length = 0;
|
||||
target = Math.min(MAX_BUBBLES, Math.max(0, count));
|
||||
for (let i = 0; i < target; i++)
|
||||
add(packedPositions[i].x, packedPositions[i].y);
|
||||
}
|
||||
seed(initialCount);
|
||||
function setTarget(count) {
|
||||
target = Math.min(MAX_BUBBLES, Math.max(0, count));
|
||||
while (balls.length > target) Composite.remove(engine.world, balls.pop());
|
||||
wake();
|
||||
}
|
||||
function gravity(x, y) {
|
||||
if (Math.hypot(engine.gravity.x - x, engine.gravity.y - y) < 0.025)
|
||||
return false;
|
||||
engine.gravity.x = x;
|
||||
engine.gravity.y = y;
|
||||
wake();
|
||||
return true;
|
||||
}
|
||||
function step(delta = 1000 / 60) {
|
||||
spawnTime += delta;
|
||||
if (balls.length < target && spawnTime >= 45) {
|
||||
spawnTime = 0;
|
||||
// Each new bubble enters at the upper inside edge, then falls naturally.
|
||||
const x = 160 + (random() - 0.5) * 55;
|
||||
const y =
|
||||
160 -
|
||||
Math.sqrt((VESSEL_RADIUS - BUBBLE_RADIUS - 2) ** 2 - (x - 160) ** 2);
|
||||
const ball = add(x, y);
|
||||
Body.setVelocity(ball, { x: (random() - 0.5) * 1.5, y: 0.6 });
|
||||
wake();
|
||||
}
|
||||
Engine.update(engine, Math.min(delta, 1000 / 60));
|
||||
// Prevent rare tunnelling during fast rotation or a suspended-tab resume.
|
||||
for (const ball of balls) {
|
||||
const dx = ball.position.x - 160;
|
||||
const dy = ball.position.y - 160;
|
||||
const distance = Math.hypot(dx, dy);
|
||||
const limit = VESSEL_RADIUS - DRAW_BUBBLE_RADIUS;
|
||||
if (distance > VESSEL_RADIUS + BUBBLE_RADIUS) {
|
||||
const nx = dx / distance;
|
||||
const ny = dy / distance;
|
||||
Body.setPosition(ball, { x: 160 + nx * limit, y: 160 + ny * limit });
|
||||
const outward = ball.velocity.x * nx + ball.velocity.y * ny;
|
||||
if (outward > 0)
|
||||
Body.setVelocity(ball, {
|
||||
x: ball.velocity.x - outward * 1.3 * nx,
|
||||
y: ball.velocity.y - outward * 1.3 * ny,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
function nudge() {
|
||||
for (const ball of balls) {
|
||||
Sleeping.set(ball, false);
|
||||
Body.setVelocity(ball, { x: (random() - 0.5) * 7, y: -2 - random() * 4 });
|
||||
}
|
||||
}
|
||||
return {
|
||||
engine,
|
||||
balls,
|
||||
step,
|
||||
seed,
|
||||
setTarget,
|
||||
gravity,
|
||||
nudge,
|
||||
awake: () =>
|
||||
balls.length < target || balls.some((ball) => !ball.isSleeping),
|
||||
dispose: () => {
|
||||
Composite.clear(engine.world, false);
|
||||
Engine.clear(engine);
|
||||
},
|
||||
};
|
||||
}
|
||||
143
shared/domain.js
Normal file
143
shared/domain.js
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
import { z } from "zod";
|
||||
|
||||
export const metricKeys = [
|
||||
"calories",
|
||||
"protein",
|
||||
"carbs",
|
||||
"fat",
|
||||
"fiber",
|
||||
"sugar",
|
||||
"sodium",
|
||||
];
|
||||
const amount = z.number().finite().min(0).max(100000);
|
||||
export const nutrientsSchema = z
|
||||
.object(Object.fromEntries(metricKeys.map((key) => [key, amount])))
|
||||
.strict();
|
||||
const date = z
|
||||
.string()
|
||||
.regex(/^\d{4}-\d{2}-\d{2}$/)
|
||||
.refine((value) => {
|
||||
const parsed = new Date(value + "T12:00:00Z");
|
||||
return (
|
||||
!Number.isNaN(+parsed) && parsed.toISOString().slice(0, 10) === value
|
||||
);
|
||||
}, "Invalid date");
|
||||
const name = z.string().trim().min(1).max(160);
|
||||
const food = z.object({
|
||||
name,
|
||||
serving: z.string().trim().min(1).max(100),
|
||||
nutrients: nutrientsSchema,
|
||||
});
|
||||
const entry = food
|
||||
.extend({
|
||||
date,
|
||||
meal: z.enum(["Breakfast", "Lunch", "Dinner", "Snacks"]),
|
||||
quantity: z.number().finite().positive().max(1000),
|
||||
})
|
||||
.strict();
|
||||
const goal = z
|
||||
.object({
|
||||
enabled: z.boolean(),
|
||||
target: z.number().finite().positive().max(100000),
|
||||
})
|
||||
.strict();
|
||||
const settings = z
|
||||
.object({
|
||||
theme: z.enum(["system", "light", "dark"]),
|
||||
accentColor: z
|
||||
.string()
|
||||
.regex(/^#[a-fA-F0-9]{6}$/)
|
||||
.optional(),
|
||||
unit: z.enum(["kg", "lb"]),
|
||||
goals: z
|
||||
.object(Object.fromEntries(metricKeys.map((key) => [key, goal])))
|
||||
.strict(),
|
||||
exportFormat: z.enum(["markdown", "csv", "json"]),
|
||||
exportDays: z.enum(["7", "30", "90", "all"]),
|
||||
exportMetrics: z.array(z.enum(metricKeys)).max(7).optional(),
|
||||
exportWeight: z.boolean().optional(),
|
||||
})
|
||||
.strict();
|
||||
export const recordSchema = z
|
||||
.object({
|
||||
id: z.string().regex(/^[\w-]{1,80}$/),
|
||||
type: z.enum(["entry", "food", "plate", "weight", "settings"]),
|
||||
updatedAt: z.string().datetime({ offset: false }),
|
||||
deleted: z.boolean(),
|
||||
data: z.unknown(),
|
||||
})
|
||||
.strict()
|
||||
.superRefine((record, ctx) => {
|
||||
if (record.type === "settings" && record.id !== "settings")
|
||||
ctx.addIssue({
|
||||
code: "custom",
|
||||
message: "Settings must use the settings ID",
|
||||
});
|
||||
if (record.type !== "settings" && record.id === "settings")
|
||||
ctx.addIssue({ code: "custom", message: "Reserved settings ID" });
|
||||
const schema = {
|
||||
entry,
|
||||
food: food.strict(),
|
||||
plate: z
|
||||
.object({
|
||||
name,
|
||||
items: z
|
||||
.array(
|
||||
food
|
||||
.extend({ quantity: z.number().finite().positive().max(1000) })
|
||||
.strict(),
|
||||
)
|
||||
.min(1)
|
||||
.max(100),
|
||||
})
|
||||
.strict(),
|
||||
weight: z
|
||||
.object({
|
||||
date,
|
||||
valueKg: z.number().finite().positive().max(1000),
|
||||
notes: z.string().max(500),
|
||||
})
|
||||
.strict(),
|
||||
settings,
|
||||
}[record.type];
|
||||
const result = schema.safeParse(record.data);
|
||||
if (!result.success)
|
||||
for (const issue of result.error.issues)
|
||||
ctx.addIssue({
|
||||
code: "custom",
|
||||
message: issue.message,
|
||||
path: ["data", ...issue.path],
|
||||
});
|
||||
});
|
||||
|
||||
// The full payload breaks same-millisecond ties identically on every device.
|
||||
export function compareRecords(a, b) {
|
||||
const timestamp = Date.parse(a.updatedAt) - Date.parse(b.updatedAt);
|
||||
if (timestamp) return timestamp;
|
||||
const left = JSON.stringify([a.deleted, a.data]);
|
||||
const right = JSON.stringify([b.deleted, b.data]);
|
||||
return left === right ? 0 : left > right ? 1 : -1;
|
||||
}
|
||||
export function emptyNutrients() {
|
||||
return Object.fromEntries(metricKeys.map((k) => [k, 0]));
|
||||
}
|
||||
export function totalNutrients(entries) {
|
||||
const total = emptyNutrients();
|
||||
for (const item of entries)
|
||||
for (const key of metricKeys)
|
||||
total[key] += (item.nutrients[key] || 0) * (item.quantity ?? 1);
|
||||
return total;
|
||||
}
|
||||
export const defaultSettings = {
|
||||
theme: "system",
|
||||
accentColor: "#8391ed",
|
||||
unit: "lb",
|
||||
exportFormat: "markdown",
|
||||
exportDays: "30",
|
||||
goals: Object.fromEntries(
|
||||
metricKeys.map((key, i) => [
|
||||
key,
|
||||
{ enabled: i < 4, target: [2000, 120, 250, 65, 30, 50, 2300][i] },
|
||||
]),
|
||||
),
|
||||
};
|
||||
55
shared/theme.js
Normal file
55
shared/theme.js
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
export const DEFAULT_ACCENT = "#8391ed";
|
||||
export const accentPresets = [
|
||||
{ name: "Periwinkle", color: "#8391ed" },
|
||||
{ name: "Sky", color: "#6daed9" },
|
||||
{ name: "Lavender", color: "#b28adc" },
|
||||
{ name: "Rose", color: "#d780a2" },
|
||||
{ name: "Amber", color: "#d9a65d" },
|
||||
{ name: "Sage", color: "#8ca37a" },
|
||||
];
|
||||
function rgb(hex) {
|
||||
return [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16));
|
||||
}
|
||||
export function mixColor(color, other, fraction) {
|
||||
const first = rgb(color);
|
||||
const second = rgb(other);
|
||||
return (
|
||||
"#" +
|
||||
first
|
||||
.map((value, i) =>
|
||||
Math.round(value * (1 - fraction) + second[i] * fraction)
|
||||
.toString(16)
|
||||
.padStart(2, "0"),
|
||||
)
|
||||
.join("")
|
||||
);
|
||||
}
|
||||
export function contrastText(color) {
|
||||
const [r, g, b] = rgb(color).map((v) => {
|
||||
const s = v / 255;
|
||||
return s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
|
||||
});
|
||||
return r * 0.2126 + g * 0.7152 + b * 0.0722 > 0.179 ? "#000000" : "#ffffff";
|
||||
}
|
||||
export function accentPalette(value, dark) {
|
||||
const accent = /^#[a-fA-F0-9]{6}$/.test(value || "") ? value : DEFAULT_ACCENT;
|
||||
const hover = mixColor(accent, dark ? "#ffffff" : "#000000", 0.12);
|
||||
return {
|
||||
"--accent": accent,
|
||||
"--accent-dark": hover,
|
||||
"--accent-soft": mixColor(
|
||||
accent,
|
||||
dark ? "#191a20" : "#ffffff",
|
||||
dark ? 0.84 : 0.9,
|
||||
),
|
||||
"--accent-text": mixColor(
|
||||
accent,
|
||||
dark ? "#ffffff" : "#000000",
|
||||
dark ? 0.28 : 0.26,
|
||||
),
|
||||
"--on-accent": contrastText(accent),
|
||||
"--on-accent-hover": contrastText(hover),
|
||||
"--orb-light": mixColor(accent, "#ffffff", dark ? 0.22 : 0.38),
|
||||
"--orb-dark": mixColor(accent, "#000000", 0.04),
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue