5ad750a52cf96ebc693232d24d201de86380bb58

Author
TheEdgeOfRage <git@theedgeofrage.com>
Committer
TheEdgeOfRage <git@theedgeofrage.com>
Date

Message

Use server-generated opening turn on new adventure

Diff

 1diff --git a/Sources/kaiwari/APIClient.swift b/Sources/kaiwari/APIClient.swift
 2index 7b5d957bcd21304fabb745717852c4374351e08c..fbfd487fcd4a4c4556f91bbe7790d468855e0a7d 100644
 3--- a/Sources/kaiwari/APIClient.swift
 4+++ b/Sources/kaiwari/APIClient.swift
 5@@ -54,6 +54,35 @@ extension TurnResponse {
 6 
 7 private struct CreateSessionResponse: Decodable, Sendable {
 8     let id: String
 9+    let action: String?
10+    let desc: String?
11+    let romaji: String?
12+    let english: String?
13+    let hasSpeech: Bool
14+    let judge: Judge?
15+    let ask: Ask?
16+    let location: String?
17+    let talk: String?
18+    let audio: String?
19+    let speakError: String?
20+    let judgeError: String?
21+
22+    var turn: TurnResponse {
23+        TurnResponse(
24+            action: action,
25+            desc: desc,
26+            romaji: romaji,
27+            english: english,
28+            hasSpeech: hasSpeech,
29+            judge: judge,
30+            ask: ask,
31+            location: location,
32+            talk: talk,
33+            audio: audio,
34+            speakError: speakError,
35+            judgeError: judgeError
36+        )
37+    }
38 }
39 
40 private struct FlashcardResponse: Decodable, Sendable {
41@@ -72,13 +101,16 @@ private struct ErrorResponse: Decodable, Sendable {
42 private let turnTimeout: TimeInterval = 200
43 
44 struct APIClient: Sendable {
45-    func createSession(using configuration: ServerConfiguration) async throws -> String {
46+    // Starting a session runs the server's opening turn (the area description),
47+    // so it takes as long as any other turn.
48+    func createSession(using configuration: ServerConfiguration) async throws -> (id: String, turn: TurnResponse) {
49         let response: CreateSessionResponse = try await request(
50             endpoint: configuration.endpoint(path: "v1/sessions"),
51             method: "POST",
52+            timeout: turnTimeout,
53             configuration: configuration
54         )
55-        return response.id
56+        return (response.id, response.turn)
57     }
58 
59     func fetchLog(sessionID: String, using configuration: ServerConfiguration) async throws -> [LogEntry] {
60diff --git a/Sources/kaiwari/GameStore.swift b/Sources/kaiwari/GameStore.swift
61index a59929387a6136491c2a39d1a30fefaa265e118d..fa2e80922db20a35d4dfeda95c96bc8cccb56a23 100644
62--- a/Sources/kaiwari/GameStore.swift
63+++ b/Sources/kaiwari/GameStore.swift
64@@ -13,7 +13,6 @@ final class GameStore {
65 
66     private static let sessionIDKey = "sessionID"
67     private static let recordCap: Duration = .seconds(15)
68-    private static let openingAction = "Look around."
69 
70     private let settings: SettingsStore
71     private let api = APIClient()
72@@ -58,12 +57,12 @@ final class GameStore {
73         setPhase(.processing)
74         defer { setPhase(.idle) }
75         do {
76-            let id = try await api.createSession(using: configuration)
77+            let (id, opening) = try await api.createSession(using: configuration)
78             entries = []
79             lastAudio = nil
80             status = nil
81             setSession(id)
82-            apply(try await api.performAction(sessionID: id, action: Self.openingAction, using: configuration))
83+            apply(opening)
84         } catch {
85             status = "turn failed: \(error.localizedDescription)"
86         }