Skip to content

Commit

Permalink
debugging..
Browse files Browse the repository at this point in the history
  • Loading branch information
IanKeen committed Aug 13, 2017
1 parent cc2d116 commit e1e487a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
59 changes: 53 additions & 6 deletions Sources/Camille/Karma/KarmaService+Adjustments.swift
Original file line number Diff line number Diff line change
@@ -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<User>, operation: Operation)
private typealias Update = (ModelPointer<User>, (current: Int, change: Int))

Expand All @@ -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<ModelPointer<User>>) 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)
Expand All @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions Sources/Camille/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit e1e487a

Please sign in to comment.