mirror of
https://git.ethanthesleepy.one/ethanaobrien/ew
synced 2026-10-12 01:24:27 +08:00
Compare commits
22
Commits
8270662666
..
v2.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78362608c8 | ||
|
|
8c58ff5b01 | ||
|
|
536e66811f | ||
|
|
5a55bbf32a | ||
|
|
02afd15890 | ||
|
|
ee4ce5fed4 | ||
|
|
9897a5d04a | ||
|
|
b1b70ab4c1 | ||
|
|
99ba099d1b | ||
|
|
f1c8e96fbe | ||
|
|
abbfb6027a | ||
|
|
b38aec4ecf | ||
|
|
0b137effb5 | ||
|
|
b08af4deb7 | ||
|
|
bd6ae99888 | ||
|
|
500159ecf5 | ||
|
|
33eb7a4002 | ||
|
|
29bf71eb9b | ||
|
|
7ef418fc51 | ||
|
|
e387f1ff0e | ||
|
|
125061863a | ||
|
|
dfc29c70e4 |
Generated
+1
-1
@@ -1085,7 +1085,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ew"
|
||||
version = "1.1.0"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"actix-multipart",
|
||||
"actix-web",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ew"
|
||||
version = "1.1.0"
|
||||
version = "2.0.0"
|
||||
edition = "2024"
|
||||
exclude = ["assets", "*.db"]
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
println!("cargo:rerun-if-changed=src");
|
||||
println!("cargo:rerun-if-changed=webui/index.html");
|
||||
println!("cargo:rerun-if-changed=webui");
|
||||
|
||||
let target = std::env::var("TARGET").unwrap();
|
||||
if target == "aarch64-linux-android" {
|
||||
|
||||
@@ -32,6 +32,7 @@ services:
|
||||
#EN_ANDROID_ASSET_HASH: ""
|
||||
#EN_IOS_ASSET_HASH: ""
|
||||
#WINDOWS_ASSET_HASH: "" # Windows asset hash (JP only; GL Windows is unused)
|
||||
#FORCE_UPDATES: "true" # Force outdated clients to update
|
||||
|
||||
# Everything below is for the "Help" page
|
||||
#ANDROID_GLOBAL: "link.to/patched/android/global.apk"
|
||||
|
||||
@@ -18,6 +18,7 @@ args=(
|
||||
[ "${NERF_CUSTOM_CARDS:-}" = "true" ] && args+=(--nerf-custom-cards)
|
||||
[ "${ENABLE_CUSTOM_3DMV:-}" = "true" ] && args+=(--enable-custom-3dmv)
|
||||
[ "${ENABLE_ARCADE:-}" = "true" ] && args+=(--enable-arcade)
|
||||
[ "${FORCE_UPDATES:-}" = "true" ] && args+=(--force-updates)
|
||||
|
||||
add_opt() {
|
||||
local value="$1" flag="$2"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
pub mod gree;
|
||||
pub mod custom_song;
|
||||
pub mod custom_card;
|
||||
pub mod custom_group;
|
||||
pub mod custom_3dmv;
|
||||
pub mod permissions;
|
||||
pub mod announcements;
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
use lazy_static::lazy_static;
|
||||
use rusqlite::params;
|
||||
use jzon::{array, object, JsonValue};
|
||||
use crate::sql::SQLite;
|
||||
|
||||
lazy_static! {
|
||||
static ref DATABASE: SQLite = SQLite::new("custom_groups.db", setup_tables);
|
||||
}
|
||||
|
||||
pub const FIRST_ID: i64 = 10_000;
|
||||
|
||||
fn setup_tables(conn: &rusqlite::Connection) {
|
||||
conn.execute_batch("CREATE TABLE IF NOT EXISTS groups (
|
||||
id INTEGER PRIMARY KEY, name TEXT NOT NULL, name_en TEXT NOT NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS custom_groups_unique_name ON groups(name COLLATE NOCASE);
|
||||
CREATE TABLE IF NOT EXISTS logos (group_id INTEGER PRIMARY KEY, md5 TEXT NOT NULL, size INTEGER NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS sequence (id INTEGER PRIMARY KEY CHECK (id=1), last_id INTEGER NOT NULL);").unwrap();
|
||||
}
|
||||
|
||||
pub fn list() -> JsonValue {
|
||||
let conn = rusqlite::Connection::open(DATABASE.get_path()).unwrap();
|
||||
let Ok(mut stmt) = conn.prepare("SELECT id, name, name_en, COALESCE(md5, ''), COALESCE(size, 0) FROM groups LEFT JOIN logos ON group_id=id ORDER BY id") else { return array![]; };
|
||||
let Ok(rows) = stmt.query_map(params![], |row| Ok(object! {
|
||||
"id": row.get::<_, i64>(0)?,
|
||||
"name": row.get::<_, String>(1)?,
|
||||
"name_en": row.get::<_, String>(2)?,
|
||||
"logo_md5": row.get::<_, String>(3)?,
|
||||
"logo_size": row.get::<_, i64>(4)?
|
||||
})) else { return array![]; };
|
||||
let mut result = array![];
|
||||
for row in rows.flatten() { result.push(row).unwrap(); }
|
||||
result
|
||||
}
|
||||
|
||||
pub fn set_logo(id: i64, md5: &str, size: i64) -> Result<(), rusqlite::Error> {
|
||||
DATABASE.lock_and_transact(|conn| {
|
||||
conn.execute("INSERT INTO logos (group_id, md5, size) VALUES (?1, ?2, ?3) ON CONFLICT(group_id) DO UPDATE SET md5=excluded.md5, size=excluded.size", params!(id, md5, size))?;
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
pub fn has_logo(md5: &str) -> bool {
|
||||
DATABASE.lock_and_select("SELECT group_id FROM logos WHERE md5=?1", params!(md5)).is_ok()
|
||||
}
|
||||
|
||||
pub fn exists(id: i64) -> bool {
|
||||
if id < FIRST_ID { return false; }
|
||||
DATABASE.lock_and_select("SELECT id FROM groups WHERE id=?1", params!(id)).is_ok()
|
||||
}
|
||||
|
||||
pub fn create(name: &str, name_en: &str) -> Result<i64, rusqlite::Error> {
|
||||
DATABASE.lock_and_transact(|conn| {
|
||||
let last: i64 = conn.query_row("SELECT last_id FROM sequence WHERE id=1", [], |row| row.get(0)).unwrap_or(FIRST_ID - 1);
|
||||
let id = last + 1;
|
||||
conn.execute("INSERT INTO groups (id, name, name_en) VALUES (?1, ?2, ?3)", params!(id, name, name_en))?;
|
||||
conn.execute("INSERT INTO sequence (id, last_id) VALUES (1, ?1) ON CONFLICT(id) DO UPDATE SET last_id=?1", params!(id))?;
|
||||
Ok(id)
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn adds_logo_storage_to_existing_group_database() {
|
||||
let conn = rusqlite::Connection::open_in_memory().unwrap();
|
||||
conn.execute_batch("CREATE TABLE groups (id INTEGER PRIMARY KEY, name TEXT NOT NULL, name_en TEXT NOT NULL);
|
||||
INSERT INTO groups VALUES (10000, 'Team A', 'Team A');").unwrap();
|
||||
setup_tables(&conn);
|
||||
setup_tables(&conn);
|
||||
assert_eq!(conn.query_row("SELECT name FROM groups WHERE id=10000", [], |row| row.get::<_, String>(0)).unwrap(), "Team A");
|
||||
conn.execute("INSERT INTO logos VALUES (10000, 'test-hash', 42)", []).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ pub const CARD: &str = "card";
|
||||
pub const CARD_UPLOAD: &str = "card.upload";
|
||||
pub const CARD_PUBLISH: &str = "card.publish";
|
||||
pub const CARD_EDIT: &str = "card.edit";
|
||||
pub const GROUP_MANAGE: &str = "group.manage";
|
||||
|
||||
pub const PERMISSION: &str = "permission";
|
||||
pub const PERMISSION_GRANT: &str = "permission.grant";
|
||||
@@ -31,6 +32,7 @@ pub const MV_EDIT: &str = "3dmv.edit";
|
||||
pub const SCOPES: &[&str] = &[
|
||||
ALL,
|
||||
CARD, CARD_UPLOAD, CARD_PUBLISH, CARD_EDIT,
|
||||
GROUP_MANAGE,
|
||||
PERMISSION, PERMISSION_GRANT, PERMISSION_REVOKE,
|
||||
ANNOUNCEMENT, ANNOUNCEMENT_MANAGE,
|
||||
MV, MV_EDIT
|
||||
|
||||
+1
-9
@@ -35,22 +35,14 @@ pub async fn run_server(in_thread: bool) -> std::io::Result<()> {
|
||||
|
||||
if args.purge {
|
||||
println!("Purging accounts...");
|
||||
// Cabinets first, so the accounts they take with them are gone before
|
||||
// purge_accounts runs its VACUUM over the same database
|
||||
let machines = crate::router::arcade::purge_machines();
|
||||
if machines > 0 {
|
||||
println!("Purged {} arcade machine(s)", machines);
|
||||
}
|
||||
println!("Purged {} arcade machines", machines);
|
||||
let ct = crate::router::userdata::purge_accounts();
|
||||
println!("Purged {} accounts", ct);
|
||||
}
|
||||
|
||||
router::custom_song::migrate::run();
|
||||
router::custom_song::sweep_audio();
|
||||
router::custom_3dmv::sweep_blobs();
|
||||
// The multi-live relay's expiry timers, on the system arbiter rather than on whichever
|
||||
// HTTP worker happened to serve the first WebSocket upgrade — a worker panic must not
|
||||
// be able to strand every room's seats for the life of the process.
|
||||
router::multi_live::start_sweeper();
|
||||
|
||||
let rv = HttpServer::new(|| App::new()
|
||||
|
||||
@@ -89,6 +89,12 @@ pub struct Args {
|
||||
#[arg(long, default_value = "", help = "Asset version for overridden asset hashes.")]
|
||||
pub asset_version: String,
|
||||
|
||||
#[arg(long, default_value = "", help = "Global asset version for overridden asset hashes.")]
|
||||
pub en_asset_version: String,
|
||||
|
||||
#[arg(long, default_value_t = false, help = "Force an application update for asset versions not marked as latest")]
|
||||
pub force_updates: bool,
|
||||
|
||||
#[arg(long, default_value = "", help = "Asset hash for windows client.")]
|
||||
pub windows_asset_hash: String,
|
||||
|
||||
@@ -115,7 +121,10 @@ pub enum Commands {
|
||||
}
|
||||
|
||||
pub fn get_args() -> Args {
|
||||
#[cfg(not(test))]
|
||||
let mut args = Args::parse();
|
||||
#[cfg(test)]
|
||||
let mut args = Args::parse_from(["ew"]);
|
||||
crate::runtime::overlay_args(&mut args);
|
||||
args
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ pub mod user;
|
||||
pub mod purchase;
|
||||
pub mod tutorial;
|
||||
pub mod mission;
|
||||
pub mod beginner_mission;
|
||||
pub mod home;
|
||||
pub mod lottery;
|
||||
pub mod friend;
|
||||
@@ -24,6 +25,7 @@ pub mod card;
|
||||
pub mod shop;
|
||||
pub mod custom_song;
|
||||
pub mod custom_card;
|
||||
pub mod custom_group;
|
||||
pub mod custom_3dmv;
|
||||
pub mod rich_text;
|
||||
pub mod webui;
|
||||
@@ -301,6 +303,7 @@ pub fn configure(cfg: &mut actix_web::web::ServiceConfig) {
|
||||
);
|
||||
cfg.configure(custom_song::web_routes);
|
||||
cfg.configure(custom_card::web_routes);
|
||||
cfg.configure(custom_group::web_routes);
|
||||
cfg.configure(custom_3dmv::web_routes);
|
||||
cfg.configure(web::routes);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,315 @@
|
||||
use jzon::{array, object, JsonValue};
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
use super::{databases, items};
|
||||
use databases::csv::{self, Region};
|
||||
|
||||
lazy_static! {
|
||||
static ref CELLS: JsonValue = csv::table(Region::Jp, "beginner_mission");
|
||||
static ref SETTINGS: JsonValue = csv::table(Region::Jp, "beginner_mission_reward_setting");
|
||||
static ref REWARDS: JsonValue = csv::table(Region::Jp, "beginner_mission_reward");
|
||||
static ref CARD_LEVELS: JsonValue = csv::table(Region::Jp, "card_level");
|
||||
static ref SKILL_LEVELS: JsonValue = csv::table(Region::Jp, "card_skill_level");
|
||||
}
|
||||
|
||||
// JP /api/mission + /api/mission/receive captures, 2024-03-31:
|
||||
// all sheets accumulate progress; only reached sheets can be claimed. Line
|
||||
// rewards are milestones in the number of completed lines, not named lines.
|
||||
const LINES: [[i64; 3]; 8] = [
|
||||
[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7],
|
||||
[2, 5, 8], [3, 6, 9], [1, 5, 9], [3, 5, 7],
|
||||
];
|
||||
|
||||
pub fn contains(id: i64) -> bool {
|
||||
CELLS.members().any(|c| c["masterMissionId"].as_i64() == Some(id))
|
||||
}
|
||||
|
||||
pub fn level(id: i64) -> i64 {
|
||||
CELLS.members().find(|c| c["masterMissionId"].as_i64() == Some(id))
|
||||
.and_then(|c| c["level"].as_i64()).unwrap_or(0)
|
||||
}
|
||||
|
||||
pub fn is_counter(id: i64) -> bool {
|
||||
contains(id) && matches!(databases::MISSION_LIST[id.to_string()]["conditionType"].as_i64(), Some(5 | 6 | 53 | 62 | 63))
|
||||
}
|
||||
|
||||
pub fn ensure(missions: &mut JsonValue) {
|
||||
for cell in CELLS.members() {
|
||||
let id = cell["masterMissionId"].as_i64().unwrap();
|
||||
let target = databases::MISSION_LIST[id.to_string()]["conditionNumber"].as_i64().unwrap();
|
||||
if let Some(mission) = missions.members_mut().find(|m| m["master_mission_id"].as_i64() == Some(id)) {
|
||||
// The old login hook kept incrementing an already completed cell.
|
||||
// The client subtracts one to animate an unclaimed completion, so
|
||||
// over-target progress would incorrectly make its "before" sheet clear.
|
||||
let progress = if mission["status"].as_i64().unwrap_or(1) >= 2 { target }
|
||||
else { mission["progress"].as_i64().unwrap_or(0).clamp(0, target) };
|
||||
mission["progress"] = progress.into();
|
||||
continue;
|
||||
}
|
||||
missions.push(object! {
|
||||
master_mission_id: id, status: 1, progress: 0,
|
||||
expire_date_time: 0, not_visible: 0
|
||||
}).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_progress(id: i64, progress: i64, missions: &mut JsonValue) -> bool {
|
||||
ensure(missions);
|
||||
let target = databases::MISSION_LIST[id.to_string()]["conditionNumber"].as_i64().unwrap();
|
||||
for mission in missions.members_mut() {
|
||||
if mission["master_mission_id"].as_i64() != Some(id) { continue; }
|
||||
if mission["status"].as_i64().unwrap_or(1) >= 2 { return false; }
|
||||
let progress = progress.max(mission["progress"].as_i64().unwrap_or(0)).min(target);
|
||||
mission["progress"] = progress.into();
|
||||
mission["status"] = if progress >= target { 2 } else { 1 }.into();
|
||||
return progress >= target;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn advance(condition: i64, value: Option<i64>, amount: i64, missions: &mut JsonValue) -> JsonValue {
|
||||
ensure(missions);
|
||||
let mut cleared = array![];
|
||||
for cell in CELLS.members() {
|
||||
let id = cell["masterMissionId"].as_i64().unwrap();
|
||||
let mst = &databases::MISSION_LIST[id.to_string()];
|
||||
if mst["conditionType"].as_i64() != Some(condition) { continue; }
|
||||
if let Some(value) = value {
|
||||
if mst["conditionValues"][0].as_i64() != Some(value) { continue; }
|
||||
}
|
||||
let old = items::get_mission_status(id, missions)["progress"].as_i64().unwrap_or(0);
|
||||
if set_progress(id, old + amount, missions) { cleared.push(id).unwrap(); }
|
||||
}
|
||||
cleared
|
||||
}
|
||||
|
||||
pub fn refresh(user: &JsonValue, missions: &mut JsonValue) -> JsonValue {
|
||||
ensure(missions);
|
||||
let mut cleared = array![];
|
||||
for cell in CELLS.members() {
|
||||
let id = cell["masterMissionId"].as_i64().unwrap();
|
||||
let mst = &databases::MISSION_LIST[id.to_string()];
|
||||
let value = mst["conditionValues"][0].as_i64().unwrap_or(0);
|
||||
let progress = match mst["conditionType"].as_i64().unwrap() {
|
||||
14 | 64 => {
|
||||
let skill = mst["conditionType"] == 64;
|
||||
user["card_list"].members().filter(|card| {
|
||||
let master = super::custom_card::card_info(card["master_card_id"].as_i64().unwrap_or(0));
|
||||
let curve = if skill { databases::CARD_RARITY[master["rarity"].to_string()]["masterCardSkillLevelId"].as_i64() }
|
||||
else { master["masterCardLevelId"].as_i64() };
|
||||
let levels = if skill { &*SKILL_LEVELS } else { &*CARD_LEVELS };
|
||||
levels.members().any(|l| l["id"].as_i64() == curve && l["level"].as_i64() == Some(value)
|
||||
&& card[if skill { "skill_exp" } else { "exp" }].as_i64().unwrap_or(0) >= l["exp"].as_i64().unwrap())
|
||||
}).count() as i64
|
||||
}
|
||||
17 => user["card_list"].members().filter(|c| !c["evolve"].is_empty()).count() as i64,
|
||||
58 => user["character_list"].members().filter_map(|c| c["exp"].as_i64()).max().unwrap_or(0),
|
||||
30 | 60 => {
|
||||
let mut best = 0;
|
||||
for deck in user["deck_list"].members() {
|
||||
let mut types = [0i64; 3];
|
||||
let mut points = [0i64; 3];
|
||||
for slot in deck["main_card_ids"].members() {
|
||||
let Some(card) = user["card_list"].members().find(|c| c["id"] == *slot) else { continue; };
|
||||
let master = super::custom_card::card_info(card["master_card_id"].as_i64().unwrap_or(0));
|
||||
if let Some(t) = master["type"].as_usize().filter(|t| (1..=3).contains(t)) { types[t-1] += 1; }
|
||||
let level = CARD_LEVELS.members().filter(|l| l["id"] == master["masterCardLevelId"]
|
||||
&& l["exp"].as_i64().unwrap() <= card["exp"].as_i64().unwrap_or(0))
|
||||
.max_by_key(|l| l["level"].as_i64());
|
||||
if let Some(l) = level {
|
||||
for (i, name) in ["smile", "pure", "cool"].iter().enumerate() {
|
||||
points[i] += master[*name].as_i64().unwrap_or(0) * l[format!("{}Ratio",name)].as_i64().unwrap_or(0) / 10000;
|
||||
}
|
||||
}
|
||||
}
|
||||
let p = if mst["conditionType"] == 30 {
|
||||
let needed = mst["conditionValues"][1].as_i64().unwrap();
|
||||
(types.iter().any(|n| *n >= needed)) as i64
|
||||
} else { *points.iter().max().unwrap() };
|
||||
best = best.max(p);
|
||||
}
|
||||
best
|
||||
}
|
||||
_ => continue,
|
||||
};
|
||||
if set_progress(id, progress, missions) { cleared.push(id).unwrap(); }
|
||||
}
|
||||
cleared
|
||||
}
|
||||
|
||||
pub fn refresh_account(user: &JsonValue, missions: &mut JsonValue) -> JsonValue {
|
||||
let mut cleared = refresh(user, missions);
|
||||
// Existing accounts may have prepared transfer credentials before missions
|
||||
// were enabled. Merely requesting a code leaves an empty password.
|
||||
if let Some(uid) = user["user"]["id"].as_i64() {
|
||||
if super::userdata::has_transfer_password(uid) {
|
||||
for id in advance(25, None, 1, missions).members() { cleared.push(id.clone()).unwrap(); }
|
||||
}
|
||||
}
|
||||
cleared
|
||||
}
|
||||
|
||||
fn sheet_cells(group: i64, level: i64) -> impl Iterator<Item = &'static JsonValue> {
|
||||
CELLS.members().filter(move |c| c["id"].as_i64() == Some(group) && c["level"].as_i64() == Some(level))
|
||||
}
|
||||
|
||||
fn claimed(group: i64, level: i64, missions: &JsonValue) -> Vec<i64> {
|
||||
sheet_cells(group, level).filter(|c| {
|
||||
items::get_mission_status(c["masterMissionId"].as_i64().unwrap(), missions)["status"] == 3
|
||||
}).filter_map(|c| c["number"].as_i64()).collect()
|
||||
}
|
||||
|
||||
pub fn claimable(id: i64, missions: &JsonValue) -> bool {
|
||||
let Some(cell) = CELLS.members().find(|c| c["masterMissionId"].as_i64() == Some(id)) else { return false; };
|
||||
CELLS.members().filter(|c| c["id"] == cell["id"] && c["level"].as_i64() < cell["level"].as_i64())
|
||||
.all(|c| items::get_mission_status(c["masterMissionId"].as_i64().unwrap(), missions)["status"] == 3)
|
||||
}
|
||||
|
||||
pub fn home_status(missions: &JsonValue) -> (usize, bool) {
|
||||
let all = CELLS.members().all(|c| items::get_mission_status(c["masterMissionId"].as_i64().unwrap(), missions)["status"] == 3);
|
||||
let count = CELLS.members().filter(|c| {
|
||||
let id = c["masterMissionId"].as_i64().unwrap();
|
||||
items::get_mission_status(id, missions)["status"] == 2 && claimable(id, missions)
|
||||
}).count();
|
||||
(count, all)
|
||||
}
|
||||
|
||||
pub fn new_rewards(before: &JsonValue, after: &JsonValue) -> Vec<JsonValue> {
|
||||
let mut settings: Vec<_> = SETTINGS.members().collect();
|
||||
// Captures return line milestones before the full-sheet reward.
|
||||
settings.sort_by_key(|s| (s["masterBeginnerMissionId"].as_i64(), s["level"].as_i64(),
|
||||
if s["number"] == 0 { 9 } else { s["number"].as_i64().unwrap() }));
|
||||
let mut result = Vec::new();
|
||||
for setting in settings {
|
||||
let group = setting["masterBeginnerMissionId"].as_i64().unwrap();
|
||||
let level = setting["level"].as_i64().unwrap();
|
||||
let number = setting["number"].as_i64().unwrap();
|
||||
// JP cross-sheet claims award bonuses only for sheets unlocked before
|
||||
// the request (recorded claims 8 and 11 in the replay fixture).
|
||||
let first = sheet_cells(group, level).next().unwrap()["masterMissionId"].as_i64().unwrap();
|
||||
if !claimable(first, before) { continue; }
|
||||
let achieved = |missions: &JsonValue| {
|
||||
let cells = claimed(group, level, missions);
|
||||
if number == 0 { cells.len() == 9 }
|
||||
else { LINES.iter().filter(|line| line.iter().all(|n| cells.contains(n))).count() >= number as usize }
|
||||
};
|
||||
if !achieved(before) && achieved(after) {
|
||||
result.extend(REWARDS.members().filter(|r| r["id"] == setting["masterBeginnerMissionRewardId"]).cloned());
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn current_inventory_drives_training_bond_and_deck_goals() {
|
||||
let mut user = object! { card_list: [], character_list: [{exp: 30}], deck_list: [] };
|
||||
let master = super::super::custom_card::card_info(10010001);
|
||||
let exp = CARD_LEVELS.members().find(|l| l["id"] == master["masterCardLevelId"] && l["level"] == 30).unwrap()["exp"].clone();
|
||||
let curve = &databases::CARD_RARITY[master["rarity"].to_string()]["masterCardSkillLevelId"];
|
||||
let skill_exp = SKILL_LEVELS.members().find(|l| l["id"] == *curve && l["level"] == 2).unwrap()["exp"].clone();
|
||||
for id in 1..=5 {
|
||||
user["card_list"].push(object! {id: id, master_card_id: 10010001,
|
||||
exp: exp.clone(), skill_exp: skill_exp.clone(), evolve: [{type: 2, count: 1}]}).unwrap();
|
||||
}
|
||||
user["deck_list"].push(object! {main_card_ids: [1,2,3,4,5,0,0,0,0]}).unwrap();
|
||||
let mut missions = array![];
|
||||
let cleared = refresh(&user, &mut missions);
|
||||
for id in [1614001,1614002,1614003,1664001,1664002,1617001,1658001,1658002,1658003,1630001] {
|
||||
assert!(cleared.contains(id), "mission {}", id);
|
||||
}
|
||||
assert!(refresh(&user, &mut missions).is_empty());
|
||||
assert_eq!(items::get_mission_status(1605001, &missions)["progress"], 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn counters_match_values_and_home_counts_only_unlocked_sheet() {
|
||||
let mut missions = array![];
|
||||
assert!(advance(63, Some(1110001), 20, &mut missions).is_empty());
|
||||
assert_eq!(advance(63, Some(8110001), 10, &mut missions), array![1663001]);
|
||||
assert_eq!(items::get_mission_status(1663002, &missions)["progress"], 10);
|
||||
assert_eq!(advance(63, Some(8110001), 10, &mut missions), array![1663002]);
|
||||
assert!(advance(6, Some(1), 1, &mut missions).is_empty());
|
||||
assert_eq!(advance(6, Some(2), 1, &mut missions), array![1606001]);
|
||||
advance(53, None, 3, &mut missions);
|
||||
assert_eq!(home_status(&missions), (2, false));
|
||||
for row in missions.members_mut() { row["status"] = 3.into(); }
|
||||
assert_eq!(home_status(&missions), (0, true));
|
||||
assert!(advance(53, None, 1, &mut missions).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn all_recorded_jp_beginner_claims_match() {
|
||||
let cases = jzon::parse(include_str!("beginner_mission_claims.json")).unwrap();
|
||||
assert_eq!(cases.len(), 15);
|
||||
for (i, case) in cases.members().enumerate() {
|
||||
let mut before = array![];
|
||||
ensure(&mut before);
|
||||
for row in before.members_mut() {
|
||||
if case["before"].contains(row["master_mission_id"].as_i64().unwrap()) { row["status"] = 3.into(); }
|
||||
}
|
||||
let mut after = before.clone();
|
||||
for row in after.members_mut() {
|
||||
if case["claim"].contains(row["master_mission_id"].as_i64().unwrap()) { row["status"] = 3.into(); }
|
||||
}
|
||||
let expected: Vec<_> = case["bonus"].members().map(|x| x.as_i64().unwrap()).collect();
|
||||
let actual: Vec<_> = new_rewards(&before, &after).iter().map(|x| x["amount"].as_i64().unwrap()).collect();
|
||||
assert_eq!(actual, expected, "JP claim {}", i);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn progress_is_capped_and_claims_are_not_reset() {
|
||||
let mut m = array![];
|
||||
advance(5, None, 12, &mut m);
|
||||
assert_eq!(items::get_mission_status(1605001, &m)["progress"], 1);
|
||||
assert_eq!(items::get_mission_status(1605002, &m)["progress"], 10);
|
||||
assert_eq!(items::get_mission_status(1605003, &m)["progress"], 12);
|
||||
assert!(!claimable(1605002, &m));
|
||||
for row in m.members_mut() { if row["master_mission_id"] == 1605001 { row["status"] = 3.into(); } }
|
||||
advance(5, None, 1, &mut m);
|
||||
assert_eq!(items::get_mission_status(1605001, &m)["status"], 3);
|
||||
for row in m.members_mut() {
|
||||
if row["master_mission_id"] == 1653001 {
|
||||
row["status"] = 2.into();
|
||||
row["progress"] = 50.into();
|
||||
}
|
||||
}
|
||||
ensure(&mut m);
|
||||
assert_eq!(items::get_mission_status(1653001, &m)["progress"], 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn captured_first_sheet_claim_awards_one_line() {
|
||||
let mut before = array![];
|
||||
ensure(&mut before);
|
||||
let mut after = before.clone();
|
||||
for row in after.members_mut() {
|
||||
if [1653001,1605001,1663001,1666001,1658001].contains(&row["master_mission_id"].as_i64().unwrap()) {
|
||||
row["status"] = 3.into();
|
||||
}
|
||||
}
|
||||
let r = new_rewards(&before, &after);
|
||||
assert_eq!(r.len(), 1);
|
||||
assert_eq!(r[0]["amount"], 50);
|
||||
assert!(new_rewards(&after, &after).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_sheet_gives_eight_lines_and_sheet_once() {
|
||||
let mut before = array![];
|
||||
ensure(&mut before);
|
||||
let mut after = before.clone();
|
||||
for row in after.members_mut() {
|
||||
if sheet_cells(1,1).any(|c| c["masterMissionId"] == row["master_mission_id"]) { row["status"] = 3.into(); }
|
||||
}
|
||||
let r = new_rewards(&before, &after);
|
||||
assert_eq!(r.len(), 9);
|
||||
assert_eq!(r.iter().map(|r| r["amount"].as_i64().unwrap()).sum::<i64>(), 1000);
|
||||
assert!(claimable(1605002, &after));
|
||||
assert!(!home_status(&after).1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,314 @@
|
||||
[
|
||||
{
|
||||
"before": [],
|
||||
"claim": [
|
||||
1653001
|
||||
],
|
||||
"bonus": []
|
||||
},
|
||||
{
|
||||
"before": [
|
||||
1653001,
|
||||
1660001,
|
||||
1666001
|
||||
],
|
||||
"claim": [
|
||||
1605001,
|
||||
1663001,
|
||||
1614001,
|
||||
1658001,
|
||||
1661001
|
||||
],
|
||||
"bonus": [
|
||||
50,
|
||||
50,
|
||||
50,
|
||||
50,
|
||||
50
|
||||
]
|
||||
},
|
||||
{
|
||||
"before": [],
|
||||
"claim": [
|
||||
1653001
|
||||
],
|
||||
"bonus": []
|
||||
},
|
||||
{
|
||||
"before": [],
|
||||
"claim": [
|
||||
1653001
|
||||
],
|
||||
"bonus": []
|
||||
},
|
||||
{
|
||||
"before": [],
|
||||
"claim": [
|
||||
1653001
|
||||
],
|
||||
"bonus": []
|
||||
},
|
||||
{
|
||||
"before": [
|
||||
1653001
|
||||
],
|
||||
"claim": [
|
||||
1605001,
|
||||
1663001,
|
||||
1660001,
|
||||
1614001
|
||||
],
|
||||
"bonus": [
|
||||
50
|
||||
]
|
||||
},
|
||||
{
|
||||
"before": [],
|
||||
"claim": [
|
||||
1653001,
|
||||
1605001,
|
||||
1663001,
|
||||
1666001,
|
||||
1658001
|
||||
],
|
||||
"bonus": [
|
||||
50
|
||||
]
|
||||
},
|
||||
{
|
||||
"before": [
|
||||
1653001,
|
||||
1605001,
|
||||
1663001,
|
||||
1666001,
|
||||
1658001
|
||||
],
|
||||
"claim": [
|
||||
1614001
|
||||
],
|
||||
"bonus": []
|
||||
},
|
||||
{
|
||||
"before": [
|
||||
1653001,
|
||||
1605001,
|
||||
1663001,
|
||||
1614001,
|
||||
1666001,
|
||||
1658001
|
||||
],
|
||||
"claim": [
|
||||
1662001,
|
||||
1660001,
|
||||
1661001,
|
||||
1653002,
|
||||
1658002,
|
||||
1630001,
|
||||
1663002
|
||||
],
|
||||
"bonus": [
|
||||
50,
|
||||
50,
|
||||
50,
|
||||
50,
|
||||
50,
|
||||
100,
|
||||
100,
|
||||
500
|
||||
]
|
||||
},
|
||||
{
|
||||
"before": [
|
||||
1653001,
|
||||
1605001,
|
||||
1662001,
|
||||
1663001,
|
||||
1660001,
|
||||
1614001,
|
||||
1666001,
|
||||
1658001,
|
||||
1661001,
|
||||
1653002,
|
||||
1658002,
|
||||
1630001,
|
||||
1663002
|
||||
],
|
||||
"claim": [
|
||||
1665001
|
||||
],
|
||||
"bonus": [
|
||||
50
|
||||
]
|
||||
},
|
||||
{
|
||||
"before": [
|
||||
1653001,
|
||||
1605001,
|
||||
1662001,
|
||||
1663001,
|
||||
1660001,
|
||||
1614001,
|
||||
1666001,
|
||||
1658001,
|
||||
1661001,
|
||||
1653002,
|
||||
1665001,
|
||||
1658002,
|
||||
1630001,
|
||||
1663002
|
||||
],
|
||||
"claim": [
|
||||
1664001,
|
||||
1614002
|
||||
],
|
||||
"bonus": [
|
||||
50
|
||||
]
|
||||
},
|
||||
{
|
||||
"before": [
|
||||
1653001,
|
||||
1605001,
|
||||
1662001,
|
||||
1663001,
|
||||
1660001,
|
||||
1614001,
|
||||
1666001,
|
||||
1658001,
|
||||
1661001,
|
||||
1653002,
|
||||
1665001,
|
||||
1664001,
|
||||
1658002,
|
||||
1630001,
|
||||
1614002,
|
||||
1663002
|
||||
],
|
||||
"claim": [
|
||||
1605002,
|
||||
1606001,
|
||||
1653003,
|
||||
1660002,
|
||||
1617001,
|
||||
1658003,
|
||||
1625001
|
||||
],
|
||||
"bonus": [
|
||||
50,
|
||||
50,
|
||||
50,
|
||||
100,
|
||||
100,
|
||||
500
|
||||
]
|
||||
},
|
||||
{
|
||||
"before": [
|
||||
1653001,
|
||||
1605001,
|
||||
1662001,
|
||||
1663001,
|
||||
1660001,
|
||||
1614001,
|
||||
1666001,
|
||||
1658001,
|
||||
1661001,
|
||||
1653002,
|
||||
1605002,
|
||||
1665001,
|
||||
1664001,
|
||||
1658002,
|
||||
1630001,
|
||||
1606001,
|
||||
1614002,
|
||||
1663002,
|
||||
1653003,
|
||||
1660002,
|
||||
1617001,
|
||||
1658003,
|
||||
1625001
|
||||
],
|
||||
"claim": [
|
||||
1626001
|
||||
],
|
||||
"bonus": [
|
||||
50,
|
||||
50
|
||||
]
|
||||
},
|
||||
{
|
||||
"before": [
|
||||
1653001,
|
||||
1605001,
|
||||
1662001,
|
||||
1663001,
|
||||
1660001,
|
||||
1614001,
|
||||
1666001,
|
||||
1658001,
|
||||
1661001,
|
||||
1653002,
|
||||
1605002,
|
||||
1665001,
|
||||
1664001,
|
||||
1658002,
|
||||
1630001,
|
||||
1606001,
|
||||
1614002,
|
||||
1663002,
|
||||
1653003,
|
||||
1660002,
|
||||
1617001,
|
||||
1658003,
|
||||
1625001,
|
||||
1626001
|
||||
],
|
||||
"claim": [
|
||||
1614003,
|
||||
1664002
|
||||
],
|
||||
"bonus": [
|
||||
50,
|
||||
50,
|
||||
50
|
||||
]
|
||||
},
|
||||
{
|
||||
"before": [
|
||||
1653001,
|
||||
1605001,
|
||||
1662001,
|
||||
1663001,
|
||||
1660001,
|
||||
1614001,
|
||||
1666001,
|
||||
1658001,
|
||||
1661001,
|
||||
1653002,
|
||||
1605002,
|
||||
1665001,
|
||||
1664001,
|
||||
1658002,
|
||||
1630001,
|
||||
1606001,
|
||||
1614002,
|
||||
1663002,
|
||||
1653003,
|
||||
1660002,
|
||||
1617001,
|
||||
1658003,
|
||||
1625001,
|
||||
1614003,
|
||||
1664002,
|
||||
1626001
|
||||
],
|
||||
"claim": [
|
||||
1605003
|
||||
],
|
||||
"bonus": [
|
||||
100,
|
||||
100,
|
||||
500
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -134,6 +134,11 @@ async fn reinforce(Session { key, body }: Session) -> impl Responder {
|
||||
let mut clear_mission_ids = array![];
|
||||
|
||||
let card = do_reinforce(&mut user, &body, "exp", 1, false, &mut array![], &mut array![], &mut clear_mission_ids);
|
||||
let mut missions = userdata::get_acc_missions(&key);
|
||||
for id in super::beginner_mission::refresh(&user, &mut missions).members() {
|
||||
clear_mission_ids.push(id.clone()).unwrap();
|
||||
}
|
||||
userdata::save_acc_missions(&key, missions);
|
||||
|
||||
userdata::save_acc(&key, user.clone());
|
||||
|
||||
@@ -150,6 +155,11 @@ async fn skill_reinforce(Session { key, body }: Session) -> impl Responder {
|
||||
let mut clear_mission_ids = array![];
|
||||
|
||||
let card = do_reinforce(&mut user, &body, "skill_exp", 10, false, &mut array![], &mut array![], &mut clear_mission_ids);
|
||||
let mut missions = userdata::get_acc_missions(&key);
|
||||
for id in super::beginner_mission::refresh(&user, &mut missions).members() {
|
||||
clear_mission_ids.push(id.clone()).unwrap();
|
||||
}
|
||||
userdata::save_acc_missions(&key, missions);
|
||||
|
||||
userdata::save_acc(&key, user.clone());
|
||||
|
||||
@@ -168,6 +178,7 @@ async fn evolve(Session { key, body }: Session) -> impl Responder {
|
||||
let mut clear_mission_ids = array![];
|
||||
|
||||
let card = do_reinforce(&mut user, &body, "", 0, true, &mut missions, &mut chats, &mut clear_mission_ids);
|
||||
for id in super::beginner_mission::refresh(&user, &mut missions).members() { clear_mission_ids.push(id.clone()).unwrap(); }
|
||||
|
||||
userdata::save_acc(&key, user.clone());
|
||||
userdata::save_acc_chats(&key, chats);
|
||||
|
||||
@@ -60,6 +60,7 @@ pub fn add_chat_from_chapter_id(chapter_id: i64, chats: &mut JsonValue) -> bool
|
||||
}
|
||||
|
||||
async fn home(Login(key): Login) -> impl Responder {
|
||||
refresh_birthday(&key, &userdata::get_acc(&key));
|
||||
let chats = userdata::get_acc_chats(&key);
|
||||
|
||||
let mut rooms = array![];
|
||||
@@ -75,6 +76,54 @@ async fn home(Login(key): Login) -> impl Responder {
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn refresh_birthday(key: &str, user: &JsonValue) {
|
||||
if !super::user::valid_birthday(user["user"]["birthday"].as_str().unwrap_or("")) { return; }
|
||||
let now = global::set_time(global::timestamp(), user["user"]["id"].as_i64().unwrap(), true);
|
||||
if !is_birthday(user, now) { return; }
|
||||
let mut chats = userdata::get_acc_chats(key);
|
||||
let mut missions = userdata::get_acc_missions(key);
|
||||
if grant_birthday_chats(user, &mut chats, &mut missions, now) {
|
||||
userdata::save_acc_chats(key, chats);
|
||||
userdata::save_acc_missions(key, missions);
|
||||
}
|
||||
}
|
||||
|
||||
fn is_birthday(user: &JsonValue, now: u64) -> bool {
|
||||
let date = global::format_datetime(now);
|
||||
let birthday = user["user"]["birthday"].as_str().unwrap_or("");
|
||||
birthday == format!("{}{}", &date[5..7], &date[8..10])
|
||||
}
|
||||
|
||||
fn grant_birthday_chats(user: &JsonValue, chats: &mut JsonValue, missions: &mut JsonValue, now: u64) -> bool {
|
||||
if !is_birthday(user, now) { return false; }
|
||||
let mut changed = false;
|
||||
for (_, master) in databases::MISSION_LIST.entries() {
|
||||
if master["conditionType"] != 75 { continue; }
|
||||
let character = &master["conditionValues"][0];
|
||||
let target = master["conditionNumber"].as_i64().unwrap();
|
||||
if !user["character_list"].members().any(|c|
|
||||
c["master_character_id"] == *character && c["exp"].as_i64().unwrap_or(0) >= target) { continue; }
|
||||
let id = master["id"].as_i64().unwrap();
|
||||
if items::get_mission_status(id, missions)["status"] == 3 { continue; }
|
||||
for reward in databases::MISSION_REWARDS[master["masterMissionRewardId"].to_string()].members().filter(|r| r["type"] == 16) {
|
||||
for (_, by_room) in databases::CHATS.entries() {
|
||||
for (_, room) in by_room.entries().filter(|(_, r)| r["id"] == reward["value"]) {
|
||||
add_chat(room["masterChatId"].as_i64().unwrap(), room["roomId"].as_i64().unwrap(), chats);
|
||||
}
|
||||
}
|
||||
}
|
||||
if !missions.members().any(|m| m["master_mission_id"] == id) {
|
||||
missions.push(object! {master_mission_id: id, status: 1, progress: 0, expire_date_time: 0, not_visible: 0}).unwrap();
|
||||
}
|
||||
for mission in missions.members_mut().filter(|m| m["master_mission_id"] == id) {
|
||||
mission["status"] = 3.into();
|
||||
mission["progress"] = target.into();
|
||||
}
|
||||
changed = true;
|
||||
}
|
||||
changed
|
||||
}
|
||||
|
||||
async fn start() -> impl Responder {
|
||||
Api(Some(object!{"select_talk_id_list":[],"get_item_list":[],"is_read":0}))
|
||||
}
|
||||
@@ -102,6 +151,25 @@ async fn end(Session { key, body }: Session) -> impl Responder {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn birthday_chats_require_the_date_and_bond_and_are_not_duplicated() {
|
||||
let _test = crate::runtime::lock_test_data_path();
|
||||
let user = object! { user: {birthday: "0531"}, character_list: [
|
||||
{master_character_id: 1001, exp: 50}, {master_character_id: 1002, exp: 49}
|
||||
]};
|
||||
let mut chats = array![];
|
||||
let mut missions = array![];
|
||||
let day = global::parse_datetime("2024/05/31 12:00:00").unwrap();
|
||||
assert!(!grant_birthday_chats(&user, &mut chats, &mut missions, day - 86400));
|
||||
assert!(grant_birthday_chats(&user, &mut chats, &mut missions, day));
|
||||
assert_eq!(chats.len(), 1);
|
||||
assert_eq!(chats[0]["chapter_id"], 100100301);
|
||||
assert_eq!(missions[0]["status"], 3);
|
||||
assert_eq!(missions[0]["progress"], 50);
|
||||
assert!(!grant_birthday_chats(&user, &mut chats, &mut missions, day));
|
||||
assert_eq!(chats.len(), 1);
|
||||
}
|
||||
|
||||
// Verbatim from an official /api/chat/home capture (JP log, the 65-occurrence
|
||||
// baseline seen on accounts that had earned no extra stamps yet). Both the JP and EN
|
||||
// chat_stamp tables reproduce it exactly from _initialStamp, which is what lets
|
||||
|
||||
@@ -13,6 +13,7 @@ use crate::router::{databases, global, rich_text, userdata, webui, Login, Api};
|
||||
use crate::router::databases::csv::{table, Region};
|
||||
use crate::router::custom_song::audio;
|
||||
use crate::database::custom_card as database;
|
||||
use crate::database::custom_group;
|
||||
use crate::database::permissions;
|
||||
use crate::runtime::get_data_path;
|
||||
use crate::lock_onto_mutex;
|
||||
@@ -700,7 +701,7 @@ pub fn owned_runtime_ids(user: &JsonValue) -> Vec<i64> {
|
||||
// The catalog is filtered per requesting user: everyone gets the published
|
||||
// cards, the owner additionally gets their drafts, and a game account that
|
||||
// already owns a card keeps resolving it even if it was since unpublished
|
||||
async fn list(Login(key): Login) -> impl Responder {
|
||||
async fn list(req: HttpRequest, Login(key): Login) -> impl Responder {
|
||||
if disabled() {
|
||||
// As if the endpoint doesn't exist - the client treats this as feature-off
|
||||
return Api(None);
|
||||
@@ -713,9 +714,17 @@ async fn list(Login(key): Login) -> impl Responder {
|
||||
nerf_clamp_card(card);
|
||||
}
|
||||
}
|
||||
let characters = database::get_characters_for_cards(uid, &cards);
|
||||
let mut characters = database::get_characters_for_cards(uid, &cards);
|
||||
if global::client_protocol_version(&req) < crate::router::custom_group::PROTOCOL_VERSION {
|
||||
for character in characters.members_mut() {
|
||||
if character["master_group_id"].as_i64().unwrap_or(0) >= custom_group::FIRST_ID {
|
||||
character["master_group_id"] = CHARACTER_GROUP_ID.into();
|
||||
}
|
||||
}
|
||||
}
|
||||
Api(Some(object!{
|
||||
"revision": database::get_revision(),
|
||||
"groups": custom_group::list(),
|
||||
"characters": characters,
|
||||
"cards": cards
|
||||
}))
|
||||
@@ -1352,6 +1361,13 @@ fn valid_color(color: &str) -> bool {
|
||||
// Every column the uploader never supplies is forced to the value all 172
|
||||
// imported characters carry. Numbers, not enum names
|
||||
fn build_character(master_character_id: i64, fields: &Fields, stored: &JsonValue) -> Result<JsonValue, String> {
|
||||
let group_text = text_of(fields, "character_master_group_id", stored, "master_group_id");
|
||||
let group_id = if group_text.is_empty() { CHARACTER_GROUP_ID } else {
|
||||
group_text.parse::<i64>().map_err(|_| String::from("Invalid group ID"))?
|
||||
};
|
||||
if group_id != CHARACTER_GROUP_ID && !custom_group::exists(group_id) {
|
||||
return Err(String::from("Custom group does not exist"));
|
||||
}
|
||||
for (key, label) in [
|
||||
("character_name", "Character name"),
|
||||
("character_name_en", "Character English name"),
|
||||
@@ -1398,7 +1414,7 @@ fn build_character(master_character_id: i64, fields: &Fields, stored: &JsonValue
|
||||
"category": CHARACTER_CATEGORY_OTHER,
|
||||
"school_grade": CHARACTER_SCHOOL_GRADE,
|
||||
"chara_category": CHARACTER_CHARA_CATEGORY,
|
||||
"master_group_id": CHARACTER_GROUP_ID,
|
||||
"master_group_id": group_id,
|
||||
"sprite_name": "",
|
||||
"display_order": master_character_id,
|
||||
"height": text("height"),
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
use actix_web::{web, HttpRequest, HttpResponse, http::header::ContentType};
|
||||
use jzon::{object, JsonValue};
|
||||
use crate::database::{custom_group, permissions};
|
||||
use crate::router::{userdata, webui, rich_text};
|
||||
use std::io::Cursor;
|
||||
use futures_util::StreamExt;
|
||||
|
||||
pub const PROTOCOL_VERSION: u32 = 6;
|
||||
|
||||
pub fn web_routes(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(web::scope("/custom_group")
|
||||
.route("/list", web::get().to(list))
|
||||
.route("/create", web::post().to(create))
|
||||
.route("/{id}/logo", web::post().to(upload_logo))
|
||||
.route("/data/{hash}/{file}", web::get().to(logo_data)));
|
||||
}
|
||||
|
||||
fn can_manage(req: &HttpRequest) -> bool {
|
||||
webui::get_login_token(req)
|
||||
.and_then(|token| userdata::webui_login_token(&token))
|
||||
.and_then(|key| userdata::get_acc(&key)["user"]["id"].as_i64())
|
||||
.is_some_and(|id| permissions::has(id, permissions::GROUP_MANAGE))
|
||||
}
|
||||
|
||||
fn encode_logo(bytes: &[u8]) -> Result<Vec<u8>, String> {
|
||||
let mut reader = image::ImageReader::new(Cursor::new(bytes))
|
||||
.with_guessed_format().map_err(|_| "Invalid image")?;
|
||||
if !matches!(reader.format(), Some(image::ImageFormat::Png | image::ImageFormat::Jpeg)) {
|
||||
return Err("Logo must be PNG or JPEG".into());
|
||||
}
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_image_width = Some(4096);
|
||||
limits.max_image_height = Some(4096);
|
||||
limits.max_alloc = Some(64 * 1024 * 1024);
|
||||
reader.limits(limits);
|
||||
let image = reader.decode().map_err(|_| "Invalid or oversized image")?.thumbnail(512, 256);
|
||||
let mut png = Cursor::new(Vec::new());
|
||||
image.write_to(&mut png, image::ImageFormat::Png).map_err(|_| "Could not encode logo")?;
|
||||
Ok(png.into_inner())
|
||||
}
|
||||
|
||||
async fn upload_logo(req: HttpRequest, id: web::Path<i64>, mut payload: web::Payload) -> HttpResponse {
|
||||
if !can_manage(&req) { return HttpResponse::Forbidden().finish(); }
|
||||
if !custom_group::exists(*id) { return HttpResponse::NotFound().finish(); }
|
||||
let mut bytes = Vec::new();
|
||||
while let Some(chunk) = payload.next().await {
|
||||
let Ok(chunk) = chunk else { return HttpResponse::BadRequest().finish(); };
|
||||
if bytes.len() + chunk.len() > 4 * 1024 * 1024 { return HttpResponse::PayloadTooLarge().finish(); }
|
||||
bytes.extend_from_slice(&chunk);
|
||||
}
|
||||
let png = match web::block(move || encode_logo(&bytes)).await {
|
||||
Ok(Ok(png)) => png,
|
||||
Ok(Err(message)) => return reply(object! { "result": "ERR", "message": message }),
|
||||
Err(_) => return HttpResponse::InternalServerError().finish(),
|
||||
};
|
||||
let hash = format!("{:x}", md5::compute(&png));
|
||||
let dir = crate::runtime::get_data_path("custom_groups");
|
||||
if store_logo(&dir, &hash, &png).is_err()
|
||||
|| custom_group::set_logo(*id, &hash, png.len() as i64).is_err() {
|
||||
return HttpResponse::InternalServerError().finish();
|
||||
}
|
||||
crate::database::custom_song::bump_revision();
|
||||
crate::database::custom_card::bump_revision();
|
||||
reply(object! { "result": "OK", "logo_md5": hash, "logo_size": png.len() })
|
||||
}
|
||||
|
||||
fn store_logo(dir: &str, hash: &str, png: &[u8]) -> std::io::Result<()> {
|
||||
std::fs::create_dir_all(dir)?;
|
||||
let destination = format!("{dir}/{hash}.png");
|
||||
if std::fs::read(&destination).is_ok_and(|bytes| bytes == png) { return Ok(()); }
|
||||
let temporary = format!("{dir}/{}.tmp", uuid::Uuid::now_v7());
|
||||
let result = std::fs::write(&temporary, png).and_then(|_| std::fs::rename(&temporary, destination));
|
||||
if result.is_err() { let _ = std::fs::remove_file(temporary); }
|
||||
result
|
||||
}
|
||||
|
||||
async fn logo_data(path: web::Path<(String, String)>) -> HttpResponse {
|
||||
let (hash, file) = path.into_inner();
|
||||
if hash.len() != 32 || !hash.bytes().all(|b| b.is_ascii_hexdigit())
|
||||
|| file != format!("{hash}.png") || !custom_group::has_logo(&hash) {
|
||||
return HttpResponse::NotFound().finish();
|
||||
}
|
||||
match std::fs::read(crate::runtime::get_data_path(&format!("custom_groups/{hash}.png"))) {
|
||||
Ok(bytes) if format!("{:x}", md5::compute(&bytes)) == hash => HttpResponse::Ok()
|
||||
.insert_header(("Cache-Control", "public, max-age=31536000, immutable"))
|
||||
.content_type("image/png").body(bytes),
|
||||
_ => HttpResponse::NotFound().finish(),
|
||||
}
|
||||
}
|
||||
|
||||
fn reply(value: JsonValue) -> HttpResponse {
|
||||
HttpResponse::Ok().insert_header(ContentType::json()).body(jzon::stringify(value))
|
||||
}
|
||||
|
||||
async fn list() -> HttpResponse {
|
||||
reply(object! { "result": "OK", "groups": custom_group::list() })
|
||||
}
|
||||
|
||||
async fn create(req: HttpRequest, body: String) -> HttpResponse {
|
||||
if !can_manage(&req) {
|
||||
return reply(object! { "result": "ERR", "message": "You do not have permission to create groups" });
|
||||
}
|
||||
let Ok(data) = jzon::parse(&body) else {
|
||||
return reply(object! { "result": "ERR", "message": "Invalid JSON" });
|
||||
};
|
||||
let name = data["name"].as_str().unwrap_or("").trim();
|
||||
let name_en = data["name_en"].as_str().unwrap_or("").trim();
|
||||
if name.is_empty() || name.chars().count() > 60 || name_en.chars().count() > 60 {
|
||||
return reply(object! { "result": "ERR", "message": "Group name must be 1-60 characters (English name at most 60)" });
|
||||
}
|
||||
if let Err(message) = rich_text::reject_tags("Group name", name, &[]) {
|
||||
return reply(object! { "result": "ERR", "message": message });
|
||||
}
|
||||
if let Err(message) = rich_text::reject_tags("English group name", name_en, &[]) {
|
||||
return reply(object! { "result": "ERR", "message": message });
|
||||
}
|
||||
match custom_group::create(name, name_en) {
|
||||
Ok(id) => reply(object! { "result": "OK", "id": id }),
|
||||
Err(_) => reply(object! { "result": "ERR", "message": "A group with that name already exists, or it could not be stored" })
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn logo_preserves_aspect_and_transparency() {
|
||||
let source = image::DynamicImage::ImageRgba8(image::RgbaImage::from_pixel(1024, 256, image::Rgba([1, 2, 3, 64])));
|
||||
let mut input = Cursor::new(Vec::new());
|
||||
source.write_to(&mut input, image::ImageFormat::Png).unwrap();
|
||||
let output = encode_logo(input.get_ref()).unwrap();
|
||||
let decoded = image::load_from_memory(&output).unwrap().to_rgba8();
|
||||
assert_eq!(decoded.dimensions(), (512, 128));
|
||||
assert_eq!(decoded.get_pixel(0, 0).0[3], 64);
|
||||
assert!(encode_logo(b"not an image").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_oversized_dimensions() {
|
||||
let source = image::DynamicImage::new_rgba8(4097, 1);
|
||||
let mut input = Cursor::new(Vec::new());
|
||||
source.write_to(&mut input, image::ImageFormat::Png).unwrap();
|
||||
assert!(encode_logo(input.get_ref()).is_err());
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn logo_upload_requires_group_permission() {
|
||||
let app = actix_web::test::init_service(actix_web::App::new().configure(web_routes)).await;
|
||||
let request = actix_web::test::TestRequest::post().uri("/custom_group/10000/logo").set_payload("invalid").to_request();
|
||||
assert_eq!(actix_web::test::call_service(&app, request).await.status(), actix_web::http::StatusCode::FORBIDDEN);
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn logo_catalog_and_content_addressed_download() {
|
||||
let _lock = crate::runtime::lock_test_data_path();
|
||||
let id = custom_group::create(&format!("Logo test {}", uuid::Uuid::now_v7()), "").unwrap();
|
||||
let mut input = Cursor::new(Vec::new());
|
||||
image::DynamicImage::new_rgba8(32, 16).write_to(&mut input, image::ImageFormat::Png).unwrap();
|
||||
let png = encode_logo(input.get_ref()).unwrap();
|
||||
let hash = format!("{:x}", md5::compute(&png));
|
||||
let dir = crate::runtime::get_data_path("custom_groups");
|
||||
store_logo(&dir, &hash, &png).unwrap();
|
||||
store_logo(&dir, &hash, &png).unwrap();
|
||||
custom_group::set_logo(id, &hash, png.len() as i64).unwrap();
|
||||
let catalog = custom_group::list();
|
||||
let row = catalog.members().find(|g| g["id"].as_i64() == Some(id)).unwrap();
|
||||
assert_eq!(row["logo_md5"].as_str(), Some(hash.as_str()));
|
||||
assert_eq!(row["logo_size"].as_usize(), Some(png.len()));
|
||||
let app = actix_web::test::init_service(actix_web::App::new().configure(web_routes)).await;
|
||||
let request = actix_web::test::TestRequest::get().uri(&format!("/custom_group/data/{hash}/{hash}.png")).to_request();
|
||||
let response = actix_web::test::call_service(&app, request).await;
|
||||
assert_eq!(response.status(), actix_web::http::StatusCode::OK);
|
||||
assert_eq!(actix_web::test::read_body(response).await.as_ref(), png);
|
||||
let request = actix_web::test::TestRequest::get().uri(&format!("/custom_group/data/{hash}/wrong.png")).to_request();
|
||||
assert_eq!(actix_web::test::call_service(&app, request).await.status(), actix_web::http::StatusCode::NOT_FOUND);
|
||||
std::fs::write(format!("{dir}/{hash}.png"), b"corrupted").unwrap();
|
||||
let request = actix_web::test::TestRequest::get().uri(&format!("/custom_group/data/{hash}/{hash}.png")).to_request();
|
||||
assert_eq!(actix_web::test::call_service(&app, request).await.status(), actix_web::http::StatusCode::NOT_FOUND);
|
||||
}
|
||||
}
|
||||
+49
-177
@@ -2,9 +2,6 @@
|
||||
// in-process symphonia + vorbis machinery
|
||||
pub mod audio;
|
||||
mod chart;
|
||||
// One-time startup regroup of charts stored before the spawn-group pairing rule; called from
|
||||
// run_server, no-op when the feature is disabled or every chart is already correctly grouped
|
||||
pub mod migrate;
|
||||
mod package;
|
||||
|
||||
use jzon::{array, object, JsonValue};
|
||||
@@ -18,6 +15,7 @@ use std::sync::Mutex;
|
||||
|
||||
use crate::router::{global, rich_text, userdata, webui, Login, Api};
|
||||
use crate::database::custom_song as database;
|
||||
use crate::database::custom_group;
|
||||
use crate::runtime::get_data_path;
|
||||
use crate::lock_onto_mutex;
|
||||
|
||||
@@ -39,6 +37,18 @@ use crate::lock_onto_mutex;
|
||||
// Shock.BAND_CATEGORY enum names
|
||||
const BAND_CATEGORIES: &[&str] = &["NONE", "MUSE", "AQOURS", "NIJIGAKU", "LIELLA", "HASUNOSORA", "OTHER", "YOHANE"];
|
||||
|
||||
fn selected_group_id(value: &str, band_category: &mut String) -> Result<i64, String> {
|
||||
if value.is_empty() || value == "0" {
|
||||
return Ok(database::band_group_id(band_category));
|
||||
}
|
||||
let id = value.parse::<i64>().map_err(|_| String::from("Invalid group ID"))?;
|
||||
if !custom_group::exists(id) {
|
||||
return Err(String::from("Custom group does not exist"));
|
||||
}
|
||||
*band_category = String::from("OTHER");
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
// NORMAL, HARD, EXPERT, MASTER
|
||||
const LEVEL_COUNT: i64 = 4;
|
||||
const DEFAULT_LEVEL_NUMBERS: &[i64] = &[3, 6, 9, 12];
|
||||
@@ -129,13 +139,20 @@ pub fn client_supports_custom_songs(req: &HttpRequest) -> bool {
|
||||
|
||||
// The catalog is filtered per requesting user: private songs only show for
|
||||
// their owner, shared songs for the owner plus their shared-user list
|
||||
async fn list(Login(key): Login) -> impl Responder {
|
||||
async fn list(req: HttpRequest, Login(key): Login) -> impl Responder {
|
||||
if disabled() {
|
||||
// As if the endpoint doesn't exist - the client treats this as feature-off
|
||||
return Api(None);
|
||||
}
|
||||
let uid = userdata::get_acc(&key)["user"]["id"].as_i64().unwrap();
|
||||
let mut songs = database::get_songs_for_user(uid);
|
||||
if global::client_protocol_version(&req) < crate::router::custom_group::PROTOCOL_VERSION {
|
||||
for song in songs.members_mut() {
|
||||
if song["master_group_id"].as_i64().unwrap_or(0) >= custom_group::FIRST_ID {
|
||||
song["master_group_id"] = database::band_group_id("OTHER").into();
|
||||
}
|
||||
}
|
||||
}
|
||||
for song in songs.members_mut() {
|
||||
// Additive field: the client turns it into the song's detail-info credit line (the
|
||||
// staff-credits text the live loading screen and the music library show). Old clients
|
||||
@@ -149,6 +166,7 @@ async fn list(Login(key): Login) -> impl Responder {
|
||||
}
|
||||
Api(Some(object!{
|
||||
"revision": database::get_revision(),
|
||||
"groups": custom_group::list(),
|
||||
"songs": songs
|
||||
}))
|
||||
}
|
||||
@@ -649,6 +667,7 @@ fn create_song(uid: i64, fields: &HashMap<String, Vec<u8>>) -> Result<i64, Strin
|
||||
if !BAND_CATEGORIES.contains(&band_category.as_str()) {
|
||||
return Err(format!("Unknown band category '{}'", band_category));
|
||||
}
|
||||
let master_group_id = selected_group_id(&field_str(fields, "master_group_id"), &mut band_category)?;
|
||||
|
||||
validate_song_text(
|
||||
&name,
|
||||
@@ -746,6 +765,7 @@ fn create_song(uid: i64, fields: &HashMap<String, Vec<u8>>) -> Result<i64, Strin
|
||||
"artist_en": field_str(fields, "artist_en"),
|
||||
"attribute": attribute,
|
||||
"band_category": band_category.clone(),
|
||||
"master_group_id": master_group_id,
|
||||
"bpm": field_f64(fields, "bpm"),
|
||||
"preview_start_sec": field_f64(fields, "preview_start_sec"),
|
||||
"preview_length_sec": field_f64(fields, "preview_length_sec"),
|
||||
@@ -761,7 +781,7 @@ fn create_song(uid: i64, fields: &HashMap<String, Vec<u8>>) -> Result<i64, Strin
|
||||
"artist": artist,
|
||||
"artist_en": field_str(fields, "artist_en"),
|
||||
"band_category": band_category.clone(),
|
||||
"master_group_id": database::band_group_id(&band_category),
|
||||
"master_group_id": master_group_id,
|
||||
"attribute": attribute,
|
||||
"bpm": field_f64(fields, "bpm").unwrap_or(DEFAULT_BPM) as f32,
|
||||
"start_wait": 2.0,
|
||||
@@ -876,6 +896,13 @@ fn update_song(music_id: i64, fields: &HashMap<String, Vec<u8>>) -> Result<(), S
|
||||
if !BAND_CATEGORIES.contains(&band_category.as_str()) {
|
||||
return Err(format!("Unknown band category '{}'", band_category));
|
||||
}
|
||||
let group_text = if fields.contains_key("master_group_id") {
|
||||
field_str(fields, "master_group_id")
|
||||
} else {
|
||||
old_manifest["master_group_id"].as_i64().filter(|id| *id >= custom_group::FIRST_ID)
|
||||
.map(|id| id.to_string()).unwrap_or_default()
|
||||
};
|
||||
let master_group_id = selected_group_id(&group_text, &mut band_category)?;
|
||||
|
||||
// The RESULTING text, so an edit that leaves a field alone is checked against what stays
|
||||
validate_song_text(
|
||||
@@ -1023,6 +1050,7 @@ fn update_song(music_id: i64, fields: &HashMap<String, Vec<u8>>) -> Result<(), S
|
||||
"artist_en": text("artist_en"),
|
||||
"attribute": attribute,
|
||||
"band_category": band_category.clone(),
|
||||
"master_group_id": master_group_id,
|
||||
"bpm": number("bpm"),
|
||||
"preview_start_sec": preview_start_sec,
|
||||
"preview_length_sec": preview_length_sec,
|
||||
@@ -1047,7 +1075,7 @@ fn update_song(music_id: i64, fields: &HashMap<String, Vec<u8>>) -> Result<(), S
|
||||
"artist": artist,
|
||||
"artist_en": text("artist_en"),
|
||||
"band_category": band_category.clone(),
|
||||
"master_group_id": database::band_group_id(&band_category),
|
||||
"master_group_id": master_group_id,
|
||||
"attribute": attribute,
|
||||
"bpm": number("bpm").unwrap_or(DEFAULT_BPM) as f32,
|
||||
"start_wait": 2.0,
|
||||
@@ -1439,109 +1467,11 @@ mod tests {
|
||||
fn field(fields: &mut HashMap<String, Vec<u8>>, key: &str, value: &str) {
|
||||
fields.insert(String::from(key), value.as_bytes().to_vec());
|
||||
}
|
||||
|
||||
// A SIF1 chart whose transcode contains 3+ simultaneous notes: 4 parallel holds into a
|
||||
// full 9-lane wall (the shape of the field-reported chart that exposed the old encoding)
|
||||
// 4 parallel holds then a 9-wide wall. Timed to fit inside the 2s test track: uploads are
|
||||
// rejected when a chart outlives its audio (validate_chart_fits_audio)
|
||||
fn wall_chart() -> Vec<u8> {
|
||||
let mut beatmap = jzon::array![];
|
||||
for position in [2, 4, 6, 8] {
|
||||
beatmap.push(jzon::object!{
|
||||
"timing_sec": 0.5, "notes_attribute": 1, "notes_level": 1,
|
||||
"effect": 3, "effect_value": 0.5, "position": position
|
||||
}).unwrap();
|
||||
}
|
||||
for position in 1..=9 {
|
||||
beatmap.push(jzon::object!{
|
||||
"timing_sec": 1.5, "notes_attribute": 1, "notes_level": 1,
|
||||
"effect": 1, "effect_value": 0.0, "position": position
|
||||
}).unwrap();
|
||||
}
|
||||
jzon::stringify(beatmap).into_bytes()
|
||||
}
|
||||
|
||||
// The startup migration: a chart stored with the PRE-pairing encoding (whole equal-time
|
||||
// clusters sharing one num) is regrouped in place, its catalog md5/size follow the new
|
||||
// bytes, the revision bumps exactly once, correctly-encoded songs stay byte-identical,
|
||||
// and a second run is a complete no-op
|
||||
#[test]
|
||||
fn startup_migration_regroups_pre_fix_charts() {
|
||||
let _lock = crate::runtime::lock_test_data_path();
|
||||
|
||||
let mut fields = HashMap::new();
|
||||
field(&mut fields, "name", "Migration Target");
|
||||
field(&mut fields, "artist", "Wall Artist");
|
||||
field(&mut fields, "attribute", "1");
|
||||
field(&mut fields, "level_number_4", "15");
|
||||
fields.insert(String::from("jacket"), test_png());
|
||||
fields.insert(String::from("audio"), test_ogg_tone(550.0));
|
||||
fields.insert(String::from("chart_4"), wall_chart());
|
||||
let target = create_song(3333, &fields).unwrap();
|
||||
|
||||
let mut fields = HashMap::new();
|
||||
field(&mut fields, "name", "Migration Control");
|
||||
field(&mut fields, "artist", "Control Artist");
|
||||
field(&mut fields, "attribute", "2");
|
||||
field(&mut fields, "level_number_1", "5");
|
||||
fields.insert(String::from("jacket"), test_png());
|
||||
fields.insert(String::from("audio"), test_ogg_tone(770.0));
|
||||
fields.insert(String::from("chart_1"), test_chart());
|
||||
let control = create_song(4444, &fields).unwrap();
|
||||
|
||||
// The upload stored the CURRENT encoding; capture it, then doctor the store back to
|
||||
// the pre-pairing form exactly as an old server would have written it: squashed
|
||||
// chart bytes on disk and the catalog md5/size matching those bytes
|
||||
let path = song_path(target, "chart_4.json");
|
||||
let fixed_bytes = fs::read(&path).unwrap();
|
||||
let mut squashed = jzon::parse(&String::from_utf8_lossy(&fixed_bytes)).unwrap();
|
||||
chart::squash_to_pre_fix(&mut squashed);
|
||||
let squashed_bytes = jzon::stringify(squashed).into_bytes();
|
||||
assert_ne!(squashed_bytes, fixed_bytes);
|
||||
fs::write(&path, &squashed_bytes).unwrap();
|
||||
let mut song = database::get_song(target).unwrap();
|
||||
let (md5, size) = asset_meta(&squashed_bytes);
|
||||
for entry in song["levels"].members_mut() {
|
||||
if entry["level"] == 4 {
|
||||
entry["md5"] = md5.clone().into();
|
||||
entry["size"] = size.into();
|
||||
}
|
||||
}
|
||||
database::update_song(target, &song);
|
||||
|
||||
let control_bytes = fs::read(song_path(control, "chart_1.json")).unwrap();
|
||||
let control_song = database::get_song(control).unwrap();
|
||||
let revision = database::get_revision();
|
||||
|
||||
migrate::run();
|
||||
|
||||
// The target chart is byte-identical to what the current transcoder stores, and the
|
||||
// catalog follows the new bytes
|
||||
let migrated = fs::read(&path).unwrap();
|
||||
assert_eq!(migrated, fixed_bytes);
|
||||
let song = database::get_song(target).unwrap();
|
||||
let level = song["levels"].members().find(|l| l["level"] == 4).unwrap();
|
||||
let (md5, size) = asset_meta(&fixed_bytes);
|
||||
assert_eq!(level["md5"].to_string(), md5);
|
||||
assert_eq!(level["size"].as_usize().unwrap(), size);
|
||||
// full_combo never depended on grouping and must not move
|
||||
assert_eq!(level["full_combo"], 9 + 4);
|
||||
|
||||
// Exactly one revision bump, and the control song is untouched
|
||||
assert_eq!(database::get_revision(), revision + 1);
|
||||
assert_eq!(fs::read(song_path(control, "chart_1.json")).unwrap(), control_bytes);
|
||||
assert_eq!(jzon::stringify(database::get_song(control).unwrap()), jzon::stringify(control_song));
|
||||
|
||||
// Idempotent: a second boot changes nothing and bumps nothing
|
||||
migrate::run();
|
||||
assert_eq!(fs::read(&path).unwrap(), fixed_bytes);
|
||||
assert_eq!(database::get_revision(), revision + 1);
|
||||
}
|
||||
// The live PLAY cue must never be a loop cue: the client reports a looping playback as
|
||||
|
||||
// The live PLAY cue must never be a loop cue: the client reports a looping playback as
|
||||
// forever-playing, and the live's end trigger waits on playback-end, so a looping play cue
|
||||
// means the live never ends. New uploads emit is_loop:false, the preview cue keeps looping,
|
||||
// and the startup migration un-loops catalogs written before the distinction existed.
|
||||
// means the live never ends. New uploads emit is_loop:false; the preview cue keeps looping.
|
||||
#[test]
|
||||
fn play_cue_never_loops() {
|
||||
let _lock = crate::runtime::lock_test_data_path();
|
||||
@@ -1563,28 +1493,6 @@ mod tests {
|
||||
assert_eq!(song["sound"]["play"]["loop_end_sec"], 0.0);
|
||||
assert_eq!(song["sound"]["select"]["is_loop"], true);
|
||||
assert!(song["sound"]["select"]["loop_end_sec"].as_f64().unwrap() > 0.0);
|
||||
|
||||
// Doctor the catalog back to the pre-fix shape an old server would have written,
|
||||
// then boot: the migration un-loops the play cue and bumps the revision once
|
||||
let mut old = song.clone();
|
||||
old["sound"]["play"]["is_loop"] = true.into();
|
||||
old["sound"]["play"]["loop_end_sec"] = old["sound"]["play"]["duration_sec"].clone();
|
||||
database::update_song(music_id, &old);
|
||||
let revision = database::get_revision();
|
||||
|
||||
migrate::run();
|
||||
|
||||
let song = database::get_song(music_id).unwrap();
|
||||
assert_eq!(song["sound"]["play"]["is_loop"], false);
|
||||
assert_eq!(song["sound"]["play"]["loop_end_sec"], 0.0);
|
||||
assert_eq!(song["sound"]["select"]["is_loop"], true);
|
||||
// The ogg bytes never moved, so the audio md5 must not change (no re-download)
|
||||
assert_eq!(song["sound"]["play"]["md5"], old["sound"]["play"]["md5"]);
|
||||
assert_eq!(database::get_revision(), revision + 1);
|
||||
|
||||
// Idempotent
|
||||
migrate::run();
|
||||
assert_eq!(database::get_revision(), revision + 1);
|
||||
}
|
||||
|
||||
// Song text is rendered by TMP with rich text on and no escaping, so a tag in a name or an
|
||||
@@ -1728,55 +1636,6 @@ mod tests {
|
||||
update_song(music_id, &fields).unwrap();
|
||||
}
|
||||
|
||||
// The startup migration also corrects the two fabricated masterdata values in stored
|
||||
// catalogs: score-rank thresholds (official constants, not per-song) and combo missions
|
||||
// (20/40/60/80% of the hardest full combo, not 25/50/75/100%)
|
||||
#[test]
|
||||
fn startup_migration_fixes_fabricated_scores_and_missions() {
|
||||
let _lock = crate::runtime::lock_test_data_path();
|
||||
|
||||
let mut fields = HashMap::new();
|
||||
field(&mut fields, "name", "Old Values");
|
||||
field(&mut fields, "artist", "Old Artist");
|
||||
field(&mut fields, "attribute", "3");
|
||||
field(&mut fields, "level_number_1", "5");
|
||||
fields.insert(String::from("jacket"), test_png());
|
||||
fields.insert(String::from("audio"), test_ogg_tone(1100.0));
|
||||
fields.insert(String::from("chart_1"), wall_chart());
|
||||
let music_id = create_song(7777, &fields).unwrap();
|
||||
|
||||
let mut song = database::get_song(music_id).unwrap();
|
||||
let hardest = song["levels"].members().last().unwrap()["full_combo"].as_i64().unwrap();
|
||||
// Doctor the catalog back to the pre-fix formulas
|
||||
let base = hardest as f64 * 200.0 * (1.0 + 5.0 / 10.0);
|
||||
song["score"] = object!{
|
||||
"c": (base * 0.5) as u32, "b": (base * 0.75) as u32, "a": base as u32, "s": (base * 1.3) as u32
|
||||
};
|
||||
song["multi_score"] = object!{
|
||||
"c": (base * 0.6) as u32, "b": (base * 0.9) as u32, "a": (base * 1.2) as u32, "s": (base * 1.56) as u32
|
||||
};
|
||||
song["mission_combo"] = jzon::array![hardest / 4, hardest / 2, hardest * 3 / 4, hardest];
|
||||
database::update_song(music_id, &song);
|
||||
let revision = database::get_revision();
|
||||
|
||||
migrate::run();
|
||||
|
||||
let song = database::get_song(music_id).unwrap();
|
||||
let (score, multi_score) = default_scores();
|
||||
assert_eq!(song["score"], score);
|
||||
assert_eq!(song["multi_score"], multi_score);
|
||||
assert_eq!(song["score"]["s"], 350000);
|
||||
assert_eq!(song["multi_score"]["s"], 1225000);
|
||||
// 20/40/60/80%, and never the full combo itself
|
||||
assert_eq!(song["mission_combo"], mission_combo(hardest));
|
||||
assert_eq!(song["mission_combo"][3].as_i64().unwrap(), (hardest as f64 * 0.8 + 0.5) as i64);
|
||||
assert!(song["mission_combo"][3].as_i64().unwrap() < hardest);
|
||||
assert_eq!(database::get_revision(), revision + 1);
|
||||
|
||||
// Idempotent
|
||||
migrate::run();
|
||||
assert_eq!(database::get_revision(), revision + 1);
|
||||
}
|
||||
|
||||
// Export a song, import the package as another user, and the served song
|
||||
// must be identical apart from the assigned music_id - INCLUDING the audio
|
||||
@@ -2127,6 +1986,19 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn custom_group_forces_other_band_and_rejects_unknown_ids() {
|
||||
let _lock = crate::runtime::lock_test_data_path();
|
||||
let unique = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
|
||||
let id = custom_group::create(&format!("Test {}", unique), "Test Band").unwrap();
|
||||
assert!(custom_group::exists(id));
|
||||
let mut band = String::from("MUSE");
|
||||
assert_eq!(selected_group_id(&id.to_string(), &mut band), Ok(id));
|
||||
assert_eq!(band, "OTHER");
|
||||
assert!(selected_group_id("999999999", &mut band).is_err());
|
||||
assert_eq!(selected_group_id("", &mut band), Ok(9999));
|
||||
}
|
||||
|
||||
// The whole feature is off unless --enable-custom-songs: endpoints 404 / go
|
||||
// empty, and the webui config the client gates its nav on reports it off.
|
||||
// When enabled everything works.
|
||||
|
||||
+140
-193
@@ -9,17 +9,17 @@ use std::collections::HashMap;
|
||||
// note_bomb_3 5, note_bomb_5 6, note_bomb_9 7, note_slide 11, note_slide_event 12,
|
||||
// note_slide_hold 13, with isHold(e) = e == 3 and isSlide(e) = e >= 11.
|
||||
//
|
||||
// SIF2 side: `type` only distinguishes an ordinary note (1) from a star/bomb note (3).
|
||||
// LiveTimeController.ToMarkerType accepts 1..3 and maps anything else to None; type 2 exists in
|
||||
// the enum but appears in ZERO of the 2146 shipped charts, so nothing emits it here.
|
||||
// SIF2 side: type 1 is a tap, 2 is a flick, and 3 is a skill/star note.
|
||||
// LiveTimeController.ToMarkerType accepts 1..3; stock charts happen not to
|
||||
// use type 2, but imported Bandori charts do.
|
||||
//
|
||||
// A SLIDE is structural, not a type. MarkerData derives it from the parent/child chain:
|
||||
// IsSliderMarker chained and the child is on a DIFFERENT line -> a slide segment
|
||||
// IsSliderLongMarker chained, child SAME line, parent different -> a slide ending in a hold
|
||||
// IsDistanceMarker has both a parent and a child -> a middle segment
|
||||
// So a hold is a chain that stays in its lane and a slide is a chain that moves across lanes;
|
||||
// both are type 1. This is also how the client counts combo (NoteData.CalcMaxCombo: a note whose
|
||||
// child shares its line does not count, its tail does).
|
||||
// both are type 1. Combo follows LiveStatusSystem.PushInputResult, not the editor-only
|
||||
// NoteData.CalcMaxCombo helper, which undercounts repeated same-lane checkpoints.
|
||||
//
|
||||
// Mapping rules:
|
||||
// - line = position - 1 (both are right-to-left)
|
||||
@@ -33,16 +33,19 @@ use std::collections::HashMap;
|
||||
// varies the blast width per effect; SIF2 has one bomb with one damage value, so the
|
||||
// four collapse into type 3 and only the radius is lost. Previously only bomb_1 mapped
|
||||
// here and the three wider ones arrived as ordinary taps.
|
||||
// - effect 11/12/13 (slide) -> CHAINED across lanes, all type 1. Slides sharing a notes_level
|
||||
// form one run: sorted by time and linked parent -> child, so each link crosses lanes and
|
||||
// the client sees a slider. A run ends on effect 13 (slide hold), whose synthesized
|
||||
// - effect 11/12/13/14 (slide) -> CHAINED across lanes, normally type 1. Slides sharing a notes_level
|
||||
// form one run: sorted by time and linked parent -> child. Cross-lane links
|
||||
// are sliders; same-lane links are hold segments. A run ends on effect 13 (slide hold), whose synthesized
|
||||
// same-line tail then makes it IsSliderLongMarker — the slide settling into a hold the
|
||||
// player releases. Verified against a real upload: every notes_level shared by more than
|
||||
// one note held exactly the slide-effect notes, each a monotonic sweep like
|
||||
// pos 9->8->7->6 with effects 11,11,11,13.
|
||||
// A lone slide with no chain partner stays a plain tap: a slider needs a cross-lane child.
|
||||
// - effect 0 (random) and anything else unknown -> plain type 1. Every effect the game
|
||||
// actually defines is covered above, so this is only a floor for hand-authored charts.
|
||||
// A lone slide with no chain partner stays a plain note. Same-lane links
|
||||
// become hold segments; effect 14 keeps the final point as a flick.
|
||||
// - effects 8/14 are custom-song extensions for a flick and a slide-end flick (type 2).
|
||||
// Effect 9 is a skill note (type 3); effect 10 is a hold with a flick tail.
|
||||
// The stock SIF1 vocabulary does not use 8/9/10/14.
|
||||
// - effect 0 (random) and anything else unknown -> plain type 1.
|
||||
// - notes_attribute is dropped (SIF2 has no per-note attribute). notes_level is consumed as
|
||||
// the chain id above and not emitted.
|
||||
// - ids are sequential from 1 in time order. num is the spawn group: the dummy
|
||||
@@ -62,8 +65,8 @@ use std::collections::HashMap;
|
||||
// ForceGroupId against the other chunk's GroupId and links the lane-closest pair).
|
||||
// - notes[0] is ALWAYS the dummy header (id 0, num 100, type 0) - the client
|
||||
// deserializes it verbatim.
|
||||
// - max_combo_count = all real notes EXCEPT hold heads whose tail is on the same
|
||||
// line (the game counts a same-lane hold as one combo for the chain)
|
||||
// - max_combo_count excludes a same-lane hold's root and a cross-lane slide settling
|
||||
// into a hold. Further same-lane checkpoints do count (see max_combo_count below).
|
||||
|
||||
// Two notes are SIMULTANEOUS (one spawn cluster) when their times agree to within this.
|
||||
// Uploaded timings are decimal literals, so notes an author meant to be simultaneous parse to
|
||||
@@ -140,6 +143,42 @@ pub fn first_note_time(chart: &JsonValue) -> Option<f64> {
|
||||
}))
|
||||
}
|
||||
|
||||
// LiveStatusSystem.PushInputResult skips combo for a non-slider long root or
|
||||
// IsSliderLongMarker. LiveInputResultControl maps those results to their child.
|
||||
// All other points count, including repeated checkpoints in a stationary slide.
|
||||
// Validate the links before repairing stored data; never guess a broken chart's count.
|
||||
pub fn max_combo_count(chart: &JsonValue) -> Option<i64> {
|
||||
let notes = &chart["notes"];
|
||||
if !notes.is_array() || notes.len() < 2 || notes[0]["id"] != 0 {
|
||||
return None;
|
||||
}
|
||||
let mut by_id = HashMap::new();
|
||||
for note in notes.members().skip(1) {
|
||||
let id = note["id"].as_i64()?;
|
||||
let line = note["line"].as_i64()?;
|
||||
if id <= 0 || !(0..9).contains(&line) || by_id.insert(id, note).is_some() {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
let mut count = 0;
|
||||
for note in notes.members().skip(1) {
|
||||
let id = note["id"].as_i64()?;
|
||||
let parent_id = note["parent_id"].as_i64()?;
|
||||
let child_id = note["child_id"].as_i64()?;
|
||||
let parent = if parent_id == 0 { None } else { Some(*by_id.get(&parent_id)?) };
|
||||
let child = if child_id == 0 { None } else { Some(*by_id.get(&child_id)?) };
|
||||
if parent_id == id || child_id == id
|
||||
|| parent.is_some_and(|p| p["child_id"] != id)
|
||||
|| child.is_some_and(|c| c["parent_id"] != id) {
|
||||
return None;
|
||||
}
|
||||
let hold_start = child.is_some_and(|c| c["line"] == note["line"])
|
||||
&& parent.is_none_or(|p| p["line"] != note["line"]);
|
||||
if !hold_start { count += 1; }
|
||||
}
|
||||
Some(count)
|
||||
}
|
||||
|
||||
struct WorkNote {
|
||||
time: f64,
|
||||
line: i64,
|
||||
@@ -153,7 +192,7 @@ struct WorkNote {
|
||||
// LiveModel.NoteEffect.isHold, widened to note_slide_hold: both carry a duration in
|
||||
// effect_value (notes.lua isTimeOver adds effect_value for note_hold and note_slide_hold alike).
|
||||
fn is_hold(effect: i64) -> bool {
|
||||
effect == 3 || effect == 13
|
||||
effect == 3 || effect == 10 || effect == 13
|
||||
}
|
||||
|
||||
// LiveModel.NoteEffect.isSlide
|
||||
@@ -231,7 +270,10 @@ pub fn transcode(beatmap: &JsonValue) -> Result<(JsonValue, i64), String> {
|
||||
work.push(WorkNote {
|
||||
time: timing,
|
||||
line: position - 1,
|
||||
kind: if is_bomb(effect) { 3 } else { 1 },
|
||||
// Effects 8/14 are the custom-song interchange's flick and
|
||||
// slide-end flick; 9 carries a Bandori skill note.
|
||||
kind: if effect == 8 || effect == 14 { 2 }
|
||||
else if effect == 9 || is_bomb(effect) { 3 } else { 1 },
|
||||
parent: None,
|
||||
child: None
|
||||
});
|
||||
@@ -249,7 +291,7 @@ pub fn transcode(beatmap: &JsonValue) -> Result<(JsonValue, i64), String> {
|
||||
work.push(WorkNote {
|
||||
time: timing + effect_value,
|
||||
line: position - 1,
|
||||
kind: 1,
|
||||
kind: if effect == 10 { 2 } else { 1 },
|
||||
parent: Some(head),
|
||||
child: None
|
||||
});
|
||||
@@ -257,8 +299,8 @@ pub fn transcode(beatmap: &JsonValue) -> Result<(JsonValue, i64), String> {
|
||||
}
|
||||
}
|
||||
|
||||
// Link each slide chain in time order. Consecutive members sit on different lines, which is
|
||||
// exactly what makes SIF2 treat the run as a slider rather than a hold. A member that already
|
||||
// Link each slide chain in time order. A cross-lane link becomes a slider
|
||||
// and a same-lane link becomes a hold segment. A member that already
|
||||
// has a child is a slide-hold, i.e. the end of the run, so the chain stops there — its tail
|
||||
// stays its child and the cross-lane parent link makes it IsSliderLongMarker.
|
||||
for (_, members) in chains.iter() {
|
||||
@@ -273,10 +315,6 @@ pub fn transcode(beatmap: &JsonValue) -> Result<(JsonValue, i64), String> {
|
||||
if work[a].child.is_some() || work[b].parent.is_some() {
|
||||
break;
|
||||
}
|
||||
if work[a].line == work[b].line {
|
||||
// Same lane would read as a hold, not a slide; skip the link rather than lie
|
||||
continue;
|
||||
}
|
||||
work[a].child = Some(b);
|
||||
work[b].parent = Some(a);
|
||||
}
|
||||
@@ -322,17 +360,9 @@ pub fn transcode(beatmap: &JsonValue) -> Result<(JsonValue, i64), String> {
|
||||
"parent_id": 0, "child_id": 0, "child_num": 0, "child_line": 0,
|
||||
"force_sync_group_id": 0
|
||||
}];
|
||||
let mut max_combo_count = 0;
|
||||
for index in order.iter() {
|
||||
let note = &work[*index];
|
||||
|
||||
// NoteData.CalcMaxCombo: a note whose child is on the SAME line (a hold) does not count,
|
||||
// its tail does. A cross-lane child (a slide segment) counts normally.
|
||||
match note.child {
|
||||
Some(child) if work[child].line == note.line => {},
|
||||
_ => max_combo_count += 1
|
||||
}
|
||||
|
||||
notes.push(object!{
|
||||
"id": ids[*index],
|
||||
"num": nums[*index],
|
||||
@@ -347,120 +377,17 @@ pub fn transcode(beatmap: &JsonValue) -> Result<(JsonValue, i64), String> {
|
||||
}).unwrap();
|
||||
}
|
||||
|
||||
Ok((object!{
|
||||
let mut chart = object!{
|
||||
"max_lane": 9,
|
||||
"sound_name": "",
|
||||
"max_combo_count": max_combo_count,
|
||||
"max_combo_count": 0,
|
||||
"notes": notes
|
||||
}, max_combo_count))
|
||||
};
|
||||
let combo = max_combo_count(&chart).ok_or("Invalid transcoded note links")?;
|
||||
chart["max_combo_count"] = combo.into();
|
||||
Ok((chart, combo))
|
||||
}
|
||||
|
||||
// Regroups a STORED transcoded chart whose spawn groups predate the pairing rule above: the
|
||||
// old transcoder gave every note of an equal-time cluster one shared num (force_sync_group_id
|
||||
// always 0), and the client renders no head markers for a group of 3+ (see the header
|
||||
// comment). This rebuilds num / force_sync_group_id in place with the same clustering the
|
||||
// transcoder now uses — equal final time, lane-sorted, chunks of two, chained
|
||||
// force_sync_group_id — and re-points child_num at each child's renumbered spawn group.
|
||||
// Everything else (ids, times, lines, types, parent/child links, max_combo_count — combo
|
||||
// counting never depended on grouping) is untouched, so on a chart the current transcoder
|
||||
// produced this reproduces the stored bytes exactly.
|
||||
//
|
||||
// Returns false (chart untouched) unless some num is shared by MORE than two notes. That
|
||||
// makes it a safe no-op on current uploads AND on official-style encodings (whose num values
|
||||
// differ from ours — e.g. gaps of 3 — but whose groups never exceed two).
|
||||
pub fn regroup(chart: &mut JsonValue) -> bool {
|
||||
// (id, time, line) per real note; the dummy header at [0] stays untouched
|
||||
let notes: Vec<(i64, f64, i64)> = chart["notes"].members().skip(1).map(|n| (
|
||||
n["id"].as_i64().unwrap_or(0),
|
||||
n["time"].as_f64().unwrap_or(0.0),
|
||||
n["line"].as_i64().unwrap_or(0)
|
||||
)).collect();
|
||||
|
||||
// Only a pre-pairing chart (some num shared 3+ ways) is rewritten
|
||||
let mut group_sizes: Vec<(i64, i64)> = Vec::new();
|
||||
for data in chart["notes"].members().skip(1) {
|
||||
let num = data["num"].as_i64().unwrap_or(0);
|
||||
match group_sizes.iter_mut().find(|(n, _)| *n == num) {
|
||||
Some((_, count)) => *count += 1,
|
||||
None => group_sizes.push((num, 1))
|
||||
}
|
||||
}
|
||||
if group_sizes.iter().all(|(_, count)| *count <= 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Time order; ids break ties (transcode issues them in time order, so this reproduces
|
||||
// the emission order the grouping pass originally saw)
|
||||
let mut order: Vec<usize> = (0..notes.len()).collect();
|
||||
order.sort_by(|a, b| notes[*a].1.total_cmp(¬es[*b].1).then(notes[*a].0.cmp(¬es[*b].0)));
|
||||
|
||||
// id -> (new num, new force_sync_group_id)
|
||||
let mut assigned: Vec<(i64, i64, i64)> = Vec::with_capacity(notes.len());
|
||||
let mut num = 100;
|
||||
let mut start = 0;
|
||||
while start < order.len() {
|
||||
let mut end = start + 1;
|
||||
while end < order.len() && simultaneous(notes[order[end]].1, notes[order[start]].1) {
|
||||
end += 1;
|
||||
}
|
||||
let mut cluster: Vec<usize> = order[start..end].to_vec();
|
||||
cluster.sort_by_key(|index| notes[*index].2);
|
||||
let mut prev_num = 0;
|
||||
for chunk in cluster.chunks(2) {
|
||||
num += 1;
|
||||
for index in chunk {
|
||||
assigned.push((notes[*index].0, num, prev_num));
|
||||
}
|
||||
prev_num = num;
|
||||
}
|
||||
start = end;
|
||||
}
|
||||
let lookup = |id: i64| assigned.iter().find(|(i, _, _)| *i == id).map(|(_, n, f)| (*n, *f));
|
||||
|
||||
for data in chart["notes"].members_mut().skip(1) {
|
||||
let Some((new_num, force)) = lookup(data["id"].as_i64().unwrap_or(0)) else { continue; };
|
||||
data["num"] = new_num.into();
|
||||
data["force_sync_group_id"] = force.into();
|
||||
let child = data["child_id"].as_i64().unwrap_or(0);
|
||||
if child != 0 {
|
||||
// child_num names the child's spawn group and must follow its new num
|
||||
data["child_num"] = lookup(child).map(|(n, _)| n).unwrap_or(0).into();
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
// Test helper: fabricates what pre-pairing servers stored, by squashing a current chart back
|
||||
// to the OLD encoding — one shared num per equal-time cluster, force_sync_group_id 0, and
|
||||
// child_num following. Lives outside the tests module so the migration tests in
|
||||
// router/custom_song.rs can build realistic pre-fix fixtures from transcode output.
|
||||
#[cfg(test)]
|
||||
pub fn squash_to_pre_fix(chart: &mut JsonValue) {
|
||||
let notes: Vec<(i64, f64)> = chart["notes"].members().skip(1)
|
||||
.map(|n| (n["id"].as_i64().unwrap(), n["time"].as_f64().unwrap()))
|
||||
.collect();
|
||||
let mut order: Vec<usize> = (0..notes.len()).collect();
|
||||
order.sort_by(|a, b| notes[*a].1.total_cmp(¬es[*b].1).then(notes[*a].0.cmp(¬es[*b].0)));
|
||||
let mut nums: Vec<(i64, i64)> = Vec::new();
|
||||
let mut num = 100;
|
||||
let mut last_time = f64::NEG_INFINITY;
|
||||
for index in order {
|
||||
if notes[index].1 != last_time {
|
||||
num += 1;
|
||||
last_time = notes[index].1;
|
||||
}
|
||||
nums.push((notes[index].0, num));
|
||||
}
|
||||
let lookup = |id: i64| nums.iter().find(|(i, _)| *i == id).map(|(_, n)| *n).unwrap_or(0);
|
||||
for data in chart["notes"].members_mut().skip(1) {
|
||||
data["num"] = lookup(data["id"].as_i64().unwrap()).into();
|
||||
data["force_sync_group_id"] = 0.into();
|
||||
let child = data["child_id"].as_i64().unwrap_or(0);
|
||||
if child != 0 {
|
||||
data["child_num"] = lookup(child).into();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
@@ -830,6 +757,56 @@ mod tests {
|
||||
assert_eq!(combo, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_same_lane_checkpoints_count_like_the_client() {
|
||||
for final_effect in [11, 14] {
|
||||
let mut beatmap = jzon::array![];
|
||||
for i in 0..13 {
|
||||
beatmap.push(sif_slide(1.0 + i as f64 * 0.1, 5,
|
||||
if i == 12 { final_effect } else { 11 }, 0.0, 500)).unwrap();
|
||||
}
|
||||
let (chart, combo) = transcode(&beatmap).unwrap();
|
||||
// The head shares the first checkpoint's result; all twelve
|
||||
// non-root points still have their own runtime judgment.
|
||||
assert_eq!(combo, 12);
|
||||
assert_eq!(chart["max_combo_count"], 12);
|
||||
assert_eq!(chart["notes"].len(), 14);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn combo_count_preserves_hold_and_cross_lane_rules() {
|
||||
for (positions, expected) in [
|
||||
(vec![5, 5], 1),
|
||||
(vec![5, 5, 5], 2),
|
||||
(vec![5, 5, 6], 2),
|
||||
(vec![4, 5, 5], 2),
|
||||
(vec![4, 5, 5, 5], 3),
|
||||
(vec![4, 5, 6], 3),
|
||||
] {
|
||||
let mut beatmap = jzon::array![];
|
||||
for (i, position) in positions.iter().enumerate() {
|
||||
beatmap.push(sif_slide(1.0 + i as f64 * 0.1, *position, 11, 0.0, 500)).unwrap();
|
||||
}
|
||||
let (_, combo) = transcode(&beatmap).unwrap();
|
||||
assert_eq!(combo, expected, "chain {:?}", positions);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn combo_repair_rejects_invalid_links() {
|
||||
let (chart, _) = transcode(&jzon::array![sif_note(1.0, 5, 3, 1.0)]).unwrap();
|
||||
for (field, value) in [("id", 2), ("parent_id", 999), ("child_id", 999), ("child_id", 1)] {
|
||||
let mut broken = chart.clone();
|
||||
broken["notes"][1][field] = value.into();
|
||||
assert_eq!(max_combo_count(&broken), None, "invalid {}={}", field, value);
|
||||
}
|
||||
let mut broken = chart;
|
||||
broken["notes"][2]["parent_id"] = 0.into();
|
||||
assert_eq!(max_combo_count(&broken), None);
|
||||
assert_eq!(max_combo_count(&object!{}), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slide_hold_needs_a_duration() {
|
||||
// The effect 3 duration check has to cover note_slide_hold too
|
||||
@@ -867,13 +844,32 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn undefined_effects_fall_back_to_taps() {
|
||||
// Not part of NoteEffect; a hand-authored chart must still transcode to something valid
|
||||
for effect in [0, 8, 9, 10] {
|
||||
for effect in [0, 15] {
|
||||
let (chart, _) = transcode(&jzon::array![sif_note(1.0, 5, effect, 0.0)]).unwrap();
|
||||
assert_eq!(chart["notes"][1]["type"], 1, "effect {}", effect);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bandori_flick_skill_and_slide_flick() {
|
||||
for (effect, kind) in [(8, 2), (9, 3), (14, 2)] {
|
||||
let (chart, _) = transcode(&jzon::array![sif_note(1.0, 5, effect, 0.0)]).unwrap();
|
||||
assert_eq!(chart["notes"][1]["type"], kind, "effect {}", effect);
|
||||
}
|
||||
let (chart, combo) = transcode(&jzon::array![
|
||||
sif_slide(1.0, 7, 11, 0.0, 2),
|
||||
sif_slide(1.3, 5, 14, 0.0, 2)
|
||||
]).unwrap();
|
||||
assert_eq!(combo, 2);
|
||||
assert_eq!(chart["notes"][2]["type"], 2);
|
||||
assert_eq!(chart["notes"][1]["child_id"], chart["notes"][2]["id"].clone());
|
||||
|
||||
let (hold, combo) = transcode(&jzon::array![sif_note(1.0, 5, 10, 0.5)]).unwrap();
|
||||
assert_eq!(combo, 1);
|
||||
assert_eq!(hold["notes"][1]["type"], 1);
|
||||
assert_eq!(hold["notes"][2]["type"], 2);
|
||||
}
|
||||
|
||||
// Lifted verbatim from a chart uploaded to the live server (custom song 10008, "Edelied",
|
||||
// exported via /custom_song/download/10008) — a swipe run of note_slide into note_slide_hold,
|
||||
// which is the shape that used to arrive as undifferentiated taps. Note the editor writes a
|
||||
@@ -908,11 +904,9 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
// The 10011 field shape: 4 parallel holds into a full 9-lane wall into a triple. Squashing
|
||||
// transcode output reproduces the old encoding exactly (one num per cluster, no force
|
||||
// links); regroup must restore the current encoding BYTE-IDENTICALLY, child_num included.
|
||||
// Parallel holds and a nine-lane wall must retain valid spawn groups.
|
||||
#[test]
|
||||
fn regroup_restores_pre_fix_wall_and_parallel_holds() {
|
||||
fn transcode_wall_and_parallel_holds() {
|
||||
let beatmap = jzon::array![
|
||||
sif_note(1.0, 2, 3, 1.0), sif_note(1.0, 4, 3, 1.0),
|
||||
sif_note(1.0, 6, 3, 1.0), sif_note(1.0, 8, 3, 1.0),
|
||||
@@ -921,23 +915,7 @@ mod tests {
|
||||
sif_note(2.75, 7, 1, 0.0), sif_note(2.75, 8, 1, 0.0), sif_note(2.75, 9, 1, 0.0),
|
||||
sif_note(3.625, 3, 1, 0.0), sif_note(3.625, 5, 1, 0.0), sif_note(3.625, 7, 1, 0.0)
|
||||
];
|
||||
let (expected, _) = transcode(&beatmap).unwrap();
|
||||
|
||||
let mut chart = expected.clone();
|
||||
squash_to_pre_fix(&mut chart);
|
||||
// Sanity: the squash really is the old encoding — whole clusters share one num
|
||||
assert_eq!(chart["notes"][1]["num"], 101);
|
||||
assert_eq!(chart["notes"][4]["num"], 101); // all 4 hold heads
|
||||
assert_eq!(chart["notes"][9]["num"], 103);
|
||||
assert_eq!(chart["notes"][17]["num"], 103); // all 9 wall notes
|
||||
assert_eq!(chart["notes"][1]["child_num"], 102, "squashed child_num must follow");
|
||||
for data in chart["notes"].members() {
|
||||
assert_eq!(data["force_sync_group_id"], 0);
|
||||
}
|
||||
|
||||
assert!(regroup(&mut chart), "a squashed chart must be rewritten");
|
||||
assert_eq!(jzon::stringify(chart.clone()), jzon::stringify(expected.clone()),
|
||||
"regroup must reproduce the current transcoder's output exactly");
|
||||
let (chart, _) = transcode(&beatmap).unwrap();
|
||||
|
||||
// Spell the wall out: adjacent-lane pairs, each later chunk force-synced to the
|
||||
// previous one (heads 101/102, tails 103/104, wall 105..109, triple 110/111)
|
||||
@@ -964,37 +942,6 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn regroup_is_a_no_op_on_current_encoding() {
|
||||
let beatmap = jzon::array![
|
||||
sif_note(1.0, 2, 1, 0.0), sif_note(1.0, 5, 1, 0.0), sif_note(1.0, 8, 1, 0.0),
|
||||
sif_note(2.0, 4, 3, 1.5)
|
||||
];
|
||||
let (chart, _) = transcode(&beatmap).unwrap();
|
||||
let before = jzon::stringify(chart.clone());
|
||||
let mut chart = chart;
|
||||
assert!(!regroup(&mut chart));
|
||||
assert_eq!(jzon::stringify(chart), before);
|
||||
}
|
||||
|
||||
// Official-shaped encodings (1132_5_Sn t=9.781: num gaps of 3, force_sync naming the other
|
||||
// pair) have groups of at most two and must never be "normalized" to our num sequence
|
||||
#[test]
|
||||
fn regroup_is_a_no_op_on_official_shaped_charts() {
|
||||
let mut chart = object!{
|
||||
"max_lane": 9, "sound_name": "", "max_combo_count": 4,
|
||||
"notes": [
|
||||
{"id": 0, "num": 100, "line": 0, "time": 0.0, "type": 0, "parent_id": 0, "child_id": 0, "child_num": 0, "child_line": 0, "force_sync_group_id": 0},
|
||||
{"id": 45, "num": 145, "line": 0, "time": 9.781, "type": 1, "parent_id": 0, "child_id": 0, "child_num": 0, "child_line": 0, "force_sync_group_id": 0},
|
||||
{"id": 46, "num": 145, "line": 1, "time": 9.781, "type": 1, "parent_id": 0, "child_id": 0, "child_num": 0, "child_line": 0, "force_sync_group_id": 0},
|
||||
{"id": 47, "num": 148, "line": 7, "time": 9.781, "type": 1, "parent_id": 0, "child_id": 0, "child_num": 0, "child_line": 0, "force_sync_group_id": 145},
|
||||
{"id": 48, "num": 148, "line": 8, "time": 9.781, "type": 1, "parent_id": 0, "child_id": 0, "child_num": 0, "child_line": 0, "force_sync_group_id": 145}
|
||||
]
|
||||
};
|
||||
let before = jzon::stringify(chart.clone());
|
||||
assert!(!regroup(&mut chart));
|
||||
assert_eq!(jzon::stringify(chart), before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_bad_charts() {
|
||||
|
||||
@@ -1,121 +0,0 @@
|
||||
use std::fs;
|
||||
|
||||
use super::{chart, database, song_path, asset_meta, LEVEL_COUNT};
|
||||
use crate::runtime::get_data_path;
|
||||
|
||||
// One-time, idempotent startup migration for charts transcoded before the spawn-group pairing
|
||||
// rule (chart.rs header): the old transcoder gave every note of an equal-time cluster ONE
|
||||
// shared num, and the client creates no head markers for a group of 3+ notes
|
||||
// (LiveMarkerControl.CreateMarkerUI plain-returns on count > 2), so 3+ simultaneous notes
|
||||
// were judged but never rendered. Stored transcoded charts are derived data with everything
|
||||
// the regroup needs (time + line per note), so they are rewritten in place — no original
|
||||
// upload required, which also covers songs from before export support.
|
||||
//
|
||||
// The same pass also corrects the stored catalog values that were fabricated rather than
|
||||
// derived from official masterdata (see below): the looping PLAY cue, the per-song score-rank
|
||||
// thresholds and the combo-mission targets.
|
||||
//
|
||||
// For each song directory with a catalog row, every chart with an over-shared num is
|
||||
// regrouped (chart::regroup), rewritten to disk, and its level's md5/size in the catalog
|
||||
// blob updated — the changed md5 re-keys the client's content-addressed cache, so clients
|
||||
// re-download the fixed chart on their next catalog sync. The revision is bumped ONCE if
|
||||
// anything changed. Charts the current transcoder produced (and official-shaped encodings)
|
||||
// are left byte-identical, so re-running every boot is free.
|
||||
pub fn run() {
|
||||
if super::disabled() {
|
||||
return;
|
||||
}
|
||||
let Ok(entries) = fs::read_dir(get_data_path("custom_songs")) else {
|
||||
// No custom_songs directory yet - nothing was ever uploaded
|
||||
return;
|
||||
};
|
||||
|
||||
let mut music_ids: Vec<i64> = entries.flatten()
|
||||
.filter_map(|entry| entry.file_name().to_string_lossy().parse::<i64>().ok())
|
||||
.collect();
|
||||
music_ids.sort();
|
||||
|
||||
let mut songs_changed = 0;
|
||||
let mut charts_changed = 0;
|
||||
for music_id in music_ids {
|
||||
// A song directory without a catalog row is never served; leave it alone
|
||||
let Some(mut song) = database::get_song(music_id) else { continue; };
|
||||
|
||||
let mut changed = false;
|
||||
|
||||
// Catalogs written before cue_json took an is_loop argument marked BOTH cues as loop
|
||||
// cues. A looping PLAY cue never reports playback-end to the client, and the live's end
|
||||
// trigger (LiveTimeController: isMusicEnded -> InLiveDelay -> EndWait) hangs off exactly
|
||||
// that, so those lives never finish. Only the metadata changes - the ogg bytes and their
|
||||
// md5 are untouched, so no re-download is needed, just a catalog resync.
|
||||
if song["sound"]["play"]["is_loop"] == true {
|
||||
song["sound"]["play"]["is_loop"] = false.into();
|
||||
song["sound"]["play"]["loop_end_sec"] = 0.0.into();
|
||||
changed = true;
|
||||
println!("Custom song {}: play cue un-looped (pre-fix catalog)", music_id);
|
||||
}
|
||||
|
||||
// Score-rank thresholds and combo-mission targets used to be invented per song. Both are
|
||||
// objective masterdata: the official live rows all carry ONE score tuple, and every
|
||||
// live_mission_combo row is round(hardest full combo * 0.2/0.4/0.6/0.8) (see
|
||||
// default_scores / mission_combo). Recompute them the way an edit would, so songs
|
||||
// uploaded before the fix stop handing out rank S for free and stop asking for a literal
|
||||
// full combo on the fourth combo mission.
|
||||
let (score, multi_score) = super::default_scores();
|
||||
if song["score"] != score || song["multi_score"] != multi_score {
|
||||
song["score"] = score;
|
||||
song["multi_score"] = multi_score;
|
||||
changed = true;
|
||||
println!("Custom song {}: score rank thresholds reset to the official values", music_id);
|
||||
}
|
||||
// Same "hardest difficulty" rule as upload/edit: the last level entry, which both write
|
||||
// in ascending level order
|
||||
if let Some(hardest) = song["levels"].members().last().and_then(|l| l["full_combo"].as_i64()) {
|
||||
let missions = super::mission_combo(hardest);
|
||||
if song["mission_combo"] != missions {
|
||||
song["mission_combo"] = missions;
|
||||
changed = true;
|
||||
println!("Custom song {}: combo missions rescaled to the official 20/40/60/80%", music_id);
|
||||
}
|
||||
}
|
||||
|
||||
for level in 1..=LEVEL_COUNT {
|
||||
let path = song_path(music_id, &format!("chart_{}.json", level));
|
||||
let Ok(bytes) = fs::read(&path) else { continue; };
|
||||
let Ok(mut chart_data) = jzon::parse(&String::from_utf8_lossy(&bytes)) else {
|
||||
println!("Custom song {} chart {}: not valid JSON, migration skipped", music_id, level);
|
||||
continue;
|
||||
};
|
||||
if !chart::regroup(&mut chart_data) {
|
||||
continue;
|
||||
}
|
||||
let new_bytes = jzon::stringify(chart_data);
|
||||
if let Err(e) = fs::write(&path, &new_bytes) {
|
||||
println!("Custom song {} chart {}: rewrite failed ({}), migration skipped", music_id, level, e);
|
||||
continue;
|
||||
}
|
||||
// The catalog md5/size must follow the served bytes or the client's
|
||||
// download-and-verify loop would never accept the asset
|
||||
let (md5, size) = asset_meta(new_bytes.as_bytes());
|
||||
for entry in song["levels"].members_mut() {
|
||||
if entry["level"] == level {
|
||||
entry["md5"] = md5.clone().into();
|
||||
entry["size"] = size.into();
|
||||
}
|
||||
}
|
||||
changed = true;
|
||||
charts_changed += 1;
|
||||
println!("Custom song {}: regrouped chart level {} (pre-pairing spawn groups)", music_id, level);
|
||||
}
|
||||
|
||||
if changed {
|
||||
database::update_song(music_id, &song);
|
||||
songs_changed += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if songs_changed > 0 {
|
||||
database::bump_revision();
|
||||
println!("Custom song spawn-group migration: rewrote {} chart(s) in {} song(s), catalog revision bumped", charts_changed, songs_changed);
|
||||
}
|
||||
}
|
||||
@@ -98,6 +98,9 @@ pub fn expand(package: &[u8], fields: &mut HashMap<String, Vec<u8>>) -> Result<(
|
||||
fields.insert(key.to_string(), manifest[key].to_string().into_bytes());
|
||||
}
|
||||
}
|
||||
// Group IDs are server-local. The package records the original ID for
|
||||
// reference, but only the destination upload form may choose its group;
|
||||
// copying the ID could silently select a different band's row there.
|
||||
for data in manifest["levels"].members() {
|
||||
let Some(level) = data["level"].as_i64() else { continue; };
|
||||
if !data["level_number"].is_null() {
|
||||
|
||||
@@ -198,6 +198,7 @@ fn init_star_event(event: &mut JsonValue) {
|
||||
|
||||
async fn event(Session { key, body }: Session) -> impl Responder {
|
||||
let master_event_id = body["master_event_id"].as_u32().unwrap();
|
||||
super::story::visit_event(&key, master_event_id);
|
||||
let mut event = get_event_data(&key, master_event_id);
|
||||
|
||||
let is_star_event = STAR_EVENT_IDS.contains(&master_event_id);
|
||||
|
||||
+50
-11
@@ -50,19 +50,31 @@ static ASSET_VERSIONS: &[AssetVersion] = &[
|
||||
AssetVersion { region: "JP", platform: "Android", version: "01a71b00f63e4dba92117ac7e60070a6", hash: "d12cecc5695da7f81f8873a3ff93752e", latest: false },
|
||||
AssetVersion { region: "JP", platform: "iOS", version: "01a71b00f63e4dba92117ac7e60070a6", hash: "7c1f61ee68ac84c82dd397a162629142", latest: false },
|
||||
|
||||
// Re-written client versions 2.6.0 -
|
||||
AssetVersion { region: "JP", platform: "Windows", version: "7c74b0df372a460d82378e55dc456260", hash: "8941d32738752c4ca932a28d866852ba", latest: true },
|
||||
AssetVersion { region: "JP", platform: "Android", version: "7c74b0df372a460d82378e55dc456260", hash: "f5ac6238570d8ff8351fda9c79e8774a", latest: true },
|
||||
AssetVersion { region: "JP", platform: "iOS", version: "7c74b0df372a460d82378e55dc456260", hash: "b77459c89ec94acc4fddc787ded62b95", latest: true },
|
||||
// Re-written client versions 2.6.0 - 2.6.1
|
||||
AssetVersion { region: "JP", platform: "Windows", version: "7c74b0df372a460d82378e55dc456260", hash: "8941d32738752c4ca932a28d866852ba", latest: false },
|
||||
AssetVersion { region: "JP", platform: "Android", version: "7c74b0df372a460d82378e55dc456260", hash: "f5ac6238570d8ff8351fda9c79e8774a", latest: false },
|
||||
AssetVersion { region: "JP", platform: "iOS", version: "7c74b0df372a460d82378e55dc456260", hash: "b77459c89ec94acc4fddc787ded62b95", latest: false },
|
||||
|
||||
// Re-written client versions 2.7.0 -
|
||||
AssetVersion { region: "JP", platform: "Windows", version: "a984f9b4c8b64f3e81f0bf11cb9c26fb", hash: "4cbd26690102b6b98f5886263bdfefcd", latest: true },
|
||||
AssetVersion { region: "JP", platform: "Android", version: "a984f9b4c8b64f3e81f0bf11cb9c26fb", hash: "4e64250bb32af39247fe7d7cdb840982", latest: true },
|
||||
AssetVersion { region: "JP", platform: "iOS", version: "a984f9b4c8b64f3e81f0bf11cb9c26fb", hash: "96615485374096183e4e0ce900671910", latest: true },
|
||||
|
||||
|
||||
AssetVersion { region: "JP", platform: "Linux", version: "7c74b0df372a460d82378e55dc456260", hash: "", latest: true },
|
||||
AssetVersion { region: "JP", platform: "macOS", version: "7c74b0df372a460d82378e55dc456260", hash: "", latest: true },
|
||||
AssetVersion { region: "JP", platform: "Linux", version: "a984f9b4c8b64f3e81f0bf11cb9c26fb", hash: "", latest: true },
|
||||
AssetVersion { region: "JP", platform: "macOS", version: "a984f9b4c8b64f3e81f0bf11cb9c26fb", hash: "", latest: true },
|
||||
|
||||
//AssetVersion { region: "JP", platform: "WebGL", version: "4c921d2443335e574a82e04ec9ea243c", hash: "e1ff7c74b20c8d216507972b6f24b9df", latest: true },
|
||||
];
|
||||
|
||||
impl AssetVersion {
|
||||
fn accepts_override(&self) -> bool {
|
||||
// The retired GL clients can still use a custom version/hash, but they are not
|
||||
// current releases. Keep override eligibility separate from update policy.
|
||||
self.latest || (self.region == "GL"
|
||||
&& self.version == "5260ff15dff8ba0c00ad91400f515f55")
|
||||
}
|
||||
|
||||
fn stock_hash(&self) -> String {
|
||||
if self.latest && self.platform == "Android" && get_easter_mode() {
|
||||
if let Some((_, easter)) = EASTER_HASHES.iter().find(|(r, _)| *r == self.region) {
|
||||
@@ -74,7 +86,11 @@ impl AssetVersion {
|
||||
|
||||
fn override_pair(&self) -> Option<(String, String)> {
|
||||
let args = crate::get_args();
|
||||
let ov = args.asset_version.as_str();
|
||||
let ov = if self.region == "GL" {
|
||||
args.en_asset_version.as_str()
|
||||
} else {
|
||||
args.asset_version.as_str()
|
||||
};
|
||||
let oh = match (self.region, self.platform) {
|
||||
("JP", "Windows") => args.windows_asset_hash.as_str(),
|
||||
("JP", "Linux") => args.linux_asset_hash.as_str(),
|
||||
@@ -95,15 +111,18 @@ impl AssetVersion {
|
||||
}
|
||||
|
||||
fn valid_hashes(asset_version: &str, platform: &str) -> Vec<String> {
|
||||
let args = crate::get_args();
|
||||
let mut out = Vec::new();
|
||||
for entry in ASSET_VERSIONS {
|
||||
if entry.platform != platform {
|
||||
continue;
|
||||
}
|
||||
if entry.version == asset_version && !entry.hash.is_empty() {
|
||||
out.push(entry.stock_hash());
|
||||
if entry.latest || !args.force_updates {
|
||||
out.push(entry.stock_hash());
|
||||
}
|
||||
}
|
||||
if entry.latest {
|
||||
if entry.accepts_override() {
|
||||
if let Some((ov, oh)) = entry.override_pair() {
|
||||
if ov == asset_version {
|
||||
out.push(oh);
|
||||
@@ -120,7 +139,7 @@ fn preferred_hash(asset_version: &str, platform: &str) -> Option<String> {
|
||||
if entry.platform != platform {
|
||||
continue;
|
||||
}
|
||||
if entry.latest {
|
||||
if entry.accepts_override() {
|
||||
if let Some((ov, oh)) = entry.override_pair() {
|
||||
if ov == asset_version {
|
||||
return Some(oh);
|
||||
@@ -160,7 +179,7 @@ pub fn get_player_region(asset_version: &str) -> Option<String> {
|
||||
if entry.version == asset_version {
|
||||
return Some(entry.region.to_string());
|
||||
}
|
||||
if entry.latest {
|
||||
if entry.accepts_override() {
|
||||
if let Some((ov, _)) = entry.override_pair() {
|
||||
if ov == asset_version {
|
||||
return Some(entry.region.to_string());
|
||||
@@ -405,6 +424,10 @@ pub fn send(mut data: JsonValue, uid: i64, req: &HttpRequest) -> HttpResponse {
|
||||
//println!("{}", jzon::stringify(data.clone()));
|
||||
data["server_time"] = set_time(data["server_time"].as_u64().unwrap_or(0), uid, true).into();
|
||||
|
||||
if data["data"].is_object() {
|
||||
super::mission::filter_response(&mut data["data"], req.headers());
|
||||
}
|
||||
|
||||
if !data["data"]["item_list"].is_empty() || !data["data"]["updated_value_list"]["item_list"].is_empty() {
|
||||
items::check_for_region(&mut data, req.headers());
|
||||
}
|
||||
@@ -489,6 +512,22 @@ pub(crate) fn get_cards(arr: JsonValue, user: &JsonValue) -> JsonValue {
|
||||
mod platform_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn host_version_overrides_keep_jp_and_global_distinct() {
|
||||
crate::runtime::apply_config_json(
|
||||
r#"{"assetVersion":"jp-test-version","enAssetVersion":"gl-test-version","jpAndroidAssetHash":"jp-test-hash","enAndroidAssetHash":"gl-test-hash"}"#,
|
||||
);
|
||||
|
||||
assert_eq!(get_player_region("jp-test-version").as_deref(), Some("JP"));
|
||||
assert_eq!(get_player_region("gl-test-version").as_deref(), Some("GL"));
|
||||
assert_eq!(preferred_hash("jp-test-version", "Android").as_deref(), Some("jp-test-hash"));
|
||||
assert_eq!(preferred_hash("gl-test-version", "Android").as_deref(), Some("gl-test-hash"));
|
||||
assert!(valid_hashes("gl-test-version", "Android").contains(&"gl-test-hash".to_string()));
|
||||
assert!(!ASSET_VERSIONS.iter().any(|entry| entry.region == "GL" && entry.latest));
|
||||
|
||||
crate::runtime::apply_config_json("{}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn standalone_player_platforms_are_known() {
|
||||
assert_eq!(parse_platform("WindowsPlayer"), "Windows");
|
||||
|
||||
@@ -324,6 +324,11 @@ async fn migration_password_register(req: HttpRequest, body: String) -> impl Res
|
||||
let pass = decrypt_transfer_password(&body["migration_password"].to_string());
|
||||
|
||||
userdata::user::migration::save_acc_transfer(user["user"]["id"].as_i64().unwrap(), &pass);
|
||||
if !pass.is_empty() {
|
||||
let mut missions = userdata::get_acc_missions(&uid);
|
||||
super::beginner_mission::advance(25, None, 1, &mut missions);
|
||||
userdata::save_acc_missions(&uid, missions);
|
||||
}
|
||||
|
||||
let resp = object!{
|
||||
result: "OK"
|
||||
|
||||
+14
-6
@@ -23,6 +23,10 @@ async fn preset(Session { key, body }: Session) -> impl Responder {
|
||||
}
|
||||
userdata::save_acc_home(&key, user);
|
||||
|
||||
let mut missions = userdata::get_acc_missions(&key);
|
||||
super::beginner_mission::advance(61, None, 1, &mut missions);
|
||||
userdata::save_acc_missions(&key, missions);
|
||||
|
||||
Api(Some(array![]))
|
||||
}
|
||||
|
||||
@@ -84,17 +88,20 @@ lazy_static! {
|
||||
}
|
||||
|
||||
async fn home(Login(key): Login) -> impl Responder {
|
||||
let account = userdata::get_acc(&key);
|
||||
super::chat::refresh_birthday(&key, &account);
|
||||
let mut user = userdata::get_acc_home(&key);
|
||||
|
||||
check_gifts(&mut user);
|
||||
|
||||
let mut user_missions = userdata::get_acc_missions(&key);
|
||||
let clear = items::completed_daily_mission(1253003, &mut user_missions);
|
||||
let mut clear = super::beginner_mission::refresh_account(&account, &mut user_missions);
|
||||
for id in items::completed_daily_mission(1253003, &mut user_missions).members() {
|
||||
clear.push(id.clone()).unwrap();
|
||||
}
|
||||
userdata::save_acc_home(&key, user.clone());
|
||||
user["clear_mission_ids"] = clear;
|
||||
if !user["clear_mission_ids"].is_empty() {
|
||||
userdata::save_acc_missions(&key, user_missions.clone());
|
||||
}
|
||||
userdata::save_acc_missions(&key, user_missions.clone());
|
||||
|
||||
let daily_missions = array![1224003, 1253003, 1273009, 1273010, 1273011, 1273012];
|
||||
|
||||
@@ -128,8 +135,9 @@ async fn home(Login(key): Login) -> impl Responder {
|
||||
let new_announcement = crate::database::announcements::latest_published_at() > seen_at;
|
||||
user["home"]["new_announcement_flag"] = (new_announcement as i32).into();
|
||||
|
||||
//todo
|
||||
user["home"]["beginner_mission_complete"] = 1.into();
|
||||
let (ready, complete) = super::beginner_mission::home_status(&user_missions);
|
||||
user["home"]["clear_beginner_mission_count"] = ready.into();
|
||||
user["home"]["beginner_mission_complete"] = (complete as i32).into();
|
||||
|
||||
Api(Some(user))
|
||||
}
|
||||
|
||||
+2
-13
@@ -422,17 +422,6 @@ pub fn update_mission_status(master_mission_id: i64, expire: u64, completed: boo
|
||||
None
|
||||
}
|
||||
|
||||
pub fn update_mission_status_multi(master_mission_id: JsonValue, expire: u64, completed: bool, claimed: bool, advance: i64, missions: &mut JsonValue) -> JsonValue {
|
||||
let mut rv = array![];
|
||||
for mission in master_mission_id.members() {
|
||||
let val = update_mission_status(mission.as_i64().unwrap(), expire, completed, claimed, advance, missions);
|
||||
if let Some(val2) = val {
|
||||
rv.push(val2).unwrap();
|
||||
}
|
||||
}
|
||||
rv
|
||||
}
|
||||
|
||||
pub fn get_mission_status(id: i64, missions: &JsonValue) -> JsonValue {
|
||||
for mission in missions.members() {
|
||||
if mission["master_mission_id"].as_i64().unwrap() == id {
|
||||
@@ -545,8 +534,8 @@ pub fn completed_daily_mission(id: i64, missions: &mut JsonValue) -> JsonValue {
|
||||
let mut rv = array![];
|
||||
if id == 1253003 {
|
||||
rv = advance_variable_mission(1153001, 1153019, 1, missions);
|
||||
if update_mission_status(1653001, 0, true, false, 1, missions).is_some() {
|
||||
rv.push(1653001).unwrap();
|
||||
for id in super::beginner_mission::advance(53, None, 1, missions).members() {
|
||||
rv.push(id.clone()).unwrap();
|
||||
}
|
||||
}
|
||||
for mission in missions.members_mut() {
|
||||
|
||||
@@ -727,6 +727,17 @@ pub fn live_end_ex(req: &HttpRequest, key: &str, body: &JsonValue, skipped: bool
|
||||
|
||||
//1273009, 1273010, 1273011, 1273012
|
||||
let mut cleared_missions = items::advance_variable_mission(1105001, 1105017, 1, &mut user_missions);
|
||||
for id in super::beginner_mission::advance(5, None, 1, &mut user_missions).members() {
|
||||
cleared_missions.push(id.clone()).unwrap();
|
||||
}
|
||||
if !skipped {
|
||||
let started = get_started_live(key, body).unwrap_or(JsonValue::Null);
|
||||
if started["is_omakase"] == 1 {
|
||||
for id in super::beginner_mission::advance(62, None, 1, &mut user_missions).members() { cleared_missions.push(id.clone()).unwrap(); }
|
||||
}
|
||||
let boost = started["live_boost"].as_i64().or(body["live_boost"].as_i64()).unwrap_or(0);
|
||||
for id in super::beginner_mission::advance(6, Some(boost), 1, &mut user_missions).members() { cleared_missions.push(id.clone()).unwrap(); }
|
||||
}
|
||||
if body["master_live_id"].to_string().len() > 1 {
|
||||
let id = body["master_live_id"].to_string().split("").collect::<Vec<_>>()[2].parse::<i64>().unwrap_or(0);
|
||||
if (1..=4).contains(&id) {
|
||||
@@ -822,6 +833,8 @@ pub fn live_end_ex(req: &HttpRequest, key: &str, body: &JsonValue, skipped: bool
|
||||
|
||||
let deck_slot = get_end_live_deck_id(&key, &body).unwrap_or(body["deck_slot"].as_i32().unwrap_or(user["user"]["main_deck_slot"].as_i32().unwrap()));
|
||||
let characters = get_live_character_list(lp_used, deck_slot, &mut user, &mut user_missions, &mut cleared_missions, &mut chats);
|
||||
for id in super::beginner_mission::refresh(&user, &mut user_missions).members() { cleared_missions.push(id.clone()).unwrap(); }
|
||||
super::story::refresh(&user, &mut user_missions);
|
||||
|
||||
userdata::save_acc(&key, user.clone());
|
||||
userdata::save_acc_home(&key, user2.clone());
|
||||
|
||||
@@ -390,6 +390,9 @@ async fn lottery_post(req: HttpRequest, Session { key, body }: Session) -> impl
|
||||
}
|
||||
|
||||
add_draw_count(&mut user, lottery_id, price_number);
|
||||
for id in super::beginner_mission::advance(63, Some(lottery_id), price["count"].as_i64().unwrap_or(0), &mut missions).members() {
|
||||
cleared_missions.push(id.clone()).unwrap();
|
||||
}
|
||||
let mut new_count = get_draw_count(&user, lottery_id, price_number);
|
||||
if is_stepup(lottery_id) && price_number == 1 {
|
||||
new_count += 1;
|
||||
|
||||
+231
-7
@@ -3,6 +3,29 @@ use actix_web::{web, HttpRequest, Responder};
|
||||
|
||||
use crate::router::{global, userdata, items, databases, Login, Session, Api};
|
||||
|
||||
pub fn filter_response(data: &mut JsonValue, headers: &actix_web::http::header::HeaderMap) {
|
||||
if items::get_region(headers) { return; }
|
||||
lazy_static::lazy_static! {
|
||||
static ref GLOBAL_IDS: std::collections::HashSet<i64> =
|
||||
databases::csv::table(databases::csv::Region::En, "mission")
|
||||
.members().filter_map(|row| row["id"].as_i64()).collect();
|
||||
}
|
||||
// Only project the response. Shared JP/global accounts keep their progress.
|
||||
for field in ["mission_list", "clear_mission_ids"] {
|
||||
if !data[field].is_array() { continue; }
|
||||
for i in (0..data[field].len()).rev() {
|
||||
let id = if field == "mission_list" { data[field][i]["master_mission_id"].as_i64() }
|
||||
else { data[field][i].as_i64() };
|
||||
if !id.is_some_and(|id| GLOBAL_IDS.contains(&id)) {
|
||||
data[field].array_remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
if data["updated_value_list"].is_object() {
|
||||
filter_response(&mut data["updated_value_list"], headers);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn routes(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(
|
||||
web::scope("/mission")
|
||||
@@ -12,6 +35,161 @@ pub fn routes(cfg: &mut web::ServiceConfig) {
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[actix_web::test]
|
||||
async fn mission_endpoint_filters_global_wire_response_not_saved_account() {
|
||||
let _test = crate::runtime::lock_test_data_path();
|
||||
let (uid, key) = userdata::starter::create("Regional missions test").unwrap();
|
||||
let mut user = userdata::get_acc(&key);
|
||||
user["tutorial_step"] = 999.into();
|
||||
userdata::save_acc(&key, user);
|
||||
for version in ["5260ff15dff8ba0c00ad91400f515f55", "4c921d2443335e574a82e04ec9ea243c"] {
|
||||
let request = actix_web::test::TestRequest::default()
|
||||
.insert_header(("aoharu-user-id", uid.to_string()))
|
||||
.insert_header(("aoharu-asset-version", version)).to_http_request();
|
||||
let response = mission(Login(key.clone())).await.respond_to(&request);
|
||||
let body = actix_web::body::to_bytes(response.into_body()).await.ok().unwrap();
|
||||
let plain = crate::encryption::decrypt_packet(std::str::from_utf8(&body).unwrap()).unwrap();
|
||||
let response = jzon::parse(&plain).unwrap();
|
||||
assert_eq!(response["code"], 0);
|
||||
let missions = &response["data"]["mission_list"];
|
||||
assert!(missions.members().any(|m| m["master_mission_id"] == 1073001));
|
||||
assert_eq!(missions.members().any(|m| m["master_mission_id"] == 1073037),
|
||||
items::get_region(request.headers()));
|
||||
assert!(userdata::get_acc_missions(&key).members()
|
||||
.any(|m| m["master_mission_id"] == 1073037));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn global_missions_match_client_masterdata_without_losing_saved_progress() {
|
||||
let request = actix_web::test::TestRequest::default()
|
||||
.insert_header(("aoharu-asset-version", "5260ff15dff8ba0c00ad91400f515f55"))
|
||||
.to_http_request();
|
||||
assert!(!items::get_region(request.headers()));
|
||||
let mut saved = array![];
|
||||
super::super::story::refresh(&object! {live_list: []}, &mut saved);
|
||||
let original = saved.clone();
|
||||
let ids = databases::csv::table(databases::csv::Region::En, "mission");
|
||||
let known: std::collections::HashSet<_> = ids.members()
|
||||
.filter_map(|r| r["id"].as_i64()).collect();
|
||||
let absent: Vec<_> = saved.members().filter_map(|m| {
|
||||
let id = m["master_mission_id"].as_i64().unwrap();
|
||||
(!known.contains(&id)).then_some(id)
|
||||
}).collect();
|
||||
assert_eq!(absent.len(), 30);
|
||||
assert!(absent.iter().all(|id| (1073037..=1073066).contains(id)));
|
||||
let expected: JsonValue = saved.members().filter(|m|
|
||||
known.contains(&m["master_mission_id"].as_i64().unwrap()))
|
||||
.cloned().collect::<Vec<_>>().into();
|
||||
let mut response = object! {mission_list: saved.clone(),
|
||||
clear_mission_ids: [1073037, 1073001, 1073066],
|
||||
updated_value_list: {mission_list: saved.clone(), clear_mission_ids: [1073001, 1073037]},
|
||||
reward_list: [{type: 1, amount: 100}]};
|
||||
filter_response(&mut response, request.headers());
|
||||
assert_eq!(response["mission_list"], expected);
|
||||
assert_eq!(response["updated_value_list"]["mission_list"], expected);
|
||||
assert_eq!(response["clear_mission_ids"], array![1073001]);
|
||||
assert_eq!(response["updated_value_list"]["clear_mission_ids"], array![1073001]);
|
||||
assert_eq!(response["reward_list"], array![object! {type: 1, amount: 100}]);
|
||||
assert_eq!(saved, original);
|
||||
let mut unrelated = object! {item_list: [], value: 42};
|
||||
let before = unrelated.clone();
|
||||
filter_response(&mut unrelated, request.headers());
|
||||
assert_eq!(unrelated, before);
|
||||
let mut jp = object! {mission_list: saved};
|
||||
let before = jp.clone();
|
||||
filter_response(&mut jp, actix_web::test::TestRequest::default().to_http_request().headers());
|
||||
assert_eq!(jp, before);
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn bond_title_claim_notifies_client_and_persists() {
|
||||
let _test = crate::runtime::lock_test_data_path();
|
||||
let (uid, key) = userdata::starter::create("Bond title test").unwrap();
|
||||
let request = || actix_web::test::TestRequest::default()
|
||||
.insert_header(("aoharu-user-id", uid.to_string())).to_http_request();
|
||||
let initial_titles = userdata::get_acc(&key)["master_title_ids"].clone();
|
||||
userdata::save_acc_missions(&key, array![object! {
|
||||
master_mission_id: 1158015, status: 2, progress: 1500, expire_date_time: 0
|
||||
}]);
|
||||
|
||||
let response = receive(request(), Session {
|
||||
key: key.clone(), body: object! {master_mission_ids: [1158015, 1158015]}
|
||||
}).await.0.unwrap();
|
||||
assert_eq!(response["reward_list"].len(), 1);
|
||||
assert_eq!(response["reward_list"][0]["type"], 8);
|
||||
assert_eq!(response["reward_list"][0]["value"], 3000015);
|
||||
assert_eq!(response["updated_value_list"]["master_title_ids"], array![3000015]);
|
||||
assert!(response["updated_value_list"]["item_list"].is_null());
|
||||
let saved = userdata::get_acc(&key);
|
||||
assert!(saved["master_title_ids"].contains(3000015));
|
||||
for title in initial_titles.members() {
|
||||
assert!(saved["master_title_ids"].contains(title.as_i64().unwrap()));
|
||||
}
|
||||
|
||||
let replay = receive(request(), Session {
|
||||
key: key.clone(), body: object! {master_mission_ids: [1158015]}
|
||||
}).await.0.unwrap();
|
||||
assert!(replay["reward_list"].is_empty());
|
||||
assert!(replay["updated_value_list"]["master_title_ids"].is_null());
|
||||
assert_eq!(userdata::get_acc(&key), saved);
|
||||
|
||||
let mut missions = userdata::get_acc_missions(&key);
|
||||
items::update_mission_status(1158054, 0, true, false, 13500, &mut missions);
|
||||
userdata::save_acc_missions(&key, missions);
|
||||
let next = receive(request(), Session {
|
||||
key: key.clone(), body: object! {master_mission_ids: [1158054]}
|
||||
}).await.0.unwrap();
|
||||
assert_eq!(next["updated_value_list"]["master_title_ids"], array![3000054]);
|
||||
let saved = userdata::get_acc(&key);
|
||||
assert!(saved["master_title_ids"].contains(3000015));
|
||||
assert!(saved["master_title_ids"].contains(3000054));
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn beginner_claims_persist_rewards_and_reject_repeats() {
|
||||
let _test = crate::runtime::lock_test_data_path();
|
||||
let (uid, key) = userdata::starter::create("Beginner test").unwrap();
|
||||
let request = || actix_web::test::TestRequest::default()
|
||||
.insert_header(("aoharu-user-id", uid.to_string())).to_http_request();
|
||||
let first = [1653001,1605001,1662001,1663001,1660001,1614001,1666001,1658001,1661001];
|
||||
let mut missions = userdata::get_acc_missions(&key);
|
||||
for id in first {
|
||||
super::super::beginner_mission::set_progress(id, i64::MAX, &mut missions);
|
||||
}
|
||||
super::super::beginner_mission::set_progress(1653002, 2, &mut missions);
|
||||
userdata::save_acc_missions(&key, missions);
|
||||
let blocked = receive(request(), Session { key: key.clone(), body: object! {master_mission_ids: [1653002]} }).await.0.unwrap();
|
||||
assert!(blocked["reward_list"].is_empty());
|
||||
let gem_before = userdata::get_acc(&key)["gem"]["total"].as_i64().unwrap();
|
||||
let mut ids: Vec<i64> = first.into();
|
||||
ids.push(first[0]);
|
||||
let body = object! {master_mission_ids: ids};
|
||||
let response = receive(request(), Session { key: key.clone(), body: body.clone() }).await.0.unwrap();
|
||||
assert_eq!(response["mission_list"].len(), 9);
|
||||
assert_eq!(response["reward_list"].len(), 18);
|
||||
let bonuses: i64 = response["reward_list"].members().filter(|r| r["type"] == 1)
|
||||
.map(|r| r["amount"].as_i64().unwrap()).sum();
|
||||
assert_eq!(bonuses, 1000);
|
||||
assert_eq!(userdata::get_acc(&key)["gem"]["total"].as_i64().unwrap(), gem_before + bonuses);
|
||||
let items = &response["updated_value_list"]["item_list"];
|
||||
let unique: std::collections::HashSet<_> = items.members().map(|i| i["master_item_id"].as_i64()).collect();
|
||||
assert_eq!(items.len(), unique.len());
|
||||
let saved = userdata::get_acc(&key);
|
||||
let replay = receive(request(), Session { key: key.clone(), body }).await.0.unwrap();
|
||||
assert!(replay["reward_list"].is_empty());
|
||||
assert_eq!(userdata::get_acc(&key), saved);
|
||||
let second = receive(request(), Session { key: key.clone(), body: object! {master_mission_ids: [1653002, 1605002]} }).await.0.unwrap();
|
||||
assert_eq!(second["mission_list"].len(), 1);
|
||||
assert_eq!(second["mission_list"][0]["master_mission_id"], 1653002);
|
||||
assert_eq!(second["mission_list"][0]["progress"], 2);
|
||||
}
|
||||
}
|
||||
|
||||
const VARIABLE_MISSIONS: [[i64; 2]; 5] = [[1153001, 1153019], [1105001, 1105017], [1101001, 1101030], [1121001, 1121019], [1112001, 1112033]];
|
||||
|
||||
async fn mission(Login(key): Login) -> impl Responder {
|
||||
@@ -25,9 +203,10 @@ async fn mission(Login(key): Login) -> impl Responder {
|
||||
}
|
||||
|
||||
let mut missions = userdata::get_acc_missions(&key);
|
||||
if items::refresh_dailies(&mut missions, global::timestamp()) {
|
||||
userdata::save_acc_missions(&key, missions.clone());
|
||||
}
|
||||
items::refresh_dailies(&mut missions, global::timestamp());
|
||||
super::story::refresh(&user, &mut missions);
|
||||
super::beginner_mission::refresh_account(&user, &mut missions);
|
||||
userdata::save_acc_missions(&key, missions.clone());
|
||||
|
||||
Api(Some(object!{
|
||||
"mission_list": missions
|
||||
@@ -39,6 +218,10 @@ async fn clear(Session { key, body }: Session) -> impl Responder {
|
||||
|
||||
let mut cleared = array![];
|
||||
for id in body["master_mission_ids"].members() {
|
||||
if super::beginner_mission::contains(id.as_i64().unwrap_or(0)) {
|
||||
let condition = databases::MISSION_LIST[id.to_string()]["conditionType"].as_i64();
|
||||
if !matches!(condition, Some(26 | 65 | 66)) { continue; }
|
||||
}
|
||||
let mission = items::get_mission_status(id.as_i64().unwrap(), &missions);
|
||||
if mission.is_empty() || mission["status"].as_i32().unwrap_or(0) >= 2 {
|
||||
continue;
|
||||
@@ -54,7 +237,11 @@ async fn clear(Session { key, body }: Session) -> impl Responder {
|
||||
}))
|
||||
}
|
||||
|
||||
async fn receive(req: HttpRequest, Session { key, body }: Session) -> impl Responder {
|
||||
static CLAIM_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
|
||||
|
||||
async fn receive(req: HttpRequest, Session { key, body }: Session) -> Api {
|
||||
// Keep the read/grant/save sequence exclusive across HTTP workers.
|
||||
let _claim = crate::lock_onto_mutex!(CLAIM_LOCK);
|
||||
let uid = global::get_uid(req.headers());
|
||||
let now = global::timestamp();
|
||||
|
||||
@@ -66,9 +253,21 @@ async fn receive(req: HttpRequest, Session { key, body }: Session) -> impl Respo
|
||||
let mut touched_gem = false;
|
||||
let mut touched_coin = false;
|
||||
let mut touched_items = array![];
|
||||
let mut touched_titles = std::collections::BTreeSet::new();
|
||||
let before = missions.clone();
|
||||
let mut requested: Vec<_> = body["master_mission_ids"].members().filter_map(|id| id.as_i64()).collect();
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
requested.retain(|id| seen.insert(*id));
|
||||
// Process beginner cells in sheet order so a single claim can unlock the next sheet.
|
||||
requested.sort_by_key(|id| super::beginner_mission::level(*id));
|
||||
|
||||
for mission in body["master_mission_ids"].members() {
|
||||
let mid = mission.as_i64().unwrap();
|
||||
for mid in requested {
|
||||
if items::get_mission_status(mid, &missions)["status"] != 2 {
|
||||
continue;
|
||||
}
|
||||
if super::beginner_mission::contains(mid) && !super::beginner_mission::claimable(mid, &missions) {
|
||||
continue;
|
||||
}
|
||||
let mission_info = databases::MISSION_LIST[mid.to_string()].clone();
|
||||
for master in databases::MISSION_REWARDS[mission_info["masterMissionRewardId"].to_string()].members() {
|
||||
let reward_type = master["type"].as_i64().unwrap();
|
||||
@@ -90,6 +289,7 @@ async fn receive(req: HttpRequest, Session { key, body }: Session) -> impl Respo
|
||||
match reward_type {
|
||||
1 => touched_gem = true,
|
||||
4 => touched_coin = true,
|
||||
8 => { touched_titles.insert(master["value"].as_i64().unwrap()); }
|
||||
_ => { touched_items.push(master["value"].clone()).unwrap(); }
|
||||
}
|
||||
}
|
||||
@@ -135,24 +335,45 @@ async fn receive(req: HttpRequest, Session { key, body }: Session) -> impl Respo
|
||||
user_id: uid,
|
||||
master_mission_id: mid,
|
||||
status: 3,
|
||||
progress: if expire != 0 { m["progress"].clone() } else { JsonValue::Null },
|
||||
progress: if expire != 0 || super::beginner_mission::is_counter(mid) { m["progress"].clone() } else { JsonValue::Null },
|
||||
expire_date: if expire != 0 { global::format_datetime(expire).into() } else { JsonValue::Null },
|
||||
received_date: global::format_datetime(now)
|
||||
}).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
for master in super::beginner_mission::new_rewards(&before, &missions) {
|
||||
let reward_type = master["type"].as_i64().unwrap();
|
||||
rewards.push(object! {
|
||||
give_type: master["giveType"].clone(), type: master["type"].clone(),
|
||||
value: master["value"].clone(), level: master["level"].clone(), amount: master["amount"].clone()
|
||||
}).unwrap();
|
||||
items::give_gift(&object! {
|
||||
reward_type: reward_type, value: master["value"].clone(), amount: master["amount"].clone()
|
||||
}, &mut user, &mut missions, &mut array![], &mut chats);
|
||||
match reward_type {
|
||||
1 => touched_gem = true,
|
||||
4 => touched_coin = true,
|
||||
8 => { touched_titles.insert(master["value"].as_i64().unwrap()); }
|
||||
_ => { touched_items.push(master["value"].clone()).unwrap(); }
|
||||
}
|
||||
}
|
||||
userdata::save_acc(&key, user.clone());
|
||||
userdata::save_acc_chats(&key, chats);
|
||||
userdata::save_acc_missions(&key, missions);
|
||||
|
||||
let mut updated_value_list = object!{};
|
||||
if !touched_titles.is_empty() {
|
||||
updated_value_list["master_title_ids"] = touched_titles.into_iter().collect::<Vec<_>>().into();
|
||||
}
|
||||
if touched_gem {
|
||||
updated_value_list["gem"] = user["gem"].clone();
|
||||
}
|
||||
if !touched_items.is_empty() {
|
||||
let mut item_list = array![];
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
for id in touched_items.members() {
|
||||
if !seen.insert(id.as_i64()) { continue; }
|
||||
for item in user["item_list"].members() {
|
||||
if item["master_item_id"] == *id {
|
||||
item_list.push(item.clone()).unwrap();
|
||||
@@ -176,6 +397,9 @@ async fn receive(req: HttpRequest, Session { key, body }: Session) -> impl Respo
|
||||
updated_value_list["point_list"] = point_list;
|
||||
}
|
||||
|
||||
let mut sorted: Vec<_> = mission_list.members().cloned().collect();
|
||||
sorted.sort_by_key(|m| m["master_mission_id"].as_i64());
|
||||
let mission_list: JsonValue = sorted.into();
|
||||
Api(Some(object!{
|
||||
"reward_list": rewards,
|
||||
"gift_list": array![],
|
||||
|
||||
@@ -404,6 +404,7 @@ async fn end(req: HttpRequest, Session { key, body }: Session) -> impl Responder
|
||||
// live_end scales exp / gold / bond / mission rewards off.
|
||||
let mut end_body = body.clone();
|
||||
end_body["use_lp"] = lp_used.into();
|
||||
end_body["live_boost"] = live_boost.into();
|
||||
if end_body["deck_slot"].is_null() {
|
||||
if let Some(slot) = started.and_then(|s| s["deck_slot"].as_i32()) {
|
||||
end_body["deck_slot"] = slot.into();
|
||||
|
||||
@@ -104,6 +104,7 @@ impl Value {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn as_str(&self) -> Option<&str> {
|
||||
match self {
|
||||
Value::Str(v) => Some(v),
|
||||
@@ -148,6 +149,7 @@ impl Map {
|
||||
self.get(key).and_then(Value::as_int)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn get_str(&self, key: &str) -> Option<&str> {
|
||||
self.get(key).and_then(Value::as_str)
|
||||
}
|
||||
|
||||
+249
-28
@@ -1,41 +1,262 @@
|
||||
use jzon::{object};
|
||||
use actix_web::{web, Responder};
|
||||
|
||||
use crate::router::{userdata, databases, Session, Api};
|
||||
use jzon::{array, object, JsonValue};
|
||||
use actix_web::{web, HttpRequest};
|
||||
use crate::router::{chat, global, items, userdata, databases, Session, Api};
|
||||
use databases::csv::{table, Region};
|
||||
|
||||
pub fn routes(cfg: &mut web::ServiceConfig) {
|
||||
cfg.route("/story/read", web::post().to(read));
|
||||
}
|
||||
|
||||
async fn read(Session { key, body }: Session) -> impl Responder {
|
||||
let mut user = userdata::get_acc(&key);
|
||||
let part = body["master_story_part_id"].as_i64().unwrap();
|
||||
let master_id = databases::STORY[part.to_string()]["masterStoryId"].as_i64().unwrap();
|
||||
static READ_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
|
||||
|
||||
let index = user["story_list"].members().position(|r| r["master_story_id"] == master_id);
|
||||
fn has_read(user: &JsonValue, story: i64, part: i64) -> bool {
|
||||
user["story_list"].members().any(|s| s["master_story_id"] == story
|
||||
&& s["master_story_part_ids"].contains(part))
|
||||
}
|
||||
|
||||
if index.is_none() {
|
||||
user["story_list"].push(object!{
|
||||
master_story_id: master_id,
|
||||
master_story_part_ids: []
|
||||
}).unwrap();
|
||||
}
|
||||
|
||||
for story in user["story_list"].members_mut() {
|
||||
if story["master_story_id"] == master_id && !story["master_story_part_ids"].contains(part) {
|
||||
story["master_story_part_ids"].push(part).unwrap();
|
||||
pub fn refresh(user: &JsonValue, missions: &mut JsonValue) {
|
||||
let mut counts = std::collections::HashMap::<i64, i64>::new();
|
||||
for live in user["live_list"].members() {
|
||||
let song = &databases::MUSIC[live["master_live_id"].to_string()];
|
||||
if !song.is_null() {
|
||||
*counts.entry(song["bandCategory"].as_i64().unwrap_or(0)).or_default()
|
||||
+= live["clear_count"].as_i64().unwrap_or(0);
|
||||
}
|
||||
}
|
||||
for (_, master) in databases::MISSION_LIST.entries() {
|
||||
let condition = master["conditionType"].as_i64().unwrap_or(0);
|
||||
if !matches!(condition, 73 | 78) { continue; }
|
||||
let id = master["id"].as_i64().unwrap();
|
||||
let value = master["conditionValues"][0].as_i64().unwrap_or(0);
|
||||
let count = if condition == 73 { counts.get(&value).copied().unwrap_or(0) }
|
||||
else { super::event::get_points(value as u32, user) };
|
||||
if condition == 78 && count == 0 { continue; }
|
||||
set_progress(id, count, missions);
|
||||
}
|
||||
}
|
||||
|
||||
userdata::save_acc(&key, user.clone());
|
||||
fn set_progress(id: i64, count: i64, missions: &mut JsonValue) {
|
||||
if !missions.members().any(|m| m["master_mission_id"] == id) {
|
||||
missions.push(object! {master_mission_id: id, status: 1, progress: 0, expire_date_time: 0, not_visible: 0}).unwrap();
|
||||
}
|
||||
let master = &databases::MISSION_LIST[id.to_string()];
|
||||
let target = master["conditionNumber"].as_i64().unwrap();
|
||||
let mission = missions.members_mut().find(|m| m["master_mission_id"] == id).unwrap();
|
||||
if mission["status"].as_i64().unwrap_or(1) >= 2 { return; }
|
||||
let progress = count.max(mission["progress"].as_i64().unwrap_or(0)).min(target);
|
||||
mission["progress"] = progress.into();
|
||||
mission["status"] = if progress >= target { 2 } else { 1 }.into();
|
||||
}
|
||||
|
||||
pub fn visit_event(key: &str, event: u32) {
|
||||
let user = userdata::get_acc(key);
|
||||
let now = global::set_time(global::timestamp(), user["user"]["id"].as_i64().unwrap(), true);
|
||||
if !super::event::is_in_session(event, now) { return; }
|
||||
let mut missions = userdata::get_acc_missions(key);
|
||||
for (_, master) in databases::MISSION_LIST.entries() {
|
||||
if master["conditionType"] == 79 && master["conditionValues"][0] == event {
|
||||
set_progress(master["id"].as_i64().unwrap(), 1, &mut missions);
|
||||
}
|
||||
}
|
||||
refresh(&user, &mut missions);
|
||||
userdata::save_acc_missions(key, missions);
|
||||
}
|
||||
|
||||
Api(Some(object!{
|
||||
"gift_list":[],
|
||||
"updated_value_list":{
|
||||
"story_list": user["story_list"].clone()
|
||||
},
|
||||
"reward_list":[],
|
||||
"clear_mission_ids":[]
|
||||
}))
|
||||
async fn read(req: HttpRequest, Session { key, body }: Session) -> Api {
|
||||
let _read = crate::lock_onto_mutex!(READ_LOCK);
|
||||
let Some(part_id) = body["master_story_part_id"].as_i64() else { return Api(None); };
|
||||
let part = &databases::STORY[part_id.to_string()];
|
||||
let Some(story_id) = part["masterStoryId"].as_i64() else { return Api(None); };
|
||||
let mut user = userdata::get_acc(&key);
|
||||
let empty = || object! {gift_list: [], updated_value_list: [], reward_list: [], clear_mission_ids: []};
|
||||
if has_read(&user, story_id, part_id) { return Api(Some(empty())); }
|
||||
if let Some(previous) = databases::STORY.entries().map(|(_, p)| p)
|
||||
.filter(|p| p["masterStoryId"] == story_id && p["number"].as_i64() < part["number"].as_i64())
|
||||
.max_by_key(|p| p["number"].as_i64()) {
|
||||
if !has_read(&user, story_id, previous["id"].as_i64().unwrap()) { return Api(None); }
|
||||
}
|
||||
let mut missions = userdata::get_acc_missions(&key);
|
||||
refresh(&user, &mut missions);
|
||||
let releases = table(Region::Jp, "story_release");
|
||||
let now = global::set_time(global::timestamp(), user["user"]["id"].as_i64().unwrap(), true);
|
||||
let stories = table(Region::Jp, "story");
|
||||
let Some(story_master) = stories.members().find(|s| s["id"] == story_id) else { return Api(None); };
|
||||
let mut fully_open = false;
|
||||
if let Some(release) = releases.members().find(|r| r["masterStoryPartId"] == part_id) {
|
||||
fully_open = global::parse_datetime(release["openedAtAfterEvent"].as_str().unwrap_or(""))
|
||||
.is_some_and(|at| now >= at);
|
||||
if story_master["type"] == 3 {
|
||||
let event_stories = table(Region::Jp, "event_story");
|
||||
let ended = event_stories.members().find(|e| e["masterStoryId"] == story_id)
|
||||
.and_then(|e| {
|
||||
let event = &databases::EVENTS[e["masterEventId"].to_string()];
|
||||
global::parse_datetime(databases::RELEASE_LABEL[event["masterReleaseLabelId"].to_string()]["closedAt"].as_str().unwrap_or(""))
|
||||
}).is_some_and(|end| now > end);
|
||||
fully_open &= ended;
|
||||
}
|
||||
let mission = release["masterMissionId"].as_i64().unwrap_or(0);
|
||||
if !fully_open {
|
||||
let rank = items::get_user_rank_data(user["user"]["exp"].as_i64().unwrap_or(0))["rank"].as_i64().unwrap_or(0);
|
||||
if rank < release["userRank"].as_i64().unwrap_or(0) { return Api(None); }
|
||||
let live = release["masterLiveId"].as_i64().unwrap_or(0);
|
||||
let score = release["liveScore"].as_i64().unwrap_or(0);
|
||||
if live != 0 && score != 0 && !user["live_list"].members().any(|l|
|
||||
l["master_live_id"] == live && l["clear_count"].as_i64().unwrap_or(0) > 0
|
||||
&& l["high_score"].as_i64().unwrap_or(0) >= score) { return Api(None); }
|
||||
}
|
||||
if !fully_open && mission != 0 && !missions.members().any(|m|
|
||||
m["master_mission_id"] == mission && m["status"].as_i64().unwrap_or(0) >= 2) {
|
||||
return Api(None);
|
||||
}
|
||||
}
|
||||
if !fully_open && (!super::event::release_label_is_open(part["masterReleaseLabelId"].as_i64().unwrap_or(0), now)
|
||||
|| !super::event::release_label_is_open(story_master["masterReleaseLabelId"].as_i64().unwrap_or(0), now)) {
|
||||
return Api(None);
|
||||
}
|
||||
let mut home = userdata::get_acc_home(&key);
|
||||
let mut chats = userdata::get_acc_chats(&key);
|
||||
let mut response = empty();
|
||||
let mut rooms = array![];
|
||||
let mut chapters = array![];
|
||||
let region = if items::get_region(req.headers()) { Region::Jp } else { Region::En };
|
||||
let mut rewards: Vec<_> = table(region, "story_reward").members()
|
||||
.filter(|r| r["id"] == part["masterStoryRewardId"]).cloned().collect();
|
||||
// Official replies group room rewards before chapter rewards.
|
||||
rewards.sort_by_key(|r| (r["type"].as_i64(), r["number"].as_i64()));
|
||||
for reward in rewards {
|
||||
let value = reward["value"].as_i64().unwrap();
|
||||
match reward["type"].as_i64().unwrap() {
|
||||
1 => {
|
||||
if home["home"]["gift_list"].len() >= items::GIFT_LIMIT { return Api(None); }
|
||||
let mut gift = reward.clone();
|
||||
let next_id = loop {
|
||||
let id = rand::random::<u64>() & ((1u64 << 53) - 1);
|
||||
if id != 0 && !home["home"]["gift_list"].members().any(|g| g["id"] == id) { break id; }
|
||||
};
|
||||
gift["id"] = next_id.into();
|
||||
let reason = if region == Region::Jp { "スクールアイドルの日常の報酬です。" } else { "Daily Life of School idols reward." };
|
||||
let gift = items::gift_item(&gift, reason, &mut home);
|
||||
response["gift_list"].push(gift).unwrap();
|
||||
}
|
||||
16 => {} // Chapter rewards below materialize room ownership.
|
||||
17 => {
|
||||
if !chat::add_chat_from_chapter_id(value, &mut chats) { continue; }
|
||||
chapters.push(value).unwrap();
|
||||
let chapter = &databases::CHAPTERS_MASTER[value.to_string()];
|
||||
let room = databases::CHATS[chapter["masterChatId"].to_string()][chapter["roomId"].to_string()]["id"].clone();
|
||||
rooms.push(room).unwrap();
|
||||
}
|
||||
_ => return Api(None),
|
||||
}
|
||||
response["reward_list"].push(object! {
|
||||
type: reward["type"].clone(), value: value,
|
||||
level: reward["level"].clone(), amount: reward["amount"].clone()
|
||||
}).unwrap();
|
||||
}
|
||||
if !chapters.is_empty() {
|
||||
response["updated_value_list"] = object! {master_chat_room_ids: rooms, master_chat_chapter_ids: chapters};
|
||||
}
|
||||
if !user["story_list"].members().any(|s| s["master_story_id"] == story_id) {
|
||||
user["story_list"].push(object! {master_story_id: story_id, master_story_part_ids: []}).unwrap();
|
||||
}
|
||||
for story in user["story_list"].members_mut().filter(|s| s["master_story_id"] == story_id) {
|
||||
story["master_story_part_ids"].push(part_id).unwrap();
|
||||
}
|
||||
userdata::save_acc_home(&key, home);
|
||||
userdata::save_acc_chats(&key, chats);
|
||||
userdata::save_acc_missions(&key, missions);
|
||||
userdata::save_acc(&key, user);
|
||||
Api(Some(response))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[actix_web::test]
|
||||
async fn event_entry_and_point_thresholds_unlock_story_in_its_original_period() {
|
||||
let _test = crate::runtime::lock_test_data_path();
|
||||
let (_, key) = userdata::starter::create("Event story test").unwrap();
|
||||
userdata::save_server_data(&key, object! {
|
||||
server_time: global::parse_datetime("2023/04/22 12:00:00").unwrap(), server_time_set: global::timestamp()
|
||||
});
|
||||
let request = || actix_web::test::TestRequest::default().to_http_request();
|
||||
let part = |id: i64| Session {key: key.clone(), body: object! {master_story_part_id: id}};
|
||||
assert!(read(request(), part(40001001)).await.0.is_none());
|
||||
visit_event(&key, 101);
|
||||
assert!(read(request(), part(40001001)).await.0.is_some());
|
||||
let mut user = userdata::get_acc(&key);
|
||||
super::super::event::give_event_points(101, 4999, &mut user);
|
||||
userdata::save_acc(&key, user);
|
||||
assert!(read(request(), part(40001002)).await.0.is_none());
|
||||
let mut user = userdata::get_acc(&key);
|
||||
super::super::event::give_event_points(101, 1, &mut user);
|
||||
userdata::save_acc(&key, user);
|
||||
assert!(read(request(), part(40001002)).await.0.is_some());
|
||||
assert!(read(request(), part(40001003)).await.0.is_none());
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn story_unlocks_before_and_after_full_release() {
|
||||
let _test = crate::runtime::lock_test_data_path();
|
||||
let (_, key) = userdata::starter::create("Story unlock test").unwrap();
|
||||
let request = || actix_web::test::TestRequest::default().to_http_request();
|
||||
let read_part = |id: i64| Session {key: key.clone(), body: object! {master_story_part_id: id}};
|
||||
userdata::save_server_data(&key, object! {
|
||||
server_time: global::parse_datetime("2023/12/01 12:00:00").unwrap(), server_time_set: global::timestamp()
|
||||
});
|
||||
assert!(read(request(), read_part(210001001)).await.0.is_none());
|
||||
let mut user = userdata::get_acc(&key);
|
||||
user["live_list"] = array![object! {master_live_id: 1100101, clear_count: 1}];
|
||||
userdata::save_acc(&key, user);
|
||||
assert!(read(request(), read_part(210001001)).await.0.is_some());
|
||||
assert!(read(request(), read_part(210001002)).await.0.is_none());
|
||||
let mut user = userdata::get_acc(&key);
|
||||
user["live_list"][0]["clear_count"] = 10.into();
|
||||
userdata::save_acc(&key, user);
|
||||
assert!(read(request(), read_part(210001002)).await.0.is_some());
|
||||
// Event parts remain gated before full release, including previous-part order.
|
||||
assert!(read(request(), read_part(40001001)).await.0.is_none());
|
||||
userdata::save_server_data(&key, object! {
|
||||
server_time: global::parse_datetime("2024/03/01 12:00:00").unwrap(), server_time_set: global::timestamp()
|
||||
});
|
||||
assert!(read(request(), read_part(40001002)).await.0.is_none());
|
||||
assert!(read(request(), read_part(40001001)).await.0.is_some());
|
||||
assert!(read(request(), read_part(40001002)).await.0.is_some());
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn story_rewards_persist_and_replays_do_not_pay_again() {
|
||||
let _test = crate::runtime::lock_test_data_path();
|
||||
let (_, key) = userdata::starter::create("Story test").unwrap();
|
||||
userdata::save_acc_chats(&key, array![]);
|
||||
let request = || actix_web::test::TestRequest::default().to_http_request();
|
||||
let reply = read(request(), Session {key: key.clone(), body: object! {master_story_part_id: 20000001}}).await.0.unwrap();
|
||||
// jp/logs.txt:1871-1902, the first Aqours introduction read.
|
||||
assert_eq!(reply["updated_value_list"], object! {
|
||||
master_chat_room_ids: [2001001, 2101001], master_chat_chapter_ids: [200100101, 210100101]
|
||||
});
|
||||
assert_eq!(reply["reward_list"].len(), 4);
|
||||
assert_eq!(reply["reward_list"][0]["type"], 16);
|
||||
assert_eq!(userdata::get_acc_chats(&key).len(), 2);
|
||||
assert!(has_read(&userdata::get_acc(&key), 129999, 20000001));
|
||||
let repeat = read(request(), Session {key: key.clone(), body: object! {master_story_part_id: 20000001}}).await.0.unwrap();
|
||||
assert!(repeat["reward_list"].is_empty());
|
||||
|
||||
// jp/logs.txt:18507-18538: daily-life gems go to gifts, not straight to the wallet.
|
||||
let mut user = userdata::get_acc(&key);
|
||||
user["live_list"] = array![object! {master_live_id: 1100101, clear_count: 1}];
|
||||
userdata::save_acc(&key, user);
|
||||
let gems = userdata::get_acc(&key)["gem"].clone();
|
||||
let before = userdata::get_acc_home(&key)["home"]["gift_list"].len();
|
||||
let first = read(request(), Session {key: key.clone(), body: object! {master_story_part_id: 210001001}}).await.0.unwrap();
|
||||
assert_eq!(first["gift_list"].len(), 1);
|
||||
assert_eq!(first["gift_list"][0]["amount"], 100);
|
||||
assert_eq!(userdata::get_acc_home(&key)["home"]["gift_list"].len(), before + 1);
|
||||
assert_eq!(userdata::get_acc(&key)["gem"], gems);
|
||||
let repeat = read(request(), Session {key: key.clone(), body: object! {master_story_part_id: 210001001}}).await.0.unwrap();
|
||||
assert!(repeat["gift_list"].is_empty());
|
||||
assert_eq!(userdata::get_acc_home(&key)["home"]["gift_list"].len(), before + 1);
|
||||
assert!(read(request(), Session {key, body: object! {master_story_part_id: -1}}).await.0.is_none());
|
||||
}
|
||||
}
|
||||
|
||||
+48
-2
@@ -48,6 +48,9 @@ async fn deck(Session { key, body }: Session) -> impl Responder {
|
||||
}
|
||||
}
|
||||
userdata::save_acc(&key, user.clone());
|
||||
let mut missions = userdata::get_acc_missions(&key);
|
||||
let cleared = super::beginner_mission::refresh(&user, &mut missions);
|
||||
userdata::save_acc_missions(&key, missions);
|
||||
|
||||
Api(Some(object!{
|
||||
"deck": {
|
||||
@@ -55,7 +58,7 @@ async fn deck(Session { key, body }: Session) -> impl Responder {
|
||||
"leader_role": 0,
|
||||
"main_card_ids": body["main_card_ids"].clone()
|
||||
},
|
||||
"clear_mission_ids": []
|
||||
"clear_mission_ids": cleared
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -170,7 +173,11 @@ async fn user_post(Session { key, body }: Session) -> impl Responder {
|
||||
user["user"]["master_title_ids"][0] = body["master_title_ids"][0].clone();
|
||||
}
|
||||
if !body["birthday"].is_null() {
|
||||
user["user"]["birthday"][0] = body["birthday"].clone();
|
||||
let Some(birthday) = body["birthday"].as_str() else { return Api(None); };
|
||||
if !valid_birthday(birthday) { return Api(None); }
|
||||
let previous = user["user"]["birthday"].as_str().unwrap_or("");
|
||||
if valid_birthday(previous) && previous != birthday { return Api(None); }
|
||||
user["user"]["birthday"] = birthday.into();
|
||||
}
|
||||
|
||||
userdata::save_acc(&key, user.clone());
|
||||
@@ -181,6 +188,40 @@ async fn user_post(Session { key, body }: Session) -> impl Responder {
|
||||
}))
|
||||
}
|
||||
|
||||
pub(super) fn valid_birthday(value: &str) -> bool {
|
||||
if value.len() != 4 || !value.bytes().all(|b| b.is_ascii_digit()) { return false; }
|
||||
let month: usize = value[..2].parse().unwrap();
|
||||
let day: u8 = value[2..].parse().unwrap();
|
||||
let days = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
||||
(1..=12).contains(&month) && day > 0 && day <= days[month - 1]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod birthday_tests {
|
||||
use super::*;
|
||||
|
||||
#[actix_web::test]
|
||||
async fn birthday_update_round_trips_as_mmdd() {
|
||||
let _test = crate::runtime::lock_test_data_path();
|
||||
let (_, key) = userdata::starter::create("Birthday test").unwrap();
|
||||
// jp/logs.txt:157322-157353 stores this as a string, not an array.
|
||||
user_post(Session {key: key.clone(), body: object! {birthday: "0331"}}).await;
|
||||
assert_eq!(userdata::get_acc(&key)["user"]["birthday"], "0331");
|
||||
user_post(Session {key: key.clone(), body: object! {birthday: "0230"}}).await;
|
||||
assert_eq!(userdata::get_acc(&key)["user"]["birthday"], "0331");
|
||||
user_post(Session {key: key.clone(), body: object! {birthday: "0401"}}).await;
|
||||
assert_eq!(userdata::get_acc(&key)["user"]["birthday"], "0331");
|
||||
let mut legacy = userdata::get_acc(&key);
|
||||
legacy["user"]["birthday"] = array!["0331"];
|
||||
userdata::save_acc(&key, legacy);
|
||||
assert_eq!(userdata::get_acc(&key)["user"]["birthday"], "0331");
|
||||
assert!(valid_birthday("0229"));
|
||||
for invalid in ["", "0001", "1301", "0100", "0431", "abcd", "2024"] {
|
||||
assert!(!valid_birthday(invalid));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn announcement(Login(key): Login) -> impl Responder {
|
||||
|
||||
let mut user = userdata::get_acc_home(&key);
|
||||
@@ -212,6 +253,11 @@ async fn register_password(Session { key, body }: Session) -> impl Responder {
|
||||
let user = userdata::get_acc(&key);
|
||||
|
||||
userdata::user::migration::save_acc_transfer(user["user"]["id"].as_i64().unwrap(), &body["pass"].to_string());
|
||||
if !body["pass"].as_str().unwrap_or("").is_empty() {
|
||||
let mut missions = userdata::get_acc_missions(&key);
|
||||
super::beginner_mission::advance(25, None, 1, &mut missions);
|
||||
userdata::save_acc_missions(&key, missions);
|
||||
}
|
||||
|
||||
Api(Some(array![]))
|
||||
}
|
||||
|
||||
@@ -337,6 +337,12 @@ pub fn get_acc(auth_key: &str) -> JsonValue {
|
||||
let mut user = get_data(auth_key, "userdata");
|
||||
cleanup_account(&mut user);
|
||||
let mut changed = remove_deleted_custom_songs(&mut user);
|
||||
// Repair the old /user writer's ["MMDD"] shape without losing the chosen birthday.
|
||||
if user["user"]["birthday"].is_array() {
|
||||
let birthday = user["user"]["birthday"][0].as_str().unwrap_or("").to_owned();
|
||||
user["user"]["birthday"] = if super::user::valid_birthday(&birthday) { birthday } else { String::new() }.into();
|
||||
changed = true;
|
||||
}
|
||||
if remove_deleted_custom_cards(&mut user) {
|
||||
changed = true;
|
||||
}
|
||||
@@ -357,7 +363,9 @@ pub fn get_acc_home(auth_key: &str) -> JsonValue {
|
||||
user
|
||||
}
|
||||
pub fn get_acc_missions(auth_key: &str) -> JsonValue {
|
||||
get_data(auth_key, "missions")
|
||||
let mut missions = get_data(auth_key, "missions");
|
||||
super::beginner_mission::ensure(&mut missions);
|
||||
missions
|
||||
}
|
||||
pub fn get_acc_loginbonus(auth_key: &str) -> JsonValue {
|
||||
get_data(auth_key, "loginbonus")
|
||||
|
||||
@@ -483,6 +483,7 @@ pub fn my_scopes(req: HttpRequest) -> HttpResponse {
|
||||
scopes: permissions::get_user_permissions(uid),
|
||||
can_upload_cards: permissions::has(uid, permissions::CARD_UPLOAD),
|
||||
can_publish_cards: permissions::has(uid, permissions::CARD_PUBLISH),
|
||||
can_manage_groups: permissions::has(uid, permissions::GROUP_MANAGE),
|
||||
can_edit_any_cards: permissions::has(uid, permissions::CARD_EDIT),
|
||||
can_edit_any_3dmv: permissions::has(uid, permissions::MV_EDIT),
|
||||
can_manage_permissions: permissions::has(uid, permissions::PERMISSION_GRANT)
|
||||
|
||||
+117
@@ -23,11 +23,31 @@ pub struct HostConfig {
|
||||
pub port: u16,
|
||||
pub jp_android_asset_hash: String,
|
||||
pub en_android_asset_hash: String,
|
||||
pub asset_version: String,
|
||||
pub en_asset_version: String,
|
||||
pub jp_ios_asset_hash: String,
|
||||
pub en_ios_asset_hash: String,
|
||||
pub windows_asset_hash: String,
|
||||
pub enable_custom_songs: bool,
|
||||
pub enable_custom_cards: bool,
|
||||
pub enable_custom_3dmv: bool,
|
||||
pub enable_arcade: bool,
|
||||
pub nerf_custom_cards: bool,
|
||||
pub owner: Vec<i64>,
|
||||
pub hidden: bool,
|
||||
pub force_updates: bool,
|
||||
pub disable_imports: bool,
|
||||
pub disable_exports: bool,
|
||||
pub linux_asset_hash: String,
|
||||
pub macos_asset_hash: String,
|
||||
pub global_android: String,
|
||||
pub japan_android: String,
|
||||
pub global_ios: String,
|
||||
pub japan_ios: String,
|
||||
pub arcade_machine_ttl: u64,
|
||||
pub arcade_session_ttl: u64,
|
||||
pub image_asset_path: String,
|
||||
pub masterdata: String,
|
||||
}
|
||||
|
||||
// Lets an embedding app (or the tests) enable the opt-in custom songs feature
|
||||
@@ -162,6 +182,38 @@ pub fn apply_config_json(json: &str) {
|
||||
cfg.port = parsed["port"].as_u64().unwrap_or(0) as u16;
|
||||
cfg.jp_android_asset_hash = s("jpAndroidAssetHash");
|
||||
cfg.en_android_asset_hash = s("enAndroidAssetHash");
|
||||
cfg.asset_version = s("assetVersion");
|
||||
cfg.en_asset_version = s("enAssetVersion");
|
||||
cfg.jp_ios_asset_hash = s("jpIosAssetHash");
|
||||
cfg.en_ios_asset_hash = s("enIosAssetHash");
|
||||
cfg.windows_asset_hash = s("windowsAssetHash");
|
||||
cfg.linux_asset_hash = s("linuxAssetHash");
|
||||
cfg.macos_asset_hash = s("macosAssetHash");
|
||||
cfg.global_android = s("globalAndroid");
|
||||
cfg.japan_android = s("japanAndroid");
|
||||
cfg.global_ios = s("globalIos");
|
||||
cfg.japan_ios = s("japanIos");
|
||||
cfg.arcade_machine_ttl = parsed["arcadeMachineTtl"].as_u64().unwrap_or(90);
|
||||
cfg.arcade_session_ttl = parsed["arcadeSessionTtl"].as_u64().unwrap_or(30);
|
||||
cfg.image_asset_path = s("imageAssetPath");
|
||||
cfg.masterdata = s("masterdata");
|
||||
cfg.owner = parsed["ownerUids"].members().filter_map(|v| v.as_i64()).collect();
|
||||
cfg.hidden = parsed["hidden"].as_bool().unwrap_or(false);
|
||||
cfg.force_updates = parsed["forceUpdates"].as_bool().unwrap_or(false);
|
||||
cfg.disable_imports = parsed["disableImports"].as_bool().unwrap_or(false);
|
||||
cfg.disable_exports = parsed["disableExports"].as_bool().unwrap_or(false);
|
||||
cfg.enable_custom_songs = parsed["enableCustomSongs"].as_bool().unwrap_or(false);
|
||||
cfg.enable_custom_cards = parsed["enableCustomCards"].as_bool().unwrap_or(false);
|
||||
cfg.nerf_custom_cards = parsed["nerfCustomCards"].as_bool().unwrap_or(false);
|
||||
cfg.enable_custom_3dmv = parsed["enableCustom3dmv"].as_bool().unwrap_or(false);
|
||||
cfg.enable_arcade = parsed["enableArcade"].as_bool().unwrap_or(false);
|
||||
let owners = cfg.owner.clone();
|
||||
let masterdata = cfg.masterdata.clone();
|
||||
let nerf_custom_cards = cfg.nerf_custom_cards;
|
||||
drop(cfg);
|
||||
update_owners(&owners);
|
||||
update_masterdata_path(&masterdata);
|
||||
set_nerf_custom_cards(nerf_custom_cards);
|
||||
}
|
||||
|
||||
pub fn overlay_args(args: &mut crate::options::Args) {
|
||||
@@ -183,6 +235,34 @@ pub fn overlay_args(args: &mut crate::options::Args) {
|
||||
}
|
||||
overlay_str!(jp_android_asset_hash);
|
||||
overlay_str!(en_android_asset_hash);
|
||||
overlay_str!(asset_version);
|
||||
overlay_str!(en_asset_version);
|
||||
overlay_str!(jp_ios_asset_hash);
|
||||
overlay_str!(en_ios_asset_hash);
|
||||
overlay_str!(windows_asset_hash);
|
||||
overlay_str!(linux_asset_hash);
|
||||
overlay_str!(macos_asset_hash);
|
||||
overlay_str!(global_android);
|
||||
overlay_str!(japan_android);
|
||||
overlay_str!(global_ios);
|
||||
overlay_str!(japan_ios);
|
||||
overlay_str!(image_asset_path);
|
||||
overlay_str!(masterdata);
|
||||
if cfg.arcade_machine_ttl != 0 {
|
||||
args.arcade_machine_ttl = cfg.arcade_machine_ttl;
|
||||
}
|
||||
if cfg.arcade_session_ttl != 0 {
|
||||
args.arcade_session_ttl = cfg.arcade_session_ttl;
|
||||
}
|
||||
// An embedding app may supply these options, but its default values must
|
||||
// not erase flags passed by the standalone server (including Docker).
|
||||
if !cfg.owner.is_empty() {
|
||||
args.owner = cfg.owner.clone();
|
||||
}
|
||||
args.hidden |= cfg.hidden;
|
||||
args.force_updates |= cfg.force_updates;
|
||||
args.disable_imports |= cfg.disable_imports;
|
||||
args.disable_exports |= cfg.disable_exports;
|
||||
// Overlay only ever enables the features; a command-line --enable-custom-*
|
||||
// flag is never overridden back to off
|
||||
if cfg.enable_custom_songs {
|
||||
@@ -226,3 +306,40 @@ pub fn lock_test_data_path() -> std::sync::MutexGuard<'static, ()> {
|
||||
set_enable_arcade(true);
|
||||
guard
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[test]
|
||||
fn android_host_config_updates_owner_and_feature_options() {
|
||||
use clap::Parser;
|
||||
let _guard = lock_test_data_path();
|
||||
apply_config_json(r#"{"ownerUids":[42,84],"hidden":true,"forceUpdates":true,"disableImports":true,"disableExports":true,"enableCustomSongs":true,"enableCustomCards":true,"nerfCustomCards":true,"enableCustom3dmv":true,"enableArcade":true,"linuxAssetHash":"linux-hash","macosAssetHash":"mac-hash","globalAndroid":"https://example.com/gl.apk","arcadeMachineTtl":45,"arcadeSessionTtl":15}"#);
|
||||
let mut args = crate::options::Args::parse_from(["ew"]);
|
||||
overlay_args(&mut args);
|
||||
assert_eq!(get_owners(), vec![42, 84]);
|
||||
assert_eq!(args.owner, vec![42, 84]);
|
||||
assert!(args.hidden && args.force_updates && args.disable_imports && args.disable_exports);
|
||||
assert!(args.enable_custom_songs && args.enable_custom_cards && args.nerf_custom_cards);
|
||||
assert!(get_nerf_custom_cards());
|
||||
assert!(args.enable_custom_3dmv && args.enable_arcade);
|
||||
assert_eq!(args.linux_asset_hash, "linux-hash");
|
||||
assert_eq!(args.macos_asset_hash, "mac-hash");
|
||||
assert_eq!(args.global_android, "https://example.com/gl.apk");
|
||||
assert_eq!(args.arcade_machine_ttl, 45);
|
||||
assert_eq!(args.arcade_session_ttl, 15);
|
||||
|
||||
apply_config_json(r#"{"ownerUids":[],"enableCustomSongs":false}"#);
|
||||
let mut reset = crate::options::Args::parse_from(["ew"]);
|
||||
overlay_args(&mut reset);
|
||||
assert!(get_owners().is_empty());
|
||||
assert!(!reset.enable_custom_songs);
|
||||
assert!(!reset.hidden);
|
||||
assert!(!get_nerf_custom_cards());
|
||||
|
||||
let mut cli = crate::options::Args::parse_from([
|
||||
"ew", "--force-updates", "--hidden", "--disable-imports",
|
||||
"--disable-exports", "--owner", "123",
|
||||
]);
|
||||
overlay_args(&mut cli);
|
||||
assert!(cli.force_updates && cli.hidden && cli.disable_imports && cli.disable_exports);
|
||||
assert_eq!(cli.owner, vec![123]);
|
||||
}
|
||||
|
||||
+1
-1
Submodule webui updated: 831d0c338a...490ccd85fc
Reference in New Issue
Block a user