Merge branch 'master' of http://82.157.175.59:3000/GenarrativeAI/Genarrative
Some checks failed
CI / verify (push) Has been cancelled
Some checks failed
CI / verify (push) Has been cancelled
This commit is contained in:
81
server-rs/crates/api-server/src/asset_billing.rs
Normal file
81
server-rs/crates/api-server/src/asset_billing.rs
Normal file
@@ -0,0 +1,81 @@
|
||||
use axum::http::StatusCode;
|
||||
use serde_json::json;
|
||||
use spacetime_client::SpacetimeClientError;
|
||||
|
||||
use crate::{http_error::AppError, state::AppState};
|
||||
|
||||
pub(crate) const ASSET_OPERATION_POINTS_COST: u64 = 1;
|
||||
|
||||
/// 资产操作统一预扣叙世币;扣费流水 ID 由业务资源 ID 参与构造,保证重试幂等。
|
||||
pub(crate) async fn consume_asset_operation_points(
|
||||
state: &AppState,
|
||||
owner_user_id: &str,
|
||||
asset_kind: &str,
|
||||
asset_id: &str,
|
||||
) -> Result<(), AppError> {
|
||||
let ledger_id = format!(
|
||||
"asset_generation_consume:{}:{}:{}",
|
||||
owner_user_id, asset_kind, asset_id
|
||||
);
|
||||
state
|
||||
.spacetime_client()
|
||||
.consume_profile_wallet_points(
|
||||
owner_user_id.to_string(),
|
||||
ASSET_OPERATION_POINTS_COST,
|
||||
ledger_id,
|
||||
current_utc_micros(),
|
||||
)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(map_asset_operation_wallet_error)
|
||||
}
|
||||
|
||||
/// 外部生成或发布 mutation 失败后补偿退款;退款失败只记日志,避免覆盖原始业务错误。
|
||||
pub(crate) async fn refund_asset_operation_points(
|
||||
state: &AppState,
|
||||
owner_user_id: &str,
|
||||
asset_kind: &str,
|
||||
asset_id: &str,
|
||||
) {
|
||||
let ledger_id = format!(
|
||||
"asset_generation_refund:{}:{}:{}",
|
||||
owner_user_id, asset_kind, asset_id
|
||||
);
|
||||
if let Err(error) = state
|
||||
.spacetime_client()
|
||||
.refund_profile_wallet_points(
|
||||
owner_user_id.to_string(),
|
||||
ASSET_OPERATION_POINTS_COST,
|
||||
ledger_id,
|
||||
current_utc_micros(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
tracing::error!(
|
||||
owner_user_id,
|
||||
asset_kind,
|
||||
asset_id,
|
||||
error = %error,
|
||||
"资产操作失败后的叙世币退款失败"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn map_asset_operation_wallet_error(error: SpacetimeClientError) -> AppError {
|
||||
let status = match &error {
|
||||
SpacetimeClientError::Runtime(_) => StatusCode::BAD_REQUEST,
|
||||
SpacetimeClientError::Procedure(message) if message.contains("叙世币余额不足") => {
|
||||
StatusCode::CONFLICT
|
||||
}
|
||||
_ => StatusCode::BAD_GATEWAY,
|
||||
};
|
||||
|
||||
AppError::from_status(status).with_details(json!({
|
||||
"provider": "profile-wallet",
|
||||
"message": error.to_string(),
|
||||
}))
|
||||
}
|
||||
|
||||
fn current_utc_micros() -> i64 {
|
||||
time::OffsetDateTime::now_utc().unix_timestamp_nanos() as i64 / 1_000
|
||||
}
|
||||
@@ -46,6 +46,7 @@ use crate::{
|
||||
AiGenerationDraftContext, AiGenerationDraftSink, AiGenerationDraftWriter,
|
||||
},
|
||||
api_response::json_success_body,
|
||||
asset_billing::{consume_asset_operation_points, refund_asset_operation_points},
|
||||
auth::AuthenticatedAccessToken,
|
||||
http_error::AppError,
|
||||
request_context::RequestContext,
|
||||
@@ -471,9 +472,30 @@ pub async fn execute_big_fish_action(
|
||||
|
||||
let owner_user_id = authenticated.claims().user_id().to_string();
|
||||
let now = current_utc_micros();
|
||||
let session = match payload.action.trim() {
|
||||
let action = payload.action.trim().to_string();
|
||||
let billed_asset_kind = match action.as_str() {
|
||||
"big_fish_generate_level_main_image" => Some("big_fish_level_main_image"),
|
||||
"big_fish_generate_level_motion" => Some("big_fish_level_motion"),
|
||||
"big_fish_generate_stage_background" => Some("big_fish_stage_background"),
|
||||
"big_fish_publish_game" => Some("big_fish_publish_game"),
|
||||
_ => None,
|
||||
};
|
||||
let billing_asset_id = format!("{session_id}:{now}");
|
||||
if let Some(asset_kind) = billed_asset_kind {
|
||||
consume_asset_operation_points(&state, &owner_user_id, asset_kind, &billing_asset_id)
|
||||
.await
|
||||
.map_err(|error| big_fish_error_response(&request_context, error))?;
|
||||
}
|
||||
|
||||
let session_result = match action.as_str() {
|
||||
"big_fish_compile_draft" => {
|
||||
compile_big_fish_draft_with_all_assets(&state, session_id, owner_user_id, now).await
|
||||
compile_big_fish_draft_with_all_assets(
|
||||
&state,
|
||||
session_id.clone(),
|
||||
owner_user_id.clone(),
|
||||
now,
|
||||
)
|
||||
.await
|
||||
}
|
||||
"big_fish_generate_level_main_image" => {
|
||||
let asset_url = generate_big_fish_formal_asset(
|
||||
@@ -486,12 +508,30 @@ pub async fn execute_big_fish_action(
|
||||
now,
|
||||
)
|
||||
.await
|
||||
.map_err(|error| big_fish_error_response(&request_context, error))?;
|
||||
.map_err(|error| {
|
||||
if let Some(asset_kind) = billed_asset_kind {
|
||||
tokio::spawn({
|
||||
let state = state.clone();
|
||||
let owner_user_id = owner_user_id.clone();
|
||||
let billing_asset_id = billing_asset_id.clone();
|
||||
async move {
|
||||
refund_asset_operation_points(
|
||||
&state,
|
||||
&owner_user_id,
|
||||
asset_kind,
|
||||
&billing_asset_id,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
});
|
||||
}
|
||||
big_fish_error_response(&request_context, error)
|
||||
})?;
|
||||
state
|
||||
.spacetime_client()
|
||||
.generate_big_fish_asset(BigFishAssetGenerateRecordInput {
|
||||
session_id,
|
||||
owner_user_id,
|
||||
owner_user_id: owner_user_id.clone(),
|
||||
session_id: session_id.clone(),
|
||||
asset_kind: "level_main_image".to_string(),
|
||||
level: payload.level,
|
||||
motion_key: None,
|
||||
@@ -511,12 +551,30 @@ pub async fn execute_big_fish_action(
|
||||
now,
|
||||
)
|
||||
.await
|
||||
.map_err(|error| big_fish_error_response(&request_context, error))?;
|
||||
.map_err(|error| {
|
||||
if let Some(asset_kind) = billed_asset_kind {
|
||||
tokio::spawn({
|
||||
let state = state.clone();
|
||||
let owner_user_id = owner_user_id.clone();
|
||||
let billing_asset_id = billing_asset_id.clone();
|
||||
async move {
|
||||
refund_asset_operation_points(
|
||||
&state,
|
||||
&owner_user_id,
|
||||
asset_kind,
|
||||
&billing_asset_id,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
});
|
||||
}
|
||||
big_fish_error_response(&request_context, error)
|
||||
})?;
|
||||
state
|
||||
.spacetime_client()
|
||||
.generate_big_fish_asset(BigFishAssetGenerateRecordInput {
|
||||
session_id,
|
||||
owner_user_id,
|
||||
owner_user_id: owner_user_id.clone(),
|
||||
session_id: session_id.clone(),
|
||||
asset_kind: "level_motion".to_string(),
|
||||
level: payload.level,
|
||||
motion_key: payload.motion_key,
|
||||
@@ -536,12 +594,30 @@ pub async fn execute_big_fish_action(
|
||||
now,
|
||||
)
|
||||
.await
|
||||
.map_err(|error| big_fish_error_response(&request_context, error))?;
|
||||
.map_err(|error| {
|
||||
if let Some(asset_kind) = billed_asset_kind {
|
||||
tokio::spawn({
|
||||
let state = state.clone();
|
||||
let owner_user_id = owner_user_id.clone();
|
||||
let billing_asset_id = billing_asset_id.clone();
|
||||
async move {
|
||||
refund_asset_operation_points(
|
||||
&state,
|
||||
&owner_user_id,
|
||||
asset_kind,
|
||||
&billing_asset_id,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
});
|
||||
}
|
||||
big_fish_error_response(&request_context, error)
|
||||
})?;
|
||||
state
|
||||
.spacetime_client()
|
||||
.generate_big_fish_asset(BigFishAssetGenerateRecordInput {
|
||||
session_id,
|
||||
owner_user_id,
|
||||
owner_user_id: owner_user_id.clone(),
|
||||
session_id: session_id.clone(),
|
||||
asset_kind: "stage_background".to_string(),
|
||||
level: None,
|
||||
motion_key: None,
|
||||
@@ -553,7 +629,7 @@ pub async fn execute_big_fish_action(
|
||||
"big_fish_publish_game" => {
|
||||
state
|
||||
.spacetime_client()
|
||||
.publish_big_fish_game(session_id, owner_user_id, now)
|
||||
.publish_big_fish_game(session_id, owner_user_id.clone(), now)
|
||||
.await
|
||||
}
|
||||
other => {
|
||||
@@ -562,8 +638,25 @@ pub async fn execute_big_fish_action(
|
||||
format!("action `{other}` is not supported").as_str(),
|
||||
));
|
||||
}
|
||||
}
|
||||
.map_err(|error| big_fish_error_response(&request_context, map_big_fish_client_error(error)))?;
|
||||
};
|
||||
let session = match session_result {
|
||||
Ok(session) => session,
|
||||
Err(error) => {
|
||||
if let Some(asset_kind) = billed_asset_kind {
|
||||
refund_asset_operation_points(
|
||||
&state,
|
||||
&owner_user_id,
|
||||
asset_kind,
|
||||
&billing_asset_id,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
return Err(big_fish_error_response(
|
||||
&request_context,
|
||||
map_big_fish_client_error(error),
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
|
||||
@@ -51,6 +51,7 @@ use crate::{
|
||||
AiGenerationDraftContext, AiGenerationDraftSink, AiGenerationDraftWriter,
|
||||
},
|
||||
api_response::json_success_body,
|
||||
asset_billing::{consume_asset_operation_points, refund_asset_operation_points},
|
||||
auth::AuthenticatedAccessToken,
|
||||
character_visual_assets::generate_character_primary_visual_for_profile,
|
||||
custom_world_agent_entities::generate_custom_world_agent_entities,
|
||||
@@ -350,20 +351,37 @@ pub async fn publish_custom_world_library_profile(
|
||||
));
|
||||
}
|
||||
|
||||
let mutation = state
|
||||
consume_asset_operation_points(&state, &owner_user_id, "custom_world_publish", &profile_id)
|
||||
.await
|
||||
.map_err(|error| custom_world_error_response(&request_context, error))?;
|
||||
|
||||
let mutation_result = state
|
||||
.spacetime_client()
|
||||
.publish_custom_world_profile(
|
||||
profile_id,
|
||||
owner_user_id,
|
||||
profile_id.clone(),
|
||||
owner_user_id.clone(),
|
||||
None,
|
||||
resolve_author_public_user_code(&state, &authenticated, &request_context)?,
|
||||
resolve_author_display_name(&state, &authenticated),
|
||||
current_utc_micros(),
|
||||
)
|
||||
.await
|
||||
.map_err(|error| {
|
||||
custom_world_error_response(&request_context, map_custom_world_client_error(error))
|
||||
})?;
|
||||
.await;
|
||||
let mutation = match mutation_result {
|
||||
Ok(mutation) => mutation,
|
||||
Err(error) => {
|
||||
refund_asset_operation_points(
|
||||
&state,
|
||||
&owner_user_id,
|
||||
"custom_world_publish",
|
||||
&profile_id,
|
||||
)
|
||||
.await;
|
||||
return Err(custom_world_error_response(
|
||||
&request_context,
|
||||
map_custom_world_client_error(error),
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
@@ -1227,7 +1245,19 @@ pub async fn execute_custom_world_agent_action(
|
||||
})?
|
||||
};
|
||||
|
||||
let result = state
|
||||
let should_bill_publish = action == "publish_world";
|
||||
if should_bill_publish {
|
||||
consume_asset_operation_points(
|
||||
&state,
|
||||
&owner_user_id,
|
||||
"custom_world_agent_publish",
|
||||
&session_id,
|
||||
)
|
||||
.await
|
||||
.map_err(|error| custom_world_error_response(&request_context, error))?;
|
||||
}
|
||||
|
||||
let result = match state
|
||||
.spacetime_client()
|
||||
.execute_custom_world_agent_action(CustomWorldAgentActionExecuteRecordInput {
|
||||
session_id: session_id.clone(),
|
||||
@@ -1238,9 +1268,24 @@ pub async fn execute_custom_world_agent_action(
|
||||
submitted_at_micros,
|
||||
})
|
||||
.await
|
||||
.map_err(|error| {
|
||||
custom_world_error_response(&request_context, map_custom_world_client_error(error))
|
||||
})?;
|
||||
{
|
||||
Ok(result) => result,
|
||||
Err(error) => {
|
||||
if should_bill_publish {
|
||||
refund_asset_operation_points(
|
||||
&state,
|
||||
&owner_user_id,
|
||||
"custom_world_agent_publish",
|
||||
&session_id,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
return Err(custom_world_error_response(
|
||||
&request_context,
|
||||
map_custom_world_client_error(error),
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
if matches!(
|
||||
action.as_str(),
|
||||
|
||||
@@ -440,108 +440,127 @@ pub async fn generate_custom_world_scene_image(
|
||||
let owner_user_id = authenticated.claims().user_id().to_string();
|
||||
let normalized = normalize_scene_image_request(payload)
|
||||
.map_err(|error| custom_world_ai_error_response(&request_context, error))?;
|
||||
let settings = require_dashscope_settings(&state)
|
||||
.map_err(|error| custom_world_ai_error_response(&request_context, error))?;
|
||||
let http_client = build_dashscope_http_client(&settings)
|
||||
.map_err(|error| custom_world_ai_error_response(&request_context, error))?;
|
||||
let reference_image =
|
||||
if let Some(reference_image_src) = normalized.reference_image_src.as_deref() {
|
||||
Some(
|
||||
resolve_reference_image_as_data_url(
|
||||
&state,
|
||||
&http_client,
|
||||
reference_image_src,
|
||||
"referenceImageSrc",
|
||||
let asset_id = format!("custom-scene-{}", current_utc_millis());
|
||||
crate::asset_billing::consume_asset_operation_points(
|
||||
&state,
|
||||
&owner_user_id,
|
||||
"scene_image",
|
||||
asset_id.as_str(),
|
||||
)
|
||||
.await
|
||||
.map_err(|error| custom_world_ai_error_response(&request_context, error))?;
|
||||
let asset_result = async {
|
||||
let settings = require_dashscope_settings(&state)?;
|
||||
let http_client = build_dashscope_http_client(&settings)?;
|
||||
let reference_image =
|
||||
if let Some(reference_image_src) = normalized.reference_image_src.as_deref() {
|
||||
Some(
|
||||
resolve_reference_image_as_data_url(
|
||||
&state,
|
||||
&http_client,
|
||||
reference_image_src,
|
||||
"referenceImageSrc",
|
||||
)
|
||||
.await?,
|
||||
)
|
||||
.await
|
||||
.map_err(|error| custom_world_ai_error_response(&request_context, error))?,
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let generated = if let Some(reference_image) = reference_image.as_deref() {
|
||||
create_reference_image_generation(
|
||||
&http_client,
|
||||
&settings,
|
||||
state.config.dashscope_reference_image_model.as_str(),
|
||||
normalized.prompt.as_str(),
|
||||
normalized.size.as_str(),
|
||||
&[reference_image.to_string()],
|
||||
Some(normalized.negative_prompt.as_str()),
|
||||
"创建参考图场景编辑任务失败",
|
||||
"参考图场景编辑未返回图片地址",
|
||||
"scene-edit",
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
None
|
||||
create_text_to_image_generation(
|
||||
&http_client,
|
||||
&settings,
|
||||
state.config.dashscope_scene_image_model.as_str(),
|
||||
normalized.prompt.as_str(),
|
||||
Some(normalized.negative_prompt.as_str()),
|
||||
normalized.size.as_str(),
|
||||
"创建场景图片生成任务失败",
|
||||
"查询场景图片任务失败",
|
||||
"场景图片生成任务失败",
|
||||
"场景图片生成超时或未返回图片地址",
|
||||
)
|
||||
.await
|
||||
}?;
|
||||
let scene_model = if reference_image.is_some() {
|
||||
state.config.dashscope_reference_image_model.clone()
|
||||
} else {
|
||||
state.config.dashscope_scene_image_model.clone()
|
||||
};
|
||||
let generated = if let Some(reference_image) = reference_image.as_deref() {
|
||||
create_reference_image_generation(
|
||||
let downloaded = download_remote_image(
|
||||
&http_client,
|
||||
&settings,
|
||||
state.config.dashscope_reference_image_model.as_str(),
|
||||
normalized.prompt.as_str(),
|
||||
normalized.size.as_str(),
|
||||
&[reference_image.to_string()],
|
||||
Some(normalized.negative_prompt.as_str()),
|
||||
"创建参考图场景编辑任务失败",
|
||||
"参考图场景编辑未返回图片地址",
|
||||
"scene-edit",
|
||||
generated.image_url.as_str(),
|
||||
"下载生成图片失败",
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
create_text_to_image_generation(
|
||||
&http_client,
|
||||
&settings,
|
||||
state.config.dashscope_scene_image_model.as_str(),
|
||||
normalized.prompt.as_str(),
|
||||
Some(normalized.negative_prompt.as_str()),
|
||||
normalized.size.as_str(),
|
||||
"创建场景图片生成任务失败",
|
||||
"查询场景图片任务失败",
|
||||
"场景图片生成任务失败",
|
||||
"场景图片生成超时或未返回图片地址",
|
||||
.await?;
|
||||
let upload = PreparedAssetUpload {
|
||||
prefix: LegacyAssetPrefix::CustomWorldScenes,
|
||||
path_segments: vec![
|
||||
sanitize_storage_segment(
|
||||
normalized
|
||||
.profile_id
|
||||
.as_deref()
|
||||
.unwrap_or(normalized.world_name.as_str()),
|
||||
"world",
|
||||
),
|
||||
sanitize_storage_segment(normalized.entity_id.as_str(), "scene"),
|
||||
asset_id.clone(),
|
||||
],
|
||||
file_name: format!("scene.{}", downloaded.extension),
|
||||
content_type: downloaded.mime_type,
|
||||
body: downloaded.bytes,
|
||||
asset_kind: "scene_image",
|
||||
entity_kind: "custom_world_landmark",
|
||||
entity_id: normalized.entity_id.clone(),
|
||||
profile_id: normalized.profile_id.clone(),
|
||||
slot: "scene_image",
|
||||
source_job_id: Some(generated.task_id.clone()),
|
||||
};
|
||||
persist_custom_world_asset(
|
||||
&state,
|
||||
&owner_user_id,
|
||||
upload,
|
||||
GeneratedAssetResponse {
|
||||
image_src: String::new(),
|
||||
asset_id: asset_id.clone(),
|
||||
source_type: "generated".to_string(),
|
||||
model: Some(scene_model),
|
||||
size: Some(normalized.size),
|
||||
task_id: Some(generated.task_id),
|
||||
prompt: Some(normalized.prompt),
|
||||
actual_prompt: generated.actual_prompt,
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
.map_err(|error| custom_world_ai_error_response(&request_context, error))?;
|
||||
let scene_model = if reference_image.is_some() {
|
||||
state.config.dashscope_reference_image_model.clone()
|
||||
} else {
|
||||
state.config.dashscope_scene_image_model.clone()
|
||||
.await;
|
||||
|
||||
let asset = match asset_result {
|
||||
Ok(asset) => asset,
|
||||
Err(error) => {
|
||||
crate::asset_billing::refund_asset_operation_points(
|
||||
&state,
|
||||
&owner_user_id,
|
||||
"scene_image",
|
||||
&asset_id,
|
||||
)
|
||||
.await;
|
||||
return Err(custom_world_ai_error_response(&request_context, error));
|
||||
}
|
||||
};
|
||||
let downloaded = download_remote_image(
|
||||
&http_client,
|
||||
generated.image_url.as_str(),
|
||||
"下载生成图片失败",
|
||||
)
|
||||
.await
|
||||
.map_err(|error| custom_world_ai_error_response(&request_context, error))?;
|
||||
let asset_id = format!("custom-scene-{}", current_utc_millis());
|
||||
let upload = PreparedAssetUpload {
|
||||
prefix: LegacyAssetPrefix::CustomWorldScenes,
|
||||
path_segments: vec![
|
||||
sanitize_storage_segment(
|
||||
normalized
|
||||
.profile_id
|
||||
.as_deref()
|
||||
.unwrap_or(normalized.world_name.as_str()),
|
||||
"world",
|
||||
),
|
||||
sanitize_storage_segment(normalized.entity_id.as_str(), "scene"),
|
||||
asset_id.clone(),
|
||||
],
|
||||
file_name: format!("scene.{}", downloaded.extension),
|
||||
content_type: downloaded.mime_type,
|
||||
body: downloaded.bytes,
|
||||
asset_kind: "scene_image",
|
||||
entity_kind: "custom_world_landmark",
|
||||
entity_id: normalized.entity_id.clone(),
|
||||
profile_id: normalized.profile_id.clone(),
|
||||
slot: "scene_image",
|
||||
source_job_id: Some(generated.task_id.clone()),
|
||||
};
|
||||
let asset = persist_custom_world_asset(
|
||||
&state,
|
||||
&owner_user_id,
|
||||
upload,
|
||||
GeneratedAssetResponse {
|
||||
image_src: String::new(),
|
||||
asset_id: asset_id.clone(),
|
||||
source_type: "generated".to_string(),
|
||||
model: Some(scene_model),
|
||||
size: Some(normalized.size),
|
||||
task_id: Some(generated.task_id),
|
||||
prompt: Some(normalized.prompt),
|
||||
actual_prompt: generated.actual_prompt,
|
||||
},
|
||||
)
|
||||
.await
|
||||
.map_err(|error| custom_world_ai_error_response(&request_context, error))?;
|
||||
|
||||
Ok(json_success_body(Some(&request_context), asset))
|
||||
}
|
||||
@@ -697,109 +716,128 @@ pub async fn generate_custom_world_cover_image(
|
||||
trim_to_option(payload.profile.name.as_deref()).unwrap_or_else(|| "world".to_string());
|
||||
let entity_id = profile_id.clone().unwrap_or_else(|| world_name.clone());
|
||||
let size = trim_to_option(payload.size.as_deref()).unwrap_or_else(|| "1600*900".to_string());
|
||||
let settings = require_dashscope_settings(&state)
|
||||
.map_err(|error| custom_world_ai_error_response(&request_context, error))?;
|
||||
let http_client = build_dashscope_http_client(&settings)
|
||||
.map_err(|error| custom_world_ai_error_response(&request_context, error))?;
|
||||
let reference_sources = collect_cover_reference_image_sources(
|
||||
&payload.profile,
|
||||
&payload.character_role_ids,
|
||||
payload.reference_image_src.as_deref().unwrap_or_default(),
|
||||
);
|
||||
let prompt = build_custom_world_cover_image_prompt(
|
||||
&payload.profile,
|
||||
&payload.character_role_ids,
|
||||
payload.user_prompt.as_deref().unwrap_or_default(),
|
||||
!reference_sources.is_empty(),
|
||||
);
|
||||
let mut reference_images = Vec::with_capacity(reference_sources.len());
|
||||
for source in &reference_sources {
|
||||
reference_images.push(
|
||||
resolve_reference_image_as_data_url(
|
||||
&state,
|
||||
&http_client,
|
||||
source.as_str(),
|
||||
"referenceImageSrc",
|
||||
)
|
||||
.await
|
||||
.map_err(|error| custom_world_ai_error_response(&request_context, error))?,
|
||||
);
|
||||
}
|
||||
let generated = if reference_images.is_empty() {
|
||||
create_text_to_image_generation(
|
||||
&http_client,
|
||||
&settings,
|
||||
state.config.dashscope_cover_image_model.as_str(),
|
||||
prompt.as_str(),
|
||||
None,
|
||||
size.as_str(),
|
||||
"创建作品封面生成任务失败",
|
||||
"查询作品封面任务失败",
|
||||
"作品封面生成任务失败",
|
||||
"作品封面生成超时或未返回图片地址",
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
create_reference_image_generation(
|
||||
&http_client,
|
||||
&settings,
|
||||
state.config.dashscope_reference_image_model.as_str(),
|
||||
prompt.as_str(),
|
||||
size.as_str(),
|
||||
&reference_images,
|
||||
None,
|
||||
"创建参考图封面任务失败",
|
||||
"封面生成未返回图片地址",
|
||||
"cover-edit",
|
||||
)
|
||||
.await
|
||||
}
|
||||
.map_err(|error| custom_world_ai_error_response(&request_context, error))?;
|
||||
let downloaded = download_remote_image(
|
||||
&http_client,
|
||||
generated.image_url.as_str(),
|
||||
"下载作品封面失败",
|
||||
)
|
||||
.await
|
||||
.map_err(|error| custom_world_ai_error_response(&request_context, error))?;
|
||||
let asset_id = format!("custom-cover-{}", current_utc_millis());
|
||||
let upload = PreparedAssetUpload {
|
||||
prefix: LegacyAssetPrefix::CustomWorldCovers,
|
||||
path_segments: vec![
|
||||
sanitize_storage_segment(entity_id.as_str(), "world"),
|
||||
asset_id.clone(),
|
||||
],
|
||||
file_name: format!("cover.{}", downloaded.extension),
|
||||
content_type: downloaded.mime_type,
|
||||
body: downloaded.bytes,
|
||||
asset_kind: "custom_world_cover",
|
||||
entity_kind: "custom_world_profile",
|
||||
entity_id,
|
||||
profile_id,
|
||||
slot: "cover",
|
||||
source_job_id: Some(generated.task_id.clone()),
|
||||
};
|
||||
let asset = persist_custom_world_asset(
|
||||
crate::asset_billing::consume_asset_operation_points(
|
||||
&state,
|
||||
&owner_user_id,
|
||||
upload,
|
||||
GeneratedAssetResponse {
|
||||
image_src: String::new(),
|
||||
asset_id: asset_id.clone(),
|
||||
source_type: "generated".to_string(),
|
||||
model: Some(if reference_images.is_empty() {
|
||||
state.config.dashscope_cover_image_model.clone()
|
||||
} else {
|
||||
state.config.dashscope_reference_image_model.clone()
|
||||
}),
|
||||
size: Some(size),
|
||||
task_id: Some(generated.task_id),
|
||||
prompt: Some(prompt),
|
||||
actual_prompt: generated.actual_prompt,
|
||||
},
|
||||
"custom_world_cover",
|
||||
asset_id.as_str(),
|
||||
)
|
||||
.await
|
||||
.map_err(|error| custom_world_ai_error_response(&request_context, error))?;
|
||||
let asset_result = async {
|
||||
let settings = require_dashscope_settings(&state)?;
|
||||
let http_client = build_dashscope_http_client(&settings)?;
|
||||
let reference_sources = collect_cover_reference_image_sources(
|
||||
&payload.profile,
|
||||
&payload.character_role_ids,
|
||||
payload.reference_image_src.as_deref().unwrap_or_default(),
|
||||
);
|
||||
let prompt = build_custom_world_cover_image_prompt(
|
||||
&payload.profile,
|
||||
&payload.character_role_ids,
|
||||
payload.user_prompt.as_deref().unwrap_or_default(),
|
||||
!reference_sources.is_empty(),
|
||||
);
|
||||
let mut reference_images = Vec::with_capacity(reference_sources.len());
|
||||
for source in &reference_sources {
|
||||
reference_images.push(
|
||||
resolve_reference_image_as_data_url(
|
||||
&state,
|
||||
&http_client,
|
||||
source.as_str(),
|
||||
"referenceImageSrc",
|
||||
)
|
||||
.await?,
|
||||
);
|
||||
}
|
||||
let generated = if reference_images.is_empty() {
|
||||
create_text_to_image_generation(
|
||||
&http_client,
|
||||
&settings,
|
||||
state.config.dashscope_cover_image_model.clone().as_str(),
|
||||
prompt.as_str(),
|
||||
None,
|
||||
size.as_str(),
|
||||
"创建作品封面生成任务失败",
|
||||
"查询作品封面任务失败",
|
||||
"作品封面生成任务失败",
|
||||
"作品封面生成超时或未返回图片地址",
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
create_reference_image_generation(
|
||||
&http_client,
|
||||
&settings,
|
||||
state.config.dashscope_reference_image_model.as_str(),
|
||||
prompt.as_str(),
|
||||
size.as_str(),
|
||||
&reference_images,
|
||||
None,
|
||||
"创建参考图封面任务失败",
|
||||
"封面生成未返回图片地址",
|
||||
"cover-edit",
|
||||
)
|
||||
.await
|
||||
}?;
|
||||
let downloaded = download_remote_image(
|
||||
&http_client,
|
||||
generated.image_url.as_str(),
|
||||
"下载作品封面失败",
|
||||
)
|
||||
.await?;
|
||||
let upload = PreparedAssetUpload {
|
||||
prefix: LegacyAssetPrefix::CustomWorldCovers,
|
||||
path_segments: vec![
|
||||
sanitize_storage_segment(entity_id.as_str(), "world"),
|
||||
asset_id.clone(),
|
||||
],
|
||||
file_name: format!("cover.{}", downloaded.extension),
|
||||
content_type: downloaded.mime_type,
|
||||
body: downloaded.bytes,
|
||||
asset_kind: "custom_world_cover",
|
||||
entity_kind: "custom_world_profile",
|
||||
entity_id,
|
||||
profile_id,
|
||||
slot: "cover",
|
||||
source_job_id: Some(generated.task_id.clone()),
|
||||
};
|
||||
persist_custom_world_asset(
|
||||
&state,
|
||||
&owner_user_id,
|
||||
upload,
|
||||
GeneratedAssetResponse {
|
||||
image_src: String::new(),
|
||||
asset_id: asset_id.clone(),
|
||||
source_type: "generated".to_string(),
|
||||
model: Some(if reference_images.is_empty() {
|
||||
state.config.dashscope_cover_image_model.clone()
|
||||
} else {
|
||||
state.config.dashscope_reference_image_model.clone()
|
||||
}),
|
||||
size: Some(size),
|
||||
task_id: Some(generated.task_id),
|
||||
prompt: Some(prompt),
|
||||
actual_prompt: generated.actual_prompt,
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
.await;
|
||||
|
||||
let asset = match asset_result {
|
||||
Ok(asset) => asset,
|
||||
Err(error) => {
|
||||
crate::asset_billing::refund_asset_operation_points(
|
||||
&state,
|
||||
&owner_user_id,
|
||||
"custom_world_cover",
|
||||
&asset_id,
|
||||
)
|
||||
.await;
|
||||
return Err(custom_world_ai_error_response(&request_context, error));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(json_success_body(Some(&request_context), asset))
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ mod ai_generation_drafts;
|
||||
mod ai_tasks;
|
||||
mod api_response;
|
||||
mod app;
|
||||
mod asset_billing;
|
||||
mod assets;
|
||||
mod auth;
|
||||
mod auth_me;
|
||||
|
||||
@@ -37,10 +37,10 @@ use shared_contracts::{
|
||||
puzzle_gallery::{PuzzleGalleryDetailResponse, PuzzleGalleryResponse},
|
||||
puzzle_runtime::{
|
||||
AdvanceLocalPuzzleNextLevelRequest, DragPuzzlePieceRequest, PuzzleBoardSnapshotResponse,
|
||||
PuzzleCellPositionResponse, PuzzleLeaderboardEntryResponse,
|
||||
PuzzleMergedGroupStateResponse, PuzzlePieceStateResponse, PuzzleRunResponse,
|
||||
PuzzleRunSnapshotResponse, PuzzleRuntimeLevelSnapshotResponse, StartPuzzleRunRequest,
|
||||
SubmitPuzzleLeaderboardRequest, SwapPuzzlePiecesRequest,
|
||||
PuzzleCellPositionResponse, PuzzleLeaderboardEntryResponse, PuzzleMergedGroupStateResponse,
|
||||
PuzzlePieceStateResponse, PuzzleRunResponse, PuzzleRunSnapshotResponse,
|
||||
PuzzleRuntimeLevelSnapshotResponse, StartPuzzleRunRequest, SubmitPuzzleLeaderboardRequest,
|
||||
SwapPuzzlePiecesRequest,
|
||||
},
|
||||
puzzle_works::{
|
||||
PutPuzzleWorkRequest, PuzzleWorkDetailResponse, PuzzleWorkMutationResponse,
|
||||
@@ -55,12 +55,11 @@ use spacetime_client::{
|
||||
PuzzleBoardRecord, PuzzleCellPositionRecord, PuzzleCreatorIntentRecord,
|
||||
PuzzleGeneratedImageCandidateRecord, PuzzleGeneratedImagesSaveRecordInput,
|
||||
PuzzleLeaderboardEntryRecord, PuzzleLeaderboardSubmitRecordInput, PuzzleMergedGroupRecord,
|
||||
PuzzlePieceStateRecord, PuzzlePublishRecordInput,
|
||||
PuzzleResultDraftRecord, PuzzleResultPreviewBlockerRecord, PuzzleResultPreviewFindingRecord,
|
||||
PuzzleResultPreviewRecord, PuzzleRunDragRecordInput, PuzzleRunRecord,
|
||||
PuzzleRunStartRecordInput, PuzzleRunSwapRecordInput, PuzzleRuntimeLevelRecord,
|
||||
PuzzleSelectCoverImageRecordInput, PuzzleWorkProfileRecord, PuzzleWorkUpsertRecordInput,
|
||||
SpacetimeClientError,
|
||||
PuzzlePieceStateRecord, PuzzlePublishRecordInput, PuzzleResultDraftRecord,
|
||||
PuzzleResultPreviewBlockerRecord, PuzzleResultPreviewFindingRecord, PuzzleResultPreviewRecord,
|
||||
PuzzleRunDragRecordInput, PuzzleRunRecord, PuzzleRunStartRecordInput, PuzzleRunSwapRecordInput,
|
||||
PuzzleRuntimeLevelRecord, PuzzleSelectCoverImageRecordInput, PuzzleWorkProfileRecord,
|
||||
PuzzleWorkUpsertRecordInput, SpacetimeClientError,
|
||||
};
|
||||
use std::convert::Infallible;
|
||||
use tokio::time::sleep;
|
||||
@@ -68,6 +67,7 @@ use tokio::time::sleep;
|
||||
use crate::{
|
||||
ai_generation_drafts::{AiGenerationDraftContext, AiGenerationDraftWriter},
|
||||
api_response::json_success_body,
|
||||
asset_billing::{consume_asset_operation_points, refund_asset_operation_points},
|
||||
auth::AuthenticatedAccessToken,
|
||||
http_error::AppError,
|
||||
prompt::puzzle_image::{PUZZLE_DEFAULT_NEGATIVE_PROMPT, build_puzzle_image_prompt},
|
||||
@@ -441,8 +441,22 @@ pub async fn execute_puzzle_agent_action(
|
||||
|
||||
let owner_user_id = authenticated.claims().user_id().to_string();
|
||||
let now = current_utc_micros();
|
||||
let action = payload.action.trim().to_string();
|
||||
let billed_asset_kind = match action.as_str() {
|
||||
"compile_puzzle_draft" => Some("puzzle_initial_image"),
|
||||
"generate_puzzle_images" => Some("puzzle_generated_image"),
|
||||
_ => None,
|
||||
};
|
||||
let billing_asset_id = format!("{session_id}:{now}");
|
||||
if let Some(asset_kind) = billed_asset_kind {
|
||||
consume_asset_operation_points(&state, &owner_user_id, asset_kind, &billing_asset_id)
|
||||
.await
|
||||
.map_err(|error| {
|
||||
puzzle_error_response(&request_context, PUZZLE_AGENT_API_BASE_PROVIDER, error)
|
||||
})?;
|
||||
}
|
||||
|
||||
let (operation_type, phase_label, phase_detail, session) = match payload.action.trim() {
|
||||
let (operation_type, phase_label, phase_detail, session) = match action.as_str() {
|
||||
"compile_puzzle_draft" => {
|
||||
let session = compile_puzzle_draft_with_initial_cover(
|
||||
&state,
|
||||
@@ -510,7 +524,7 @@ pub async fn execute_puzzle_agent_action(
|
||||
.save_puzzle_generated_images(
|
||||
PuzzleGeneratedImagesSaveRecordInput {
|
||||
session_id: session.session_id,
|
||||
owner_user_id,
|
||||
owner_user_id: owner_user_id.clone(),
|
||||
candidates_json,
|
||||
saved_at_micros: now,
|
||||
},
|
||||
@@ -565,13 +579,18 @@ pub async fn execute_puzzle_agent_action(
|
||||
}
|
||||
"publish_puzzle_work" => {
|
||||
let (work_id, profile_id) = build_stable_puzzle_work_ids(&session_id);
|
||||
let profile = state
|
||||
consume_asset_operation_points(&state, &owner_user_id, "puzzle_publish_work", &work_id)
|
||||
.await
|
||||
.map_err(|error| {
|
||||
puzzle_error_response(&request_context, PUZZLE_AGENT_API_BASE_PROVIDER, error)
|
||||
})?;
|
||||
let profile_result = state
|
||||
.spacetime_client()
|
||||
.publish_puzzle_work(PuzzlePublishRecordInput {
|
||||
session_id: session_id.clone(),
|
||||
owner_user_id: owner_user_id.clone(),
|
||||
// 发布沿用 session 派生的稳定作品 ID,避免草稿卡与已发布卡重复。
|
||||
work_id,
|
||||
work_id: work_id.clone(),
|
||||
profile_id,
|
||||
author_display_name: resolve_author_display_name(&state, &authenticated),
|
||||
level_name: payload.level_name.clone(),
|
||||
@@ -579,14 +598,24 @@ pub async fn execute_puzzle_agent_action(
|
||||
theme_tags: payload.theme_tags.clone(),
|
||||
published_at_micros: now,
|
||||
})
|
||||
.await
|
||||
.map_err(|error| {
|
||||
puzzle_error_response(
|
||||
.await;
|
||||
let profile = match profile_result {
|
||||
Ok(profile) => profile,
|
||||
Err(error) => {
|
||||
refund_asset_operation_points(
|
||||
&state,
|
||||
&owner_user_id,
|
||||
"puzzle_publish_work",
|
||||
&work_id,
|
||||
)
|
||||
.await;
|
||||
return Err(puzzle_error_response(
|
||||
&request_context,
|
||||
PUZZLE_AGENT_API_BASE_PROVIDER,
|
||||
map_puzzle_client_error(error),
|
||||
)
|
||||
})?;
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let session = state
|
||||
.spacetime_client()
|
||||
@@ -626,6 +655,22 @@ pub async fn execute_puzzle_agent_action(
|
||||
};
|
||||
|
||||
let session = session.map_err(|error| {
|
||||
if let Some(asset_kind) = billed_asset_kind {
|
||||
tokio::spawn({
|
||||
let state = state.clone();
|
||||
let owner_user_id = owner_user_id.clone();
|
||||
let billing_asset_id = billing_asset_id.clone();
|
||||
async move {
|
||||
refund_asset_operation_points(
|
||||
&state,
|
||||
&owner_user_id,
|
||||
asset_kind,
|
||||
&billing_asset_id,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
});
|
||||
}
|
||||
puzzle_error_response(
|
||||
&request_context,
|
||||
PUZZLE_AGENT_API_BASE_PROVIDER,
|
||||
|
||||
@@ -7,18 +7,23 @@ use axum::{
|
||||
use module_runtime::{
|
||||
PROFILE_RECHARGE_PAYMENT_CHANNEL_MOCK, RuntimeProfileMembershipBenefitRecord,
|
||||
RuntimeProfileRechargeCenterRecord, RuntimeProfileRechargeOrderRecord,
|
||||
RuntimeProfileRechargeProductRecord, RuntimeReferralInviteCenterRecord,
|
||||
RuntimeReferralRedeemRecord,
|
||||
RuntimeProfileRechargeProductRecord, RuntimeProfileWalletLedgerSourceType,
|
||||
RuntimeReferralInviteCenterRecord, RuntimeReferralRedeemRecord,
|
||||
};
|
||||
use serde_json::{Value, json};
|
||||
use shared_contracts::runtime::{
|
||||
CreateProfileRechargeOrderRequest, CreateProfileRechargeOrderResponse,
|
||||
ProfileDashboardSummaryResponse, ProfileMembershipBenefitResponse, ProfileMembershipResponse,
|
||||
ProfilePlayStatsResponse, ProfilePlayedWorkSummaryResponse, ProfileRechargeCenterResponse,
|
||||
ProfileRechargeOrderResponse, ProfileRechargeProductResponse,
|
||||
ProfileReferralInviteCenterResponse, ProfileWalletLedgerEntryResponse,
|
||||
ProfileWalletLedgerResponse, RedeemProfileReferralInviteCodeRequest,
|
||||
RedeemProfileReferralInviteCodeResponse,
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_ASSET_GENERATION_CONSUME,
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_ASSET_GENERATION_REFUND,
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_INVITE_INVITEE_REWARD,
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_INVITE_INVITER_REWARD,
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_POINTS_RECHARGE,
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_SNAPSHOT_SYNC, ProfileDashboardSummaryResponse,
|
||||
ProfileMembershipBenefitResponse, ProfileMembershipResponse, ProfilePlayStatsResponse,
|
||||
ProfilePlayedWorkSummaryResponse, ProfileRechargeCenterResponse, ProfileRechargeOrderResponse,
|
||||
ProfileRechargeProductResponse, ProfileReferralInviteCenterResponse,
|
||||
ProfileWalletLedgerEntryResponse, ProfileWalletLedgerResponse,
|
||||
RedeemProfileReferralInviteCodeRequest, RedeemProfileReferralInviteCodeResponse,
|
||||
};
|
||||
use spacetime_client::SpacetimeClientError;
|
||||
use time::OffsetDateTime;
|
||||
@@ -82,7 +87,8 @@ pub async fn get_profile_wallet_ledger(
|
||||
id: entry.wallet_ledger_id,
|
||||
amount_delta: entry.amount_delta,
|
||||
balance_after: entry.balance_after,
|
||||
source_type: entry.source_type.as_str().to_string(),
|
||||
source_type: format_profile_wallet_ledger_source_type(entry.source_type)
|
||||
.to_string(),
|
||||
created_at: entry.created_at,
|
||||
})
|
||||
.collect(),
|
||||
@@ -90,6 +96,31 @@ pub async fn get_profile_wallet_ledger(
|
||||
))
|
||||
}
|
||||
|
||||
fn format_profile_wallet_ledger_source_type(
|
||||
source_type: RuntimeProfileWalletLedgerSourceType,
|
||||
) -> &'static str {
|
||||
match source_type {
|
||||
RuntimeProfileWalletLedgerSourceType::SnapshotSync => {
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_SNAPSHOT_SYNC
|
||||
}
|
||||
RuntimeProfileWalletLedgerSourceType::InviteInviterReward => {
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_INVITE_INVITER_REWARD
|
||||
}
|
||||
RuntimeProfileWalletLedgerSourceType::InviteInviteeReward => {
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_INVITE_INVITEE_REWARD
|
||||
}
|
||||
RuntimeProfileWalletLedgerSourceType::PointsRecharge => {
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_POINTS_RECHARGE
|
||||
}
|
||||
RuntimeProfileWalletLedgerSourceType::AssetGenerationConsume => {
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_ASSET_GENERATION_CONSUME
|
||||
}
|
||||
RuntimeProfileWalletLedgerSourceType::AssetGenerationRefund => {
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_ASSET_GENERATION_REFUND
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_profile_recharge_center(
|
||||
State(state): State<AppState>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
@@ -367,6 +398,10 @@ fn build_redeem_profile_referral_invite_code_response(
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use module_runtime::RuntimeProfileWalletLedgerSourceType;
|
||||
|
||||
use super::format_profile_wallet_ledger_source_type;
|
||||
|
||||
use axum::{
|
||||
body::Body,
|
||||
http::{Request, StatusCode},
|
||||
@@ -381,6 +416,22 @@ mod tests {
|
||||
|
||||
use crate::{app::build_router, config::AppConfig, state::AppState};
|
||||
|
||||
#[test]
|
||||
fn profile_wallet_ledger_source_type_formats_asset_generation_values() {
|
||||
assert_eq!(
|
||||
format_profile_wallet_ledger_source_type(
|
||||
RuntimeProfileWalletLedgerSourceType::AssetGenerationConsume
|
||||
),
|
||||
shared_contracts::runtime::PROFILE_WALLET_LEDGER_SOURCE_TYPE_ASSET_GENERATION_CONSUME
|
||||
);
|
||||
assert_eq!(
|
||||
format_profile_wallet_ledger_source_type(
|
||||
RuntimeProfileWalletLedgerSourceType::AssetGenerationRefund
|
||||
),
|
||||
shared_contracts::runtime::PROFILE_WALLET_LEDGER_SOURCE_TYPE_ASSET_GENERATION_REFUND
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn profile_dashboard_requires_authentication() {
|
||||
let app = build_router(AppState::new(AppConfig::default()).expect("state should build"));
|
||||
|
||||
@@ -259,6 +259,8 @@ pub enum RuntimeProfileWalletLedgerSourceType {
|
||||
InviteInviterReward,
|
||||
InviteInviteeReward,
|
||||
PointsRecharge,
|
||||
AssetGenerationConsume,
|
||||
AssetGenerationRefund,
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "spacetime-types", derive(SpacetimeType))]
|
||||
@@ -399,12 +401,29 @@ pub struct RuntimeProfileWalletLedgerProcedureResult {
|
||||
pub error_message: Option<String>,
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "spacetime-types", derive(SpacetimeType))]
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct RuntimeProfileWalletAdjustmentProcedureResult {
|
||||
pub ok: bool,
|
||||
pub record: Option<RuntimeProfileDashboardSnapshot>,
|
||||
pub error_message: Option<String>,
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "spacetime-types", derive(SpacetimeType))]
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct RuntimeProfileWalletLedgerListInput {
|
||||
pub user_id: String,
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "spacetime-types", derive(SpacetimeType))]
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct RuntimeProfileWalletAdjustmentInput {
|
||||
pub user_id: String,
|
||||
pub amount: u64,
|
||||
pub ledger_id: String,
|
||||
pub created_at_micros: i64,
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "spacetime-types", derive(SpacetimeType))]
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct RuntimeReferralInviteCenterSnapshot {
|
||||
@@ -515,6 +534,8 @@ pub enum RuntimeBrowseHistoryFieldError {
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum RuntimeProfileFieldError {
|
||||
MissingUserId,
|
||||
MissingLedgerId,
|
||||
InvalidWalletAmount,
|
||||
MissingInviteCode,
|
||||
MissingProductId,
|
||||
MissingWorldKey,
|
||||
@@ -877,6 +898,26 @@ pub fn build_runtime_profile_wallet_ledger_list_input(
|
||||
Ok(RuntimeProfileWalletLedgerListInput { user_id })
|
||||
}
|
||||
|
||||
pub fn build_runtime_profile_wallet_adjustment_input(
|
||||
user_id: String,
|
||||
amount: u64,
|
||||
ledger_id: String,
|
||||
created_at_micros: i64,
|
||||
) -> Result<RuntimeProfileWalletAdjustmentInput, RuntimeProfileFieldError> {
|
||||
let user_id = normalize_runtime_profile_user_id(user_id)?;
|
||||
let ledger_id =
|
||||
normalize_required_string(ledger_id).ok_or(RuntimeProfileFieldError::MissingLedgerId)?;
|
||||
if amount == 0 || amount > i64::MAX as u64 {
|
||||
return Err(RuntimeProfileFieldError::InvalidWalletAmount);
|
||||
}
|
||||
Ok(RuntimeProfileWalletAdjustmentInput {
|
||||
user_id,
|
||||
amount,
|
||||
ledger_id,
|
||||
created_at_micros,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn build_runtime_profile_recharge_center_get_input(
|
||||
user_id: String,
|
||||
) -> Result<RuntimeProfileRechargeCenterGetInput, RuntimeProfileFieldError> {
|
||||
@@ -1465,6 +1506,8 @@ impl RuntimeProfileWalletLedgerSourceType {
|
||||
Self::InviteInviterReward => "invite_inviter_reward",
|
||||
Self::InviteInviteeReward => "invite_invitee_reward",
|
||||
Self::PointsRecharge => "points_recharge",
|
||||
Self::AssetGenerationConsume => "asset_generation_consume",
|
||||
Self::AssetGenerationRefund => "asset_generation_refund",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1697,6 +1740,8 @@ impl std::fmt::Display for RuntimeProfileFieldError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::MissingUserId => f.write_str("profile.user_id 不能为空"),
|
||||
Self::MissingLedgerId => f.write_str("profile.wallet_ledger_id 不能为空"),
|
||||
Self::InvalidWalletAmount => f.write_str("profile.wallet_amount 必须大于 0"),
|
||||
Self::MissingInviteCode => f.write_str("referral.invite_code 不能为空"),
|
||||
Self::MissingProductId => f.write_str("recharge.product_id 不能为空"),
|
||||
Self::MissingWorldKey => f.write_str("profile.world_key 不能为空"),
|
||||
@@ -1962,6 +2007,14 @@ mod tests {
|
||||
RuntimeProfileWalletLedgerSourceType::PointsRecharge.as_str(),
|
||||
"points_recharge"
|
||||
);
|
||||
assert_eq!(
|
||||
RuntimeProfileWalletLedgerSourceType::AssetGenerationConsume.as_str(),
|
||||
"asset_generation_consume"
|
||||
);
|
||||
assert_eq!(
|
||||
RuntimeProfileWalletLedgerSourceType::AssetGenerationRefund.as_str(),
|
||||
"asset_generation_refund"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -7,6 +7,10 @@ pub const PROFILE_WALLET_LEDGER_SOURCE_TYPE_SNAPSHOT_SYNC: &str = "snapshot_sync
|
||||
pub const PROFILE_WALLET_LEDGER_SOURCE_TYPE_POINTS_RECHARGE: &str = "points_recharge";
|
||||
pub const PROFILE_WALLET_LEDGER_SOURCE_TYPE_INVITE_INVITER_REWARD: &str = "invite_inviter_reward";
|
||||
pub const PROFILE_WALLET_LEDGER_SOURCE_TYPE_INVITE_INVITEE_REWARD: &str = "invite_invitee_reward";
|
||||
pub const PROFILE_WALLET_LEDGER_SOURCE_TYPE_ASSET_GENERATION_CONSUME: &str =
|
||||
"asset_generation_consume";
|
||||
pub const PROFILE_WALLET_LEDGER_SOURCE_TYPE_ASSET_GENERATION_REFUND: &str =
|
||||
"asset_generation_refund";
|
||||
pub const BROWSE_HISTORY_THEME_MODE_MARTIAL: &str = "martial";
|
||||
pub const BROWSE_HISTORY_THEME_MODE_ARCANE: &str = "arcane";
|
||||
pub const BROWSE_HISTORY_THEME_MODE_MACHINA: &str = "machina";
|
||||
@@ -752,19 +756,83 @@ mod tests {
|
||||
#[test]
|
||||
fn profile_wallet_ledger_response_uses_camel_case_fields() {
|
||||
let payload = serde_json::to_value(ProfileWalletLedgerResponse {
|
||||
entries: vec![ProfileWalletLedgerEntryResponse {
|
||||
id: "ledger-1".to_string(),
|
||||
amount_delta: 12,
|
||||
balance_after: 80,
|
||||
source_type: PROFILE_WALLET_LEDGER_SOURCE_TYPE_SNAPSHOT_SYNC.to_string(),
|
||||
created_at: "2026-04-22T10:00:00Z".to_string(),
|
||||
}],
|
||||
entries: vec![
|
||||
ProfileWalletLedgerEntryResponse {
|
||||
id: "ledger-1".to_string(),
|
||||
amount_delta: 12,
|
||||
balance_after: 80,
|
||||
source_type: PROFILE_WALLET_LEDGER_SOURCE_TYPE_SNAPSHOT_SYNC.to_string(),
|
||||
created_at: "2026-04-22T10:00:00Z".to_string(),
|
||||
},
|
||||
ProfileWalletLedgerEntryResponse {
|
||||
id: "ledger-2".to_string(),
|
||||
amount_delta: 30,
|
||||
balance_after: 110,
|
||||
source_type: PROFILE_WALLET_LEDGER_SOURCE_TYPE_INVITE_INVITER_REWARD
|
||||
.to_string(),
|
||||
created_at: "2026-04-22T10:01:00Z".to_string(),
|
||||
},
|
||||
ProfileWalletLedgerEntryResponse {
|
||||
id: "ledger-3".to_string(),
|
||||
amount_delta: 30,
|
||||
balance_after: 140,
|
||||
source_type: PROFILE_WALLET_LEDGER_SOURCE_TYPE_INVITE_INVITEE_REWARD
|
||||
.to_string(),
|
||||
created_at: "2026-04-22T10:02:00Z".to_string(),
|
||||
},
|
||||
ProfileWalletLedgerEntryResponse {
|
||||
id: "ledger-4".to_string(),
|
||||
amount_delta: 60,
|
||||
balance_after: 200,
|
||||
source_type: PROFILE_WALLET_LEDGER_SOURCE_TYPE_POINTS_RECHARGE.to_string(),
|
||||
created_at: "2026-04-22T10:03:00Z".to_string(),
|
||||
},
|
||||
ProfileWalletLedgerEntryResponse {
|
||||
id: "ledger-5".to_string(),
|
||||
amount_delta: -1,
|
||||
balance_after: 199,
|
||||
source_type: PROFILE_WALLET_LEDGER_SOURCE_TYPE_ASSET_GENERATION_CONSUME
|
||||
.to_string(),
|
||||
created_at: "2026-04-22T10:04:00Z".to_string(),
|
||||
},
|
||||
ProfileWalletLedgerEntryResponse {
|
||||
id: "ledger-6".to_string(),
|
||||
amount_delta: 1,
|
||||
balance_after: 200,
|
||||
source_type: PROFILE_WALLET_LEDGER_SOURCE_TYPE_ASSET_GENERATION_REFUND
|
||||
.to_string(),
|
||||
created_at: "2026-04-22T10:05:00Z".to_string(),
|
||||
},
|
||||
],
|
||||
})
|
||||
.expect("payload should serialize");
|
||||
|
||||
assert_eq!(payload["entries"][0]["amountDelta"], json!(12));
|
||||
assert_eq!(payload["entries"][0]["balanceAfter"], json!(80));
|
||||
assert_eq!(payload["entries"][0]["sourceType"], json!("snapshot_sync"));
|
||||
assert_eq!(
|
||||
payload["entries"][0]["sourceType"],
|
||||
json!(PROFILE_WALLET_LEDGER_SOURCE_TYPE_SNAPSHOT_SYNC)
|
||||
);
|
||||
assert_eq!(
|
||||
payload["entries"][1]["sourceType"],
|
||||
json!(PROFILE_WALLET_LEDGER_SOURCE_TYPE_INVITE_INVITER_REWARD)
|
||||
);
|
||||
assert_eq!(
|
||||
payload["entries"][2]["sourceType"],
|
||||
json!(PROFILE_WALLET_LEDGER_SOURCE_TYPE_INVITE_INVITEE_REWARD)
|
||||
);
|
||||
assert_eq!(
|
||||
payload["entries"][3]["sourceType"],
|
||||
json!(PROFILE_WALLET_LEDGER_SOURCE_TYPE_POINTS_RECHARGE)
|
||||
);
|
||||
assert_eq!(
|
||||
payload["entries"][4]["sourceType"],
|
||||
json!(PROFILE_WALLET_LEDGER_SOURCE_TYPE_ASSET_GENERATION_CONSUME)
|
||||
);
|
||||
assert_eq!(
|
||||
payload["entries"][5]["sourceType"],
|
||||
json!(PROFILE_WALLET_LEDGER_SOURCE_TYPE_ASSET_GENERATION_REFUND)
|
||||
);
|
||||
assert_eq!(
|
||||
payload["entries"][0]["createdAt"],
|
||||
json!("2026-04-22T10:00:00Z")
|
||||
|
||||
@@ -250,5 +250,4 @@ impl SpacetimeClient {
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -130,7 +130,7 @@ use module_runtime::{
|
||||
build_runtime_profile_recharge_center_record,
|
||||
build_runtime_profile_recharge_order_create_input,
|
||||
build_runtime_profile_save_archive_list_input, build_runtime_profile_save_archive_record,
|
||||
build_runtime_profile_save_archive_resume_input,
|
||||
build_runtime_profile_save_archive_resume_input, build_runtime_profile_wallet_adjustment_input,
|
||||
build_runtime_profile_wallet_ledger_entry_record,
|
||||
build_runtime_profile_wallet_ledger_list_input, build_runtime_referral_invite_center_get_input,
|
||||
build_runtime_referral_invite_center_record, build_runtime_referral_redeem_input,
|
||||
|
||||
@@ -125,6 +125,19 @@ impl From<module_runtime::RuntimeProfileWalletLedgerListInput>
|
||||
}
|
||||
}
|
||||
|
||||
impl From<module_runtime::RuntimeProfileWalletAdjustmentInput>
|
||||
for RuntimeProfileWalletAdjustmentInput
|
||||
{
|
||||
fn from(input: module_runtime::RuntimeProfileWalletAdjustmentInput) -> Self {
|
||||
Self {
|
||||
user_id: input.user_id,
|
||||
amount: input.amount,
|
||||
ledger_id: input.ledger_id,
|
||||
created_at_micros: input.created_at_micros,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<module_runtime::RuntimeProfileRechargeCenterGetInput>
|
||||
for RuntimeProfileRechargeCenterGetInput
|
||||
{
|
||||
@@ -663,6 +676,28 @@ pub(crate) fn map_runtime_profile_wallet_ledger_procedure_result(
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub(crate) fn map_runtime_profile_wallet_adjustment_procedure_result(
|
||||
result: RuntimeProfileWalletAdjustmentProcedureResult,
|
||||
) -> Result<RuntimeProfileDashboardRecord, SpacetimeClientError> {
|
||||
if !result.ok {
|
||||
return Err(SpacetimeClientError::Procedure(
|
||||
result
|
||||
.error_message
|
||||
.unwrap_or_else(|| "SpacetimeDB procedure 返回未知错误".to_string()),
|
||||
));
|
||||
}
|
||||
|
||||
let snapshot = result.record.ok_or_else(|| {
|
||||
SpacetimeClientError::Procedure(
|
||||
"SpacetimeDB procedure 未返回 profile dashboard 快照".to_string(),
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(build_runtime_profile_dashboard_record(
|
||||
map_runtime_profile_dashboard_snapshot(snapshot),
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) fn map_runtime_profile_recharge_center_procedure_result(
|
||||
result: RuntimeProfileRechargeCenterProcedureResult,
|
||||
) -> Result<RuntimeProfileRechargeCenterRecord, SpacetimeClientError> {
|
||||
@@ -3236,6 +3271,12 @@ pub(crate) fn map_runtime_profile_wallet_ledger_source_type_back(
|
||||
crate::module_bindings::RuntimeProfileWalletLedgerSourceType::PointsRecharge => {
|
||||
module_runtime::RuntimeProfileWalletLedgerSourceType::PointsRecharge
|
||||
}
|
||||
crate::module_bindings::RuntimeProfileWalletLedgerSourceType::AssetGenerationConsume => {
|
||||
module_runtime::RuntimeProfileWalletLedgerSourceType::AssetGenerationConsume
|
||||
}
|
||||
crate::module_bindings::RuntimeProfileWalletLedgerSourceType::AssetGenerationRefund => {
|
||||
module_runtime::RuntimeProfileWalletLedgerSourceType::AssetGenerationRefund
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
|
||||
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
|
||||
|
||||
#![allow(unused, clippy::all)]
|
||||
use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws};
|
||||
|
||||
use super::runtime_profile_wallet_adjustment_input_type::RuntimeProfileWalletAdjustmentInput;
|
||||
use super::runtime_profile_wallet_adjustment_procedure_result_type::RuntimeProfileWalletAdjustmentProcedureResult;
|
||||
|
||||
#[derive(__lib::ser::Serialize, __lib::de::Deserialize, Clone, PartialEq, Debug)]
|
||||
#[sats(crate = __lib)]
|
||||
struct ConsumeProfileWalletPointsAndReturnArgs {
|
||||
pub input: RuntimeProfileWalletAdjustmentInput,
|
||||
}
|
||||
|
||||
impl __sdk::InModule for ConsumeProfileWalletPointsAndReturnArgs {
|
||||
type Module = super::RemoteModule;
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
/// Extension trait for access to the procedure `consume_profile_wallet_points_and_return`.
|
||||
///
|
||||
/// Implemented for [`super::RemoteProcedures`].
|
||||
pub trait consume_profile_wallet_points_and_return {
|
||||
fn consume_profile_wallet_points_and_return(&self, input: RuntimeProfileWalletAdjustmentInput) {
|
||||
self.consume_profile_wallet_points_and_return_then(input, |_, _| {});
|
||||
}
|
||||
|
||||
fn consume_profile_wallet_points_and_return_then(
|
||||
&self,
|
||||
input: RuntimeProfileWalletAdjustmentInput,
|
||||
|
||||
__callback: impl FnOnce(
|
||||
&super::ProcedureEventContext,
|
||||
Result<RuntimeProfileWalletAdjustmentProcedureResult, __sdk::InternalError>,
|
||||
) + Send
|
||||
+ 'static,
|
||||
);
|
||||
}
|
||||
|
||||
impl consume_profile_wallet_points_and_return for super::RemoteProcedures {
|
||||
fn consume_profile_wallet_points_and_return_then(
|
||||
&self,
|
||||
input: RuntimeProfileWalletAdjustmentInput,
|
||||
|
||||
__callback: impl FnOnce(
|
||||
&super::ProcedureEventContext,
|
||||
Result<RuntimeProfileWalletAdjustmentProcedureResult, __sdk::InternalError>,
|
||||
) + Send
|
||||
+ 'static,
|
||||
) {
|
||||
self.imp
|
||||
.invoke_procedure_with_callback::<_, RuntimeProfileWalletAdjustmentProcedureResult>(
|
||||
"consume_profile_wallet_points_and_return",
|
||||
ConsumeProfileWalletPointsAndReturnArgs { input },
|
||||
__callback,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -118,6 +118,7 @@ pub mod complete_ai_task_and_return_procedure;
|
||||
pub mod confirm_asset_object_and_return_procedure;
|
||||
pub mod confirm_asset_object_reducer;
|
||||
pub mod consume_inventory_item_input_type;
|
||||
pub mod consume_profile_wallet_points_and_return_procedure;
|
||||
pub mod continue_story_and_return_procedure;
|
||||
pub mod continue_story_reducer;
|
||||
pub mod create_ai_task_and_return_procedure;
|
||||
@@ -343,6 +344,7 @@ pub mod quest_step_snapshot_type;
|
||||
pub mod quest_treasure_inspected_signal_type;
|
||||
pub mod quest_turn_in_input_type;
|
||||
pub mod redeem_profile_referral_invite_code_procedure;
|
||||
pub mod refund_profile_wallet_points_and_return_procedure;
|
||||
pub mod refresh_session_type;
|
||||
pub mod resolve_combat_action_and_return_procedure;
|
||||
pub mod resolve_combat_action_input_type;
|
||||
@@ -405,6 +407,8 @@ pub mod runtime_profile_save_archive_list_input_type;
|
||||
pub mod runtime_profile_save_archive_procedure_result_type;
|
||||
pub mod runtime_profile_save_archive_resume_input_type;
|
||||
pub mod runtime_profile_save_archive_snapshot_type;
|
||||
pub mod runtime_profile_wallet_adjustment_input_type;
|
||||
pub mod runtime_profile_wallet_adjustment_procedure_result_type;
|
||||
pub mod runtime_profile_wallet_ledger_entry_snapshot_type;
|
||||
pub mod runtime_profile_wallet_ledger_list_input_type;
|
||||
pub mod runtime_profile_wallet_ledger_procedure_result_type;
|
||||
@@ -583,6 +587,7 @@ pub use complete_ai_task_and_return_procedure::complete_ai_task_and_return;
|
||||
pub use confirm_asset_object_and_return_procedure::confirm_asset_object_and_return;
|
||||
pub use confirm_asset_object_reducer::confirm_asset_object;
|
||||
pub use consume_inventory_item_input_type::ConsumeInventoryItemInput;
|
||||
pub use consume_profile_wallet_points_and_return_procedure::consume_profile_wallet_points_and_return;
|
||||
pub use continue_story_and_return_procedure::continue_story_and_return;
|
||||
pub use continue_story_reducer::continue_story;
|
||||
pub use create_ai_task_and_return_procedure::create_ai_task_and_return;
|
||||
@@ -808,6 +813,7 @@ pub use quest_step_snapshot_type::QuestStepSnapshot;
|
||||
pub use quest_treasure_inspected_signal_type::QuestTreasureInspectedSignal;
|
||||
pub use quest_turn_in_input_type::QuestTurnInInput;
|
||||
pub use redeem_profile_referral_invite_code_procedure::redeem_profile_referral_invite_code;
|
||||
pub use refund_profile_wallet_points_and_return_procedure::refund_profile_wallet_points_and_return;
|
||||
pub use refresh_session_type::RefreshSession;
|
||||
pub use resolve_combat_action_and_return_procedure::resolve_combat_action_and_return;
|
||||
pub use resolve_combat_action_input_type::ResolveCombatActionInput;
|
||||
@@ -870,6 +876,8 @@ pub use runtime_profile_save_archive_list_input_type::RuntimeProfileSaveArchiveL
|
||||
pub use runtime_profile_save_archive_procedure_result_type::RuntimeProfileSaveArchiveProcedureResult;
|
||||
pub use runtime_profile_save_archive_resume_input_type::RuntimeProfileSaveArchiveResumeInput;
|
||||
pub use runtime_profile_save_archive_snapshot_type::RuntimeProfileSaveArchiveSnapshot;
|
||||
pub use runtime_profile_wallet_adjustment_input_type::RuntimeProfileWalletAdjustmentInput;
|
||||
pub use runtime_profile_wallet_adjustment_procedure_result_type::RuntimeProfileWalletAdjustmentProcedureResult;
|
||||
pub use runtime_profile_wallet_ledger_entry_snapshot_type::RuntimeProfileWalletLedgerEntrySnapshot;
|
||||
pub use runtime_profile_wallet_ledger_list_input_type::RuntimeProfileWalletLedgerListInput;
|
||||
pub use runtime_profile_wallet_ledger_procedure_result_type::RuntimeProfileWalletLedgerProcedureResult;
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
|
||||
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
|
||||
|
||||
#![allow(unused, clippy::all)]
|
||||
use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws};
|
||||
|
||||
use super::runtime_profile_wallet_adjustment_input_type::RuntimeProfileWalletAdjustmentInput;
|
||||
use super::runtime_profile_wallet_adjustment_procedure_result_type::RuntimeProfileWalletAdjustmentProcedureResult;
|
||||
|
||||
#[derive(__lib::ser::Serialize, __lib::de::Deserialize, Clone, PartialEq, Debug)]
|
||||
#[sats(crate = __lib)]
|
||||
struct RefundProfileWalletPointsAndReturnArgs {
|
||||
pub input: RuntimeProfileWalletAdjustmentInput,
|
||||
}
|
||||
|
||||
impl __sdk::InModule for RefundProfileWalletPointsAndReturnArgs {
|
||||
type Module = super::RemoteModule;
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
/// Extension trait for access to the procedure `refund_profile_wallet_points_and_return`.
|
||||
///
|
||||
/// Implemented for [`super::RemoteProcedures`].
|
||||
pub trait refund_profile_wallet_points_and_return {
|
||||
fn refund_profile_wallet_points_and_return(&self, input: RuntimeProfileWalletAdjustmentInput) {
|
||||
self.refund_profile_wallet_points_and_return_then(input, |_, _| {});
|
||||
}
|
||||
|
||||
fn refund_profile_wallet_points_and_return_then(
|
||||
&self,
|
||||
input: RuntimeProfileWalletAdjustmentInput,
|
||||
|
||||
__callback: impl FnOnce(
|
||||
&super::ProcedureEventContext,
|
||||
Result<RuntimeProfileWalletAdjustmentProcedureResult, __sdk::InternalError>,
|
||||
) + Send
|
||||
+ 'static,
|
||||
);
|
||||
}
|
||||
|
||||
impl refund_profile_wallet_points_and_return for super::RemoteProcedures {
|
||||
fn refund_profile_wallet_points_and_return_then(
|
||||
&self,
|
||||
input: RuntimeProfileWalletAdjustmentInput,
|
||||
|
||||
__callback: impl FnOnce(
|
||||
&super::ProcedureEventContext,
|
||||
Result<RuntimeProfileWalletAdjustmentProcedureResult, __sdk::InternalError>,
|
||||
) + Send
|
||||
+ 'static,
|
||||
) {
|
||||
self.imp
|
||||
.invoke_procedure_with_callback::<_, RuntimeProfileWalletAdjustmentProcedureResult>(
|
||||
"refund_profile_wallet_points_and_return",
|
||||
RefundProfileWalletPointsAndReturnArgs { input },
|
||||
__callback,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
|
||||
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
|
||||
|
||||
#![allow(unused, clippy::all)]
|
||||
use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws};
|
||||
|
||||
#[derive(__lib::ser::Serialize, __lib::de::Deserialize, Clone, PartialEq, Debug)]
|
||||
#[sats(crate = __lib)]
|
||||
pub struct RuntimeProfileWalletAdjustmentInput {
|
||||
pub user_id: String,
|
||||
pub amount: u64,
|
||||
pub ledger_id: String,
|
||||
pub created_at_micros: i64,
|
||||
}
|
||||
|
||||
impl __sdk::InModule for RuntimeProfileWalletAdjustmentInput {
|
||||
type Module = super::RemoteModule;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
|
||||
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
|
||||
|
||||
#![allow(unused, clippy::all)]
|
||||
use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws};
|
||||
|
||||
use super::runtime_profile_dashboard_snapshot_type::RuntimeProfileDashboardSnapshot;
|
||||
|
||||
#[derive(__lib::ser::Serialize, __lib::de::Deserialize, Clone, PartialEq, Debug)]
|
||||
#[sats(crate = __lib)]
|
||||
pub struct RuntimeProfileWalletAdjustmentProcedureResult {
|
||||
pub ok: bool,
|
||||
pub record: Option<RuntimeProfileDashboardSnapshot>,
|
||||
pub error_message: Option<String>,
|
||||
}
|
||||
|
||||
impl __sdk::InModule for RuntimeProfileWalletAdjustmentProcedureResult {
|
||||
type Module = super::RemoteModule;
|
||||
}
|
||||
@@ -15,6 +15,10 @@ pub enum RuntimeProfileWalletLedgerSourceType {
|
||||
InviteInviteeReward,
|
||||
|
||||
PointsRecharge,
|
||||
|
||||
AssetGenerationConsume,
|
||||
|
||||
AssetGenerationRefund,
|
||||
}
|
||||
|
||||
impl __sdk::InModule for RuntimeProfileWalletLedgerSourceType {
|
||||
|
||||
@@ -89,6 +89,67 @@ impl SpacetimeClient {
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn consume_profile_wallet_points(
|
||||
&self,
|
||||
user_id: String,
|
||||
amount: u64,
|
||||
ledger_id: String,
|
||||
created_at_micros: i64,
|
||||
) -> Result<RuntimeProfileDashboardRecord, SpacetimeClientError> {
|
||||
let procedure_input = build_runtime_profile_wallet_adjustment_input(
|
||||
user_id,
|
||||
amount,
|
||||
ledger_id,
|
||||
created_at_micros,
|
||||
)
|
||||
.map_err(|error| SpacetimeClientError::Runtime(error.to_string()))?
|
||||
.into();
|
||||
|
||||
self.call_after_connect(move |connection, sender| {
|
||||
connection
|
||||
.procedures()
|
||||
.consume_profile_wallet_points_and_return_then(
|
||||
procedure_input,
|
||||
move |_, result| {
|
||||
let mapped = result
|
||||
.map_err(|error| SpacetimeClientError::Procedure(error.to_string()))
|
||||
.and_then(map_runtime_profile_wallet_adjustment_procedure_result);
|
||||
send_once(&sender, mapped);
|
||||
},
|
||||
);
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn refund_profile_wallet_points(
|
||||
&self,
|
||||
user_id: String,
|
||||
amount: u64,
|
||||
ledger_id: String,
|
||||
created_at_micros: i64,
|
||||
) -> Result<RuntimeProfileDashboardRecord, SpacetimeClientError> {
|
||||
let procedure_input = build_runtime_profile_wallet_adjustment_input(
|
||||
user_id,
|
||||
amount,
|
||||
ledger_id,
|
||||
created_at_micros,
|
||||
)
|
||||
.map_err(|error| SpacetimeClientError::Runtime(error.to_string()))?
|
||||
.into();
|
||||
|
||||
self.call_after_connect(move |connection, sender| {
|
||||
connection
|
||||
.procedures()
|
||||
.refund_profile_wallet_points_and_return_then(procedure_input, move |_, result| {
|
||||
let mapped = result
|
||||
.map_err(|error| SpacetimeClientError::Procedure(error.to_string()))
|
||||
.and_then(map_runtime_profile_wallet_adjustment_procedure_result);
|
||||
send_once(&sender, mapped);
|
||||
});
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_profile_recharge_center(
|
||||
&self,
|
||||
user_id: String,
|
||||
|
||||
@@ -238,6 +238,60 @@ pub fn list_profile_wallet_ledger(
|
||||
}
|
||||
}
|
||||
|
||||
// 资产生成由 Axum 调用外部模型,钱包扣费必须先在 SpacetimeDB 内原子落账。
|
||||
#[spacetimedb::procedure]
|
||||
pub fn consume_profile_wallet_points_and_return(
|
||||
ctx: &mut ProcedureContext,
|
||||
input: RuntimeProfileWalletAdjustmentInput,
|
||||
) -> RuntimeProfileWalletAdjustmentProcedureResult {
|
||||
match ctx.try_with_tx(|tx| {
|
||||
apply_profile_wallet_adjustment(
|
||||
tx,
|
||||
input.clone(),
|
||||
RuntimeProfileWalletLedgerSourceType::AssetGenerationConsume,
|
||||
true,
|
||||
)
|
||||
}) {
|
||||
Ok(record) => RuntimeProfileWalletAdjustmentProcedureResult {
|
||||
ok: true,
|
||||
record: Some(record),
|
||||
error_message: None,
|
||||
},
|
||||
Err(message) => RuntimeProfileWalletAdjustmentProcedureResult {
|
||||
ok: false,
|
||||
record: None,
|
||||
error_message: Some(message),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// 生成链路失败时由 Axum 调用退款,ledger_id 幂等保证重复补偿不会重复加钱。
|
||||
#[spacetimedb::procedure]
|
||||
pub fn refund_profile_wallet_points_and_return(
|
||||
ctx: &mut ProcedureContext,
|
||||
input: RuntimeProfileWalletAdjustmentInput,
|
||||
) -> RuntimeProfileWalletAdjustmentProcedureResult {
|
||||
match ctx.try_with_tx(|tx| {
|
||||
apply_profile_wallet_adjustment(
|
||||
tx,
|
||||
input.clone(),
|
||||
RuntimeProfileWalletLedgerSourceType::AssetGenerationRefund,
|
||||
false,
|
||||
)
|
||||
}) {
|
||||
Ok(record) => RuntimeProfileWalletAdjustmentProcedureResult {
|
||||
ok: true,
|
||||
record: Some(record),
|
||||
error_message: None,
|
||||
},
|
||||
Err(message) => RuntimeProfileWalletAdjustmentProcedureResult {
|
||||
ok: false,
|
||||
record: None,
|
||||
error_message: Some(message),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// play stats 与 dashboard 共用 dashboard projection 的 total_play_time / updated_at,避免 Axum 侧拼装。
|
||||
#[spacetimedb::procedure]
|
||||
pub fn get_profile_play_stats(
|
||||
@@ -1370,15 +1424,91 @@ fn apply_profile_wallet_delta(
|
||||
ledger_id: &str,
|
||||
created_at: Timestamp,
|
||||
) -> Result<u64, String> {
|
||||
let amount_delta =
|
||||
i64::try_from(amount_delta).map_err(|_| "profile.wallet_amount 超出上限".to_string())?;
|
||||
apply_profile_wallet_signed_delta(
|
||||
ctx,
|
||||
user_id,
|
||||
amount_delta,
|
||||
source_type,
|
||||
ledger_id,
|
||||
created_at,
|
||||
false,
|
||||
)
|
||||
}
|
||||
|
||||
fn apply_profile_wallet_adjustment(
|
||||
ctx: &ReducerContext,
|
||||
input: RuntimeProfileWalletAdjustmentInput,
|
||||
source_type: RuntimeProfileWalletLedgerSourceType,
|
||||
consume: bool,
|
||||
) -> Result<RuntimeProfileDashboardSnapshot, String> {
|
||||
let validated_input = build_runtime_profile_wallet_adjustment_input(
|
||||
input.user_id,
|
||||
input.amount,
|
||||
input.ledger_id,
|
||||
input.created_at_micros,
|
||||
)
|
||||
.map_err(|error| error.to_string())?;
|
||||
let created_at = Timestamp::from_micros_since_unix_epoch(validated_input.created_at_micros);
|
||||
let amount_delta = if consume {
|
||||
-(validated_input.amount as i64)
|
||||
} else {
|
||||
validated_input.amount as i64
|
||||
};
|
||||
|
||||
apply_profile_wallet_signed_delta(
|
||||
ctx,
|
||||
&validated_input.user_id,
|
||||
amount_delta,
|
||||
source_type,
|
||||
&validated_input.ledger_id,
|
||||
created_at,
|
||||
true,
|
||||
)?;
|
||||
get_profile_dashboard_snapshot(
|
||||
ctx,
|
||||
RuntimeProfileDashboardGetInput {
|
||||
user_id: validated_input.user_id,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn apply_profile_wallet_signed_delta(
|
||||
ctx: &ReducerContext,
|
||||
user_id: &str,
|
||||
amount_delta: i64,
|
||||
source_type: RuntimeProfileWalletLedgerSourceType,
|
||||
ledger_id: &str,
|
||||
created_at: Timestamp,
|
||||
idempotent: bool,
|
||||
) -> Result<u64, String> {
|
||||
if idempotent
|
||||
&& ctx
|
||||
.db
|
||||
.profile_wallet_ledger()
|
||||
.wallet_ledger_id()
|
||||
.find(&ledger_id.to_string())
|
||||
.is_some()
|
||||
{
|
||||
return Ok(profile_wallet_balance(ctx, user_id));
|
||||
}
|
||||
|
||||
let current = ctx
|
||||
.db
|
||||
.profile_dashboard_state()
|
||||
.user_id()
|
||||
.find(&user_id.to_string());
|
||||
let previous_balance = current.as_ref().map(|row| row.wallet_balance).unwrap_or(0);
|
||||
let next_balance = previous_balance
|
||||
.checked_add(amount_delta)
|
||||
.ok_or_else(|| "profile.wallet_balance 超出上限".to_string())?;
|
||||
let next_balance = if amount_delta >= 0 {
|
||||
previous_balance
|
||||
.checked_add(amount_delta as u64)
|
||||
.ok_or_else(|| "profile.wallet_balance 超出上限".to_string())?
|
||||
} else {
|
||||
previous_balance
|
||||
.checked_sub(amount_delta.unsigned_abs())
|
||||
.ok_or_else(|| "叙世币余额不足".to_string())?
|
||||
};
|
||||
let created_state_at = current
|
||||
.as_ref()
|
||||
.map(|row| row.created_at)
|
||||
@@ -1413,7 +1543,7 @@ fn apply_profile_wallet_delta(
|
||||
ctx.db.profile_wallet_ledger().insert(ProfileWalletLedger {
|
||||
wallet_ledger_id: ledger_id.to_string(),
|
||||
user_id: user_id.to_string(),
|
||||
amount_delta: amount_delta as i64,
|
||||
amount_delta,
|
||||
balance_after: next_balance,
|
||||
source_type,
|
||||
created_at,
|
||||
|
||||
Reference in New Issue
Block a user