Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions Sources/DHKit/EncounterSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,18 @@ import Observation
// MARK: - SessionPhase

/// The lifecycle phase of a live encounter session.
public enum SessionPhase: String, Codable, Sendable {
public enum SessionPhase: String, Sendable {
case running
case paused
}

extension SessionPhase: Codable {
public init(from decoder: any Decoder) throws {
let raw = try decoder.singleValueContainer().decode(String.self)
self = SessionPhase(rawValue: raw) ?? .running
}
}

// MARK: - EncounterSession

/// The live state of a Daggerheart encounter being run at the table.
Expand Down Expand Up @@ -108,8 +115,6 @@ public final class EncounterSession: Identifiable, Hashable {
/// Running total of spotlight grants in this encounter.
public var spotlightCount: Int

// MARK: Lifecycle

/// The current lifecycle phase of this session.
public private(set) var phase: SessionPhase

Expand Down Expand Up @@ -158,11 +163,13 @@ public final class EncounterSession: Identifiable, Hashable {

/// Pause this session. The session persists and can be resumed later.
public func pause() {
guard phase != .paused else { return }
phase = .paused
}

/// Resume a paused session.
public func resume() {
guard phase != .running else { return }
phase = .running
}

Expand Down Expand Up @@ -513,9 +520,7 @@ extension EncounterSession: @MainActor Codable {
let spotlightedSlotID = try c.decodeIfPresent(UUID.self, forKey: .spotlightedSlotID)
let spotlightCount = try c.decode(Int.self, forKey: .spotlightCount)
let gmNotes = try c.decode(String.self, forKey: .gmNotes)
let phase =
(try c.decodeIfPresent(String.self, forKey: .phase))
.flatMap(SessionPhase.init(rawValue:)) ?? .running
let phase = try c.decodeIfPresent(SessionPhase.self, forKey: .phase) ?? .running
let definitionID = try c.decodeIfPresent(UUID.self, forKey: .definitionID)
let definitionSnapshotDate = try c.decodeIfPresent(Date.self, forKey: .definitionSnapshotDate)
self.init(
Expand Down
4 changes: 2 additions & 2 deletions Tests/DHKitTests/EncounterSessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ import Testing
{"id":"\(UUID().uuidString)","name":"Legacy","adversarySlots":[],"playerSlots":[],\
"environmentSlots":[],"fearPool":0,"hopePool":0,"spotlightCount":0,"gmNotes":""}
"""
let data = json.data(using: .utf8)!
let data = try #require(json.data(using: .utf8))
let decoded = try JSONDecoder().decode(EncounterSession.self, from: data)
#expect(decoded.phase == .running)
}
Expand All @@ -676,7 +676,7 @@ import Testing
"environmentSlots":[],"fearPool":0,"hopePool":0,"spotlightCount":0,\
"gmNotes":"","phase":"completed"}
"""
let data = json.data(using: .utf8)!
let data = try #require(json.data(using: .utf8))
let decoded = try JSONDecoder().decode(EncounterSession.self, from: data)
#expect(decoded.phase == .running)
}
Expand Down
Loading