diff --git a/Sources/Camille/Karma/KarmaService+Adjustments.swift b/Sources/Camille/Karma/KarmaService+Adjustments.swift index 53399b3..cf54833 100644 --- a/Sources/Camille/Karma/KarmaService+Adjustments.swift +++ b/Sources/Camille/Karma/KarmaService+Adjustments.swift @@ -1,6 +1,23 @@ import Chameleon import Foundation +extension Sequence { + func then(_ closure: (Iterator.Element) -> Void) -> Self { + for item in self { + closure(item) + } + return self + } +} + +class Debug { + private(set) var message: String = "" + + func add(_ m: String) { + message += "\(m)\n" + } +} + private typealias PartialUpdate = (user: ModelPointer, operation: Operation) private typealias Update = (ModelPointer, (current: Int, change: Int)) @@ -16,20 +33,28 @@ private enum Operation: String { } } -private let trimCharacterSet = CharacterSet.whitespaces.union(CharacterSet(charactersIn: ">:")) - extension KarmaService { func adjust(bot: SlackBot, message: MessageDecorator) throws { guard !message.isIM else { return } + let d = Debug() + d.add("Karma Tests:") + d.add(message.text) + func partialUpdate(from link: MessageDecorator.Link>) throws -> PartialUpdate? { + d.add("testing \(link.value.id) against \(try message.sender().id)") + guard try link.value.id != message.sender().id else { return nil } - guard let operation = Operation(rawValue: message.text + let possibleOperation = message.text .substring(from: link.range.upperBound) - .trimmingCharacters(in: trimCharacterSet) - .components(separatedBy: .whitespaces) - .first ?? "") + .trimCharacters(in: [" ", ">", ":"]) + .components(separatedBy: " ") + .first ?? "" + + d.add("possible operation: '\(possibleOperation)' - from \(message.text.substring(from: link.range.upperBound)) - link \(link.range.lowerBound)..<\(link.range.upperBound)") + + guard let operation = Operation(rawValue: possibleOperation) else { return nil } return (link.value, operation) @@ -43,11 +68,33 @@ extension KarmaService { let updates = try message .mentionedUsers + .then { link in + let u = try? link.value.value() + d.add(u?.name ?? "no name?") + } .flatMap(partialUpdate) .group(by: { $0.user }) + .then { key, value in + d.add("Partial Updates:") + + func v(v: PartialUpdate) -> String { + switch v.operation { + case .plus: return "plus" + case .minus: return "minus" + } + } + + d.add("ID: \(key.id): \(value.map(v).joined(separator: ","))") + } .map(consolidatePartialUpdates) + .then { update in + d.add("Updates:") + d.add("ID: \(update.key.id): \(update.value.current) > \(update.value.change)") + } .filter { $0.value.change != 0 } + debug(d.message) + guard !updates.isEmpty else { return } let response = try message.respond() diff --git a/Sources/Camille/main.swift b/Sources/Camille/main.swift index c28b31d..ba9b25c 100644 --- a/Sources/Camille/main.swift +++ b/Sources/Camille/main.swift @@ -22,4 +22,13 @@ let bot = SlackBot( ] ) +func debug(_ message: String) { + let chatMessage = ChatMessage( + channel: "U04UAVAEB", // @iankeen + text: message + ) + + try? bot.send(chatMessage) +} + bot.start()