krz/daybyday

iOS learning games for young children: days, months, seasons, numbers, letters, animals and more. education ios kids swift https://krz.sh/apps/daybyday/

DayByDay/Speech/SpokenLines.swift

main
daybyday/DayByDay/Speech/SpokenLines.swift history · blame · raw

109 lines · 4787 bytes

1 symbol in this file
  • next function, line 102
  1//
  2//  SpokenLines.swift
  3//  DayByDay
  4//
  5
  6import Foundation
  7
  8/// Every sentence the app can speak, built from the same producers the games
  9/// and screens use. The voice clips are rendered from this list.
 10nonisolated enum SpokenLines {
 11    static let all: Set<String> = {
 12        var lines: [String] = []
 13
 14        // Cards, hub buttons, picker, finish screen.
 15        for c in Category.gridCases { lines += c.quizItems.map(\.name) }
 16        lines += LearnColor.allCases.map(\.name)
 17        lines += LearnAnimal.allCases.map(\.cardLine)
 18        lines += LearnLetter.allCases.map(\.cardLine)
 19        for day in DayOfWeek.allCases {
 20            lines.append(Lines.dayCard(day, picture: nil))
 21            lines += DayPictureKind.allCases.map { Lines.dayCard(day, picture: $0) }
 22        }
 23        lines += Game.allCases.map(\.title)
 24        lines += [Lines.lookAndListen, Lines.whoIsPlaying, Lines.greeting, Lines.youDidIt,
 25                  Lines.spinPrompt, Lines.weatherQuestion, Lines.itsRaining]
 26        lines += Lines.spokenNames.map { Lines.greeting(for: $0) }
 27        lines += PraisePhrases.all
 28
 29        // Question rounds: prompts and every correction.
 30        var rng = LineSeed(state: 1)
 31        for c in Category.gridCases {
 32            for game in c.games where game.usesRound {
 33                let source = c.questionSource(for: game)
 34                var previous: QuizItem?
 35                for i in 0..<3_000 {
 36                    let q = source.next(correctCount: i % 5, excluding: previous, using: &rng)
 37                    lines.append(q.prompt)
 38                    for choice in q.choices where choice != q.answer { lines.append(q.correction(for: choice)) }
 39                    previous = q.answer
 40                }
 41            }
 42        }
 43
 44        // Placement, sorting, moves, counting, tracing, year wheel.
 45        var puzzles = [SequencePuzzle.weekTrain(using: &rng)]
 46        puzzles += DayOfWeek.allCases.map { SequencePuzzle.yesterdayTomorrow(today: $0, using: &rng) }
 47        for var p in puzzles {
 48            while let step = p.current {
 49                lines += [step.prompt, p.correction()]
 50                _ = p.place(step.answer)
 51            }
 52            lines.append(p.summary)
 53        }
 54        for food in LearnFood.allCases {
 55            lines += [SortRound.question(for: food), "\(SortRound.fact(for: food)) Try again!"]
 56        }
 57        lines += LearnBodyPart.allCases.map(\.command)
 58        for thing in CountingRound.things {
 59            lines.append(Lines.countPrompt(thing))
 60            for n in 1...10 {
 61                lines.append(CountingRound(target: n, thing: thing, using: &rng).summary())
 62                lines.append(LearnNumber(rawValue: n)!.name)
 63            }
 64        }
 65        for item in Category.shapes.quizItems + Category.alphabet.quizItems {
 66            lines += [Lines.tracePrompt(item), Lines.traceSuccess(item), Lines.traceMiss(item)]
 67        }
 68        lines += MonthOfYear.allCases.map(YearWheel.line)
 69
 70        // Today routine over a leap year, with every birthday and picture line.
 71        var calendar = Calendar(identifier: .gregorian)
 72        calendar.timeZone = TimeZone(identifier: "UTC")!
 73        var date = calendar.date(from: DateComponents(year: 2028, month: 1, day: 1, hour: 9))!
 74        for i in 0..<366 {
 75            let parts = calendar.dateComponents([.month, .day], from: date)
 76            let picture = DayPictureKind.allCases[i % DayPictureKind.allCases.count]
 77            for birthday in [(month: parts.month!, day: parts.day!), (month: parts.month!, day: parts.day! % 28 + 1)] {
 78                lines += RoutineScript(date: date, calendar: calendar, name: nil, birthday: birthday,
 79                                       picture: picture).lines.map(\.spoken)
 80            }
 81            date = calendar.date(byAdding: .day, value: 1, to: date)!
 82        }
 83        lines += Lines.stickerWeathers.map(Lines.sticker)
 84
 85        // Sticker calendar: every date, with and without each sticker weather.
 86        let days = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
 87        for (i, month) in MonthOfYear.allCases.enumerated() {
 88            for day in 1...days[i] {
 89                lines.append(Lines.calendarDay(month, day, weather: nil))
 90                lines += Lines.stickerWeathers.map { Lines.calendarDay(month, day, weather: $0) }
 91            }
 92        }
 93
 94        return Set(lines.flatMap(SpokenText.sentences))
 95    }()
 96}
 97
 98/// SplitMix64, so the catalog is the same on every run.
 99nonisolated private struct LineSeed: RandomNumberGenerator {
100    var state: UInt64
101
102    mutating func next() -> UInt64 {
103        state &+= 0x9E37_79B9_7F4A_7C15
104        var z = state
105        z = (z ^ (z >> 30)) &* 0xBF58_476D_1CE4_E5B9
106        z = (z ^ (z >> 27)) &* 0x94D0_49BB_1331_11EB
107        return z ^ (z >> 31)
108    }
109}