Multi live

This commit is contained in:
Ethan O'Brien
2026-08-11 14:41:28 -05:00
parent 75613dafb0
commit 27d7b8de82
18 changed files with 5650 additions and 83 deletions
+186 -6
View File
@@ -25,7 +25,39 @@ pub fn routes(cfg: &mut web::ServiceConfig) {
);
}
fn get_event_data(key: &str, event_id: u32) -> JsonValue {
// Whether a release_label window is open at `now`. A blank _openedAt has always been
// open and a blank _closedAt never closes, which is how the evergreen label (id 1) is
// expressed. _releaseStatus other than 1 is never released at all.
pub fn release_label_is_open(label_id: i64, now: u64) -> bool {
let label = &databases::RELEASE_LABEL[label_id.to_string()];
if label.is_empty() || label["releaseStatus"].as_i64().unwrap_or(0) != 1 {
return false;
}
if let Some(opened) = global::parse_datetime(&label["openedAt"].to_string()) {
if now < opened {
return false;
}
}
if let Some(closed) = global::parse_datetime(&label["closedAt"].to_string()) {
if now > closed {
return false;
}
}
true
}
// Whether an event is currently running, by its own release label window. ew had no
// in-session test before this — nothing else evaluates release_label — so this is the
// one place to extend if the event listing ever needs the same question answered.
pub fn is_in_session(event_id: u32, now: u64) -> bool {
let event = &databases::EVENTS[event_id.to_string()];
if event.is_empty() {
return false;
}
release_label_is_open(event["masterReleaseLabelId"].as_i64().unwrap_or(0), now)
}
pub fn get_event_data(key: &str, event_id: u32) -> JsonValue {
let mut event = userdata::get_acc_event(key);
let is_star_event = STAR_EVENT_IDS.contains(&event_id);
//println!("is_star_event: {}, {}", is_star_event, event_id);
@@ -50,10 +82,61 @@ fn get_event_data(key: &str, event_id: u32) -> JsonValue {
event[event_id.to_string()]["star_event"]["star_event_bonus_daily_count"] = 0.into();
}
normalise_event_shape(&mut event[event_id.to_string()]);
event[event_id.to_string()].clone()
}
fn save_event_data(key: &str, event_id: u32, data: JsonValue) {
// The client's /api/event response types are [Serializable] C# classes, and Unity's
// JsonUtility maps them structurally: `score_ranking`, `member_ranking` and `lottery_box`
// are OBJECTS (Shock.ProtocolData.ScoreRanking / MemberRanking / LotteryBox), not arrays.
// new_user_event.json used to seed them as `[]`, which the client cannot bind - and
// MngEventData.SendGetEvent's success callback dereferences `r.data.score_ranking` and
// friends unguarded, so a mis-shaped payload takes out the whole recv and its `onComplete`
// is never called. That callback is the completion of a GATING TaskFlow.Step in
// LiveRestartSelectScene.ReloadScene, so the scene hangs on its loading screen forever.
//
// The template is fixed, but accounts created before that keep their stored blob, so the
// shapes are normalised on the way out as well. Coercion only ever replaces a value the
// client could not have parsed anyway; a well-formed object is left exactly as it is.
// `set_member` already writes member_ranking as an object, which is the shape this agrees
// with.
fn normalise_event_shape(event: &mut JsonValue) {
fn ensure_object(slot: &mut JsonValue, template: JsonValue) {
if !slot.is_object() {
*slot = template;
} else {
for (key, value) in template.entries() {
if slot[key].is_null() {
slot[key] = value.clone();
}
}
}
}
ensure_object(&mut event["point_ranking"], object! { rank: 0, point: 0 });
ensure_object(&mut event["score_ranking"], object! { all_rank: 0, group_rank: 0, score: 0 });
ensure_object(
&mut event["member_ranking"],
object! { master_character_id: 0, rank: 0, point: 0 },
);
ensure_object(
&mut event["lottery_box"],
object! { master_lottery_id: 0, reset_count: 0, draw_count_list: array![] },
);
// Read by EventData.SetMultiParameter (help count, penalty window, disconnect flag);
// absent keys bind as 0/false, but sending them keeps the payload self-describing.
for key in ["is_disconnected", "help_count", "penalty_remaining_time"] {
if event[key].is_null() {
event[key] = 0.into();
}
}
if !event["mission_list"].is_array() {
event["mission_list"] = array![];
}
}
pub fn save_event_data(key: &str, event_id: u32, data: JsonValue) {
let mut event = userdata::get_acc_event(key);
// Check for old version of event data
@@ -209,7 +292,7 @@ async fn set_member(Session { key, body }: Session) -> impl Responder {
}))
}
fn get_rank(event: u32, user_id: u64) -> u32 {
pub fn get_rank(event: u32, user_id: u64) -> u32 {
let scores = crate::router::event_ranking::get_raw_info(event);
let mut i=1;
@@ -254,10 +337,14 @@ fn get_star_rank(points: i64) -> i64 {
const LIMIT_COINS: i64 = 2000000000;
fn give_event_points(event_id: u32, amount: i64, user: &mut JsonValue) -> bool {
// The row is keyed by BOTH the event and the point type, exactly as get_points reads it
// back. Matching on the type alone credited whichever event's row happened to come first
// in the list — an account that had ever played a different event got its points there,
// and get_points (which does check the id) then reported 0 for the event just played.
pub fn give_event_points(event_id: u32, amount: i64, user: &mut JsonValue) -> bool {
let mut has = false;
for data in user["event_point_list"].members_mut() {
if data["type"] == 1 {
if data["type"] == 1 && data["master_event_id"] == event_id {
has = true;
let new_amount = data["amount"].as_i64().unwrap() + amount;
if new_amount > LIMIT_COINS {
@@ -278,7 +365,7 @@ fn give_event_points(event_id: u32, amount: i64, user: &mut JsonValue) -> bool {
false
}
fn get_points(event_id: u32, user: &JsonValue) -> i64 {
pub fn get_points(event_id: u32, user: &JsonValue) -> i64 {
for data in user["event_point_list"].members() {
if data["type"] == 1 && data["master_event_id"] == event_id {
return data["amount"].as_i64().unwrap()
@@ -366,3 +453,96 @@ async fn event_end(req: HttpRequest, Session { key, body }: Session) -> impl Res
async fn event_skip(req: HttpRequest, Session { key, body }: Session) -> impl Responder {
Api(event_live(&req, &key, &body, true))
}
#[cfg(test)]
mod tests {
use super::*;
// The client binds these with Unity's JsonUtility against [Serializable] classes:
// ScoreRanking { all_rank, group_rank, score }, MemberRanking { master_character_id,
// rank, point }, PointRanking { rank, point }, LotteryBox { master_lottery_id,
// reset_count, draw_count_list }. An array where an object is expected cannot bind, and
// MngEventData.SendGetEvent's callback dereferences them unguarded.
#[test]
fn the_new_user_template_already_has_the_client_shapes() {
let template: JsonValue =
jzon::parse(&include_file!("src/router/userdata/new_user_event.json")).unwrap();
for key in ["point_ranking", "score_ranking", "member_ranking", "lottery_box"] {
assert!(template[key].is_object(), "{} must be an object", key);
}
assert!(template["mission_list"].is_array());
assert_eq!(template["score_ranking"]["all_rank"], 0);
assert_eq!(template["member_ranking"]["master_character_id"], 0);
assert_eq!(template["lottery_box"]["draw_count_list"], array![]);
}
#[test]
fn stored_blobs_with_the_old_array_shapes_are_coerced() {
// Exactly what accounts created before the template fix have on disk.
let mut stored = object! {
point_ranking: object! { point: 12 },
score_ranking: array![],
member_ranking: array![],
lottery_box: array![],
mission_list: array![],
};
normalise_event_shape(&mut stored);
assert!(stored["score_ranking"].is_object());
assert_eq!(stored["score_ranking"]["all_rank"], 0);
assert!(stored["member_ranking"].is_object());
assert!(stored["lottery_box"].is_object());
assert_eq!(stored["lottery_box"]["draw_count_list"], array![]);
// A key that was already there survives; the missing sibling is filled in.
assert_eq!(stored["point_ranking"]["point"], 12);
assert_eq!(stored["point_ranking"]["rank"], 0);
// The multi trio EventData.SetMultiParameter reads.
assert_eq!(stored["is_disconnected"], 0);
assert_eq!(stored["help_count"], 0);
assert_eq!(stored["penalty_remaining_time"], 0);
}
#[test]
fn well_formed_data_is_left_alone() {
let mut live = object! {
point_ranking: object! { rank: 3, point: 900 },
score_ranking: object! { all_rank: 7, group_rank: 2, score: 4242 },
member_ranking: object! { master_character_id: 5, rank: 1, point: 10 },
lottery_box: object! { master_lottery_id: 8, reset_count: 1, draw_count_list: array![] },
mission_list: array![],
is_disconnected: 1,
help_count: 4,
penalty_remaining_time: 600,
};
let before = live.clone();
normalise_event_shape(&mut live);
assert_eq!(live, before);
}
// give_event_points and get_points must agree on what identifies a row. They did not:
// the write matched on the point type alone, so an account that had ever earned points
// in ANY event credited every later event to that first row — and get_points, which
// does compare the event id, then reported 0 for the event actually played.
#[test]
fn event_points_land_in_the_row_for_that_event() {
let mut user = object!{ event_point_list: array![] };
give_event_points(108, 35, &mut user);
assert_eq!(get_points(108, &user), 35);
// A second event opens a row of its own and leaves the first one alone.
give_event_points(111, 70, &mut user);
assert_eq!(get_points(111, &user), 70);
assert_eq!(get_points(108, &user), 35);
assert_eq!(user["event_point_list"].len(), 2);
// And a second live in the first event still adds to the first row.
give_event_points(108, 35, &mut user);
assert_eq!(get_points(108, &user), 70);
assert_eq!(get_points(111, &user), 70);
assert_eq!(user["event_point_list"].len(), 2);
// An event never played is worth nothing, not somebody else's total.
assert_eq!(get_points(115, &user), 0);
}
}