Cotton code
101 lines · 4.5 KB · SwiftRaw
// The paper chat's rules, as pure functions — which paper a note is written on,// when a message becomes a letter, and how several photographs are laid down.//// The approved references are `ref-chat-1` (a one-to-one thread),// `ref-chat-2-longmessage` (the letter) and `ref-chat-3-group` (a group). What// the screen draws is in `MessageThreadScreen.swift`; what it DECIDES is here,// so a test holds every decision without a screen.import Foundationenum ChatPaper {    // ── Which paper ───────────────────────────────────────────────────────    /// The dyes a group member's notes can be written on, in rotation order.    /// BLUE IS NOT AMONG THEM: blue is the owner's own paper, and a member on    /// blue would read as something the owner wrote.    static let memberDyes: [PaperDye] = [.cream, .sage, .straw, .peach, .pink]    /// The paper one note is written on.    ///    /// Mine is always blue and theirs, in a one-to-one thread, always cream.    /// In a group each member gets a dye of their own: `assigned` is the    /// thread's table from `groupDyes(_:)`, and a sender it does not name    /// falls back to their preferred dye.    static func dye(        mine: Bool, sender: String?, isGroup: Bool, assigned: [String: PaperDye] = [:]    ) -> PaperDye {        if mine { return .blue }        guard isGroup else { return .cream }        guard let who = memberKey(sender) else { return .cream }        return assigned[who] ?? memberDyes[preferredSlot(who)]    }    /// Every sender in a group thread, given a dye of their own.    ///    /// Each member PREFERS the dye a stable hash of who they are points at, so    /// the same person is the same paper on every open and every device, and a    /// newly loaded older message never shuffles everybody the way a    /// first-seen order would. Two members preferring one dye would read as    /// one person, though, so a contested dye goes to the alphabetically first    /// of them and the others take the next free dye along the rotation. Only    /// past five members does a dye repeat.    static func groupDyes(_ senders: [String?]) -> [String: PaperDye] {        let members = Set(senders.compactMap(memberKey)).sorted()        var out: [String: PaperDye] = [:]        var taken = Set<Int>()        var losers: [String] = []        for who in members {            let slot = preferredSlot(who)            if taken.insert(slot).inserted {                out[who] = memberDyes[slot]            } else {                losers.append(who)            }        }        for who in losers {            let start = preferredSlot(who)            let free = (0..<memberDyes.count)                .map { (start + $0) % memberDyes.count }                .first { !taken.contains($0) }            let slot = free ?? start            taken.insert(slot)            out[who] = memberDyes[slot]        }        return out    }    /// Case and surrounding space do not make a second person; a blank name    /// is nobody.    static func memberKey(_ sender: String?) -> String? {        let who = (sender ?? "").trimmingCharacters(in: .whitespacesAndNewlines).lowercased()        return who.isEmpty ? nil : who    }    private static func preferredSlot(_ key: String) -> Int {        Int(CapColour.fnv1a(key) % UInt64(memberDyes.count))    }    // ── When a message is a letter ────────────────────────────────────────    /// A message this long is written as a letter: the same paper, page wide,    /// growing with the words. Below it a note hugs its words.    static let letterCharacters = 320    /// …or one broken into this many lines, which reads as a letter however    /// short the lines are.    static let letterLines = 8    /// Whether `body` is a letter. Never a page count: a letter is one sheet    /// that grows, however long it is.    static func isLetter(_ body: String?) -> Bool {        guard let body else { return false }        let trimmed = body.trimmingCharacters(in: .whitespacesAndNewlines)        if trimmed.count >= letterCharacters { return true }        return trimmed.split(separator: "\n", omittingEmptySubsequences: false).count >= letterLines    }    // ── Several photographs ───────────────────────────────────────────────    static let clusterMax = 4}