Skip to content

Commit

Permalink
Migrate Akinator guesses/answers to async
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Jul 3, 2024
1 parent dd1e2d7 commit 4999924
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
27 changes: 13 additions & 14 deletions Sources/D2Commands/Fun/AkinatorCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,25 @@ public class AkinatorCommand: StringCommand {
return
}

session.answer(with: answer).listen {
// TODO: Remove once onSubscriptionMessage is async
Task {
do {
let question = try $0.get()
let question = try await session.answer(with: answer)
if question.progression > progressionThreshold {
session.guess().listen {
do {
guard let guess = try $0.get().first else { throw AkinatorError.noGuesses }
output.append(self.embed(of: guess))
} catch {
output.append(error, errorText: "Error while guessing")
}

self.sessions[channelId] = nil
context.unsubscribeFromChannel()
do {
guard let guess = try await session.guess().first else { throw AkinatorError.noGuesses }
await output.append(self.embed(of: guess))
} catch {
await output.append(error, errorText: "Error while guessing")
}

self.sessions[channelId] = nil
context.unsubscribeFromChannel()
} else {
output.append(self.embed(of: question))
await output.append(self.embed(of: question))
}
} catch {
output.append(error, errorText: "Error while answering")
await output.append(error, errorText: "Error while answering")
}
}
}
Expand Down
29 changes: 15 additions & 14 deletions Sources/D2NetAPIs/Akinator/AkinatorSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,30 @@ public struct AkinatorSession {
)
}

public func answer(with answer: AkinatorAnswer) -> Promise<AkinatorQuestion, any Error> {
Promise.catching { try HTTPRequest(host: serverUrl.host!, port: serverUrl.port, path: "\(serverUrl.path)/answer", query: [
public func answer(with answer: AkinatorAnswer) async throws -> AkinatorQuestion {
let request = try HTTPRequest(host: serverUrl.host!, port: serverUrl.port, path: "\(serverUrl.path)/answer", query: [
"session": session,
"signature": signature,
"step": "\(step)",
"answer": "\(answer.value)"
], headers: headers) }
.then { $0.fetchJSONAsync(as: AkinatorResponse.StepInformation.self) }
.mapCatching {
guard let step = Int($0.parameters.step) else { throw AkinatorError.invalidStep($0.parameters.step) }
self.step = step
return try $0.parameters.asQuestion()
}
], headers: headers)

let stepInfo = try await request.fetchJSON(as: AkinatorResponse.StepInformation.self)
guard let step = Int(stepInfo.parameters.step) else { throw AkinatorError.invalidStep(stepInfo.parameters.step) }
self.step = step

return try stepInfo.parameters.asQuestion()
}

public func guess() -> Promise<[AkinatorGuess], any Error> {
Promise.catching { try HTTPRequest(host: serverUrl.host!, port: serverUrl.port, path: "\(serverUrl.path)/list", query: [
public func guess() async throws -> [AkinatorGuess] {
let request = try HTTPRequest(host: serverUrl.host!, port: serverUrl.port, path: "\(serverUrl.path)/list", query: [
"session": session,
"signature": signature,
"step": "\(step)"
], headers: headers) }
.then { $0.fetchJSONAsync(as: AkinatorResponse.Guess.self) }
.mapCatching { try $0.parameters.characters.map { try $0.asGuess() } }
], headers: headers)

let guess = try await request.fetchJSON(as: AkinatorResponse.Guess.self)
return try guess.parameters.characters.map { try $0.asGuess() }
}

private static func getApiKey() async throws -> ApiKey {
Expand Down

0 comments on commit 4999924

Please sign in to comment.