fix: 按契约显示统一创作表头
This commit is contained in:
@@ -12,7 +12,7 @@ use crate::format_utc_micros;
|
||||
use shared_contracts::creation_entry_config::{
|
||||
CreationEntryConfigResponse, CreationEntryEventBannerResponse, CreationEntryStartCardResponse,
|
||||
CreationEntryTypeModalResponse, CreationEntryTypeResponse,
|
||||
encode_unified_creation_spec_response, resolve_unified_creation_spec_response_with_entry_title,
|
||||
encode_unified_creation_spec_response, resolve_unified_creation_spec_response,
|
||||
};
|
||||
|
||||
/// 将创作入口领域快照转换为前后台共享的 HTTP 契约响应。
|
||||
@@ -45,9 +45,8 @@ pub fn build_creation_entry_config_response(
|
||||
.creation_types
|
||||
.into_iter()
|
||||
.map(|item| {
|
||||
let unified_creation_spec = resolve_unified_creation_spec_response_with_entry_title(
|
||||
let unified_creation_spec = resolve_unified_creation_spec_response(
|
||||
item.id.as_str(),
|
||||
item.title.as_str(),
|
||||
item.unified_creation_spec_json.as_deref(),
|
||||
);
|
||||
CreationEntryTypeResponse {
|
||||
|
||||
@@ -494,7 +494,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn creation_entry_response_uses_entry_title_for_legacy_unified_creation_title() {
|
||||
fn creation_entry_response_uses_unified_creation_contract_title() {
|
||||
let response = build_creation_entry_config_response(CreationEntryConfigSnapshot {
|
||||
config_id: CREATION_ENTRY_CONFIG_GLOBAL_ID.to_string(),
|
||||
start_card: CreationEntryStartCardSnapshot {
|
||||
@@ -543,7 +543,7 @@ mod tests {
|
||||
.unified_creation_spec
|
||||
.as_ref()
|
||||
.map(|spec| spec.title.as_str()),
|
||||
Some("定制拼图")
|
||||
Some("想做个什么玩法?")
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -99,7 +99,6 @@ pub struct UnifiedCreationFieldResponse {
|
||||
}
|
||||
|
||||
pub const UNIFIED_CREATION_FIELD_KINDS: [&str; 4] = ["text", "select", "image", "audio"];
|
||||
pub const LEGACY_UNIFIED_CREATION_TITLE: &str = "想做个什么玩法?";
|
||||
|
||||
pub fn build_phase1_unified_creation_spec(play_id: &str) -> Option<UnifiedCreationSpecResponse> {
|
||||
let (workspace_stage, generation_stage, result_stage, fields) = match play_id {
|
||||
@@ -138,16 +137,7 @@ pub fn build_phase1_unified_creation_spec(play_id: &str) -> Option<UnifiedCreati
|
||||
"jump-hop-workspace",
|
||||
"jump-hop-generating",
|
||||
"jump-hop-result",
|
||||
vec![
|
||||
unified_creation_field("workTitle", "text", "作品标题", true),
|
||||
unified_creation_field("workDescription", "text", "作品简介", true),
|
||||
unified_creation_field("themeTags", "text", "主题标签", true),
|
||||
unified_creation_field("difficulty", "select", "难度", true),
|
||||
unified_creation_field("stylePreset", "select", "风格", true),
|
||||
unified_creation_field("characterPrompt", "text", "角色提示词", true),
|
||||
unified_creation_field("tilePrompt", "text", "地块提示词", true),
|
||||
unified_creation_field("endMoodPrompt", "text", "终点氛围", false),
|
||||
],
|
||||
vec![unified_creation_field("themeText", "text", "主题", true)],
|
||||
),
|
||||
"wooden-fish" => (
|
||||
"wooden-fish-workspace",
|
||||
@@ -236,26 +226,6 @@ pub fn default_unified_creation_title(play_id: &str) -> Option<&'static str> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn normalize_unified_creation_spec_title_for_entry(
|
||||
play_id: &str,
|
||||
entry_title: &str,
|
||||
mut spec: UnifiedCreationSpecResponse,
|
||||
) -> UnifiedCreationSpecResponse {
|
||||
let entry_title = entry_title.trim();
|
||||
if entry_title.is_empty() {
|
||||
return spec;
|
||||
}
|
||||
|
||||
let spec_title = spec.title.trim();
|
||||
let default_title = default_unified_creation_title(play_id).unwrap_or_default();
|
||||
if spec_title == LEGACY_UNIFIED_CREATION_TITLE
|
||||
|| (!default_title.is_empty() && spec_title == default_title)
|
||||
{
|
||||
spec.title = entry_title.to_string();
|
||||
}
|
||||
spec
|
||||
}
|
||||
|
||||
pub fn validate_unified_creation_spec_response(
|
||||
spec: &UnifiedCreationSpecResponse,
|
||||
) -> Result<(), String> {
|
||||
@@ -339,27 +309,10 @@ pub fn resolve_unified_creation_spec_response(
|
||||
play_id: &str,
|
||||
value: Option<&str>,
|
||||
) -> Option<UnifiedCreationSpecResponse> {
|
||||
resolve_unified_creation_spec_response_with_entry_title(
|
||||
play_id,
|
||||
default_unified_creation_title(play_id).unwrap_or_default(),
|
||||
value,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn resolve_unified_creation_spec_response_with_entry_title(
|
||||
play_id: &str,
|
||||
entry_title: &str,
|
||||
value: Option<&str>,
|
||||
) -> Option<UnifiedCreationSpecResponse> {
|
||||
let spec = match value {
|
||||
match value {
|
||||
Some(raw) => decode_unified_creation_spec_response(raw).ok(),
|
||||
None => build_phase1_unified_creation_spec(play_id),
|
||||
}?;
|
||||
Some(normalize_unified_creation_spec_title_for_entry(
|
||||
play_id,
|
||||
entry_title,
|
||||
spec,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn unified_creation_field(
|
||||
@@ -400,18 +353,8 @@ mod tests {
|
||||
|
||||
let jump_hop = build_phase1_unified_creation_spec("jump-hop").expect("jump-hop spec");
|
||||
assert_eq!(jump_hop.title, "跳一跳");
|
||||
assert!(
|
||||
jump_hop
|
||||
.fields
|
||||
.iter()
|
||||
.any(|field| field.id == "stylePreset")
|
||||
);
|
||||
assert!(
|
||||
jump_hop
|
||||
.fields
|
||||
.iter()
|
||||
.any(|field| field.id == "endMoodPrompt")
|
||||
);
|
||||
assert_eq!(jump_hop.fields.len(), 1);
|
||||
assert_eq!(jump_hop.fields[0].id, "themeText");
|
||||
|
||||
let wooden_fish =
|
||||
build_phase1_unified_creation_spec("wooden-fish").expect("wooden-fish spec");
|
||||
@@ -438,7 +381,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unified_creation_spec_title_can_fallback_to_entry_title() {
|
||||
fn unified_creation_spec_title_uses_contract_content() {
|
||||
let raw = r#"{
|
||||
"playId": "puzzle",
|
||||
"title": "想做个什么玩法?",
|
||||
@@ -455,25 +398,10 @@ mod tests {
|
||||
]
|
||||
}"#;
|
||||
|
||||
let spec = resolve_unified_creation_spec_response_with_entry_title(
|
||||
"puzzle",
|
||||
"定制拼图",
|
||||
Some(raw),
|
||||
)
|
||||
.expect("puzzle spec");
|
||||
let spec =
|
||||
resolve_unified_creation_spec_response("puzzle", Some(raw)).expect("puzzle spec");
|
||||
|
||||
assert_eq!(spec.title, "定制拼图");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unified_creation_spec_title_keeps_admin_custom_copy() {
|
||||
let mut spec = build_phase1_unified_creation_spec("jump-hop").expect("jump-hop spec");
|
||||
spec.title = "你的跳一跳是...".to_string();
|
||||
|
||||
let normalized =
|
||||
normalize_unified_creation_spec_title_for_entry("jump-hop", "跳一跳", spec);
|
||||
|
||||
assert_eq!(normalized.title, "你的跳一跳是...");
|
||||
assert_eq!(spec.title, "想做个什么玩法?");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -122,8 +122,7 @@ fn upsert_creation_entry_type_config_in_tx(
|
||||
if input.title.trim().is_empty() {
|
||||
return Err("入口标题不能为空".to_string());
|
||||
}
|
||||
let unified_creation_spec_json =
|
||||
normalize_unified_creation_spec_json(&id, input.title.trim(), &input)?;
|
||||
let unified_creation_spec_json = normalize_unified_creation_spec_json(&id, &input)?;
|
||||
let row = CreationEntryTypeConfig {
|
||||
id: id.clone(),
|
||||
title: input.title.trim().to_string(),
|
||||
@@ -298,7 +297,6 @@ fn seed_creation_entry_config_if_missing(ctx: &ReducerContext) {
|
||||
migrate_baby_object_match_entry_from_old_coming_soon_default(ctx, now);
|
||||
migrate_wooden_fish_entry_from_old_puzzle_image_default(ctx, now);
|
||||
migrate_jump_hop_entry_from_old_puzzle_default(ctx, now);
|
||||
migrate_unified_creation_titles_to_entry_defaults(ctx, now);
|
||||
}
|
||||
|
||||
fn migrate_rpg_entry_from_old_hidden_default(ctx: &ReducerContext, now: Timestamp) {
|
||||
@@ -479,51 +477,6 @@ fn migrate_jump_hop_entry_from_old_puzzle_default(ctx: &ReducerContext, now: Tim
|
||||
});
|
||||
}
|
||||
|
||||
fn migrate_unified_creation_titles_to_entry_defaults(ctx: &ReducerContext, now: Timestamp) {
|
||||
let rows = ctx
|
||||
.db
|
||||
.creation_entry_type_config()
|
||||
.iter()
|
||||
.collect::<Vec<_>>();
|
||||
for row in rows {
|
||||
let Some(raw_spec_json) = row.unified_creation_spec_json.as_deref() else {
|
||||
continue;
|
||||
};
|
||||
let Ok(spec) =
|
||||
shared_contracts::creation_entry_config::decode_unified_creation_spec_response(
|
||||
raw_spec_json,
|
||||
)
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let normalized =
|
||||
shared_contracts::creation_entry_config::normalize_unified_creation_spec_title_for_entry(
|
||||
&row.id,
|
||||
&row.title,
|
||||
spec.clone(),
|
||||
);
|
||||
if normalized.title == spec.title {
|
||||
continue;
|
||||
}
|
||||
let Ok(unified_creation_spec_json) =
|
||||
shared_contracts::creation_entry_config::encode_unified_creation_spec_response(
|
||||
&normalized,
|
||||
)
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
|
||||
ctx.db
|
||||
.creation_entry_type_config()
|
||||
.id()
|
||||
.update(CreationEntryTypeConfig {
|
||||
unified_creation_spec_json: Some(unified_creation_spec_json),
|
||||
updated_at: now,
|
||||
..row
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn default_creation_entry_type_configs(now: Timestamp) -> Vec<CreationEntryTypeConfig> {
|
||||
module_runtime::default_creation_entry_type_snapshots(now.to_micros_since_unix_epoch())
|
||||
.into_iter()
|
||||
@@ -547,7 +500,6 @@ fn default_creation_entry_type_configs(now: Timestamp) -> Vec<CreationEntryTypeC
|
||||
|
||||
fn normalize_unified_creation_spec_json(
|
||||
id: &str,
|
||||
entry_title: &str,
|
||||
input: &CreationEntryTypeAdminUpsertInput,
|
||||
) -> Result<Option<String>, String> {
|
||||
let Some(spec_json) = input.unified_creation_spec_json.as_deref() else {
|
||||
@@ -560,12 +512,6 @@ fn normalize_unified_creation_spec_json(
|
||||
|
||||
let spec =
|
||||
shared_contracts::creation_entry_config::decode_unified_creation_spec_response(normalized)?;
|
||||
let spec =
|
||||
shared_contracts::creation_entry_config::normalize_unified_creation_spec_title_for_entry(
|
||||
id,
|
||||
entry_title,
|
||||
spec,
|
||||
);
|
||||
shared_contracts::creation_entry_config::validate_unified_creation_spec_for_play(id, &spec)?;
|
||||
shared_contracts::creation_entry_config::encode_unified_creation_spec_response(&spec).map(Some)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user