This commit is contained in:
2026-05-15 06:36:48 +08:00
620 changed files with 126075 additions and 6322 deletions

View File

@@ -13,8 +13,8 @@ use module_match3d::{
Match3DItemSnapshot as DomainMatch3DItemSnapshot, Match3DItemState as DomainMatch3DItemState,
Match3DRunSnapshot as DomainMatch3DRunSnapshot, Match3DRunStatus as DomainMatch3DRunStatus,
Match3DTraySlot as DomainMatch3DTraySlot, confirm_click_at as confirm_domain_click_at,
resolve_run_timer_at as resolve_domain_run_timer_at, start_run_with_seed_at_and_item_type_count,
stop_run_at as stop_domain_run_at,
resolve_run_timer_at as resolve_domain_run_timer_at,
start_run_with_seed_at_and_item_type_count, stop_run_at as stop_domain_run_at,
};
use serde::Serialize;
use serde::de::DeserializeOwned;
@@ -1259,12 +1259,12 @@ fn validate_publishable_work(row: &Match3DWorkProfileRow) -> Result<(), String>
return Err("match3d 发布需要至少 1 个标签".to_string());
}
let config = parse_config(&row.config_json)?;
let required_item_types =
module_match3d::resolve_match3d_item_type_count_for_difficulty(
config.clear_count,
config.difficulty,
) as usize;
let ready_item_types = count_ready_generated_item_types(row.generated_item_assets_json.as_deref())?;
let required_item_types = module_match3d::resolve_match3d_item_type_count_for_difficulty(
config.clear_count,
config.difficulty,
) as usize;
let ready_item_types =
count_ready_generated_item_types(row.generated_item_assets_json.as_deref())?;
if ready_item_types < required_item_types {
return Err(format!(
"match3d 发布需要至少 {required_item_types} 种物品素材,当前已有 {ready_item_types}"

View File

@@ -193,6 +193,7 @@ macro_rules! migration_tables {
public_work_play_daily_stat,
public_work_like,
profile_membership,
profile_recharge_product_config,
profile_recharge_order,
profile_feedback_submission,
profile_save_archive,

View File

@@ -17,15 +17,14 @@ use module_puzzle::{
PuzzleRunNextLevelInput, PuzzleRunPauseInput, PuzzleRunProcedureResult, PuzzleRunPropInput,
PuzzleRunSnapshot, PuzzleRunStartInput, PuzzleRunSwapInput, PuzzleRuntimeLevelStatus,
PuzzleSelectCoverImageInput, PuzzleUiBackgroundSaveInput, PuzzleWorkDeleteInput,
PuzzleWorkGetInput,
PuzzleWorkLikeRecordInput as PuzzleWorkLikeInput, PuzzleWorkPointIncentiveClaimInput,
PuzzleWorkProcedureResult, PuzzleWorkProfile, PuzzleWorkRemixInput, PuzzleWorkUpsertInput,
PuzzleWorksListInput, PuzzleWorksProcedureResult, apply_publish_overrides_to_draft,
apply_selected_candidate, build_form_draft_from_seed, build_result_preview,
compile_result_draft_from_seed, create_work_profile, infer_anchor_pack, normalize_puzzle_draft,
normalize_puzzle_levels, normalize_theme_tags, publish_work_profile, replace_puzzle_level,
select_next_profiles, selected_profile_level_after_runtime_level, selected_puzzle_level,
tag_similarity_score,
PuzzleWorkGetInput, PuzzleWorkLikeRecordInput as PuzzleWorkLikeInput,
PuzzleWorkPointIncentiveClaimInput, PuzzleWorkProcedureResult, PuzzleWorkProfile,
PuzzleWorkRemixInput, PuzzleWorkUpsertInput, PuzzleWorksListInput, PuzzleWorksProcedureResult,
apply_publish_overrides_to_draft, apply_selected_candidate, build_form_draft_from_seed,
build_result_preview, compile_result_draft_from_seed, create_work_profile, infer_anchor_pack,
normalize_puzzle_draft, normalize_puzzle_levels, normalize_theme_tags, publish_work_profile,
replace_puzzle_level, select_next_profiles, selected_profile_level_after_runtime_level,
selected_puzzle_level, tag_similarity_score,
};
use module_runtime::RuntimeProfileWalletLedgerSourceType;
use module_runtime::visible_runtime_profile_user_tags;
@@ -1062,12 +1061,10 @@ fn save_puzzle_ui_background_tx(
let mut next_level = target_level;
next_level.ui_background_prompt = Some(input.prompt.trim().to_string());
next_level.ui_background_image_src = Some(input.image_src.trim().to_string());
next_level.ui_background_image_object_key = input
.image_object_key
.and_then(|value| {
let trimmed = value.trim().to_string();
(!trimmed.is_empty()).then_some(trimmed)
});
next_level.ui_background_image_object_key = input.image_object_key.and_then(|value| {
let trimmed = value.trim().to_string();
(!trimmed.is_empty()).then_some(trimmed)
});
let draft = replace_puzzle_level(&draft, next_level).map_err(|error| error.to_string())?;
let saved_at = Timestamp::from_micros_since_unix_epoch(input.saved_at_micros);

View File

@@ -6,6 +6,7 @@ const PUBLIC_WORK_RECENT_PLAY_WINDOW_DAYS: i64 = 7;
const PROFILE_REFERRAL_INVITED_USERS_LIMIT: usize = 20;
const PROFILE_NEW_USER_REGISTRATION_LEDGER_PREFIX: &str = "new-user-registration";
const PROFILE_TASK_SYSTEM_USER_ID: &str = "system:profile-task";
const PROFILE_RECHARGE_PRODUCT_SYSTEM_USER_ID: &str = "system:recharge-product";
const PROFILE_TASK_LOGIN_EVENT_ID_PREFIX: &str = "daily-login";
const PROFILE_TRACKING_PROFILE_MODULE_KEY: &str = "profile";
@@ -328,6 +329,28 @@ pub struct ProfileMembership {
pub(crate) updated_at: Timestamp,
}
#[spacetimedb::table(accessor = profile_recharge_product_config)]
#[derive(Clone)]
pub struct ProfileRechargeProductConfig {
#[primary_key]
pub(crate) product_id: String,
pub(crate) title: String,
pub(crate) price_cents: u64,
pub(crate) kind: RuntimeProfileRechargeProductKind,
pub(crate) points_amount: u64,
pub(crate) bonus_points: u64,
pub(crate) duration_days: u32,
pub(crate) badge_label: String,
pub(crate) description: String,
pub(crate) tier: RuntimeProfileMembershipTier,
pub(crate) enabled: bool,
pub(crate) sort_order: i32,
pub(crate) created_by: String,
pub(crate) created_at: Timestamp,
pub(crate) updated_by: String,
pub(crate) updated_at: Timestamp,
}
#[spacetimedb::table(
accessor = profile_recharge_order,
index(accessor = by_profile_recharge_order_user_id, btree(columns = [user_id])),
@@ -655,6 +678,44 @@ pub fn admin_disable_profile_task_config(
}
}
#[spacetimedb::procedure]
pub fn admin_list_profile_recharge_products(
ctx: &mut ProcedureContext,
input: RuntimeProfileRechargeProductAdminListInput,
) -> RuntimeProfileRechargeProductAdminListProcedureResult {
match ctx.try_with_tx(|tx| list_profile_recharge_product_config_snapshots(tx, input.clone())) {
Ok(entries) => RuntimeProfileRechargeProductAdminListProcedureResult {
ok: true,
entries,
error_message: None,
},
Err(message) => RuntimeProfileRechargeProductAdminListProcedureResult {
ok: false,
entries: Vec::new(),
error_message: Some(message),
},
}
}
#[spacetimedb::procedure]
pub fn admin_upsert_profile_recharge_product(
ctx: &mut ProcedureContext,
input: RuntimeProfileRechargeProductAdminUpsertInput,
) -> RuntimeProfileRechargeProductAdminProcedureResult {
match ctx.try_with_tx(|tx| upsert_profile_recharge_product_config_record(tx, input.clone())) {
Ok(record) => RuntimeProfileRechargeProductAdminProcedureResult {
ok: true,
record: Some(record),
error_message: None,
},
Err(message) => RuntimeProfileRechargeProductAdminProcedureResult {
ok: false,
record: None,
error_message: Some(message),
},
}
}
// 新用户注册赠送由后端注册链路调用;流水 ID 固定,保证重试不重复发放。
#[spacetimedb::procedure]
pub fn grant_new_user_registration_wallet_reward(
@@ -1454,6 +1515,22 @@ fn build_public_work_like_id(source_type: &str, profile_id: &str, user_id: &str)
mod tests {
use super::*;
#[test]
fn point_recharge_display_is_resolved_per_product() {
let products = runtime_profile_recharge_point_products();
let repeated = resolve_profile_recharge_product_display(products[0].clone(), true);
let untouched = resolve_profile_recharge_product_display(products[1].clone(), false);
assert_eq!(repeated.product_id, "points_60");
assert_eq!(repeated.bonus_points, 0);
assert_eq!(repeated.badge_label, "");
assert_eq!(repeated.description, "60泥点");
assert_eq!(untouched.product_id, "points_180");
assert_eq!(untouched.bonus_points, 180);
assert_eq!(untouched.badge_label, "首充双倍");
assert_eq!(untouched.description, "首充送180泥点");
}
#[test]
fn duplicate_tracking_event_ids_are_treated_as_idempotent_replays() {
assert!(should_skip_existing_tracking_event_id(true));
@@ -2091,8 +2168,8 @@ fn create_profile_recharge_order_record(
input.created_at_micros,
)
.map_err(|error| error.to_string())?;
let product = runtime_profile_recharge_product_by_id(&validated_input.product_id)
.ok_or_else(|| "recharge.product_id 不存在".to_string())?;
let product = enabled_profile_recharge_product_by_id(ctx, &validated_input.product_id)
.ok_or_else(|| "recharge.product_id 不存在或已下架".to_string())?;
let created_at = Timestamp::from_micros_since_unix_epoch(validated_input.created_at_micros);
let should_settle_immediately =
validated_input.payment_channel == PROFILE_RECHARGE_PAYMENT_CHANNEL_MOCK;
@@ -2201,7 +2278,7 @@ fn mark_profile_recharge_order_paid_record(
return Err("profile_recharge_order 当前状态不能确认支付".to_string());
}
let product = runtime_profile_recharge_product_by_id(&order.product_id)
let product = profile_recharge_product_by_id(ctx, &order.product_id)
.ok_or_else(|| "recharge.product_id 不存在".to_string())?;
let paid_at = Timestamp::from_micros_since_unix_epoch(validated_input.paid_at_micros);
let (points_delta, membership_expires_at) = apply_profile_recharge_purchase(
@@ -2238,7 +2315,7 @@ fn apply_profile_recharge_purchase(
) -> Result<(i64, Option<Timestamp>), String> {
match product.kind {
RuntimeProfileRechargeProductKind::Points => {
let has_recharged = has_profile_points_recharged(ctx, user_id);
let has_recharged = has_profile_product_recharged(ctx, user_id, &product.product_id);
let points_delta =
resolve_runtime_profile_points_recharge_delta(product, has_recharged);
apply_profile_wallet_delta(
@@ -2907,6 +2984,7 @@ fn build_profile_recharge_center_snapshot(
ctx: &ReducerContext,
user_id: &str,
) -> RuntimeProfileRechargeCenterSnapshot {
ensure_default_profile_recharge_product_config(ctx);
let wallet_balance = ctx
.db
.profile_dashboard_state()
@@ -2915,16 +2993,36 @@ fn build_profile_recharge_center_snapshot(
.map(|row| row.wallet_balance)
.unwrap_or(0);
let has_points_recharged = has_profile_points_recharged(ctx, user_id);
let mut point_products = Vec::new();
let mut membership_products = Vec::new();
for row in profile_recharge_product_config_rows(ctx, false) {
let product = build_profile_recharge_product_snapshot_from_config_row(&row);
match product.kind {
RuntimeProfileRechargeProductKind::Points => {
let has_product_recharged =
has_profile_product_recharged(ctx, user_id, &product.product_id);
point_products.push(resolve_profile_recharge_product_display(
product,
has_product_recharged,
));
}
RuntimeProfileRechargeProductKind::Membership => {
membership_products.push(product);
}
}
}
RuntimeProfileRechargeCenterSnapshot {
user_id: user_id.to_string(),
wallet_balance,
membership: build_profile_membership_snapshot(ctx, user_id),
point_products: runtime_profile_recharge_point_products(),
membership_products: runtime_profile_recharge_membership_products(),
point_products,
membership_products,
benefits: runtime_profile_membership_benefits(),
latest_order: latest_profile_recharge_order(ctx, user_id)
.map(|row| build_profile_recharge_order_snapshot_from_row(&row)),
has_points_recharged: has_profile_points_recharged(ctx, user_id),
has_points_recharged,
}
}
@@ -3050,6 +3148,21 @@ fn list_profile_task_config_snapshots(
Ok(entries)
}
fn list_profile_recharge_product_config_snapshots(
ctx: &ReducerContext,
input: RuntimeProfileRechargeProductAdminListInput,
) -> Result<Vec<RuntimeProfileRechargeProductConfigSnapshot>, String> {
let _validated_input =
build_runtime_profile_recharge_product_admin_list_input(input.admin_user_id)
.map_err(|error| error.to_string())?;
ensure_default_profile_recharge_product_config(ctx);
Ok(profile_recharge_product_config_rows(ctx, true)
.iter()
.map(build_profile_recharge_product_config_snapshot_from_row)
.collect())
}
fn admin_list_profile_redeem_code_records(
ctx: &ReducerContext,
input: RuntimeProfileRedeemCodeAdminListInput,
@@ -3095,6 +3208,74 @@ fn admin_list_profile_invite_code_records(
Ok(entries)
}
fn upsert_profile_recharge_product_config_record(
ctx: &ReducerContext,
input: RuntimeProfileRechargeProductAdminUpsertInput,
) -> Result<RuntimeProfileRechargeProductConfigSnapshot, String> {
let validated_input = build_runtime_profile_recharge_product_admin_upsert_input(
input.admin_user_id,
input.product_id,
input.title,
input.price_cents,
input.kind,
input.points_amount,
input.bonus_points,
input.duration_days,
input.badge_label,
input.description,
input.tier,
input.enabled,
input.sort_order,
input.updated_at_micros,
)
.map_err(|error| error.to_string())?;
ensure_default_profile_recharge_product_config(ctx);
let updated_at = Timestamp::from_micros_since_unix_epoch(validated_input.updated_at_micros);
let existing = ctx
.db
.profile_recharge_product_config()
.product_id()
.find(&validated_input.product_id);
if let Some(row) = existing.as_ref() {
ctx.db
.profile_recharge_product_config()
.product_id()
.delete(&row.product_id);
}
let inserted = ctx
.db
.profile_recharge_product_config()
.insert(ProfileRechargeProductConfig {
product_id: validated_input.product_id,
title: validated_input.title,
price_cents: validated_input.price_cents,
kind: validated_input.kind,
points_amount: validated_input.points_amount,
bonus_points: validated_input.bonus_points,
duration_days: validated_input.duration_days,
badge_label: validated_input.badge_label,
description: validated_input.description,
tier: validated_input.tier,
enabled: validated_input.enabled,
sort_order: validated_input.sort_order,
created_by: existing
.as_ref()
.map(|row| row.created_by.clone())
.unwrap_or_else(|| validated_input.admin_user_id.clone()),
created_at: existing
.as_ref()
.map(|row| row.created_at)
.unwrap_or(updated_at),
updated_by: validated_input.admin_user_id,
updated_at,
});
Ok(build_profile_recharge_product_config_snapshot_from_row(
&inserted,
))
}
fn upsert_profile_task_config_record(
ctx: &ReducerContext,
input: RuntimeProfileTaskConfigAdminUpsertInput,
@@ -3516,6 +3697,96 @@ fn ensure_default_profile_task_config(ctx: &ReducerContext) -> ProfileTaskConfig
})
}
fn ensure_default_profile_recharge_product_config(ctx: &ReducerContext) {
if ctx.db.profile_recharge_product_config().count() > 0 {
return;
}
let now = ctx.timestamp;
for (sort_order, product) in runtime_profile_recharge_point_products()
.into_iter()
.chain(runtime_profile_recharge_membership_products())
.enumerate()
{
ctx.db
.profile_recharge_product_config()
.insert(ProfileRechargeProductConfig {
product_id: product.product_id,
title: product.title,
price_cents: product.price_cents,
kind: product.kind,
points_amount: product.points_amount,
bonus_points: product.bonus_points,
duration_days: product.duration_days,
badge_label: product.badge_label,
description: product.description,
tier: product.tier,
enabled: true,
sort_order: sort_order as i32,
created_by: PROFILE_RECHARGE_PRODUCT_SYSTEM_USER_ID.to_string(),
created_at: now,
updated_by: PROFILE_RECHARGE_PRODUCT_SYSTEM_USER_ID.to_string(),
updated_at: now,
});
}
}
fn profile_recharge_product_config_rows(
ctx: &ReducerContext,
include_disabled: bool,
) -> Vec<ProfileRechargeProductConfig> {
ensure_default_profile_recharge_product_config(ctx);
let mut rows = ctx
.db
.profile_recharge_product_config()
.iter()
.filter(|row| include_disabled || row.enabled)
.collect::<Vec<_>>();
rows.sort_by(|left, right| {
left.sort_order
.cmp(&right.sort_order)
.then_with(|| left.product_id.cmp(&right.product_id))
});
rows
}
fn profile_recharge_product_by_id(
ctx: &ReducerContext,
product_id: &str,
) -> Option<RuntimeProfileRechargeProductSnapshot> {
ensure_default_profile_recharge_product_config(ctx);
ctx.db
.profile_recharge_product_config()
.product_id()
.find(&product_id.to_string())
.map(|row| build_profile_recharge_product_snapshot_from_config_row(&row))
}
fn enabled_profile_recharge_product_by_id(
ctx: &ReducerContext,
product_id: &str,
) -> Option<RuntimeProfileRechargeProductSnapshot> {
ensure_default_profile_recharge_product_config(ctx);
ctx.db
.profile_recharge_product_config()
.product_id()
.find(&product_id.to_string())
.filter(|row| row.enabled)
.map(|row| build_profile_recharge_product_snapshot_from_config_row(&row))
}
fn resolve_profile_recharge_product_display(
mut product: RuntimeProfileRechargeProductSnapshot,
has_product_recharged: bool,
) -> RuntimeProfileRechargeProductSnapshot {
if product.kind == RuntimeProfileRechargeProductKind::Points && has_product_recharged {
product.bonus_points = 0;
product.badge_label.clear();
product.description = product.title.clone();
}
product
}
fn build_profile_membership_snapshot(
ctx: &ReducerContext,
user_id: &str,
@@ -3752,9 +4023,19 @@ fn apply_profile_wallet_signed_delta(
}
fn has_profile_points_recharged(ctx: &ReducerContext, user_id: &str) -> bool {
ctx.db.profile_wallet_ledger().iter().any(|row| {
ctx.db.profile_recharge_order().iter().any(|row| {
row.user_id == user_id
&& row.source_type == RuntimeProfileWalletLedgerSourceType::PointsRecharge
&& row.kind == RuntimeProfileRechargeProductKind::Points
&& row.status == RuntimeProfileRechargeOrderStatus::Paid
})
}
fn has_profile_product_recharged(ctx: &ReducerContext, user_id: &str, product_id: &str) -> bool {
ctx.db.profile_recharge_order().iter().any(|row| {
row.user_id == user_id
&& row.product_id == product_id
&& row.kind == RuntimeProfileRechargeProductKind::Points
&& row.status == RuntimeProfileRechargeOrderStatus::Paid
})
}
@@ -3892,6 +4173,46 @@ fn build_profile_task_config_snapshot_from_row(
}
}
fn build_profile_recharge_product_config_snapshot_from_row(
row: &ProfileRechargeProductConfig,
) -> RuntimeProfileRechargeProductConfigSnapshot {
RuntimeProfileRechargeProductConfigSnapshot {
product_id: row.product_id.clone(),
title: row.title.clone(),
price_cents: row.price_cents,
kind: row.kind,
points_amount: row.points_amount,
bonus_points: row.bonus_points,
duration_days: row.duration_days,
badge_label: row.badge_label.clone(),
description: row.description.clone(),
tier: row.tier,
enabled: row.enabled,
sort_order: row.sort_order,
created_by: row.created_by.clone(),
created_at_micros: row.created_at.to_micros_since_unix_epoch(),
updated_by: row.updated_by.clone(),
updated_at_micros: row.updated_at.to_micros_since_unix_epoch(),
}
}
fn build_profile_recharge_product_snapshot_from_config_row(
row: &ProfileRechargeProductConfig,
) -> RuntimeProfileRechargeProductSnapshot {
RuntimeProfileRechargeProductSnapshot {
product_id: row.product_id.clone(),
title: row.title.clone(),
price_cents: row.price_cents,
kind: row.kind,
points_amount: row.points_amount,
bonus_points: row.bonus_points,
duration_days: row.duration_days,
badge_label: row.badge_label.clone(),
description: row.description.clone(),
tier: row.tier,
}
}
fn build_profile_recharge_order_snapshot_from_row(
row: &ProfileRechargeOrder,
) -> RuntimeProfileRechargeOrderSnapshot {