web-frontend/src/fakeServerState.ts

97 lines
No EOL
3.4 KiB
TypeScript

import { Temporal } from "temporal-polyfill"
import type { BrewingEvent, Configuration, SteepingTimeChange, TypeOfTea } from "@/backend"
import { random } from "lodash-es"
import { nanoid } from "nanoid"
const localTimeZoneId = Temporal.Now.timeZoneId()
const configuration: Configuration = {
defaultSteepingTime: Temporal.Duration.from({ minutes: 4, seconds: 30 }),
feedbackTimeout: Temporal.Duration.from({ hours: 2 })
}
const typesOfTea: TypeOfTea[] = [
{
id: nanoid(),
name: "Pfefferminz",
steepingTime: Temporal.Duration.from({ minutes: 5, seconds: 20 }),
registrationTimestamp: Temporal.ZonedDateTime.from({ timeZone: localTimeZoneId, year: 2024, month: 2, day: 20, hour: 14, minute: 24 }).toInstant()
},
{
id: nanoid(),
name: "Türkischer Apfel",
steepingTime: Temporal.Duration.from({ minutes: 3, seconds: 50 }),
registrationTimestamp: Temporal.ZonedDateTime.from({ timeZone: localTimeZoneId, year: 2024, month: 3, day: 1, hour: 16, minute: 1 }).toInstant()
},
{
id: nanoid(),
name: "Fenchel-Anis-Kümmel",
steepingTime: Temporal.Duration.from({ minutes: 8 }),
registrationTimestamp: Temporal.ZonedDateTime.from({ timeZone: localTimeZoneId, year: 2024, month: 3, day: 9, hour: 9, minute: 55 }).toInstant()
},
{
id: nanoid(),
name: "Hagebutte",
steepingTime: Temporal.Duration.from({ minutes: 4, seconds: 30 }),
registrationTimestamp: Temporal.ZonedDateTime.from({ timeZone: localTimeZoneId, year: 2024, month: 3, day: 29, hour: 15, minute: 34 }).toInstant()
},
]
const steepingTimeChanges: SteepingTimeChange[] = []
const brewingEvents: BrewingEvent[] = []
for (const type of typesOfTea) {
const numberOfBrewingEvents = random(5, 10)
let nextTimestamp = type.registrationTimestamp
let lastTwoSteepingTimeSeconds: number[] = [type.steepingTime.total({ unit: "seconds" })]
for (let i = 0; i < numberOfBrewingEvents; i++) {
brewingEvents.push({
id: nanoid(),
typeOfTeaId: type.id,
timestamp: nextTimestamp,
})
if (random(0, 100) < 20) {
let newSteepingTimeSeconds: number
const direction = random(0, 1) === 1 ? "increase" : "decrease"
const currentSeconds = lastTwoSteepingTimeSeconds[0]
if (lastTwoSteepingTimeSeconds.length === 1) {
newSteepingTimeSeconds = direction === "increase" ? currentSeconds + 30 : currentSeconds - 30
} else {
const lastDelta = lastTwoSteepingTimeSeconds[1] - lastTwoSteepingTimeSeconds[0]
const lastDirection = lastDelta > 0 ? "increase" : "decrease"
if (direction === lastDirection) {
newSteepingTimeSeconds = direction === "increase" ? currentSeconds + 30 : currentSeconds - 30
} else {
newSteepingTimeSeconds = Math.round(Math.abs(lastTwoSteepingTimeSeconds[0] - lastTwoSteepingTimeSeconds[1]) / 2)
}
}
steepingTimeChanges.push({
id: nanoid(),
typeOfTeaId: type.id,
timestamp: nextTimestamp.add({ hours: random(0, 1), minutes: random(0, 30) }),
newValue: Temporal.Duration.from({ seconds: newSteepingTimeSeconds })
})
}
nextTimestamp = nextTimestamp.add(Temporal.Duration.from({ hours: random(0, 14 * 24), minutes: random(0, 60) }))
}
}
brewingEvents.sort((a, b) => Temporal.Instant.compare(a.timestamp, b.timestamp))
steepingTimeChanges.sort((a, b) => Temporal.Instant.compare(a.timestamp, b.timestamp))
export const fakeServerState: {
configuration: Configuration
typesOfTea: TypeOfTea[]
steepingTimeChanges: SteepingTimeChange[]
brewingEvents: BrewingEvent[]
} = {
configuration,
typesOfTea,
steepingTimeChanges,
brewingEvents
}