use super::*; pub(crate) fn map_bark_battle_draft_config_procedure_result( result: BarkBattleProcedureResult, ) -> Result { if !result.ok { return Err(SpacetimeClientError::procedure_failed(result.error_message)); } result .draft_config .ok_or_else(|| SpacetimeClientError::missing_snapshot("Bark Battle draft config")) .map(bark_battle_draft_config_to_value) } pub(crate) fn map_bark_battle_runtime_config_procedure_result( result: BarkBattleProcedureResult, ) -> Result { if !result.ok { return Err(SpacetimeClientError::procedure_failed(result.error_message)); } result .runtime_config .ok_or_else(|| SpacetimeClientError::missing_snapshot("Bark Battle runtime config")) .map(bark_battle_runtime_config_to_value) } pub(crate) fn map_bark_battle_run_procedure_result( result: BarkBattleProcedureResult, ) -> Result { if !result.ok { return Err(SpacetimeClientError::procedure_failed(result.error_message)); } result .run .ok_or_else(|| SpacetimeClientError::missing_snapshot("Bark Battle run")) .map(bark_battle_run_to_value) } pub(crate) fn map_bark_battle_draft_config_row( row: BarkBattleDraftConfigRow, ) -> BarkBattleDraftConfigRecord { serde_json::json!({ "draftId": row.draft_id, "ownerUserId": row.owner_user_id, "workId": row.work_id, "configVersion": row.config_version, "rulesetVersion": row.ruleset_version, "difficultyPreset": row.difficulty_preset, "configJson": row.config_json, "editorStateJson": row.editor_state_json, "createdAtMicros": row.created_at.to_micros_since_unix_epoch(), "updatedAtMicros": row.updated_at.to_micros_since_unix_epoch(), }) } pub(crate) fn map_bark_battle_published_config_row( row: BarkBattlePublishedConfigRow, ) -> BarkBattleRuntimeConfigRecord { serde_json::json!({ "workId": row.work_id, "ownerUserId": row.owner_user_id, "sourceDraftId": row.source_draft_id, "configVersion": row.config_version, "rulesetVersion": row.ruleset_version, "difficultyPreset": row.difficulty_preset, "configJson": row.config_json, "publishedSnapshotJson": row.published_snapshot_json, "publishedAtMicros": row.published_at.to_micros_since_unix_epoch(), "updatedAtMicros": row.updated_at.to_micros_since_unix_epoch(), }) } pub(crate) fn map_bark_battle_gallery_view_row( row: BarkBattleGalleryViewRow, recent_play_count_7d: u32, ) -> serde_json::Value { serde_json::json!({ "workId": row.work_id, "ownerUserId": row.owner_user_id, "sourceDraftId": row.source_draft_id, "configVersion": row.config_version, "rulesetVersion": row.ruleset_version, "difficultyPreset": row.difficulty_preset, "title": row.title, "description": row.description, "themeDescription": row.theme_description, "playerImageDescription": row.player_image_description, "opponentImageDescription": row.opponent_image_description, "onomatopoeia": row.onomatopoeia, "playerCharacterImageSrc": row.player_character_image_src, "opponentCharacterImageSrc": row.opponent_character_image_src, "uiBackgroundImageSrc": row.ui_background_image_src, "status": "published", "publishReady": true, "playCount": row.play_count, "finishCount": row.finish_count, "recentPlayCount7d": recent_play_count_7d, "updatedAtMicros": row.updated_at_micros, "publishedAtMicros": row.published_at_micros, }) } fn bark_battle_draft_config_to_value(snapshot: BarkBattleDraftConfigSnapshot) -> serde_json::Value { serde_json::json!({ "draftId": snapshot.draft_id, "ownerUserId": snapshot.owner_user_id, "workId": snapshot.work_id, "configVersion": snapshot.config_version, "rulesetVersion": snapshot.ruleset_version, "difficultyPreset": snapshot.difficulty_preset, "configJson": snapshot.config_json, "editorStateJson": snapshot.editor_state_json, "createdAtMicros": snapshot.created_at_micros, "updatedAtMicros": snapshot.updated_at_micros, }) } fn bark_battle_runtime_config_to_value( snapshot: BarkBattleRuntimeConfigSnapshot, ) -> serde_json::Value { serde_json::json!({ "workId": snapshot.work_id, "ownerUserId": snapshot.owner_user_id, "sourceDraftId": snapshot.source_draft_id, "configVersion": snapshot.config_version, "rulesetVersion": snapshot.ruleset_version, "difficultyPreset": snapshot.difficulty_preset, "configJson": snapshot.config_json, "publishedSnapshotJson": snapshot.published_snapshot_json, "publishedAtMicros": snapshot.published_at_micros, "updatedAtMicros": snapshot.updated_at_micros, }) } fn bark_battle_run_to_value(snapshot: BarkBattleRunSnapshot) -> serde_json::Value { serde_json::json!({ "runId": snapshot.run_id, "ownerUserId": snapshot.owner_user_id, "workId": snapshot.work_id, "configVersion": snapshot.config_version, "rulesetVersion": snapshot.ruleset_version, "difficultyPreset": snapshot.difficulty_preset, "status": snapshot.status, "clientStartedAtMicros": snapshot.client_started_at_micros, "serverStartedAtMicros": snapshot.server_started_at_micros, "clientFinishedAtMicros": snapshot.client_finished_at_micros, "serverFinishedAtMicros": snapshot.server_finished_at_micros, "metricsJson": snapshot.metrics_json, "serverResult": snapshot.server_result, "validationStatus": snapshot.validation_status, "antiCheatFlagsJson": snapshot.anti_cheat_flags_json, "leaderboardScore": snapshot.leaderboard_score, "scoreId": snapshot.score_id, }) } #[cfg(test)] mod tests { use super::*; #[test] fn bark_battle_gallery_mapper_keeps_custom_onomatopoeia() { let row = BarkBattleGalleryViewRow { work_id: "BB-33333333".to_string(), owner_user_id: "user-3".to_string(), author_display_name: "声浪玩家".to_string(), source_draft_id: Some("bark-battle-draft-3".to_string()), config_version: 1, ruleset_version: "bark-battle-ruleset-v1".to_string(), difficulty_preset: "normal".to_string(), title: "声浪公开赛".to_string(), description: "画廊映射测试".to_string(), theme_description: "霓虹竞技场".to_string(), player_image_description: "星际猫骑士".to_string(), opponent_image_description: "机器人拳手".to_string(), onomatopoeia: vec!["轰!".to_string(), "炸场!".to_string()], player_character_image_src: Some("/assets/player.png".to_string()), opponent_character_image_src: Some("/assets/opponent.png".to_string()), ui_background_image_src: Some("/assets/background.png".to_string()), play_count: 8, finish_count: 5, updated_at_micros: 1_713_686_401_234_567, published_at_micros: 1_713_686_401_234_000, }; let value = map_bark_battle_gallery_view_row(row, 3); assert_eq!(value["onomatopoeia"], serde_json::json!(["轰!", "炸场!"])); assert_eq!(value["recentPlayCount7d"], serde_json::json!(3)); } }