Files
Genarrative/server-rs/crates/spacetime-client/src/mapper/story.rs

292 lines
10 KiB
Rust

use super::*;
impl From<module_runtime::RuntimeSnapshotUpsertInput> for RuntimeSnapshotUpsertInput {
fn from(input: module_runtime::RuntimeSnapshotUpsertInput) -> Self {
Self {
user_id: input.user_id,
saved_at_micros: input.saved_at_micros,
bottom_tab: input.bottom_tab,
game_state_json: input.game_state_json,
current_story_json: input.current_story_json,
updated_at_micros: input.updated_at_micros,
}
}
}
impl From<DomainStorySessionInput> for StorySessionInput {
fn from(input: DomainStorySessionInput) -> Self {
Self {
story_session_id: input.story_session_id,
runtime_session_id: input.runtime_session_id,
actor_user_id: input.actor_user_id,
world_profile_id: input.world_profile_id,
initial_prompt: input.initial_prompt,
opening_summary: input.opening_summary,
created_at_micros: input.created_at_micros,
}
}
}
impl From<DomainStoryContinueInput> for StoryContinueInput {
fn from(input: DomainStoryContinueInput) -> Self {
Self {
story_session_id: input.story_session_id,
event_id: input.event_id,
narrative_text: input.narrative_text,
choice_function_id: input.choice_function_id,
updated_at_micros: input.updated_at_micros,
}
}
}
impl From<DomainStorySessionStateInput> for StorySessionStateInput {
fn from(input: DomainStorySessionStateInput) -> Self {
Self {
story_session_id: input.story_session_id,
}
}
}
pub(crate) fn map_asset_history_list_result(
result: AssetHistoryListResult,
) -> Result<Vec<AssetHistoryEntryRecord>, SpacetimeClientError> {
if !result.ok {
return Err(SpacetimeClientError::procedure_failed(result.error_message));
}
Ok(result
.entries
.into_iter()
.map(map_asset_history_entry_snapshot)
.map(build_asset_history_entry_record)
.collect())
}
pub(crate) fn map_runtime_browse_history_procedure_result(
result: RuntimeBrowseHistoryProcedureResult,
) -> Result<Vec<RuntimeBrowseHistoryRecord>, SpacetimeClientError> {
if !result.ok {
return Err(SpacetimeClientError::procedure_failed(result.error_message));
}
Ok(result
.entries
.into_iter()
.map(|snapshot| {
build_runtime_browse_history_record(map_runtime_browse_history_snapshot(snapshot))
})
.collect())
}
pub(crate) fn map_story_session_procedure_result(
result: StorySessionProcedureResult,
) -> Result<StorySessionResultRecord, SpacetimeClientError> {
if !result.ok {
return Err(SpacetimeClientError::procedure_failed(result.error_message));
}
let session = result
.session
.ok_or_else(|| SpacetimeClientError::missing_snapshot("story session 快照"))?;
let event = result
.event
.ok_or_else(|| SpacetimeClientError::missing_snapshot("story event 快照"))?;
Ok(StorySessionResultRecord {
session: map_story_session_snapshot(session),
event: map_story_event_snapshot(event),
})
}
pub(crate) fn map_story_session_state_procedure_result(
result: StorySessionStateProcedureResult,
) -> Result<StorySessionStateRecord, SpacetimeClientError> {
if !result.ok {
return Err(SpacetimeClientError::procedure_failed(result.error_message));
}
let session = result
.session
.ok_or_else(|| SpacetimeClientError::missing_snapshot("story session state 快照"))?;
Ok(StorySessionStateRecord {
session: map_story_session_snapshot(session),
events: result
.events
.into_iter()
.map(map_story_event_snapshot)
.collect(),
})
}
pub(crate) fn map_asset_history_entry_snapshot(
snapshot: AssetHistoryEntrySnapshot,
) -> module_assets::AssetHistoryEntrySnapshot {
module_assets::AssetHistoryEntrySnapshot {
asset_object_id: snapshot.asset_object_id,
asset_kind: snapshot.asset_kind,
image_src: snapshot.image_src,
owner_user_id: snapshot.owner_user_id,
profile_id: snapshot.profile_id,
entity_id: snapshot.entity_id,
created_at_micros: snapshot.created_at_micros,
updated_at_micros: snapshot.updated_at_micros,
}
}
pub(crate) fn map_runtime_browse_history_snapshot(
snapshot: RuntimeBrowseHistorySnapshot,
) -> module_runtime::RuntimeBrowseHistorySnapshot {
module_runtime::RuntimeBrowseHistorySnapshot {
browse_history_id: snapshot.browse_history_id,
user_id: snapshot.user_id,
owner_user_id: snapshot.owner_user_id,
profile_id: snapshot.profile_id,
world_name: snapshot.world_name,
subtitle: snapshot.subtitle,
summary_text: snapshot.summary_text,
cover_image_src: snapshot.cover_image_src,
theme_mode: map_runtime_browse_history_theme_mode_back(snapshot.theme_mode),
author_display_name: snapshot.author_display_name,
visited_at_micros: snapshot.visited_at_micros,
created_at_micros: snapshot.created_at_micros,
updated_at_micros: snapshot.updated_at_micros,
}
}
pub(crate) fn map_runtime_snapshot_snapshot(
snapshot: RuntimeSnapshot,
) -> module_runtime::RuntimeSnapshot {
module_runtime::RuntimeSnapshot {
user_id: snapshot.user_id,
version: snapshot.version,
saved_at_micros: snapshot.saved_at_micros,
bottom_tab: snapshot.bottom_tab,
game_state_json: snapshot.game_state_json,
current_story_json: snapshot.current_story_json,
created_at_micros: snapshot.created_at_micros,
updated_at_micros: snapshot.updated_at_micros,
}
}
pub(crate) fn map_runtime_profile_save_archive_snapshot(
snapshot: RuntimeProfileSaveArchiveSnapshot,
) -> module_runtime::RuntimeProfileSaveArchiveSnapshot {
module_runtime::RuntimeProfileSaveArchiveSnapshot {
archive_id: snapshot.archive_id,
user_id: snapshot.user_id,
world_key: snapshot.world_key,
owner_user_id: snapshot.owner_user_id,
profile_id: snapshot.profile_id,
world_type: snapshot.world_type,
world_name: snapshot.world_name,
subtitle: snapshot.subtitle,
summary_text: snapshot.summary_text,
cover_image_src: snapshot.cover_image_src,
saved_at_micros: snapshot.saved_at_micros,
bottom_tab: snapshot.bottom_tab,
game_state_json: snapshot.game_state_json,
current_story_json: snapshot.current_story_json,
created_at_micros: snapshot.created_at_micros,
updated_at_micros: snapshot.updated_at_micros,
}
}
pub(crate) fn map_story_session_snapshot(snapshot: StorySessionSnapshot) -> StorySessionRecord {
StorySessionRecord {
story_session_id: snapshot.story_session_id,
runtime_session_id: snapshot.runtime_session_id,
actor_user_id: snapshot.actor_user_id,
world_profile_id: snapshot.world_profile_id,
initial_prompt: snapshot.initial_prompt,
opening_summary: snapshot.opening_summary,
latest_narrative_text: snapshot.latest_narrative_text,
latest_choice_function_id: snapshot.latest_choice_function_id,
status: map_story_session_status(snapshot.status)
.as_str()
.to_string(),
version: snapshot.version,
created_at: format_timestamp_micros(snapshot.created_at_micros),
updated_at: format_timestamp_micros(snapshot.updated_at_micros),
}
}
pub(crate) fn map_story_event_snapshot(snapshot: StoryEventSnapshot) -> StoryEventRecord {
StoryEventRecord {
event_id: snapshot.event_id,
story_session_id: snapshot.story_session_id,
event_kind: map_story_event_kind(snapshot.event_kind)
.as_str()
.to_string(),
narrative_text: snapshot.narrative_text,
choice_function_id: snapshot.choice_function_id,
created_at: format_timestamp_micros(snapshot.created_at_micros),
}
}
pub(crate) fn map_runtime_browse_history_theme_mode_back(
value: crate::module_bindings::RuntimeBrowseHistoryThemeMode,
) -> module_runtime::RuntimeBrowseHistoryThemeMode {
match value {
crate::module_bindings::RuntimeBrowseHistoryThemeMode::Martial => {
module_runtime::RuntimeBrowseHistoryThemeMode::Martial
}
crate::module_bindings::RuntimeBrowseHistoryThemeMode::Arcane => {
module_runtime::RuntimeBrowseHistoryThemeMode::Arcane
}
crate::module_bindings::RuntimeBrowseHistoryThemeMode::Machina => {
module_runtime::RuntimeBrowseHistoryThemeMode::Machina
}
crate::module_bindings::RuntimeBrowseHistoryThemeMode::Tide => {
module_runtime::RuntimeBrowseHistoryThemeMode::Tide
}
crate::module_bindings::RuntimeBrowseHistoryThemeMode::Rift => {
module_runtime::RuntimeBrowseHistoryThemeMode::Rift
}
crate::module_bindings::RuntimeBrowseHistoryThemeMode::Mythic => {
module_runtime::RuntimeBrowseHistoryThemeMode::Mythic
}
}
}
pub(crate) fn map_story_session_status(value: StorySessionStatus) -> DomainStorySessionStatus {
match value {
StorySessionStatus::Active => DomainStorySessionStatus::Active,
StorySessionStatus::Completed => DomainStorySessionStatus::Completed,
StorySessionStatus::Archived => DomainStorySessionStatus::Archived,
}
}
pub(crate) fn map_story_event_kind(value: StoryEventKind) -> DomainStoryEventKind {
match value {
StoryEventKind::SessionStarted => DomainStoryEventKind::SessionStarted,
StoryEventKind::StoryContinued => DomainStoryEventKind::StoryContinued,
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct VisualNovelRuntimeEventRecordInput {
pub event_id: String,
pub run_id: String,
pub owner_user_id: String,
pub profile_id: Option<String>,
pub event_kind: String,
pub client_event_id: Option<String>,
pub history_entry_id: Option<String>,
pub payload_json: String,
pub occurred_at_micros: i64,
}
#[derive(Clone, Debug, PartialEq)]
pub struct VisualNovelRuntimeEventRecord {
pub event_id: String,
pub run_id: Option<String>,
pub owner_user_id: String,
pub profile_id: Option<String>,
pub event_kind: String,
pub client_event_id: Option<String>,
pub history_entry_id: Option<String>,
pub payload: serde_json::Value,
pub occurred_at: String,
}