Add user played work stats for puzzle and big fish
Some checks failed
CI / verify (pull_request) Has been cancelled
Some checks failed
CI / verify (pull_request) Has been cancelled
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
use crate::big_fish::tables::{big_fish_agent_message, big_fish_creation_session};
|
||||
use crate::runtime::{
|
||||
ProfilePlayedWorkUpsertInput, add_profile_observed_play_time, upsert_profile_played_work,
|
||||
};
|
||||
use crate::*;
|
||||
|
||||
const INITIAL_BIG_FISH_CREATION_PROGRESS_PERCENT: u32 = 0;
|
||||
@@ -150,6 +153,25 @@ pub fn compile_big_fish_draft(
|
||||
}
|
||||
}
|
||||
|
||||
#[spacetimedb::procedure]
|
||||
pub fn record_big_fish_play(
|
||||
ctx: &mut ProcedureContext,
|
||||
input: BigFishPlayReportInput,
|
||||
) -> BigFishSessionProcedureResult {
|
||||
match ctx.try_with_tx(|tx| record_big_fish_play_tx(tx, input.clone())) {
|
||||
Ok(session) => BigFishSessionProcedureResult {
|
||||
ok: true,
|
||||
session: Some(session),
|
||||
error_message: None,
|
||||
},
|
||||
Err(message) => BigFishSessionProcedureResult {
|
||||
ok: false,
|
||||
session: None,
|
||||
error_message: Some(message),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create_big_fish_session_tx(
|
||||
ctx: &ReducerContext,
|
||||
input: BigFishSessionCreateInput,
|
||||
@@ -544,6 +566,67 @@ pub(crate) fn compile_big_fish_draft_tx(
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn record_big_fish_play_tx(
|
||||
ctx: &ReducerContext,
|
||||
input: BigFishPlayReportInput,
|
||||
) -> Result<BigFishSessionSnapshot, String> {
|
||||
validate_play_report_input(&input).map_err(|error| error.to_string())?;
|
||||
let session = ctx
|
||||
.db
|
||||
.big_fish_creation_session()
|
||||
.session_id()
|
||||
.find(&input.session_id)
|
||||
.filter(|row| row.stage == BigFishCreationStage::Published)
|
||||
.ok_or_else(|| "big_fish_creation_session 不存在或尚未发布".to_string())?;
|
||||
let draft = session
|
||||
.draft_json
|
||||
.as_deref()
|
||||
.map(deserialize_draft)
|
||||
.transpose()
|
||||
.map_err(|error| format!("big_fish.draft_json 非法: {error}"))?;
|
||||
let title = draft
|
||||
.as_ref()
|
||||
.map(|value| value.title.trim().to_string())
|
||||
.filter(|value| !value.is_empty())
|
||||
.unwrap_or_else(|| "大鱼吃小鱼".to_string());
|
||||
let subtitle = draft
|
||||
.as_ref()
|
||||
.and_then(|value| {
|
||||
let subtitle = value.subtitle.trim();
|
||||
if subtitle.is_empty() {
|
||||
let core_fun = value.core_fun.trim();
|
||||
(!core_fun.is_empty()).then(|| core_fun.to_string())
|
||||
} else {
|
||||
Some(subtitle.to_string())
|
||||
}
|
||||
})
|
||||
.unwrap_or_default();
|
||||
let world_key = format!("big-fish:{}", session.session_id);
|
||||
|
||||
upsert_profile_played_work(
|
||||
ctx,
|
||||
ProfilePlayedWorkUpsertInput {
|
||||
user_id: input.user_id.clone(),
|
||||
world_key: world_key.clone(),
|
||||
owner_user_id: Some(session.owner_user_id.clone()),
|
||||
profile_id: Some(session.session_id.clone()),
|
||||
world_type: Some("BIG_FISH".to_string()),
|
||||
world_title: title,
|
||||
world_subtitle: subtitle,
|
||||
played_at_micros: input.reported_at_micros,
|
||||
},
|
||||
)?;
|
||||
add_profile_observed_play_time(
|
||||
ctx,
|
||||
&input.user_id,
|
||||
&world_key,
|
||||
input.elapsed_ms,
|
||||
input.reported_at_micros,
|
||||
)?;
|
||||
|
||||
build_big_fish_session_snapshot(ctx, &session)
|
||||
}
|
||||
|
||||
pub(crate) fn build_big_fish_session_snapshot(
|
||||
ctx: &ReducerContext,
|
||||
row: &BigFishCreationSession,
|
||||
|
||||
Reference in New Issue
Block a user