From 9dea6ae9ae6cffcc25b68d95b5acc4f6bcab2418 Mon Sep 17 00:00:00 2001 From: iamsub <294696795+iamiamsub@users.noreply.github.com> Date: Tue, 29 Sep 2026 12:58:18 +0900 Subject: [PATCH 1/7] otoca: first plugin for otoca d'or (NCG) Handles the game.* XRPC module of ealocal.dll: getVersion, addUser, getCardInfo, setCardInfo (issues the QR card ids), report, items, makeup and key cards. Field names, node types and limits follow the psmap tables in ealocal.dll; every reply was checked against them. --- otoca@asphyxia/README.md | 39 +++ otoca@asphyxia/index.ts | 514 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 553 insertions(+) create mode 100644 otoca@asphyxia/README.md create mode 100644 otoca@asphyxia/index.ts diff --git a/otoca@asphyxia/README.md b/otoca@asphyxia/README.md new file mode 100644 index 0000000..658a4a4 --- /dev/null +++ b/otoca@asphyxia/README.md @@ -0,0 +1,39 @@ +# otoca d'or + +Plugin Version: **v0.1.0** + +--- + +Supported Versions + - otoca d'or (NCG:J:A:A:2019012900) + +--- + +How it works + - The game's network code lives in `modules/ealocal.dll`. It sends XRPC module `game` on service `local` + (the `local` URL comes from CORE's `services.get`). Every field name, node type and array limit here + follows the psmap tables in that DLL. + - Players are identified by the printed cards, not by an e-amusement pass: + - `game.addUser` creates the player and the first doll. + - `game.setCardInfo` prints a card. The server issues its `otocard_id` (8 characters of + `0123456789ABCDEFGHJKLMNPRSTUWXYZ`), which the game packs into the card's QR code. + - Scanning a card sends `game.getCardInfo` with that id and gets the player, the doll and the closet back. + - The key card (`game.bindKeyUser`) is a second printed card tied to the player; its id is issued the same way. + - `game.getVersion` answers episode/phase 99, which the game clamps to the newest it has. + +Settings + - Version marker: unlock step sent in `game.getVersion` (the game checks 1 to 8). + - Limited-time enemies: turns on all 8 limited-time enemy slots. + +Not done yet + - Checked only with recorded-style requests, not on a cabinet yet. + - Gift codes (`receiveGift` / `receiveItem`) always answer "nothing". + - Random bonuses (kira offsets, accessory offsets) are all 0. + - `battle_result` 1 is taken as a win; stamps, rival cards and item types other than equip/material + are only logged. + - No WebUI. + +--- + +Changelog + - 0.1.0: first version. diff --git a/otoca@asphyxia/index.ts b/otoca@asphyxia/index.ts new file mode 100644 index 0000000..230ddeb --- /dev/null +++ b/otoca@asphyxia/index.ts @@ -0,0 +1,514 @@ +// otoca d'or (NCG). The XRPC module "game" (service "local") is implemented by the game's ealocal.dll; +// field names, types and limits follow its psmap tables (see README). + +interface Accessory { + aseq: number; + id: number; + gr: number; + offset: number[]; // param_offset, skill_offset_1..3 +} + +interface User { + collection: 'user'; + user_seq: number; + user_name: string; + keycard_id: string; + key_inquire_id: string; + powder_num: number; + stamp_num: number; + balance: number; + ticket_half_price: boolean; + ticket_rc_first: boolean; + equip: { [id: string]: number[] }; // count per grade 0..2 + material: { [id: string]: number[] }; + accessory: Accessory[]; + next_aseq: number; + cardbg: number[]; + enemy: { [team: string]: number }; // enemy_team_id -> highest level beaten + last_enemy: { [episode: string]: number[] }; // [enemy_team_id, enemy_team_level] + friendly: { [team: string]: number }; // enemy_team_id -> friend point + release_state: number; + stamp_conv_num: number; + user_flags: number[]; + last_time: number; + greeting_level: number; + user_marker: number; + mission_no: number; + question_id: number; + sort_type: number; + disp_skill: number; +} + +interface Doll { + collection: 'doll'; + doll_seq: number; + user_seq: number; + doll_id: number; + doll_name: string; + doll_seed: number; + doll_level: number; + is_frank: boolean; + equip: number[][]; // [equip_id, equip_grade] + accessory: number[]; // accessory_seq + kira: number[]; // hp, atk, mat, spd + makeup: number[]; // hair_color, eye_color +} + +interface Card { + collection: 'card'; + otocard_id: string; + inquire_id: string; + user_seq: number; + doll_seq: number; + kira_type: number; + created: number; +} + +interface Counter { + collection: 'counter'; + name: string; + value: number; +} + +// Game side clamps episode/phase to its own newest (FUN_101227c0), so a large value means "latest". +const EPISODE = 99; +const PHASE = 99; +// Alphabet of the ids the game packs into its QR codes (5 bits per char). +const ID_CHARS = '0123456789ABCDEFGHJKLMNPRSTUWXYZ'; + +const now = () => Math.floor(Date.now() / 1000); +const randU32 = () => Math.floor(Math.random() * 0xffffffff); +const u8 = (v: number) => K.ITEM('u8', v || 0); +const u32 = (v: number) => K.ITEM('u32', v || 0); + +async function nextSeq(name: string) { + const c = await DB.FindOne({ collection: 'counter', name }); + const value = (c ? c.value : 0) + 1; + await DB.Upsert({ collection: 'counter', name }, { $set: { value } }); + return value; +} + +// ponytail: random ids with a uniqueness check, fine for a home server's card count. +async function newId() { + for (;;) { + let id = ''; + for (let i = 0; i < 8; i++) id += ID_CHARS[Math.floor(Math.random() * ID_CHARS.length)]; + if (id === '00000000') continue; + const used = (await DB.Count({ collection: 'card', otocard_id: id })) + + (await DB.Count({ collection: 'user', keycard_id: id })); + if (!used) return id; + } +} + +function newUser(user_seq: number, user_name: string): User { + return { + collection: 'user', user_seq, user_name, keycard_id: '', key_inquire_id: '', + powder_num: 0, stamp_num: 0, balance: 0, ticket_half_price: false, ticket_rc_first: false, + equip: {}, material: {}, accessory: [], next_aseq: 1, cardbg: [], + enemy: {}, last_enemy: {}, friendly: {}, release_state: 0, stamp_conv_num: 0, user_flags: [0, 0, 0, 0], + last_time: 0, greeting_level: 0, user_marker: 0, mission_no: 0, question_id: 0, sort_type: 0, disp_skill: 0, + }; +} + +async function newDoll(user_seq: number, doll_id: number, doll_name: string, is_frank: boolean): Promise { + const doll: Doll = { + collection: 'doll', doll_seq: await nextSeq('doll'), user_seq, doll_id, doll_name, doll_seed: randU32(), + doll_level: 1, is_frank, equip: [], accessory: [], kira: [0, 0, 0, 0], makeup: [0, 0], + }; + await DB.Insert(doll); + return doll; +} + +const getUser = (user_seq: number) => DB.FindOne({ collection: 'user', user_seq }); +const getDoll = (doll_seq: number) => DB.FindOne({ collection: 'doll', doll_seq }); + +async function save(key: object, doc: T) { + const { _id, ...rest } = doc as any; + await DB.Update(key, { $set: rest }); +} +const saveUser = (u: User) => save({ collection: 'user', user_seq: u.user_seq }, u); +const saveDoll = (d: Doll) => save({ collection: 'doll', doll_seq: d.doll_seq }, d); + +function log(method: string, data: any) { + console.log(`[otoca] ${method} ${JSON.stringify(data)}`); +} + +// ---- closet ---------------------------------------------------------------- + +// item_type 0 = equip (accessories are equips with their own accessory_seq), 1 = material. +// ponytail: other item types are only logged until their meaning is seen in real traffic. +function giveItem(u: User, type: number, id: number, grade: number, n: number, isAcc: boolean, made: Accessory[]) { + if (type === 0 && isAcc) { + // ponytail: offsets 0 (no random bonus) until the real ranges are known. + const a = { aseq: u.next_aseq++, id, gr: grade, offset: [0, 0, 0, 0] }; + u.accessory.push(a); + made.push(a); + return; + } + const bag = type === 0 ? u.equip : type === 1 ? u.material : null; + if (!bag || grade > 2) return log(`unknown item type=${type} id=${id} grade=${grade}`, n); + const gr = bag[id] || [0, 0, 0]; + gr[grade] = Math.max(0, gr[grade] + n); + if (gr.some(c => c > 0)) bag[id] = gr; + else delete bag[id]; +} + +function takeItem(u: User, aseq: number, type: number, id: number, grade: number, n: number) { + if (aseq) u.accessory = u.accessory.filter(a => a.aseq !== aseq); + else giveItem(u, type, id, grade, -n, false, []); +} + +const accessoryNode = (a: Accessory) => ({ + accessory_seq: u32(a.aseq), equip_id: u32(a.id), equip_grade: u8(a.gr), + param_offset: u8(a.offset[0]), skill_offset_1: u8(a.offset[1]), + skill_offset_2: u8(a.offset[2]), skill_offset_3: u8(a.offset[3]), +}); + +// compact form (getCardInfo ver="1"): one per id, gr[g] = count of grade g, -1 = none +const gradeList = (bag: { [id: string]: number[] }) => + Object.keys(bag).map(id => ({ id: u32(+id), gr: K.ARRAY('s16', bag[id].map(c => (c > 0 ? c : -1))) })); + +// ---- handlers -------------------------------------------------------------- + +const getVersion: EPR = async (info, data, send) => { + const limited = U.GetConfig('limited_enemy') ? 1 : 0; + send.object({ + expire: u32(600), + episode: u8(EPISODE), + phase: u8(PHASE), + marker: u8(U.GetConfig('marker')), + campaign: { + trial_play: K.ITEM('bool', false), + trial_shop: K.ITEM('bool', false), + starkira_free: K.ITEM('bool', false), + stamp_double: K.ITEM('bool', false), + limited_enemy: K.ARRAY('bool', [limited, limited, limited, limited, limited, limited, limited, limited]), + }, + question: { id: u8(0), reward: { type: u8(0), id: u32(0), grade: u8(0) } }, + }); +}; + +const addUser: EPR = async (info, data, send) => { + const user = newUser(await nextSeq('user'), $(data).str('user_name', '')); + user.last_time = now(); + await DB.Insert(user); + const doll = await newDoll(user.user_seq, $(data).number('doll_id', 0), $(data).str('doll_name', ''), $(data).bool('is_frank')); + send.object({ + user_seq: u32(user.user_seq), + doll_seq: u32(doll.doll_seq), + doll_seed: u32(doll.doll_seed), + powder_num: u32(user.powder_num), + stamp_num: u32(user.stamp_num), + mission_no: u8(user.mission_no), + question_id: u8(user.question_id), + ticket_rc_first: K.ITEM('bool', user.ticket_rc_first), + }); +}; + +const addDoll: EPR = async (info, data, send) => { + const doll = await newDoll($(data).number('user_seq'), $(data).number('doll_id', 0), $(data).str('doll_name', ''), false); + send.object({ doll_seq: u32(doll.doll_seq), doll_seed: u32(doll.doll_seed) }); +}; + +const getCardInfo: EPR = async (info, data, send) => { + const otocard_id = $(data).str('otocard_id', ''); + const card = await DB.FindOne({ collection: 'card', otocard_id }); + const user = card && (await getUser(card.user_seq)); + const doll = card && (await getDoll(card.doll_seq)); + if (!user || !doll) { + log(`getCardInfo: unknown card ${otocard_id}`, {}); + return send.deny(); + } + const accessories = doll.accessory.map(s => user.accessory.find(a => a.aseq === s)).filter(a => a); + send.object({ + user_seq: u32(user.user_seq), + user_name: K.ITEM('str', user.user_name), + doll_seq: u32(doll.doll_seq), + doll_id: u32(doll.doll_id), + doll_name: K.ITEM('str', doll.doll_name), + doll_level: u32(doll.doll_level), + doll_seed: u32(doll.doll_seed), + locked: K.ITEM('bool', false), + luck_offset: u32(0), + equip: doll.equip.map(e => ({ equip_id: u32(e[0]), equip_grade: u8(e[1]) })), + accessory: accessories.map(accessoryNode), + score: { + release_state: u32(user.release_state), + stamp_conv_num: u32(user.stamp_conv_num), + user_flags: K.ARRAY('u8', user.user_flags), + last_time: K.ITEM('time', user.last_time), + curr_time: K.ITEM('time', now()), + greeting_level: u8(user.greeting_level), + user_marker: u8(user.user_marker), + mission_no: u8(user.mission_no), + setting: { sort_type: u8(user.sort_type), disp_skill: u8(user.disp_skill) }, + question_id: u8(user.question_id), + enemy: Object.keys(user.enemy).map(t => ({ enemy_team_id: u32(+t), enemy_team_level: u32(user.enemy[t]) })), + last_enemy: Object.keys(user.last_enemy).map(ep => ({ + episode: u8(+ep), enemy_team_id: u32(user.last_enemy[ep][0]), enemy_team_level: u32(user.last_enemy[ep][1]), + })), + enemy_detail: { i: Object.keys(user.friendly).map(t => ({ id: K.ITEM('u16', +t), pt: u32(user.friendly[t]) })) }, + }, + offset: { kira_hp: u8(doll.kira[0]), kira_atk: u8(doll.kira[1]), kira_mat: u8(doll.kira[2]), kira_spd: u8(doll.kira[3]) }, + active_key: K.ITEM('bool', !!user.keycard_id), + version: { episode: u8(EPISODE), phase: u8(PHASE) }, + makeup: { hair_color: u8(doll.makeup[0]), eye_color: u8(doll.makeup[1]) }, + closet: { + powder_num: u32(user.powder_num), + stamp_num: u32(user.stamp_num), + wallet: { + balance: u32(user.balance), + ticket_half_price: K.ITEM('bool', user.ticket_half_price), + ticket_rc_first: K.ITEM('bool', user.ticket_rc_first), + }, + equip: { i: gradeList(user.equip) }, + material: { i: gradeList(user.material) }, + accessory: { + i: user.accessory.map(a => ({ aseq: u32(a.aseq), id: u32(a.id), gr: u8(a.gr), offset: K.ARRAY('u8', a.offset) })), + }, + cardbg: user.cardbg.map(id => ({ id: u8(id) })), + }, + }); +}; + +const setCardInfo: EPR = async (info, data, send) => { + const d = await getDoll($(data).number('doll_seq')); + if (!d) return send.deny(); + d.doll_level = $(data).number('doll_level', d.doll_level); + d.doll_id = $(data).number('doll_id', d.doll_id); + d.doll_name = $(data).str('doll_name', d.doll_name); + d.equip = $(data).elements('equip').map(e => [e.number('equip_id', 0), e.number('equip_grade', 0)]).filter(e => e[0]); + d.accessory = ($(data).numbers('accessory_seq') || []).filter(s => s); + await saveDoll(d); + const card: Card = { + collection: 'card', otocard_id: await newId(), inquire_id: await newId(), user_seq: d.user_seq, + doll_seq: d.doll_seq, kira_type: $(data).number('kira_type', 0), created: now(), + }; + await DB.Insert(card); + log('setCardInfo', { from: $(data).str('otocard_id'), to: card.otocard_id, kira_type: card.kira_type }); + send.object({ + otocard_id: K.ITEM('str', card.otocard_id), + inquire_id: K.ITEM('str', card.inquire_id), + offset: { kira_hp: u8(d.kira[0]), kira_atk: u8(d.kira[1]), kira_mat: u8(d.kira[2]), kira_spd: u8(d.kira[3]) }, + }); +}; + +const report: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + const b = $(data).element('battle'); + log('report', { battle: b && b.obj, user_marker: $(data).number('user_marker') }); + if (b) { + const team = b.number('enemy_team_id', 0); + const level = b.number('enemy_team_level', 0); + // ponytail: battle_result 1 taken as a win until real traffic says otherwise. + if (team && b.number('battle_result') === 1) u.enemy[team] = Math.max(u.enemy[team] || 0, level); + if (team) u.last_enemy[b.number('episode', 0)] = [team, level]; + } + u.user_flags = $(data).numbers('user_flags', u.user_flags); + u.user_marker = $(data).number('user_marker', u.user_marker); + u.sort_type = $(data).number('setting.sort_type', u.sort_type); + u.disp_skill = $(data).number('setting.disp_skill', u.disp_skill); + u.last_time = now(); + await saveUser(u); + const d = await getDoll($(data).number('doll_seq')); + if (d) { + d.doll_level = $(data).number('doll_level', d.doll_level); + await saveDoll(d); + } + send.success(); +}; + +const addExp: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + u.balance += $(data).number('gold.add_amount', 0); + const team = $(data).number('friendly.enemy_team_id', 0); + if (team) u.friendly[team] = (u.friendly[team] || 0) + $(data).number('friendly.add_point', 0); + await saveUser(u); + send.object({ balance: u32(u.balance), friend_point: u32(team ? u.friendly[team] : 0) }); +}; + +const addGold: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + u.balance += $(data).number('add_amount', 0); + await saveUser(u); + send.object({ balance: u32(u.balance) }); +}; + +const addItem: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + const made: Accessory[] = []; + for (const i of $(data).elements('item')) { + giveItem(u, i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), 1, i.bool('is_accessory'), made); + } + await saveUser(u); + send.object({ accessory: made.map(accessoryNode) }); +}; + +const delItem: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + for (const i of $(data).elements('item')) { + takeItem(u, i.number('accessory_seq', 0), i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), 1); + } + log('delItem drop (not added)', $(data).elements('drop').map(e => e.obj)); + await saveUser(u); + send.success(); +}; + +const sellItem: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + for (const i of $(data).elements('item')) { + const n = i.number('item_num', 1); + takeItem(u, i.number('accessory_seq', 0), i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), n); + u.balance += i.number('price', 0) * n; + } + const made: Accessory[] = []; + for (const i of $(data).elements('drop')) { + giveItem(u, i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), 1, i.bool('is_accessory'), made); + } + log('sellItem', { client_balance: $(data).number('balance'), balance: u.balance }); + await saveUser(u); + send.object({ accessory: made.map(accessoryNode), balance: u32(u.balance) }); +}; + +const composition: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + for (const k of ['item_in_1', 'item_in_2']) { + const i = $(data).element(k); + if (i) takeItem(u, i.number('accessory_seq', 0), i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), 1); + } + const r = $(data).element('item_result'); + const made: Accessory[] = []; + if (r) giveItem(u, r.number('item_type', 0), r.number('item_id', 0), r.number('item_grade', 0), 1, r.bool('accessory_flag'), made); + await saveUser(u); + send.object({ accessory: accessoryNode(made[0] || { aseq: 0, id: 0, gr: 0, offset: [0, 0, 0, 0] }) }); +}; + +const addStamp: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + log('addStamp', data); + const before = u.stamp_num; + u.stamp_num += 1; + await saveUser(u); + send.object({ + powder_num: u32(u.powder_num), stamp_num: u32(before), stamp_after_num: u32(u.stamp_num), stamp_conv_rate: u32(0), + }); +}; + +const setMakeup: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + const d = await getDoll($(data).number('doll_seq')); + if (!u || !d) return send.deny(); + d.makeup = [$(data).number('makeup.hair_color', 0), $(data).number('makeup.eye_color', 0)]; + u.balance = Math.max(0, u.balance - $(data).number('payment', 0)); + log('setMakeup', { client_balance: $(data).number('balance'), balance: u.balance, ticket: $(data).number('ticket_type') }); + await saveDoll(d); + await saveUser(u); + send.object({ balance: u32(u.balance) }); +}; + +// No gift codes on this server: every code is answered with status 0. +const receiveGift: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + log('receiveGift', data); + send.object({ + status: $(data).elements('info').map(i => ({ gift_type: u8(i.number('gift_type', 0)), gift_status: u8(0) })), + powder_num: u32(u ? u.powder_num : 0), + }); +}; + +const receiveItem: EPR = async (info, data, send) => { + log('receiveItem', data); + send.object({ gift_type: u8($(data).number('gift_type', 0)), gift_status: u8(0) }); +}; + +const setUserField = (field: 'mission_no' | 'question_id'): EPR => async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (u) { + u[field] = $(data).number(field, u[field]); + await saveUser(u); + } + send.success(); +}; + +const bindKeyUser: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + u.keycard_id = await newId(); + u.key_inquire_id = await newId(); + await saveUser(u); + send.object({ + keycard_id: K.ITEM('str', u.keycard_id), + key_inquire_id: K.ITEM('str', u.key_inquire_id), + accessory: accessoryNode({ aseq: 0, id: 0, gr: 0, offset: [0, 0, 0, 0] }), + }); +}; + +const checkKeyUser: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + log('checkKeyUser', { scanned: $(data).str('keycard_id'), bound: u.keycard_id }); + send.object({ keycard_id: K.ITEM('str', u.keycard_id), key_inquire_id: K.ITEM('str', u.key_inquire_id) }); +}; + +const logOnly = (method: string): EPR => async (info, data, send) => { + log(method, data); + send.success(); +}; + +export function register() { + R.GameCode('NCG'); + R.Contributor('iamsub'); + + R.Config('marker', { + name: 'Version marker', + desc: 'Unlock step sent in game.getVersion (the game checks 1-8).', + type: 'integer', + default: 8, + range: [0, 255], + }); + R.Config('limited_enemy', { + name: 'Limited-time enemies', + desc: 'Turn on all 8 limited-time enemy slots.', + type: 'boolean', + default: false, + }); + + R.Route('game.getVersion', getVersion); + R.Route('game.addUser', addUser); + R.Route('game.addDoll', addDoll); + R.Route('game.getCardInfo', getCardInfo); + R.Route('game.setCardInfo', setCardInfo); + R.Route('game.report', report); + R.Route('game.addExp', addExp); + R.Route('game.addGold', addGold); + R.Route('game.addItem', addItem); + R.Route('game.delItem', delItem); + R.Route('game.sellItem', sellItem); + R.Route('game.composition', composition); + R.Route('game.addStamp', addStamp); + R.Route('game.setMakeup', setMakeup); + R.Route('game.receiveGift', receiveGift); + R.Route('game.receiveItem', receiveItem); + R.Route('game.reportMission', setUserField('mission_no')); + R.Route('game.reportQuestion', setUserField('question_id')); + R.Route('game.bindKeyUser', bindKeyUser); + R.Route('game.checkKeyUser', checkKeyUser); + R.Route('game.copyKeyCard', logOnly('copyKeyCard')); + R.Route('game.checkRivalCard', logOnly('checkRivalCard')); + R.Route('game.setRivalCard', logOnly('setRivalCard')); + R.Route('game.reportTrial', true); + R.Route('game.cancel', true); + + R.Unhandled(async (info, data, send) => { + console.error(`[otoca] unhandled ${info.module}.${info.method}: ${JSON.stringify(data)}`); + send.success(); + }); +} From 2b3ff254c0b2065200a025416da895132cfa24ad Mon Sep 17 00:00:00 2001 From: iamsub <294696795+iamiamsub@users.noreply.github.com> Date: Tue, 29 Sep 2026 13:31:10 +0900 Subject: [PATCH 2/7] otoca: battle_result 0 is a win, keep every beaten team/level The game appends the fought team/level to score/enemy only when battle_result is 0 (seen on a cabinet: a clear sent 0), and the list is team/level pairs rather than one level per team. --- otoca@asphyxia/README.md | 2 +- otoca@asphyxia/index.ts | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/otoca@asphyxia/README.md b/otoca@asphyxia/README.md index 658a4a4..e53266f 100644 --- a/otoca@asphyxia/README.md +++ b/otoca@asphyxia/README.md @@ -29,7 +29,7 @@ Not done yet - Checked only with recorded-style requests, not on a cabinet yet. - Gift codes (`receiveGift` / `receiveItem`) always answer "nothing". - Random bonuses (kira offsets, accessory offsets) are all 0. - - `battle_result` 1 is taken as a win; stamps, rival cards and item types other than equip/material + - Stamps, rival cards and item types other than equip/material are only logged. - No WebUI. diff --git a/otoca@asphyxia/index.ts b/otoca@asphyxia/index.ts index 230ddeb..fe48000 100644 --- a/otoca@asphyxia/index.ts +++ b/otoca@asphyxia/index.ts @@ -24,7 +24,7 @@ interface User { accessory: Accessory[]; next_aseq: number; cardbg: number[]; - enemy: { [team: string]: number }; // enemy_team_id -> highest level beaten + enemy: number[][]; // [enemy_team_id, enemy_team_level] of every team/level beaten last_enemy: { [episode: string]: number[] }; // [enemy_team_id, enemy_team_level] friendly: { [team: string]: number }; // enemy_team_id -> friend point release_state: number; @@ -105,7 +105,7 @@ function newUser(user_seq: number, user_name: string): User { collection: 'user', user_seq, user_name, keycard_id: '', key_inquire_id: '', powder_num: 0, stamp_num: 0, balance: 0, ticket_half_price: false, ticket_rc_first: false, equip: {}, material: {}, accessory: [], next_aseq: 1, cardbg: [], - enemy: {}, last_enemy: {}, friendly: {}, release_state: 0, stamp_conv_num: 0, user_flags: [0, 0, 0, 0], + enemy: [], last_enemy: {}, friendly: {}, release_state: 0, stamp_conv_num: 0, user_flags: [0, 0, 0, 0], last_time: 0, greeting_level: 0, user_marker: 0, mission_no: 0, question_id: 0, sort_type: 0, disp_skill: 0, }; } @@ -243,7 +243,7 @@ const getCardInfo: EPR = async (info, data, send) => { mission_no: u8(user.mission_no), setting: { sort_type: u8(user.sort_type), disp_skill: u8(user.disp_skill) }, question_id: u8(user.question_id), - enemy: Object.keys(user.enemy).map(t => ({ enemy_team_id: u32(+t), enemy_team_level: u32(user.enemy[t]) })), + enemy: user.enemy.map(e => ({ enemy_team_id: u32(e[0]), enemy_team_level: u32(e[1]) })), last_enemy: Object.keys(user.last_enemy).map(ep => ({ episode: u8(+ep), enemy_team_id: u32(user.last_enemy[ep][0]), enemy_team_level: u32(user.last_enemy[ep][1]), })), @@ -301,8 +301,9 @@ const report: EPR = async (info, data, send) => { if (b) { const team = b.number('enemy_team_id', 0); const level = b.number('enemy_team_level', 0); - // ponytail: battle_result 1 taken as a win until real traffic says otherwise. - if (team && b.number('battle_result') === 1) u.enemy[team] = Math.max(u.enemy[team] || 0, level); + // battle_result 0 = win: the game itself appends the pair to score/enemy only then (game.dll FUN_100bb1d0) + const beaten = u.enemy.some(e => e[0] === team && e[1] === level); + if (team && b.number('battle_result') === 0 && !beaten && u.enemy.length < 200) u.enemy.push([team, level]); if (team) u.last_enemy[b.number('episode', 0)] = [team, level]; } u.user_flags = $(data).numbers('user_flags', u.user_flags); From 44bd5a29fefb89334b2c17b785357c231cc0d57c Mon Sep 17 00:00:00 2001 From: iamsub <294696795+iamiamsub@users.noreply.github.com> Date: Tue, 29 Sep 2026 14:54:16 +0900 Subject: [PATCH 3/7] otoca: rebuild every response from a full game.dll analysis - addUser/addDoll: doll_seed within 0-23327 (higher values disable growth), starting equipment from dollinitdata on the doll and in the closet, mission 1, first powder and rival ticket - getCardInfo: locked/active_key follow the key card, accessories are counted in the equip list, worn accessories fill equip[4]/[5], the scanned card's own kira bonus, version 5/11 - setCardInfo: kira rolled per card (0-9), equip slots kept in place, star powder and decoseal spent - checkKeyUser answers status 10 for a key that is not the player's - addItem rows line up with the items; delItem adds the kept drops; sellItem handles the sell scene's placeholder seqs and takes the game's balance; composition takes consumed accessories off the doll - addStamp: 10-square card (stamp_conv_rate 0 divided by zero), powder per full card; reportMission advances; questionnaires off - receiveGift honours only the game's own offline card --- otoca@asphyxia/README.md | 42 ++- otoca@asphyxia/index.ts | 736 +++++++++++++++++++++++---------------- 2 files changed, 462 insertions(+), 316 deletions(-) diff --git a/otoca@asphyxia/README.md b/otoca@asphyxia/README.md index e53266f..c1d9642 100644 --- a/otoca@asphyxia/README.md +++ b/otoca@asphyxia/README.md @@ -1,6 +1,6 @@ # otoca d'or -Plugin Version: **v0.1.0** +Plugin Version: **v0.2.0** --- @@ -12,28 +12,42 @@ Supported Versions How it works - The game's network code lives in `modules/ealocal.dll`. It sends XRPC module `game` on service `local` (the `local` URL comes from CORE's `services.get`). Every field name, node type and array limit here - follows the psmap tables in that DLL. + follows the psmap tables in that DLL, and every value follows how game.dll uses it. - Players are identified by the printed cards, not by an e-amusement pass: - - `game.addUser` creates the player and the first doll. + - `game.addUser` creates the player and the first doll (level 1, the doll's starting equipment). - `game.setCardInfo` prints a card. The server issues its `otocard_id` (8 characters of - `0123456789ABCDEFGHJKLMNPRSTUWXYZ`), which the game packs into the card's QR code. - - Scanning a card sends `game.getCardInfo` with that id and gets the player, the doll and the closet back. - - The key card (`game.bindKeyUser`) is a second printed card tied to the player; its id is issued the same way. - - `game.getVersion` answers episode/phase 99, which the game clamps to the newest it has. + `0123456789ABCDEFGHJKLMNPRSTUWXYZ`), which the game packs into the card's QR code, and rolls the + kira bonus printed on it. + - Scanning a card sends `game.getCardInfo` with that id and gets the player, the doll, the closet and + that card's kira bonus back. An unknown card answers status 1, and the game then plays offline. + - The key card (`game.bindKeyUser`) is a second printed card tied to the player. Once a key is bound, + scanning a card asks for the key card (`game.checkKeyUser`) and the closet holds 999 items instead of 50. + - `game.getVersion` and getCardInfo both send episode 5 / phase 11, the newest the game knows. + - Without a camera, printed card images can be scanned with a hook that feeds an image to the game's + camera library (libcamera.dll) in place of the camera frame. Settings - - Version marker: unlock step sent in `game.getVersion` (the game checks 1 to 8). - - Limited-time enemies: turns on all 8 limited-time enemy slots. + - Version marker: unlocks hair/eye colours in the salon and the mode-select tips (the game checks 1 to 8). + - Limited-time enemy: shows the one limited-time enemy the data has (episode 3). + - Double stamps: two stamps per play. + - Free star kira: star kira cards need no star powder. + +Server policy (the game does not decide these) + - Kira bonus: star kira gives +1-3 to two stats, gold kira +1-5 to all four. + - Accessory stats: 80/90/100% of the base values for grade N/R/SR. + - One stamp per play; a full card of 10 gives one star powder. New players start with one powder + and the first rival card ticket. + - "New doll" unlocks once any doll reaches level 5. + - No questionnaires, and old campaign gift cards answer "expired". The game's own offline card + (printed when setCardInfo fails) gives its apology gift once. Not done yet - - Checked only with recorded-style requests, not on a cabinet yet. - - Gift codes (`receiveGift` / `receiveItem`) always answer "nothing". - - Random bonuses (kira offsets, accessory offsets) are all 0. - - Stamps, rival cards and item types other than equip/material - are only logged. - No WebUI. --- Changelog + - 0.2.0: every response rebuilt from a full analysis of game.dll (starting equipment, doll seed range, + key card binding, per-card kira, stamp card of 10, missions, closet rules for accessories, selling + and discarding). - 0.1.0: first version. diff --git a/otoca@asphyxia/index.ts b/otoca@asphyxia/index.ts index fe48000..5c9fba6 100644 --- a/otoca@asphyxia/index.ts +++ b/otoca@asphyxia/index.ts @@ -1,11 +1,12 @@ // otoca d'or (NCG). The XRPC module "game" (service "local") is implemented by the game's ealocal.dll; -// field names, types and limits follow its psmap tables (see README). +// field names, types and limits follow its psmap tables, and every value below follows how game.dll +// uses it (see README). Values marked "guess" are server policy the game code does not decide. interface Accessory { aseq: number; id: number; gr: number; - offset: number[]; // param_offset, skill_offset_1..3 + offset: number[]; // param_offset, skill_offset_1..3 (percent of the equip's base values) } interface User { @@ -16,27 +17,28 @@ interface User { key_inquire_id: string; powder_num: number; stamp_num: number; + stamp_conv_num: number; balance: number; ticket_half_price: boolean; ticket_rc_first: boolean; - equip: { [id: string]: number[] }; // count per grade 0..2 + equip: { [id: string]: number[] }; // count per grade 0..2, accessories not included material: { [id: string]: number[] }; + decoseal: { [id: string]: number }; accessory: Accessory[]; next_aseq: number; cardbg: number[]; - enemy: number[][]; // [enemy_team_id, enemy_team_level] of every team/level beaten - last_enemy: { [episode: string]: number[] }; // [enemy_team_id, enemy_team_level] - friendly: { [team: string]: number }; // enemy_team_id -> friend point release_state: number; - stamp_conv_num: number; user_flags: number[]; last_time: number; - greeting_level: number; user_marker: number; mission_no: number; question_id: number; sort_type: number; disp_skill: number; + enemy: number[][]; // [team, level] of every team/level beaten + last_enemy: number[][]; // [episode, team, level], newest first + friendly: { [team: string]: number }; + gifts_used: string[]; } interface Doll { @@ -47,11 +49,10 @@ interface Doll { doll_name: string; doll_seed: number; doll_level: number; - is_frank: boolean; - equip: number[][]; // [equip_id, equip_grade] - accessory: number[]; // accessory_seq - kira: number[]; // hp, atk, mat, spd + equip: number[][]; // 4 slots [equip_id, grade]: weapon, top, bottom, shoes + accessory: number[]; // 2 slots of accessory_seq, 0 = empty makeup: number[]; // hair_color, eye_color + luck_offset: number; } interface Card { @@ -61,6 +62,9 @@ interface Card { user_seq: number; doll_seq: number; kira_type: number; + kira: number[]; // hp, atk, mat, spd bonus printed on this card + decoseal_id: number; + from_otocard_id: string; created: number; } @@ -70,16 +74,37 @@ interface Counter { value: number; } -// Game side clamps episode/phase to its own newest (FUN_101227c0), so a large value means "latest". -const EPISODE = 99; -const PHASE = 99; +// versiondata.csv: base 5-2, newest 5-11. getCardInfo's version is applied the same way, so both send this. +const EPISODE = 5; +const PHASE = 11; // Alphabet of the ids the game packs into its QR codes (5 bits per char). const ID_CHARS = '0123456789ABCDEFGHJKLMNPRSTUWXYZ'; +// equipdata.csv rows with kind 4 (accessories): they carry an accessory_seq. +const ACCESSORY_IDS: number[][] = [ + [41, 52], [144, 203], [206, 212], [293, 347], [351, 356], [360, 361], [365, 366], + [470, 531], [535, 536], [584, 609], [619, 626], [648, 664], [696, 720], [731, 740], +]; +// dollinitdata.csv: starting equips (weapon, top, bottom, shoes) per doll_id. +const DOLL_INIT: { [doll_id: number]: number[] } = { + 1: [9, 18, 29, 40], 2: [9, 20, 31, 40], 3: [9, 17, 28, 40], + 4: [9, 19, 30, 40], 5: [9, 21, 32, 40], 6: [9, 409, 438, 40], +}; +// guess: the game only multiplies by these; this matches normal equips of the same grade. +const ACCESSORY_OFFSET = [[80, 100, 100, 0], [90, 100, 100, 50], [100, 100, 100, 100]]; +const TEMP_ASEQ = 1000000000; // the game's own placeholder seqs for this play's drops (sell scene) +const MAX_BALANCE = 9999999; const now = () => Math.floor(Date.now() / 1000); -const randU32 = () => Math.floor(Math.random() * 0xffffffff); +const rand = (n: number) => Math.floor(Math.random() * n); const u8 = (v: number) => K.ITEM('u8', v || 0); const u32 = (v: number) => K.ITEM('u32', v || 0); +const bool = (v: boolean) => K.ITEM('bool', !!v); +const isAccessory = (type: number, id: number) => + type === 0 && ACCESSORY_IDS.some(([a, b]) => id >= a && id <= b); + +function log(method: string, data: any) { + console.log(`[otoca] ${method} ${JSON.stringify(data)}`); +} async function nextSeq(name: string) { const c = await DB.FindOne({ collection: 'counter', name }); @@ -92,7 +117,7 @@ async function nextSeq(name: string) { async function newId() { for (;;) { let id = ''; - for (let i = 0; i < 8; i++) id += ID_CHARS[Math.floor(Math.random() * ID_CHARS.length)]; + for (let i = 0; i < 8; i++) id += ID_CHARS[rand(ID_CHARS.length)]; if (id === '00000000') continue; const used = (await DB.Count({ collection: 'card', otocard_id: id })) + (await DB.Count({ collection: 'user', keycard_id: id })); @@ -100,64 +125,61 @@ async function newId() { } } -function newUser(user_seq: number, user_name: string): User { - return { - collection: 'user', user_seq, user_name, keycard_id: '', key_inquire_id: '', - powder_num: 0, stamp_num: 0, balance: 0, ticket_half_price: false, ticket_rc_first: false, - equip: {}, material: {}, accessory: [], next_aseq: 1, cardbg: [], - enemy: [], last_enemy: {}, friendly: {}, release_state: 0, stamp_conv_num: 0, user_flags: [0, 0, 0, 0], - last_time: 0, greeting_level: 0, user_marker: 0, mission_no: 0, question_id: 0, sort_type: 0, disp_skill: 0, - }; -} - -async function newDoll(user_seq: number, doll_id: number, doll_name: string, is_frank: boolean): Promise { - const doll: Doll = { - collection: 'doll', doll_seq: await nextSeq('doll'), user_seq, doll_id, doll_name, doll_seed: randU32(), - doll_level: 1, is_frank, equip: [], accessory: [], kira: [0, 0, 0, 0], makeup: [0, 0], - }; - await DB.Insert(doll); - return doll; -} - const getUser = (user_seq: number) => DB.FindOne({ collection: 'user', user_seq }); const getDoll = (doll_seq: number) => DB.FindOne({ collection: 'doll', doll_seq }); -async function save(key: object, doc: T) { +async function save(key: object, doc: object) { const { _id, ...rest } = doc as any; await DB.Update(key, { $set: rest }); } const saveUser = (u: User) => save({ collection: 'user', user_seq: u.user_seq }, u); const saveDoll = (d: Doll) => save({ collection: 'doll', doll_seq: d.doll_seq }, d); -function log(method: string, data: any) { - console.log(`[otoca] ${method} ${JSON.stringify(data)}`); -} - // ---- closet ---------------------------------------------------------------- -// item_type 0 = equip (accessories are equips with their own accessory_seq), 1 = material. -// ponytail: other item types are only logged until their meaning is seen in real traffic. -function giveItem(u: User, type: number, id: number, grade: number, n: number, isAcc: boolean, made: Accessory[]) { - if (type === 0 && isAcc) { - // ponytail: offsets 0 (no random bonus) until the real ranges are known. - const a = { aseq: u.next_aseq++, id, gr: grade, offset: [0, 0, 0, 0] }; - u.accessory.push(a); - made.push(a); - return; - } - const bag = type === 0 ? u.equip : type === 1 ? u.material : null; - if (!bag || grade > 2) return log(`unknown item type=${type} id=${id} grade=${grade}`, n); +function addCount(bag: { [id: string]: number[] }, id: number, grade: number, n: number) { + if (!id || grade > 2) return; const gr = bag[id] || [0, 0, 0]; gr[grade] = Math.max(0, gr[grade] + n); if (gr.some(c => c > 0)) bag[id] = gr; else delete bag[id]; } -function takeItem(u: User, aseq: number, type: number, id: number, grade: number, n: number) { - if (aseq) u.accessory = u.accessory.filter(a => a.aseq !== aseq); - else giveItem(u, type, id, grade, -n, false, []); +function makeAccessory(u: User, id: number, grade: number): Accessory { + const gr = Math.min(grade, 2); + const a = { aseq: u.next_aseq++, id, gr, offset: ACCESSORY_OFFSET[gr].slice() }; + u.accessory.push(a); + return a; } +// item_type 0 equip (kind 4 = accessory), 1 material, 2 decoseal. Returns the accessory made, if any. +function giveItem(u: User, type: number, id: number, grade: number): Accessory | null { + if (isAccessory(type, id)) return makeAccessory(u, id, grade); + if (type === 0) addCount(u.equip, id, grade, 1); + else if (type === 1) addCount(u.material, id, grade, 1); + else if (type === 2 && id) u.decoseal[id] = (u.decoseal[id] || 0) + 1; + else log(`unknown item type=${type} id=${id} grade=${grade}`, {}); + return null; +} + +async function removeAccessory(u: User, aseq: number) { + u.accessory = u.accessory.filter(a => a.aseq !== aseq); + // the game also takes a removed accessory off the doll wearing it + for (const d of await DB.Find({ collection: 'doll', user_seq: u.user_seq })) { + if (d.accessory.includes(aseq)) { + d.accessory = d.accessory.map(s => (s === aseq ? 0 : s)); + await saveDoll(d); + } + } +} + +async function takeItem(u: User, aseq: number, type: number, id: number, grade: number, n: number) { + if (aseq) await removeAccessory(u, aseq); + else if (type === 0) addCount(u.equip, id, grade, -n); + else if (type === 1) addCount(u.material, id, grade, -n); +} + +const EMPTY_ACCESSORY: Accessory = { aseq: 0, id: 0, gr: 0, offset: [0, 0, 0, 0] }; const accessoryNode = (a: Accessory) => ({ accessory_seq: u32(a.aseq), equip_id: u32(a.id), equip_grade: u8(a.gr), param_offset: u8(a.offset[0]), skill_offset_1: u8(a.offset[1]), @@ -168,6 +190,55 @@ const accessoryNode = (a: Accessory) => ({ const gradeList = (bag: { [id: string]: number[] }) => Object.keys(bag).map(id => ({ id: u32(+id), gr: K.ARRAY('s16', bag[id].map(c => (c > 0 ? c : -1))) })); +// The game counts accessories in the equip list as well (AddAccessory adds to both). +function equipWithAccessories(u: User) { + const bag: { [id: string]: number[] } = {}; + for (const id of Object.keys(u.equip)) bag[id] = u.equip[id].slice(); + for (const a of u.accessory) addCount(bag, a.id, a.gr, 1); + return bag; +} + +// ---- users and dolls ------------------------------------------------------- + +function newUser(user_seq: number, user_name: string): User { + return { + collection: 'user', user_seq, user_name, keycard_id: '', key_inquire_id: '', + powder_num: 1, // guess: non-zero shows the first-play powder message + stamp_num: 0, stamp_conv_num: 0, balance: 0, ticket_half_price: false, + ticket_rc_first: true, // guess: first rival card ticket for new players + equip: {}, material: {}, decoseal: {}, accessory: [], next_aseq: 1, cardbg: [], + release_state: 0, user_flags: [0, 0, 0, 0], last_time: 0, user_marker: 0, + mission_no: 1, // 0 would never start the missions + question_id: 0, sort_type: 0, disp_skill: 0, enemy: [], last_enemy: [], friendly: {}, gifts_used: [], + }; +} + +// Same as the game does locally: level 1, starting equips (grade 0), which also go into the closet. +async function newDoll(u: User, doll_id: number, doll_name: string): Promise { + const init = DOLL_INIT[doll_id] || [0, 0, 0, 0]; + const doll: Doll = { + collection: 'doll', doll_seq: await nextSeq('doll'), user_seq: u.user_seq, doll_id, doll_name, + doll_seed: rand(23328), // growth type + 3 * five stat offsets in base 6; 23328 and up disables growth + doll_level: 1, equip: init.map(id => [id, 0]), accessory: [0, 0], makeup: [0, 0], luck_offset: 0, + }; + for (const id of init) addCount(u.equip, id, 0, 1); + await DB.Insert(doll); + return doll; +} + +// "New doll" on the entry menu stays disabled while release_state is 0 (label disable_lv5). +async function updateRelease(u: User) { + const dolls = await DB.Find({ collection: 'doll', user_seq: u.user_seq }); + if (dolls.some(d => d.doll_level >= 5)) u.release_state = 1; +} + +// guess: 1 = played again within a day, 2 = back after two weeks, 0 = otherwise +function greeting(last: number) { + if (!last) return 0; + const days = (now() - last) / 86400; + return days < 1 ? 1 : days >= 14 ? 2 : 0; +} + // ---- handlers -------------------------------------------------------------- const getVersion: EPR = async (info, data, send) => { @@ -178,267 +249,113 @@ const getVersion: EPR = async (info, data, send) => { phase: u8(PHASE), marker: u8(U.GetConfig('marker')), campaign: { - trial_play: K.ITEM('bool', false), - trial_shop: K.ITEM('bool', false), - starkira_free: K.ITEM('bool', false), - stamp_double: K.ITEM('bool', false), - limited_enemy: K.ARRAY('bool', [limited, limited, limited, limited, limited, limited, limited, limited]), + trial_play: bool(false), + trial_shop: bool(false), + starkira_free: bool(U.GetConfig('starkira_free')), + stamp_double: bool(U.GetConfig('stamp_double')), + limited_enemy: K.ARRAY('bool', [0, 0, limited, 0, 0, 0, 0, 0]), // only slot 2 has an enemy (team 63) }, question: { id: u8(0), reward: { type: u8(0), id: u32(0), grade: u8(0) } }, }); }; const addUser: EPR = async (info, data, send) => { - const user = newUser(await nextSeq('user'), $(data).str('user_name', '')); - user.last_time = now(); - await DB.Insert(user); - const doll = await newDoll(user.user_seq, $(data).number('doll_id', 0), $(data).str('doll_name', ''), $(data).bool('is_frank')); + const u = newUser(await nextSeq('user'), $(data).str('user_name', '')); + const d = await newDoll(u, $(data).number('doll_id', 0), $(data).str('doll_name', '')); + await DB.Insert(u); send.object({ - user_seq: u32(user.user_seq), - doll_seq: u32(doll.doll_seq), - doll_seed: u32(doll.doll_seed), - powder_num: u32(user.powder_num), - stamp_num: u32(user.stamp_num), - mission_no: u8(user.mission_no), - question_id: u8(user.question_id), - ticket_rc_first: K.ITEM('bool', user.ticket_rc_first), + user_seq: u32(u.user_seq), + doll_seq: u32(d.doll_seq), + doll_seed: u32(d.doll_seed), + powder_num: u32(u.powder_num), + stamp_num: u32(u.stamp_num), + mission_no: u8(u.mission_no), + question_id: u8(u.question_id), + ticket_rc_first: bool(u.ticket_rc_first), }); }; const addDoll: EPR = async (info, data, send) => { - const doll = await newDoll($(data).number('user_seq'), $(data).number('doll_id', 0), $(data).str('doll_name', ''), false); - send.object({ doll_seq: u32(doll.doll_seq), doll_seed: u32(doll.doll_seed) }); + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + const d = await newDoll(u, $(data).number('doll_id', 0), $(data).str('doll_name', '')); + await saveUser(u); + send.object({ doll_seq: u32(d.doll_seq), doll_seed: u32(d.doll_seed) }); }; const getCardInfo: EPR = async (info, data, send) => { const otocard_id = $(data).str('otocard_id', ''); const card = await DB.FindOne({ collection: 'card', otocard_id }); - const user = card && (await getUser(card.user_seq)); - const doll = card && (await getDoll(card.doll_seq)); - if (!user || !doll) { + const u = card && (await getUser(card.user_seq)); + const d = card && (await getDoll(card.doll_seq)); + if (!u || !d) { + // status 1: the game plays offline with what the QR holds log(`getCardInfo: unknown card ${otocard_id}`, {}); return send.deny(); } - const accessories = doll.accessory.map(s => user.accessory.find(a => a.aseq === s)).filter(a => a); + const worn = d.accessory.map(s => u.accessory.find(a => a.aseq === s) || EMPTY_ACCESSORY); send.object({ - user_seq: u32(user.user_seq), - user_name: K.ITEM('str', user.user_name), - doll_seq: u32(doll.doll_seq), - doll_id: u32(doll.doll_id), - doll_name: K.ITEM('str', doll.doll_name), - doll_level: u32(doll.doll_level), - doll_seed: u32(doll.doll_seed), - locked: K.ITEM('bool', false), - luck_offset: u32(0), - equip: doll.equip.map(e => ({ equip_id: u32(e[0]), equip_grade: u8(e[1]) })), - accessory: accessories.map(accessoryNode), + user_seq: u32(u.user_seq), + user_name: K.ITEM('str', u.user_name), + doll_seq: u32(d.doll_seq), + doll_id: u32(d.doll_id), + doll_name: K.ITEM('str', d.doll_name), + doll_level: u32(d.doll_level), + doll_seed: u32(d.doll_seed), + locked: bool(!!u.keycard_id), // tied to a key card: reads the key next, closet limit 999 + luck_offset: u32(d.luck_offset), + equip: [...d.equip, ...worn.map(a => [a.id, a.gr])].map(e => ({ equip_id: u32(e[0]), equip_grade: u8(e[1]) })), + accessory: worn.map(accessoryNode), score: { - release_state: u32(user.release_state), - stamp_conv_num: u32(user.stamp_conv_num), - user_flags: K.ARRAY('u8', user.user_flags), - last_time: K.ITEM('time', user.last_time), + release_state: u32(u.release_state), + stamp_conv_num: u32(u.stamp_conv_num), + user_flags: K.ARRAY('u8', u.user_flags), + last_time: K.ITEM('time', u.last_time), curr_time: K.ITEM('time', now()), - greeting_level: u8(user.greeting_level), - user_marker: u8(user.user_marker), - mission_no: u8(user.mission_no), - setting: { sort_type: u8(user.sort_type), disp_skill: u8(user.disp_skill) }, - question_id: u8(user.question_id), - enemy: user.enemy.map(e => ({ enemy_team_id: u32(e[0]), enemy_team_level: u32(e[1]) })), - last_enemy: Object.keys(user.last_enemy).map(ep => ({ - episode: u8(+ep), enemy_team_id: u32(user.last_enemy[ep][0]), enemy_team_level: u32(user.last_enemy[ep][1]), - })), - enemy_detail: { i: Object.keys(user.friendly).map(t => ({ id: K.ITEM('u16', +t), pt: u32(user.friendly[t]) })) }, + greeting_level: u8(greeting(u.last_time)), + user_marker: u8(u.user_marker), + mission_no: u8(u.mission_no), + setting: { sort_type: u8(u.sort_type), disp_skill: u8(u.disp_skill) }, + question_id: u8(u.question_id), + enemy: u.enemy.map(e => ({ enemy_team_id: u32(e[0]), enemy_team_level: u32(e[1]) })), + last_enemy: u.last_enemy.map(e => ({ episode: u8(e[0]), enemy_team_id: u32(e[1]), enemy_team_level: u32(e[2]) })), + enemy_detail: { i: Object.keys(u.friendly).map(t => ({ id: K.ITEM('u16', +t), pt: u32(u.friendly[t]) })) }, }, - offset: { kira_hp: u8(doll.kira[0]), kira_atk: u8(doll.kira[1]), kira_mat: u8(doll.kira[2]), kira_spd: u8(doll.kira[3]) }, - active_key: K.ITEM('bool', !!user.keycard_id), + // the bonus printed on this card; the game applies it to one battle + offset: { kira_hp: u8(card.kira[0]), kira_atk: u8(card.kira[1]), kira_mat: u8(card.kira[2]), kira_spd: u8(card.kira[3]) }, + active_key: bool(!!u.keycard_id), version: { episode: u8(EPISODE), phase: u8(PHASE) }, - makeup: { hair_color: u8(doll.makeup[0]), eye_color: u8(doll.makeup[1]) }, + makeup: { hair_color: u8(d.makeup[0]), eye_color: u8(d.makeup[1]) }, closet: { - powder_num: u32(user.powder_num), - stamp_num: u32(user.stamp_num), + powder_num: u32(u.powder_num), + stamp_num: u32(u.stamp_num), wallet: { - balance: u32(user.balance), - ticket_half_price: K.ITEM('bool', user.ticket_half_price), - ticket_rc_first: K.ITEM('bool', user.ticket_rc_first), + balance: u32(u.balance), + ticket_half_price: bool(u.ticket_half_price), + ticket_rc_first: bool(u.ticket_rc_first), }, - equip: { i: gradeList(user.equip) }, - material: { i: gradeList(user.material) }, + equip: { i: gradeList(equipWithAccessories(u)) }, + material: { i: gradeList(u.material) }, + decoseal: Object.keys(u.decoseal).slice(0, 1).map(id => ({ decoseal_id: u32(+id), decoseal_num: u32(u.decoseal[id]) })), accessory: { - i: user.accessory.map(a => ({ aseq: u32(a.aseq), id: u32(a.id), gr: u8(a.gr), offset: K.ARRAY('u8', a.offset) })), + i: u.accessory.map(a => ({ aseq: u32(a.aseq), id: u32(a.id), gr: u8(a.gr), offset: K.ARRAY('u8', a.offset) })), }, - cardbg: user.cardbg.map(id => ({ id: u8(id) })), + cardbg: u.cardbg.slice(0, 4).map(id => ({ id: u8(id) })), }, }); }; -const setCardInfo: EPR = async (info, data, send) => { - const d = await getDoll($(data).number('doll_seq')); - if (!d) return send.deny(); - d.doll_level = $(data).number('doll_level', d.doll_level); - d.doll_id = $(data).number('doll_id', d.doll_id); - d.doll_name = $(data).str('doll_name', d.doll_name); - d.equip = $(data).elements('equip').map(e => [e.number('equip_id', 0), e.number('equip_grade', 0)]).filter(e => e[0]); - d.accessory = ($(data).numbers('accessory_seq') || []).filter(s => s); - await saveDoll(d); - const card: Card = { - collection: 'card', otocard_id: await newId(), inquire_id: await newId(), user_seq: d.user_seq, - doll_seq: d.doll_seq, kira_type: $(data).number('kira_type', 0), created: now(), - }; - await DB.Insert(card); - log('setCardInfo', { from: $(data).str('otocard_id'), to: card.otocard_id, kira_type: card.kira_type }); - send.object({ - otocard_id: K.ITEM('str', card.otocard_id), - inquire_id: K.ITEM('str', card.inquire_id), - offset: { kira_hp: u8(d.kira[0]), kira_atk: u8(d.kira[1]), kira_mat: u8(d.kira[2]), kira_spd: u8(d.kira[3]) }, - }); -}; - -const report: EPR = async (info, data, send) => { +const checkKeyUser: EPR = async (info, data, send) => { const u = await getUser($(data).number('user_seq')); - if (!u) return send.deny(); - const b = $(data).element('battle'); - log('report', { battle: b && b.obj, user_marker: $(data).number('user_marker') }); - if (b) { - const team = b.number('enemy_team_id', 0); - const level = b.number('enemy_team_level', 0); - // battle_result 0 = win: the game itself appends the pair to score/enemy only then (game.dll FUN_100bb1d0) - const beaten = u.enemy.some(e => e[0] === team && e[1] === level); - if (team && b.number('battle_result') === 0 && !beaten && u.enemy.length < 200) u.enemy.push([team, level]); - if (team) u.last_enemy[b.number('episode', 0)] = [team, level]; + const keycard_id = $(data).str('keycard_id', ''); + if (!u || !u.keycard_id || u.keycard_id !== keycard_id) { + log('checkKeyUser: not this user\'s key', { keycard_id, bound: u && u.keycard_id }); + return send.status(10); // the game shows "key does not match" and lets the player rescan } - u.user_flags = $(data).numbers('user_flags', u.user_flags); - u.user_marker = $(data).number('user_marker', u.user_marker); - u.sort_type = $(data).number('setting.sort_type', u.sort_type); - u.disp_skill = $(data).number('setting.disp_skill', u.disp_skill); - u.last_time = now(); - await saveUser(u); - const d = await getDoll($(data).number('doll_seq')); - if (d) { - d.doll_level = $(data).number('doll_level', d.doll_level); - await saveDoll(d); - } - send.success(); -}; - -const addExp: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (!u) return send.deny(); - u.balance += $(data).number('gold.add_amount', 0); - const team = $(data).number('friendly.enemy_team_id', 0); - if (team) u.friendly[team] = (u.friendly[team] || 0) + $(data).number('friendly.add_point', 0); - await saveUser(u); - send.object({ balance: u32(u.balance), friend_point: u32(team ? u.friendly[team] : 0) }); -}; - -const addGold: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (!u) return send.deny(); - u.balance += $(data).number('add_amount', 0); - await saveUser(u); - send.object({ balance: u32(u.balance) }); -}; - -const addItem: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (!u) return send.deny(); - const made: Accessory[] = []; - for (const i of $(data).elements('item')) { - giveItem(u, i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), 1, i.bool('is_accessory'), made); - } - await saveUser(u); - send.object({ accessory: made.map(accessoryNode) }); -}; - -const delItem: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (!u) return send.deny(); - for (const i of $(data).elements('item')) { - takeItem(u, i.number('accessory_seq', 0), i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), 1); - } - log('delItem drop (not added)', $(data).elements('drop').map(e => e.obj)); - await saveUser(u); - send.success(); -}; - -const sellItem: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (!u) return send.deny(); - for (const i of $(data).elements('item')) { - const n = i.number('item_num', 1); - takeItem(u, i.number('accessory_seq', 0), i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), n); - u.balance += i.number('price', 0) * n; - } - const made: Accessory[] = []; - for (const i of $(data).elements('drop')) { - giveItem(u, i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), 1, i.bool('is_accessory'), made); - } - log('sellItem', { client_balance: $(data).number('balance'), balance: u.balance }); - await saveUser(u); - send.object({ accessory: made.map(accessoryNode), balance: u32(u.balance) }); -}; - -const composition: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (!u) return send.deny(); - for (const k of ['item_in_1', 'item_in_2']) { - const i = $(data).element(k); - if (i) takeItem(u, i.number('accessory_seq', 0), i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), 1); - } - const r = $(data).element('item_result'); - const made: Accessory[] = []; - if (r) giveItem(u, r.number('item_type', 0), r.number('item_id', 0), r.number('item_grade', 0), 1, r.bool('accessory_flag'), made); - await saveUser(u); - send.object({ accessory: accessoryNode(made[0] || { aseq: 0, id: 0, gr: 0, offset: [0, 0, 0, 0] }) }); -}; - -const addStamp: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (!u) return send.deny(); - log('addStamp', data); - const before = u.stamp_num; - u.stamp_num += 1; - await saveUser(u); - send.object({ - powder_num: u32(u.powder_num), stamp_num: u32(before), stamp_after_num: u32(u.stamp_num), stamp_conv_rate: u32(0), - }); -}; - -const setMakeup: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - const d = await getDoll($(data).number('doll_seq')); - if (!u || !d) return send.deny(); - d.makeup = [$(data).number('makeup.hair_color', 0), $(data).number('makeup.eye_color', 0)]; - u.balance = Math.max(0, u.balance - $(data).number('payment', 0)); - log('setMakeup', { client_balance: $(data).number('balance'), balance: u.balance, ticket: $(data).number('ticket_type') }); - await saveDoll(d); - await saveUser(u); - send.object({ balance: u32(u.balance) }); -}; - -// No gift codes on this server: every code is answered with status 0. -const receiveGift: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - log('receiveGift', data); - send.object({ - status: $(data).elements('info').map(i => ({ gift_type: u8(i.number('gift_type', 0)), gift_status: u8(0) })), - powder_num: u32(u ? u.powder_num : 0), - }); -}; - -const receiveItem: EPR = async (info, data, send) => { - log('receiveItem', data); - send.object({ gift_type: u8($(data).number('gift_type', 0)), gift_status: u8(0) }); -}; - -const setUserField = (field: 'mission_no' | 'question_id'): EPR => async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (u) { - u[field] = $(data).number(field, u[field]); - await saveUser(u); - } - send.success(); + send.object({ keycard_id: K.ITEM('str', u.keycard_id), key_inquire_id: K.ITEM('str', u.key_inquire_id) }); }; +// A new key replaces the old one (first key or "remake"). The id goes into the key card's QR. const bindKeyUser: EPR = async (info, data, send) => { const u = await getUser($(data).number('user_seq')); if (!u) return send.deny(); @@ -448,19 +365,223 @@ const bindKeyUser: EPR = async (info, data, send) => { send.object({ keycard_id: K.ITEM('str', u.keycard_id), key_inquire_id: K.ITEM('str', u.key_inquire_id), - accessory: accessoryNode({ aseq: 0, id: 0, gr: 0, offset: [0, 0, 0, 0] }), + accessory: accessoryNode(EMPTY_ACCESSORY), // guess: the first-key bonus item is unknown, so none }); }; -const checkKeyUser: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (!u) return send.deny(); - log('checkKeyUser', { scanned: $(data).str('keycard_id'), bound: u.keycard_id }); - send.object({ keycard_id: K.ITEM('str', u.keycard_id), key_inquire_id: K.ITEM('str', u.key_inquire_id) }); +const setCardInfo: EPR = async (info, data, send) => { + const d = await getDoll($(data).number('doll_seq')); + const u = d && (await getUser(d.user_seq)); + if (!u || !d) return send.deny(); + d.doll_level = Math.max(1, Math.min(31, $(data).number('doll_level', d.doll_level))); + d.doll_name = $(data).str('doll_name', d.doll_name); + d.equip = $(data).elements('equip').slice(0, 4).map(e => [e.number('equip_id', 0), e.number('equip_grade', 0)]); + d.accessory = ($(data).numbers('accessory_seq') || []).concat([0, 0]).slice(0, 2); + await saveDoll(d); + + // kira_type 0 normal, 1 star, 2 gold, 3 star while starkira_free is on + const kira_type = $(data).number('kira_type', 0); + const kira = [0, 0, 0, 0]; + // guess: the real distribution is unknown; values must stay 0-9 (one digit on the card) + const star = kira_type === 1 || kira_type === 3; + const slots = kira_type === 2 ? [0, 1, 2, 3] : star ? [0, 1, 2, 3].sort(() => Math.random() - 0.5).slice(0, 2) : []; + for (const k of slots) kira[k] = kira_type === 2 ? 1 + rand(5) : 1 + rand(3); + if (kira_type === 1) u.powder_num = Math.max(0, u.powder_num - 1); // the game spends one star powder locally + const decoseal_id = $(data).number('decoseal_id', 0); + if (decoseal_id && u.decoseal[decoseal_id]) u.decoseal[decoseal_id]--; // guess: nothing else reports it + await updateRelease(u); + await saveUser(u); + + const card: Card = { + collection: 'card', otocard_id: await newId(), inquire_id: await newId(), user_seq: u.user_seq, + doll_seq: d.doll_seq, kira_type, kira, decoseal_id, from_otocard_id: $(data).str('otocard_id', ''), created: now(), + }; + await DB.Insert(card); + log('setCardInfo', { otocard_id: card.otocard_id, kira_type, kira }); + send.object({ + otocard_id: K.ITEM('str', card.otocard_id), + inquire_id: K.ITEM('str', card.inquire_id), + offset: { kira_hp: u8(kira[0]), kira_atk: u8(kira[1]), kira_mat: u8(kira[2]), kira_spd: u8(kira[3]) }, + }); }; -const logOnly = (method: string): EPR => async (info, data, send) => { - log(method, data); +const report: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.success(); + const b = $(data).element('battle'); + if (b) { + const team = b.number('enemy_team_id', 0); + const level = b.number('enemy_team_level', 0); + const episode = b.number('episode', 0); + // battle_result 0 win, 1 lose, 2 draw; the game adds a beaten pair to score/enemy only on a win + if (team && b.number('battle_result', 1) === 0 && u.enemy.length < 200 && + !u.enemy.some(e => e[0] === team && e[1] === level)) u.enemy.push([team, level]); + if (team) u.last_enemy = [[episode, team, level], ...u.last_enemy.filter(e => e[0] !== episode)].slice(0, 6); + } + u.user_flags = $(data).numbers('user_flags', u.user_flags); + u.user_marker = $(data).number('user_marker', u.user_marker); + u.sort_type = $(data).number('setting.sort_type', u.sort_type); + u.disp_skill = $(data).number('setting.disp_skill', u.disp_skill); + u.last_time = now(); + const d = await getDoll($(data).number('doll_seq')); + if (d && d.user_seq === u.user_seq) { + d.doll_level = Math.max(1, Math.min(31, $(data).number('doll_level', d.doll_level))); + await saveDoll(d); + } + await updateRelease(u); + await saveUser(u); + send.success(); +}; + +// Only gold and friend points: doll levels are raised by the game itself. +const addExp: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + u.balance = Math.min(MAX_BALANCE, u.balance + $(data).number('gold.add_amount', 0)); + const team = $(data).number('friendly.enemy_team_id', 0); + if (team) u.friendly[team] = (u.friendly[team] || 0) + $(data).number('friendly.add_point', 0); + await saveUser(u); + send.object({ balance: u32(u.balance), friend_point: u32(team ? u.friendly[team] : 0) }); +}; + +const addItem: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + // accessory[i] must line up with item[i]; rows that are not accessories stay zero + const made = $(data).elements('item').map(i => + giveItem(u, i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0)) || EMPTY_ACCESSORY); + const gift = $(data).str('gift_code', ''); + if ($(data).number('gift_type', 0) === 4 && gift && !u.gifts_used.includes(gift)) u.gifts_used.push(gift); + await saveUser(u); + send.object({ accessory: made.map(accessoryNode) }); +}; + +// item: what the player threw away (one row per piece). drop: this play's drops that are kept. +const delItem: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + for (const i of $(data).elements('drop')) giveItem(u, i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0)); + for (const i of $(data).elements('item')) { + await takeItem(u, i.number('accessory_seq', 0), i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), 1); + } + await saveUser(u); + send.success(); +}; + +const sellItem: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + const items = $(data).elements('item'); + const soldTemp = items.map(i => i.number('accessory_seq', 0)).filter(s => s >= TEMP_ASEQ); + // drops first (sell scene after a full closet): a dropped accessory sold right away comes back as TEMP_ASEQ + n + const made: Accessory[] = []; + $(data).elements('drop').forEach((i, n) => { + const type = i.number('item_type', 0), id = i.number('item_id', 0), grade = i.number('item_grade', 0); + if (isAccessory(type, id) && soldTemp.includes(TEMP_ASEQ + n)) return; + const a = giveItem(u, type, id, grade); + if (a) made.push(a); + }); + for (const i of items) { + const aseq = i.number('accessory_seq', 0); + if (aseq >= TEMP_ASEQ) continue; + await takeItem(u, aseq, i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), i.number('item_num', 1)); + } + // the game computes the balance after selling (prices from pricelist.csv) and never reads ours + u.balance = Math.max(0, Math.min(MAX_BALANCE, $(data).number('balance', u.balance))); + await saveUser(u); + send.object({ balance: u32(u.balance), accessory: made.map(accessoryNode) }); +}; + +const composition: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + for (const k of ['item_in_1', 'item_in_2']) { + const i = $(data).element(k); + if (i) await takeItem(u, i.number('accessory_seq', 0), i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), 1); + } + const r = $(data).element('item_result'); + const made = r ? giveItem(u, r.number('item_type', 0), r.number('item_id', 0), r.number('item_grade', 0)) : null; + await saveUser(u); + send.object({ accessory: accessoryNode(made || EMPTY_ACCESSORY) }); +}; + +const setMakeup: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + const d = await getDoll($(data).number('doll_seq')); + if (!u || !d) return send.deny(); + d.makeup = [$(data).number('makeup.hair_color', 0), $(data).number('makeup.eye_color', 0)]; + // payment is only the last purchase; the game sends the balance after paying + u.balance = Math.max(0, Math.min(MAX_BALANCE, $(data).number('balance', u.balance))); + if ($(data).number('ticket_type', 0) === 1) u.ticket_half_price = false; + await saveDoll(d); + await saveUser(u); + send.object({ balance: u32(u.balance) }); +}; + +// The stamp card has 10 squares; a full card turns into star powder. +const addStamp: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + const after = u.stamp_num + (U.GetConfig('stamp_double') ? 2 : 1); // guess: one stamp per play + const full = Math.floor(after / 10); + u.stamp_num = after % 10; + u.powder_num += full; // guess: one powder per full card + u.stamp_conv_num += full; + await saveUser(u); + send.object({ + powder_num: u32(u.powder_num), + stamp_num: u32(u.stamp_num), + stamp_conv_rate: u32(10), // squares per card; the game divides by it + stamp_after_num: u32(after), + }); +}; + +const reportMission: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + const n = $(data).number('mission_no', 0); + if (u && u.mission_no === n) { + u.mission_no = Math.min(n + 1, 9); // the game treats every mission below mission_no as done + await saveUser(u); + } + send.success(); +}; + +// score/question_id is the questionnaire still to show; no questionnaires are offered, so it stays 0. +const reportQuestion: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (u) { + u.question_id = 0; + await saveUser(u); + } + send.success(); +}; + +// Gift QRs: only type 4 (the offline card the game prints when setCardInfo fails) is honoured. +// The apology gift itself is taken later by addItem with gift_type 4. Past campaigns answer 5 (expired). +const receiveGift: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + const gift = $(data).element('info'); + const type = gift ? gift.number('gift_type', 0) : 0; + const code = gift ? gift.str('gift_code', '') : ''; + const status = type !== 4 ? 5 : u && u.gifts_used.includes(code) ? 2 : 1; + send.object({ + powder_num: u32(u ? u.powder_num : 0), // always overwrites the game's count + status: [{ gift_type: u8(type), gift_status: u8(status) }], + }); +}; + +const receiveItem: EPR = async (info, data, send) => { + send.object({ gift_type: u8(7), gift_status: u8(5) }); +}; + +// The game spends ticket_rc_first itself when a rival card is chosen and tells nobody else. +const setRivalCard: EPR = async (info, data, send) => { + const card = await DB.FindOne({ collection: 'card', otocard_id: $(data).str('otocard_id', '') }); + const u = card && (await getUser(card.user_seq)); + if (u && u.ticket_rc_first) { + u.ticket_rc_first = false; + await saveUser(u); + } send.success(); }; @@ -470,14 +591,26 @@ export function register() { R.Config('marker', { name: 'Version marker', - desc: 'Unlock step sent in game.getVersion (the game checks 1-8).', + desc: 'Unlocks hair/eye colours in the salon and mode-select tips (the game checks 1-8).', type: 'integer', default: 8, range: [0, 255], }); R.Config('limited_enemy', { - name: 'Limited-time enemies', - desc: 'Turn on all 8 limited-time enemy slots.', + name: 'Limited-time enemy', + desc: 'Show the limited-time enemy (あい / ぷっちコンテスト, episode 3).', + type: 'boolean', + default: false, + }); + R.Config('stamp_double', { + name: 'Double stamps', + desc: 'Two stamps per play, with the campaign icon on the title screen.', + type: 'boolean', + default: false, + }); + R.Config('starkira_free', { + name: 'Free star kira', + desc: 'Star kira cards need no star powder.', type: 'boolean', default: false, }); @@ -486,27 +619,26 @@ export function register() { R.Route('game.addUser', addUser); R.Route('game.addDoll', addDoll); R.Route('game.getCardInfo', getCardInfo); + R.Route('game.checkKeyUser', checkKeyUser); + R.Route('game.bindKeyUser', bindKeyUser); + R.Route('game.copyKeyCard', true); + R.Route('game.cancel', true); R.Route('game.setCardInfo', setCardInfo); R.Route('game.report', report); R.Route('game.addExp', addExp); - R.Route('game.addGold', addGold); R.Route('game.addItem', addItem); R.Route('game.delItem', delItem); R.Route('game.sellItem', sellItem); R.Route('game.composition', composition); - R.Route('game.addStamp', addStamp); R.Route('game.setMakeup', setMakeup); + R.Route('game.addStamp', addStamp); + R.Route('game.reportMission', reportMission); + R.Route('game.reportQuestion', reportQuestion); + R.Route('game.reportTrial', true); R.Route('game.receiveGift', receiveGift); R.Route('game.receiveItem', receiveItem); - R.Route('game.reportMission', setUserField('mission_no')); - R.Route('game.reportQuestion', setUserField('question_id')); - R.Route('game.bindKeyUser', bindKeyUser); - R.Route('game.checkKeyUser', checkKeyUser); - R.Route('game.copyKeyCard', logOnly('copyKeyCard')); - R.Route('game.checkRivalCard', logOnly('checkRivalCard')); - R.Route('game.setRivalCard', logOnly('setRivalCard')); - R.Route('game.reportTrial', true); - R.Route('game.cancel', true); + R.Route('game.checkRivalCard', true); + R.Route('game.setRivalCard', setRivalCard); R.Unhandled(async (info, data, send) => { console.error(`[otoca] unhandled ${info.module}.${info.method}: ${JSON.stringify(data)}`); From 4a8ad31779dd01aef6ba62b728567d7492837366 Mon Sep 17 00:00:00 2001 From: iamsub <294696795+iamiamsub@users.noreply.github.com> Date: Tue, 29 Sep 2026 15:16:11 +0900 Subject: [PATCH 4/7] otoca: follow the arcade's rules from the fan wiki and official site - kira cards: +1 to 1-3 random stats (was a guessed +1-3 / +1-5) - accessory stats and skills vary within the wiki's per-grade ranges - a card keeps the coord it was printed with; scanning an old card plays the latest doll in that coord, even with items sold since - the first key card comes with the Pink March Ribbon (tailor tutorial) - an offline card's present also gives a stamp, pressed with the next play's stamp - all 8 letters add the letter wallpaper to closet/cardbg - salon trial ticket on by default --- otoca@asphyxia/README.md | 22 ++++++++----- otoca@asphyxia/index.ts | 68 +++++++++++++++++++++++++++++----------- 2 files changed, 64 insertions(+), 26 deletions(-) diff --git a/otoca@asphyxia/README.md b/otoca@asphyxia/README.md index c1d9642..29d1998 100644 --- a/otoca@asphyxia/README.md +++ b/otoca@asphyxia/README.md @@ -1,6 +1,6 @@ # otoca d'or -Plugin Version: **v0.2.0** +Plugin Version: **v0.3.0** --- @@ -18,8 +18,9 @@ How it works - `game.setCardInfo` prints a card. The server issues its `otocard_id` (8 characters of `0123456789ABCDEFGHJKLMNPRSTUWXYZ`), which the game packs into the card's QR code, and rolls the kira bonus printed on it. - - Scanning a card sends `game.getCardInfo` with that id and gets the player, the doll, the closet and - that card's kira bonus back. An unknown card answers status 1, and the game then plays offline. + - Scanning a card sends `game.getCardInfo` with that id and gets the player, the latest doll in the + coord printed on that card, the closet and that card's kira bonus back. An unknown card answers + status 1, and the game then plays offline. - The key card (`game.bindKeyUser`) is a second printed card tied to the player. Once a key is bound, scanning a card asks for the key card (`game.checkKeyUser`) and the closet holds 999 items instead of 50. - `game.getVersion` and getCardInfo both send episode 5 / phase 11, the newest the game knows. @@ -28,18 +29,20 @@ How it works Settings - Version marker: unlocks hair/eye colours in the salon and the mode-select tips (the game checks 1 to 8). + - Salon trial ticket: one free hair/eye colour change for each player (on by default, as in the arcade). - Limited-time enemy: shows the one limited-time enemy the data has (episode 3). - Double stamps: two stamps per play. - Free star kira: star kira cards need no star powder. -Server policy (the game does not decide these) - - Kira bonus: star kira gives +1-3 to two stats, gold kira +1-5 to all four. - - Accessory stats: 80/90/100% of the base values for grade N/R/SR. +Server-side rules (from the arcade as documented by the fan wiki and the official site) + - Kira cards give +1 to 1-3 random stats. + - Accessories vary per piece: stats 60-80% (N), 70-90% (R), 80-100% (SR) of the base, skills likewise. - One stamp per play; a full card of 10 gives one star powder. New players start with one powder - and the first rival card ticket. + and the first rival card ticket. The first key card comes with the Pink March Ribbon. - "New doll" unlocks once any doll reaches level 5. + - All 8 letters from Nyandora give a wallpaper (which one is inferred from the data). - No questionnaires, and old campaign gift cards answer "expired". The game's own offline card - (printed when setCardInfo fails) gives its apology gift once. + (printed when setCardInfo fails) gives its apology gift and a stamp once. Not done yet - No WebUI. @@ -47,6 +50,9 @@ Not done yet --- Changelog + - 0.3.0: rules checked against the fan wiki and the official site: kira +1 to 1-3 stats, accessory + variance per grade, each card keeps its printed coord, first-key ribbon, offline-card stamp, + letter wallpaper, salon trial ticket. - 0.2.0: every response rebuilt from a full analysis of game.dll (starting equipment, doll seed range, key card binding, per-card kira, stamp card of 10, missions, closet rules for accessories, selling and discarding). diff --git a/otoca@asphyxia/index.ts b/otoca@asphyxia/index.ts index 5c9fba6..7c4dab2 100644 --- a/otoca@asphyxia/index.ts +++ b/otoca@asphyxia/index.ts @@ -18,6 +18,7 @@ interface User { powder_num: number; stamp_num: number; stamp_conv_num: number; + stamp_bonus: number; // stamps from gifts, pressed with the next addStamp balance: number; ticket_half_price: boolean; ticket_rc_first: boolean; @@ -63,6 +64,10 @@ interface Card { doll_seq: number; kira_type: number; kira: number[]; // hp, atk, mat, spd bonus printed on this card + // the coord printed on this card: scanning an old card plays the latest doll in that card's coord, + // even with items sold since then + equip: number[][]; + accessory: Accessory[]; decoseal_id: number; from_otocard_id: string; created: number; @@ -89,8 +94,14 @@ const DOLL_INIT: { [doll_id: number]: number[] } = { 1: [9, 18, 29, 40], 2: [9, 20, 31, 40], 3: [9, 17, 28, 40], 4: [9, 19, 30, 40], 5: [9, 21, 32, 40], 6: [9, 409, 438, 40], }; -// guess: the game only multiplies by these; this matches normal equips of the same grade. -const ACCESSORY_OFFSET = [[80, 100, 100, 0], [90, 100, 100, 50], [100, 100, 100, 100]]; +// Accessory stat / skill 1-3 percent ranges per grade N, R, SR (wiki item list: accessories vary per piece). +const ACCESSORY_OFFSET = [ + [[60, 80], [80, 100], [50, 100], [0, 0]], + [[70, 90], [100, 100], [80, 100], [40, 50]], + [[80, 100], [100, 100], [100, 100], [80, 100]], +]; +const FIRST_KEY_ACCESSORY = 144; // ピンクマーチリボン, made in the tailor tutorial when the first key is made +const LETTER_WALLPAPER = 29; // guess: card_bg_0018 (nyandora night sky), the reward for all 8 letters const TEMP_ASEQ = 1000000000; // the game's own placeholder seqs for this play's drops (sell scene) const MAX_BALANCE = 9999999; @@ -147,7 +158,7 @@ function addCount(bag: { [id: string]: number[] }, id: number, grade: number, n: function makeAccessory(u: User, id: number, grade: number): Accessory { const gr = Math.min(grade, 2); - const a = { aseq: u.next_aseq++, id, gr, offset: ACCESSORY_OFFSET[gr].slice() }; + const a = { aseq: u.next_aseq++, id, gr, offset: ACCESSORY_OFFSET[gr].map(([lo, hi]) => lo + rand(hi - lo + 1)) }; u.accessory.push(a); return a; } @@ -203,9 +214,11 @@ function equipWithAccessories(u: User) { function newUser(user_seq: number, user_name: string): User { return { collection: 'user', user_seq, user_name, keycard_id: '', key_inquire_id: '', - powder_num: 1, // guess: non-zero shows the first-play powder message - stamp_num: 0, stamp_conv_num: 0, balance: 0, ticket_half_price: false, - ticket_rc_first: true, // guess: first rival card ticket for new players + // shows the first-play powder message; with the first stamp card this makes the 2 kira cards + // the wiki gives players without a key + powder_num: 1, + stamp_num: 0, stamp_conv_num: 0, stamp_bonus: 0, balance: 0, ticket_half_price: false, + ticket_rc_first: true, // the first rival card print guarantees a higher rarity (wiki) equip: {}, material: {}, decoseal: {}, accessory: [], next_aseq: 1, cardbg: [], release_state: 0, user_flags: [0, 0, 0, 0], last_time: 0, user_marker: 0, mission_no: 1, // 0 would never start the missions @@ -250,7 +263,7 @@ const getVersion: EPR = async (info, data, send) => { marker: u8(U.GetConfig('marker')), campaign: { trial_play: bool(false), - trial_shop: bool(false), + trial_shop: bool(U.GetConfig('trial_shop')), // one free salon try per player; the game keeps the flag starkira_free: bool(U.GetConfig('starkira_free')), stamp_double: bool(U.GetConfig('stamp_double')), limited_enemy: K.ARRAY('bool', [0, 0, limited, 0, 0, 0, 0, 0]), // only slot 2 has an enemy (team 63) @@ -293,7 +306,7 @@ const getCardInfo: EPR = async (info, data, send) => { log(`getCardInfo: unknown card ${otocard_id}`, {}); return send.deny(); } - const worn = d.accessory.map(s => u.accessory.find(a => a.aseq === s) || EMPTY_ACCESSORY); + const worn = card.accessory; send.object({ user_seq: u32(u.user_seq), user_name: K.ITEM('str', u.user_name), @@ -304,7 +317,7 @@ const getCardInfo: EPR = async (info, data, send) => { doll_seed: u32(d.doll_seed), locked: bool(!!u.keycard_id), // tied to a key card: reads the key next, closet limit 999 luck_offset: u32(d.luck_offset), - equip: [...d.equip, ...worn.map(a => [a.id, a.gr])].map(e => ({ equip_id: u32(e[0]), equip_grade: u8(e[1]) })), + equip: [...card.equip, ...worn.map(a => [a.id, a.gr])].map(e => ({ equip_id: u32(e[0]), equip_grade: u8(e[1]) })), accessory: worn.map(accessoryNode), score: { release_state: u32(u.release_state), @@ -361,11 +374,13 @@ const bindKeyUser: EPR = async (info, data, send) => { if (!u) return send.deny(); u.keycard_id = await newId(); u.key_inquire_id = await newId(); + // the first key comes with the tailor tutorial's result; the game adds it only when is_first is set + const bonus = $(data).bool('is_first') ? makeAccessory(u, FIRST_KEY_ACCESSORY, 0) : EMPTY_ACCESSORY; await saveUser(u); send.object({ keycard_id: K.ITEM('str', u.keycard_id), key_inquire_id: K.ITEM('str', u.key_inquire_id), - accessory: accessoryNode(EMPTY_ACCESSORY), // guess: the first-key bonus item is unknown, so none + accessory: accessoryNode(bonus), }); }; @@ -379,22 +394,25 @@ const setCardInfo: EPR = async (info, data, send) => { d.accessory = ($(data).numbers('accessory_seq') || []).concat([0, 0]).slice(0, 2); await saveDoll(d); - // kira_type 0 normal, 1 star, 2 gold, 3 star while starkira_free is on + // kira_type 0 normal, 1 star, 2 gold, 3 star while starkira_free is on. + // A kira card gives +1 to 1-3 random stats (wiki); the card shows each as one digit. const kira_type = $(data).number('kira_type', 0); const kira = [0, 0, 0, 0]; - // guess: the real distribution is unknown; values must stay 0-9 (one digit on the card) - const star = kira_type === 1 || kira_type === 3; - const slots = kira_type === 2 ? [0, 1, 2, 3] : star ? [0, 1, 2, 3].sort(() => Math.random() - 0.5).slice(0, 2) : []; - for (const k of slots) kira[k] = kira_type === 2 ? 1 + rand(5) : 1 + rand(3); + if (kira_type) for (const k of [0, 1, 2, 3].sort(() => Math.random() - 0.5).slice(0, 1 + rand(3))) kira[k] = 1; if (kira_type === 1) u.powder_num = Math.max(0, u.powder_num - 1); // the game spends one star powder locally const decoseal_id = $(data).number('decoseal_id', 0); if (decoseal_id && u.decoseal[decoseal_id]) u.decoseal[decoseal_id]--; // guess: nothing else reports it await updateRelease(u); await saveUser(u); + // Snapshot the worn accessories. One sold since comes from the card this play started with. + const from_otocard_id = $(data).str('otocard_id', ''); + const from = from_otocard_id ? await DB.FindOne({ collection: 'card', otocard_id: from_otocard_id }) : null; + const accessory = d.accessory.map(s => (s && (u.accessory.find(a => a.aseq === s) || + (from && from.accessory.find(a => a.aseq === s)))) || EMPTY_ACCESSORY); const card: Card = { collection: 'card', otocard_id: await newId(), inquire_id: await newId(), user_seq: u.user_seq, - doll_seq: d.doll_seq, kira_type, kira, decoseal_id, from_otocard_id: $(data).str('otocard_id', ''), created: now(), + doll_seq: d.doll_seq, kira_type, kira, equip: d.equip, accessory, decoseal_id, from_otocard_id, created: now(), }; await DB.Insert(card); log('setCardInfo', { otocard_id: card.otocard_id, kira_type, kira }); @@ -451,7 +469,11 @@ const addItem: EPR = async (info, data, send) => { const made = $(data).elements('item').map(i => giveItem(u, i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0)) || EMPTY_ACCESSORY); const gift = $(data).str('gift_code', ''); - if ($(data).number('gift_type', 0) === 4 && gift && !u.gifts_used.includes(gift)) u.gifts_used.push(gift); + // gift_type 4: the present for an offline card, which also gives one stamp (official notice) + if ($(data).number('gift_type', 0) === 4 && gift && !u.gifts_used.includes(gift)) { + u.gifts_used.push(gift); + u.stamp_bonus++; + } await saveUser(u); send.object({ accessory: made.map(accessoryNode) }); }; @@ -522,7 +544,9 @@ const setMakeup: EPR = async (info, data, send) => { const addStamp: EPR = async (info, data, send) => { const u = await getUser($(data).number('user_seq')); if (!u) return send.deny(); - const after = u.stamp_num + (U.GetConfig('stamp_double') ? 2 : 1); // guess: one stamp per play + // one stamp per play (official), plus stamps from gifts; the game animates old -> after one by one + const after = u.stamp_num + (U.GetConfig('stamp_double') ? 2 : 1) + u.stamp_bonus; + u.stamp_bonus = 0; const full = Math.floor(after / 10); u.stamp_num = after % 10; u.powder_num += full; // guess: one powder per full card @@ -541,6 +565,8 @@ const reportMission: EPR = async (info, data, send) => { const n = $(data).number('mission_no', 0); if (u && u.mission_no === n) { u.mission_no = Math.min(n + 1, 9); // the game treats every mission below mission_no as done + // all 8 letters: the original wallpaper (closet/cardbg adds to the wallpapers the game offers) + if (u.mission_no === 9 && !u.cardbg.includes(LETTER_WALLPAPER) && u.cardbg.length < 4) u.cardbg.push(LETTER_WALLPAPER); await saveUser(u); } send.success(); @@ -608,6 +634,12 @@ export function register() { type: 'boolean', default: false, }); + R.Config('trial_shop', { + name: 'Salon trial ticket', + desc: 'One free hair/eye colour change in the salon for each player (as in the arcade).', + type: 'boolean', + default: true, + }); R.Config('starkira_free', { name: 'Free star kira', desc: 'Star kira cards need no star powder.', From 7518cd2abc51fc67c04f4a091c08c67e72155414 Mon Sep 17 00:00:00 2001 From: iamsub <294696795+iamiamsub@users.noreply.github.com> Date: Tue, 29 Sep 2026 15:23:48 +0900 Subject: [PATCH 5/7] otoca: split the plugin into handlers, models and helpers index.ts now only registers settings and routes. Handlers are grouped by part of the game (version, card, key, print, play, item, gift), the stored documents live in models/, and the closet, player creation, db access, game data and helpers each have their own file. No behavior change: the test flow gives the same results. --- otoca@asphyxia/README.md | 6 + otoca@asphyxia/closet.ts | 70 ++++ otoca@asphyxia/data.ts | 27 ++ otoca@asphyxia/db.ts | 36 ++ otoca@asphyxia/handlers/card.ts | 92 +++++ otoca@asphyxia/handlers/gift.ts | 21 + otoca@asphyxia/handlers/item.ts | 84 ++++ otoca@asphyxia/handlers/key.ts | 31 ++ otoca@asphyxia/handlers/play.ts | 86 ++++ otoca@asphyxia/handlers/print.ts | 56 +++ otoca@asphyxia/handlers/version.ts | 21 + otoca@asphyxia/index.ts | 640 +---------------------------- otoca@asphyxia/models/card.ts | 18 + otoca@asphyxia/models/counter.ts | 5 + otoca@asphyxia/models/doll.ts | 13 + otoca@asphyxia/models/user.ts | 39 ++ otoca@asphyxia/player.ts | 48 +++ otoca@asphyxia/utils.ts | 9 + 18 files changed, 684 insertions(+), 618 deletions(-) create mode 100644 otoca@asphyxia/closet.ts create mode 100644 otoca@asphyxia/data.ts create mode 100644 otoca@asphyxia/db.ts create mode 100644 otoca@asphyxia/handlers/card.ts create mode 100644 otoca@asphyxia/handlers/gift.ts create mode 100644 otoca@asphyxia/handlers/item.ts create mode 100644 otoca@asphyxia/handlers/key.ts create mode 100644 otoca@asphyxia/handlers/play.ts create mode 100644 otoca@asphyxia/handlers/print.ts create mode 100644 otoca@asphyxia/handlers/version.ts create mode 100644 otoca@asphyxia/models/card.ts create mode 100644 otoca@asphyxia/models/counter.ts create mode 100644 otoca@asphyxia/models/doll.ts create mode 100644 otoca@asphyxia/models/user.ts create mode 100644 otoca@asphyxia/player.ts create mode 100644 otoca@asphyxia/utils.ts diff --git a/otoca@asphyxia/README.md b/otoca@asphyxia/README.md index 29d1998..d1a3a2d 100644 --- a/otoca@asphyxia/README.md +++ b/otoca@asphyxia/README.md @@ -27,6 +27,12 @@ How it works - Without a camera, printed card images can be scanned with a hook that feeds an image to the game's camera library (libcamera.dll) in place of the camera frame. +Code layout + - `index.ts`: settings and routes. `handlers/`: one file per part of the game (version, card, key, + print, play, item, gift). `models/`: the stored documents (user, doll, card, counter). + - `closet.ts`: adding and removing items, and the closet's response nodes. `player.ts`: new players + and dolls. `db.ts`: ids and reads/writes. `data.ts`: values from the game's data. `utils.ts`: helpers. + Settings - Version marker: unlocks hair/eye colours in the salon and the mode-select tips (the game checks 1 to 8). - Salon trial ticket: one free hair/eye colour change for each player (on by default, as in the arcade). diff --git a/otoca@asphyxia/closet.ts b/otoca@asphyxia/closet.ts new file mode 100644 index 0000000..e83f6ad --- /dev/null +++ b/otoca@asphyxia/closet.ts @@ -0,0 +1,70 @@ +// The player's closet: equips, materials, deco seals and accessories, and their response nodes. +import { ACCESSORY_IDS, ACCESSORY_OFFSET } from './data'; +import { saveDoll } from './db'; +import { Doll } from './models/doll'; +import { Accessory, User } from './models/user'; +import { log, rand, u8, u32 } from './utils'; + +export const isAccessory = (type: number, id: number) => + type === 0 && ACCESSORY_IDS.some(([a, b]) => id >= a && id <= b); + +export function addCount(bag: { [id: string]: number[] }, id: number, grade: number, n: number) { + if (!id || grade > 2) return; + const gr = bag[id] || [0, 0, 0]; + gr[grade] = Math.max(0, gr[grade] + n); + if (gr.some(c => c > 0)) bag[id] = gr; + else delete bag[id]; +} + +export function makeAccessory(u: User, id: number, grade: number): Accessory { + const gr = Math.min(grade, 2); + const a = { aseq: u.next_aseq++, id, gr, offset: ACCESSORY_OFFSET[gr].map(([lo, hi]) => lo + rand(hi - lo + 1)) }; + u.accessory.push(a); + return a; +} + +// item_type 0 equip (kind 4 = accessory), 1 material, 2 decoseal. Returns the accessory made, if any. +export function giveItem(u: User, type: number, id: number, grade: number): Accessory | null { + if (isAccessory(type, id)) return makeAccessory(u, id, grade); + if (type === 0) addCount(u.equip, id, grade, 1); + else if (type === 1) addCount(u.material, id, grade, 1); + else if (type === 2 && id) u.decoseal[id] = (u.decoseal[id] || 0) + 1; + else log(`unknown item type=${type} id=${id} grade=${grade}`, {}); + return null; +} + +async function removeAccessory(u: User, aseq: number) { + u.accessory = u.accessory.filter(a => a.aseq !== aseq); + // the game also takes a removed accessory off the doll wearing it + for (const d of await DB.Find({ collection: 'doll', user_seq: u.user_seq })) { + if (d.accessory.includes(aseq)) { + d.accessory = d.accessory.map(s => (s === aseq ? 0 : s)); + await saveDoll(d); + } + } +} + +export async function takeItem(u: User, aseq: number, type: number, id: number, grade: number, n: number) { + if (aseq) await removeAccessory(u, aseq); + else if (type === 0) addCount(u.equip, id, grade, -n); + else if (type === 1) addCount(u.material, id, grade, -n); +} + +export const EMPTY_ACCESSORY: Accessory = { aseq: 0, id: 0, gr: 0, offset: [0, 0, 0, 0] }; +export const accessoryNode = (a: Accessory) => ({ + accessory_seq: u32(a.aseq), equip_id: u32(a.id), equip_grade: u8(a.gr), + param_offset: u8(a.offset[0]), skill_offset_1: u8(a.offset[1]), + skill_offset_2: u8(a.offset[2]), skill_offset_3: u8(a.offset[3]), +}); + +// compact form (getCardInfo ver="1"): one per id, gr[g] = count of grade g, -1 = none +export const gradeList = (bag: { [id: string]: number[] }) => + Object.keys(bag).map(id => ({ id: u32(+id), gr: K.ARRAY('s16', bag[id].map(c => (c > 0 ? c : -1))) })); + +// The game counts accessories in the equip list as well (AddAccessory adds to both). +export function equipWithAccessories(u: User) { + const bag: { [id: string]: number[] } = {}; + for (const id of Object.keys(u.equip)) bag[id] = u.equip[id].slice(); + for (const a of u.accessory) addCount(bag, a.id, a.gr, 1); + return bag; +} diff --git a/otoca@asphyxia/data.ts b/otoca@asphyxia/data.ts new file mode 100644 index 0000000..aa37d43 --- /dev/null +++ b/otoca@asphyxia/data.ts @@ -0,0 +1,27 @@ +// Values taken from the game's data (data_ncg/paramdb) and the arcade's rules. + +// versiondata.csv: base 5-2, newest 5-11. getCardInfo's version is applied the same way, so both send this. +export const EPISODE = 5; +export const PHASE = 11; +// Alphabet of the ids the game packs into its QR codes (5 bits per char). +export const ID_CHARS = '0123456789ABCDEFGHJKLMNPRSTUWXYZ'; +// equipdata.csv rows with kind 4 (accessories): they carry an accessory_seq. +export const ACCESSORY_IDS: number[][] = [ + [41, 52], [144, 203], [206, 212], [293, 347], [351, 356], [360, 361], [365, 366], + [470, 531], [535, 536], [584, 609], [619, 626], [648, 664], [696, 720], [731, 740], +]; +// dollinitdata.csv: starting equips (weapon, top, bottom, shoes) per doll_id. +export const DOLL_INIT: { [doll_id: number]: number[] } = { + 1: [9, 18, 29, 40], 2: [9, 20, 31, 40], 3: [9, 17, 28, 40], + 4: [9, 19, 30, 40], 5: [9, 21, 32, 40], 6: [9, 409, 438, 40], +}; +// Accessory stat / skill 1-3 percent ranges per grade N, R, SR (wiki item list: accessories vary per piece). +export const ACCESSORY_OFFSET = [ + [[60, 80], [80, 100], [50, 100], [0, 0]], + [[70, 90], [100, 100], [80, 100], [40, 50]], + [[80, 100], [100, 100], [100, 100], [80, 100]], +]; +export const FIRST_KEY_ACCESSORY = 144; // ピンクマーチリボン, made in the tailor tutorial when the first key is made +export const LETTER_WALLPAPER = 29; // guess: card_bg_0018 (nyandora night sky), the reward for all 8 letters +export const TEMP_ASEQ = 1000000000; // the game's own placeholder seqs for this play's drops (sell scene) +export const MAX_BALANCE = 9999999; diff --git a/otoca@asphyxia/db.ts b/otoca@asphyxia/db.ts new file mode 100644 index 0000000..e765190 --- /dev/null +++ b/otoca@asphyxia/db.ts @@ -0,0 +1,36 @@ +import { ID_CHARS } from './data'; +import { Card } from './models/card'; +import { Counter } from './models/counter'; +import { Doll } from './models/doll'; +import { User } from './models/user'; +import { rand } from './utils'; + +export async function nextSeq(name: string) { + const c = await DB.FindOne({ collection: 'counter', name }); + const value = (c ? c.value : 0) + 1; + await DB.Upsert({ collection: 'counter', name }, { $set: { value } }); + return value; +} + +// ponytail: random ids with a uniqueness check, fine for a home server's card count. +export async function newId() { + for (;;) { + let id = ''; + for (let i = 0; i < 8; i++) id += ID_CHARS[rand(ID_CHARS.length)]; + if (id === '00000000') continue; + const used = (await DB.Count({ collection: 'card', otocard_id: id })) + + (await DB.Count({ collection: 'user', keycard_id: id })); + if (!used) return id; + } +} + +export const getUser = (user_seq: number) => DB.FindOne({ collection: 'user', user_seq }); +export const getDoll = (doll_seq: number) => DB.FindOne({ collection: 'doll', doll_seq }); +export const getCard = (otocard_id: string) => DB.FindOne({ collection: 'card', otocard_id }); + +async function save(key: object, doc: object) { + const { _id, ...rest } = doc as any; + await DB.Update(key, { $set: rest }); +} +export const saveUser = (u: User) => save({ collection: 'user', user_seq: u.user_seq }, u); +export const saveDoll = (d: Doll) => save({ collection: 'doll', doll_seq: d.doll_seq }, d); diff --git a/otoca@asphyxia/handlers/card.ts b/otoca@asphyxia/handlers/card.ts new file mode 100644 index 0000000..470e8b4 --- /dev/null +++ b/otoca@asphyxia/handlers/card.ts @@ -0,0 +1,92 @@ +// New players and dolls, and scanning a printed card. +import { accessoryNode, equipWithAccessories, gradeList } from '../closet'; +import { EPISODE, PHASE } from '../data'; +import { getCard, getDoll, getUser, nextSeq, saveUser } from '../db'; +import { greeting, newDoll, newUser } from '../player'; +import { bool, log, now, u8, u32 } from '../utils'; + +export const addUser: EPR = async (info, data, send) => { + const u = newUser(await nextSeq('user'), $(data).str('user_name', '')); + const d = await newDoll(u, $(data).number('doll_id', 0), $(data).str('doll_name', '')); + await DB.Insert(u); + send.object({ + user_seq: u32(u.user_seq), + doll_seq: u32(d.doll_seq), + doll_seed: u32(d.doll_seed), + powder_num: u32(u.powder_num), + stamp_num: u32(u.stamp_num), + mission_no: u8(u.mission_no), + question_id: u8(u.question_id), + ticket_rc_first: bool(u.ticket_rc_first), + }); +}; + +export const addDoll: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + const d = await newDoll(u, $(data).number('doll_id', 0), $(data).str('doll_name', '')); + await saveUser(u); + send.object({ doll_seq: u32(d.doll_seq), doll_seed: u32(d.doll_seed) }); +}; + +export const getCardInfo: EPR = async (info, data, send) => { + const otocard_id = $(data).str('otocard_id', ''); + const card = await getCard(otocard_id); + const u = card && (await getUser(card.user_seq)); + const d = card && (await getDoll(card.doll_seq)); + if (!u || !d) { + // status 1: the game plays offline with what the QR holds + log(`getCardInfo: unknown card ${otocard_id}`, {}); + return send.deny(); + } + const worn = card.accessory; + send.object({ + user_seq: u32(u.user_seq), + user_name: K.ITEM('str', u.user_name), + doll_seq: u32(d.doll_seq), + doll_id: u32(d.doll_id), + doll_name: K.ITEM('str', d.doll_name), + doll_level: u32(d.doll_level), + doll_seed: u32(d.doll_seed), + locked: bool(!!u.keycard_id), // tied to a key card: reads the key next, closet limit 999 + luck_offset: u32(d.luck_offset), + equip: [...card.equip, ...worn.map(a => [a.id, a.gr])].map(e => ({ equip_id: u32(e[0]), equip_grade: u8(e[1]) })), + accessory: worn.map(accessoryNode), + score: { + release_state: u32(u.release_state), + stamp_conv_num: u32(u.stamp_conv_num), + user_flags: K.ARRAY('u8', u.user_flags), + last_time: K.ITEM('time', u.last_time), + curr_time: K.ITEM('time', now()), + greeting_level: u8(greeting(u.last_time)), + user_marker: u8(u.user_marker), + mission_no: u8(u.mission_no), + setting: { sort_type: u8(u.sort_type), disp_skill: u8(u.disp_skill) }, + question_id: u8(u.question_id), + enemy: u.enemy.map(e => ({ enemy_team_id: u32(e[0]), enemy_team_level: u32(e[1]) })), + last_enemy: u.last_enemy.map(e => ({ episode: u8(e[0]), enemy_team_id: u32(e[1]), enemy_team_level: u32(e[2]) })), + enemy_detail: { i: Object.keys(u.friendly).map(t => ({ id: K.ITEM('u16', +t), pt: u32(u.friendly[t]) })) }, + }, + // the bonus printed on this card; the game applies it to one battle + offset: { kira_hp: u8(card.kira[0]), kira_atk: u8(card.kira[1]), kira_mat: u8(card.kira[2]), kira_spd: u8(card.kira[3]) }, + active_key: bool(!!u.keycard_id), + version: { episode: u8(EPISODE), phase: u8(PHASE) }, + makeup: { hair_color: u8(d.makeup[0]), eye_color: u8(d.makeup[1]) }, + closet: { + powder_num: u32(u.powder_num), + stamp_num: u32(u.stamp_num), + wallet: { + balance: u32(u.balance), + ticket_half_price: bool(u.ticket_half_price), + ticket_rc_first: bool(u.ticket_rc_first), + }, + equip: { i: gradeList(equipWithAccessories(u)) }, + material: { i: gradeList(u.material) }, + decoseal: Object.keys(u.decoseal).slice(0, 1).map(id => ({ decoseal_id: u32(+id), decoseal_num: u32(u.decoseal[id]) })), + accessory: { + i: u.accessory.map(a => ({ aseq: u32(a.aseq), id: u32(a.id), gr: u8(a.gr), offset: K.ARRAY('u8', a.offset) })), + }, + cardbg: u.cardbg.slice(0, 4).map(id => ({ id: u8(id) })), + }, + }); +}; diff --git a/otoca@asphyxia/handlers/gift.ts b/otoca@asphyxia/handlers/gift.ts new file mode 100644 index 0000000..18d3c94 --- /dev/null +++ b/otoca@asphyxia/handlers/gift.ts @@ -0,0 +1,21 @@ +// Gift QRs scanned after the doll card. +import { getUser } from '../db'; +import { u8, u32 } from '../utils'; + +// Only type 4 (the offline card the game prints when setCardInfo fails) is honoured. +// The apology gift itself is taken later by addItem with gift_type 4. Past campaigns answer 5 (expired). +export const receiveGift: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + const gift = $(data).element('info'); + const type = gift ? gift.number('gift_type', 0) : 0; + const code = gift ? gift.str('gift_code', '') : ''; + const status = type !== 4 ? 5 : u && u.gifts_used.includes(code) ? 2 : 1; + send.object({ + powder_num: u32(u ? u.powder_num : 0), // always overwrites the game's count + status: [{ gift_type: u8(type), gift_status: u8(status) }], + }); +}; + +export const receiveItem: EPR = async (info, data, send) => { + send.object({ gift_type: u8(7), gift_status: u8(5) }); +}; diff --git a/otoca@asphyxia/handlers/item.ts b/otoca@asphyxia/handlers/item.ts new file mode 100644 index 0000000..e033e42 --- /dev/null +++ b/otoca@asphyxia/handlers/item.ts @@ -0,0 +1,84 @@ +// Closet changes: drops, discarding, selling, the tailor and the salon. +import { accessoryNode, EMPTY_ACCESSORY, giveItem, isAccessory, takeItem } from '../closet'; +import { MAX_BALANCE, TEMP_ASEQ } from '../data'; +import { getDoll, getUser, saveDoll, saveUser } from '../db'; +import { Accessory } from '../models/user'; +import { u32 } from '../utils'; + +export const addItem: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + // accessory[i] must line up with item[i]; rows that are not accessories stay zero + const made = $(data).elements('item').map(i => + giveItem(u, i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0)) || EMPTY_ACCESSORY); + const gift = $(data).str('gift_code', ''); + // gift_type 4: the present for an offline card, which also gives one stamp (official notice) + if ($(data).number('gift_type', 0) === 4 && gift && !u.gifts_used.includes(gift)) { + u.gifts_used.push(gift); + u.stamp_bonus++; + } + await saveUser(u); + send.object({ accessory: made.map(accessoryNode) }); +}; + +// item: what the player threw away (one row per piece). drop: this play's drops that are kept. +export const delItem: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + for (const i of $(data).elements('drop')) giveItem(u, i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0)); + for (const i of $(data).elements('item')) { + await takeItem(u, i.number('accessory_seq', 0), i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), 1); + } + await saveUser(u); + send.success(); +}; + +export const sellItem: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + const items = $(data).elements('item'); + const soldTemp = items.map(i => i.number('accessory_seq', 0)).filter(s => s >= TEMP_ASEQ); + // drops first (sell scene after a full closet): a dropped accessory sold right away comes back as TEMP_ASEQ + n + const made: Accessory[] = []; + $(data).elements('drop').forEach((i, n) => { + const type = i.number('item_type', 0), id = i.number('item_id', 0), grade = i.number('item_grade', 0); + if (isAccessory(type, id) && soldTemp.includes(TEMP_ASEQ + n)) return; + const a = giveItem(u, type, id, grade); + if (a) made.push(a); + }); + for (const i of items) { + const aseq = i.number('accessory_seq', 0); + if (aseq >= TEMP_ASEQ) continue; + await takeItem(u, aseq, i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), i.number('item_num', 1)); + } + // the game computes the balance after selling (prices from pricelist.csv) and never reads ours + u.balance = Math.max(0, Math.min(MAX_BALANCE, $(data).number('balance', u.balance))); + await saveUser(u); + send.object({ balance: u32(u.balance), accessory: made.map(accessoryNode) }); +}; + +export const composition: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + for (const k of ['item_in_1', 'item_in_2']) { + const i = $(data).element(k); + if (i) await takeItem(u, i.number('accessory_seq', 0), i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), 1); + } + const r = $(data).element('item_result'); + const made = r ? giveItem(u, r.number('item_type', 0), r.number('item_id', 0), r.number('item_grade', 0)) : null; + await saveUser(u); + send.object({ accessory: accessoryNode(made || EMPTY_ACCESSORY) }); +}; + +export const setMakeup: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + const d = await getDoll($(data).number('doll_seq')); + if (!u || !d) return send.deny(); + d.makeup = [$(data).number('makeup.hair_color', 0), $(data).number('makeup.eye_color', 0)]; + // payment is only the last purchase; the game sends the balance after paying + u.balance = Math.max(0, Math.min(MAX_BALANCE, $(data).number('balance', u.balance))); + if ($(data).number('ticket_type', 0) === 1) u.ticket_half_price = false; + await saveDoll(d); + await saveUser(u); + send.object({ balance: u32(u.balance) }); +}; diff --git a/otoca@asphyxia/handlers/key.ts b/otoca@asphyxia/handlers/key.ts new file mode 100644 index 0000000..855588e --- /dev/null +++ b/otoca@asphyxia/handlers/key.ts @@ -0,0 +1,31 @@ +// The key card (ニャンドラのカギ): a second printed card tied to the player. +import { accessoryNode, EMPTY_ACCESSORY, makeAccessory } from '../closet'; +import { FIRST_KEY_ACCESSORY } from '../data'; +import { getUser, newId, saveUser } from '../db'; +import { log } from '../utils'; + +export const checkKeyUser: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + const keycard_id = $(data).str('keycard_id', ''); + if (!u || !u.keycard_id || u.keycard_id !== keycard_id) { + log('checkKeyUser: not this user\'s key', { keycard_id, bound: u && u.keycard_id }); + return send.status(10); // the game shows "key does not match" and lets the player rescan + } + send.object({ keycard_id: K.ITEM('str', u.keycard_id), key_inquire_id: K.ITEM('str', u.key_inquire_id) }); +}; + +// A new key replaces the old one (first key or "remake"). The id goes into the key card's QR. +export const bindKeyUser: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + u.keycard_id = await newId(); + u.key_inquire_id = await newId(); + // the first key comes with the tailor tutorial's result; the game adds it only when is_first is set + const bonus = $(data).bool('is_first') ? makeAccessory(u, FIRST_KEY_ACCESSORY, 0) : EMPTY_ACCESSORY; + await saveUser(u); + send.object({ + keycard_id: K.ITEM('str', u.keycard_id), + key_inquire_id: K.ITEM('str', u.key_inquire_id), + accessory: accessoryNode(bonus), + }); +}; diff --git a/otoca@asphyxia/handlers/play.ts b/otoca@asphyxia/handlers/play.ts new file mode 100644 index 0000000..735c846 --- /dev/null +++ b/otoca@asphyxia/handlers/play.ts @@ -0,0 +1,86 @@ +// What a play reports: battle results, gold and friend points, stamps, letters and questionnaires. +import { LETTER_WALLPAPER, MAX_BALANCE } from '../data'; +import { getDoll, getUser, saveDoll, saveUser } from '../db'; +import { updateRelease } from '../player'; +import { now, u32 } from '../utils'; + +export const report: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.success(); + const b = $(data).element('battle'); + if (b) { + const team = b.number('enemy_team_id', 0); + const level = b.number('enemy_team_level', 0); + const episode = b.number('episode', 0); + // battle_result 0 win, 1 lose, 2 draw; the game adds a beaten pair to score/enemy only on a win + if (team && b.number('battle_result', 1) === 0 && u.enemy.length < 200 && + !u.enemy.some(e => e[0] === team && e[1] === level)) u.enemy.push([team, level]); + if (team) u.last_enemy = [[episode, team, level], ...u.last_enemy.filter(e => e[0] !== episode)].slice(0, 6); + } + u.user_flags = $(data).numbers('user_flags', u.user_flags); + u.user_marker = $(data).number('user_marker', u.user_marker); + u.sort_type = $(data).number('setting.sort_type', u.sort_type); + u.disp_skill = $(data).number('setting.disp_skill', u.disp_skill); + u.last_time = now(); + const d = await getDoll($(data).number('doll_seq')); + if (d && d.user_seq === u.user_seq) { + d.doll_level = Math.max(1, Math.min(31, $(data).number('doll_level', d.doll_level))); + await saveDoll(d); + } + await updateRelease(u); + await saveUser(u); + send.success(); +}; + +// Only gold and friend points: doll levels are raised by the game itself. +export const addExp: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + u.balance = Math.min(MAX_BALANCE, u.balance + $(data).number('gold.add_amount', 0)); + const team = $(data).number('friendly.enemy_team_id', 0); + if (team) u.friendly[team] = (u.friendly[team] || 0) + $(data).number('friendly.add_point', 0); + await saveUser(u); + send.object({ balance: u32(u.balance), friend_point: u32(team ? u.friendly[team] : 0) }); +}; + +// The stamp card has 10 squares; a full card turns into star powder. +export const addStamp: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (!u) return send.deny(); + // one stamp per play (official), plus stamps from gifts; the game animates old -> after one by one + const after = u.stamp_num + (U.GetConfig('stamp_double') ? 2 : 1) + u.stamp_bonus; + u.stamp_bonus = 0; + const full = Math.floor(after / 10); + u.stamp_num = after % 10; + u.powder_num += full; // one powder per full card (official) + u.stamp_conv_num += full; + await saveUser(u); + send.object({ + powder_num: u32(u.powder_num), + stamp_num: u32(u.stamp_num), + stamp_conv_rate: u32(10), // squares per card; the game divides by it + stamp_after_num: u32(after), + }); +}; + +export const reportMission: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + const n = $(data).number('mission_no', 0); + if (u && u.mission_no === n) { + u.mission_no = Math.min(n + 1, 9); // the game treats every mission below mission_no as done + // all 8 letters: the original wallpaper (closet/cardbg adds to the wallpapers the game offers) + if (u.mission_no === 9 && !u.cardbg.includes(LETTER_WALLPAPER) && u.cardbg.length < 4) u.cardbg.push(LETTER_WALLPAPER); + await saveUser(u); + } + send.success(); +}; + +// score/question_id is the questionnaire still to show; no questionnaires are offered, so it stays 0. +export const reportQuestion: EPR = async (info, data, send) => { + const u = await getUser($(data).number('user_seq')); + if (u) { + u.question_id = 0; + await saveUser(u); + } + send.success(); +}; diff --git a/otoca@asphyxia/handlers/print.ts b/otoca@asphyxia/handlers/print.ts new file mode 100644 index 0000000..80e6dbb --- /dev/null +++ b/otoca@asphyxia/handlers/print.ts @@ -0,0 +1,56 @@ +// Printing doll cards and rival cards. +import { EMPTY_ACCESSORY } from '../closet'; +import { getCard, getDoll, getUser, newId, saveDoll, saveUser } from '../db'; +import { Card } from '../models/card'; +import { updateRelease } from '../player'; +import { log, now, rand, u8 } from '../utils'; + +export const setCardInfo: EPR = async (info, data, send) => { + const d = await getDoll($(data).number('doll_seq')); + const u = d && (await getUser(d.user_seq)); + if (!u || !d) return send.deny(); + d.doll_level = Math.max(1, Math.min(31, $(data).number('doll_level', d.doll_level))); + d.doll_name = $(data).str('doll_name', d.doll_name); + d.equip = $(data).elements('equip').slice(0, 4).map(e => [e.number('equip_id', 0), e.number('equip_grade', 0)]); + d.accessory = ($(data).numbers('accessory_seq') || []).concat([0, 0]).slice(0, 2); + await saveDoll(d); + + // kira_type 0 normal, 1 star, 2 gold, 3 star while starkira_free is on. + // A kira card gives +1 to 1-3 random stats (wiki); the card shows each as one digit. + const kira_type = $(data).number('kira_type', 0); + const kira = [0, 0, 0, 0]; + if (kira_type) for (const k of [0, 1, 2, 3].sort(() => Math.random() - 0.5).slice(0, 1 + rand(3))) kira[k] = 1; + if (kira_type === 1) u.powder_num = Math.max(0, u.powder_num - 1); // the game spends one star powder locally + const decoseal_id = $(data).number('decoseal_id', 0); + if (decoseal_id && u.decoseal[decoseal_id]) u.decoseal[decoseal_id]--; // guess: nothing else reports it + await updateRelease(u); + await saveUser(u); + + // Snapshot the worn accessories. One sold since comes from the card this play started with. + const from_otocard_id = $(data).str('otocard_id', ''); + const from = from_otocard_id ? await getCard(from_otocard_id) : null; + const accessory = d.accessory.map(s => (s && (u.accessory.find(a => a.aseq === s) || + (from && from.accessory.find(a => a.aseq === s)))) || EMPTY_ACCESSORY); + const card: Card = { + collection: 'card', otocard_id: await newId(), inquire_id: await newId(), user_seq: u.user_seq, + doll_seq: d.doll_seq, kira_type, kira, equip: d.equip, accessory, decoseal_id, from_otocard_id, created: now(), + }; + await DB.Insert(card); + log('setCardInfo', { otocard_id: card.otocard_id, kira_type, kira }); + send.object({ + otocard_id: K.ITEM('str', card.otocard_id), + inquire_id: K.ITEM('str', card.inquire_id), + offset: { kira_hp: u8(kira[0]), kira_atk: u8(kira[1]), kira_mat: u8(kira[2]), kira_spd: u8(kira[3]) }, + }); +}; + +// The game spends ticket_rc_first itself when a rival card is chosen and tells nobody else. +export const setRivalCard: EPR = async (info, data, send) => { + const card = await getCard($(data).str('otocard_id', '')); + const u = card && (await getUser(card.user_seq)); + if (u && u.ticket_rc_first) { + u.ticket_rc_first = false; + await saveUser(u); + } + send.success(); +}; diff --git a/otoca@asphyxia/handlers/version.ts b/otoca@asphyxia/handlers/version.ts new file mode 100644 index 0000000..4b63f9b --- /dev/null +++ b/otoca@asphyxia/handlers/version.ts @@ -0,0 +1,21 @@ +import { EPISODE, PHASE } from '../data'; +import { bool, u8, u32 } from '../utils'; + +// Sent by ealocal every `expire` seconds from the attract loop; the game waits for the first one at boot. +export const getVersion: EPR = async (info, data, send) => { + const limited = U.GetConfig('limited_enemy') ? 1 : 0; + send.object({ + expire: u32(600), + episode: u8(EPISODE), + phase: u8(PHASE), + marker: u8(U.GetConfig('marker')), + campaign: { + trial_play: bool(false), + trial_shop: bool(U.GetConfig('trial_shop')), // one free salon try per player; the game keeps the flag + starkira_free: bool(U.GetConfig('starkira_free')), + stamp_double: bool(U.GetConfig('stamp_double')), + limited_enemy: K.ARRAY('bool', [0, 0, limited, 0, 0, 0, 0, 0]), // only slot 2 has an enemy (team 63) + }, + question: { id: u8(0), reward: { type: u8(0), id: u32(0), grade: u8(0) } }, + }); +}; diff --git a/otoca@asphyxia/index.ts b/otoca@asphyxia/index.ts index 7c4dab2..766e851 100644 --- a/otoca@asphyxia/index.ts +++ b/otoca@asphyxia/index.ts @@ -1,615 +1,13 @@ // otoca d'or (NCG). The XRPC module "game" (service "local") is implemented by the game's ealocal.dll; -// field names, types and limits follow its psmap tables, and every value below follows how game.dll -// uses it (see README). Values marked "guess" are server policy the game code does not decide. - -interface Accessory { - aseq: number; - id: number; - gr: number; - offset: number[]; // param_offset, skill_offset_1..3 (percent of the equip's base values) -} - -interface User { - collection: 'user'; - user_seq: number; - user_name: string; - keycard_id: string; - key_inquire_id: string; - powder_num: number; - stamp_num: number; - stamp_conv_num: number; - stamp_bonus: number; // stamps from gifts, pressed with the next addStamp - balance: number; - ticket_half_price: boolean; - ticket_rc_first: boolean; - equip: { [id: string]: number[] }; // count per grade 0..2, accessories not included - material: { [id: string]: number[] }; - decoseal: { [id: string]: number }; - accessory: Accessory[]; - next_aseq: number; - cardbg: number[]; - release_state: number; - user_flags: number[]; - last_time: number; - user_marker: number; - mission_no: number; - question_id: number; - sort_type: number; - disp_skill: number; - enemy: number[][]; // [team, level] of every team/level beaten - last_enemy: number[][]; // [episode, team, level], newest first - friendly: { [team: string]: number }; - gifts_used: string[]; -} - -interface Doll { - collection: 'doll'; - doll_seq: number; - user_seq: number; - doll_id: number; - doll_name: string; - doll_seed: number; - doll_level: number; - equip: number[][]; // 4 slots [equip_id, grade]: weapon, top, bottom, shoes - accessory: number[]; // 2 slots of accessory_seq, 0 = empty - makeup: number[]; // hair_color, eye_color - luck_offset: number; -} - -interface Card { - collection: 'card'; - otocard_id: string; - inquire_id: string; - user_seq: number; - doll_seq: number; - kira_type: number; - kira: number[]; // hp, atk, mat, spd bonus printed on this card - // the coord printed on this card: scanning an old card plays the latest doll in that card's coord, - // even with items sold since then - equip: number[][]; - accessory: Accessory[]; - decoseal_id: number; - from_otocard_id: string; - created: number; -} - -interface Counter { - collection: 'counter'; - name: string; - value: number; -} - -// versiondata.csv: base 5-2, newest 5-11. getCardInfo's version is applied the same way, so both send this. -const EPISODE = 5; -const PHASE = 11; -// Alphabet of the ids the game packs into its QR codes (5 bits per char). -const ID_CHARS = '0123456789ABCDEFGHJKLMNPRSTUWXYZ'; -// equipdata.csv rows with kind 4 (accessories): they carry an accessory_seq. -const ACCESSORY_IDS: number[][] = [ - [41, 52], [144, 203], [206, 212], [293, 347], [351, 356], [360, 361], [365, 366], - [470, 531], [535, 536], [584, 609], [619, 626], [648, 664], [696, 720], [731, 740], -]; -// dollinitdata.csv: starting equips (weapon, top, bottom, shoes) per doll_id. -const DOLL_INIT: { [doll_id: number]: number[] } = { - 1: [9, 18, 29, 40], 2: [9, 20, 31, 40], 3: [9, 17, 28, 40], - 4: [9, 19, 30, 40], 5: [9, 21, 32, 40], 6: [9, 409, 438, 40], -}; -// Accessory stat / skill 1-3 percent ranges per grade N, R, SR (wiki item list: accessories vary per piece). -const ACCESSORY_OFFSET = [ - [[60, 80], [80, 100], [50, 100], [0, 0]], - [[70, 90], [100, 100], [80, 100], [40, 50]], - [[80, 100], [100, 100], [100, 100], [80, 100]], -]; -const FIRST_KEY_ACCESSORY = 144; // ピンクマーチリボン, made in the tailor tutorial when the first key is made -const LETTER_WALLPAPER = 29; // guess: card_bg_0018 (nyandora night sky), the reward for all 8 letters -const TEMP_ASEQ = 1000000000; // the game's own placeholder seqs for this play's drops (sell scene) -const MAX_BALANCE = 9999999; - -const now = () => Math.floor(Date.now() / 1000); -const rand = (n: number) => Math.floor(Math.random() * n); -const u8 = (v: number) => K.ITEM('u8', v || 0); -const u32 = (v: number) => K.ITEM('u32', v || 0); -const bool = (v: boolean) => K.ITEM('bool', !!v); -const isAccessory = (type: number, id: number) => - type === 0 && ACCESSORY_IDS.some(([a, b]) => id >= a && id <= b); - -function log(method: string, data: any) { - console.log(`[otoca] ${method} ${JSON.stringify(data)}`); -} - -async function nextSeq(name: string) { - const c = await DB.FindOne({ collection: 'counter', name }); - const value = (c ? c.value : 0) + 1; - await DB.Upsert({ collection: 'counter', name }, { $set: { value } }); - return value; -} - -// ponytail: random ids with a uniqueness check, fine for a home server's card count. -async function newId() { - for (;;) { - let id = ''; - for (let i = 0; i < 8; i++) id += ID_CHARS[rand(ID_CHARS.length)]; - if (id === '00000000') continue; - const used = (await DB.Count({ collection: 'card', otocard_id: id })) + - (await DB.Count({ collection: 'user', keycard_id: id })); - if (!used) return id; - } -} - -const getUser = (user_seq: number) => DB.FindOne({ collection: 'user', user_seq }); -const getDoll = (doll_seq: number) => DB.FindOne({ collection: 'doll', doll_seq }); - -async function save(key: object, doc: object) { - const { _id, ...rest } = doc as any; - await DB.Update(key, { $set: rest }); -} -const saveUser = (u: User) => save({ collection: 'user', user_seq: u.user_seq }, u); -const saveDoll = (d: Doll) => save({ collection: 'doll', doll_seq: d.doll_seq }, d); - -// ---- closet ---------------------------------------------------------------- - -function addCount(bag: { [id: string]: number[] }, id: number, grade: number, n: number) { - if (!id || grade > 2) return; - const gr = bag[id] || [0, 0, 0]; - gr[grade] = Math.max(0, gr[grade] + n); - if (gr.some(c => c > 0)) bag[id] = gr; - else delete bag[id]; -} - -function makeAccessory(u: User, id: number, grade: number): Accessory { - const gr = Math.min(grade, 2); - const a = { aseq: u.next_aseq++, id, gr, offset: ACCESSORY_OFFSET[gr].map(([lo, hi]) => lo + rand(hi - lo + 1)) }; - u.accessory.push(a); - return a; -} - -// item_type 0 equip (kind 4 = accessory), 1 material, 2 decoseal. Returns the accessory made, if any. -function giveItem(u: User, type: number, id: number, grade: number): Accessory | null { - if (isAccessory(type, id)) return makeAccessory(u, id, grade); - if (type === 0) addCount(u.equip, id, grade, 1); - else if (type === 1) addCount(u.material, id, grade, 1); - else if (type === 2 && id) u.decoseal[id] = (u.decoseal[id] || 0) + 1; - else log(`unknown item type=${type} id=${id} grade=${grade}`, {}); - return null; -} - -async function removeAccessory(u: User, aseq: number) { - u.accessory = u.accessory.filter(a => a.aseq !== aseq); - // the game also takes a removed accessory off the doll wearing it - for (const d of await DB.Find({ collection: 'doll', user_seq: u.user_seq })) { - if (d.accessory.includes(aseq)) { - d.accessory = d.accessory.map(s => (s === aseq ? 0 : s)); - await saveDoll(d); - } - } -} - -async function takeItem(u: User, aseq: number, type: number, id: number, grade: number, n: number) { - if (aseq) await removeAccessory(u, aseq); - else if (type === 0) addCount(u.equip, id, grade, -n); - else if (type === 1) addCount(u.material, id, grade, -n); -} - -const EMPTY_ACCESSORY: Accessory = { aseq: 0, id: 0, gr: 0, offset: [0, 0, 0, 0] }; -const accessoryNode = (a: Accessory) => ({ - accessory_seq: u32(a.aseq), equip_id: u32(a.id), equip_grade: u8(a.gr), - param_offset: u8(a.offset[0]), skill_offset_1: u8(a.offset[1]), - skill_offset_2: u8(a.offset[2]), skill_offset_3: u8(a.offset[3]), -}); - -// compact form (getCardInfo ver="1"): one per id, gr[g] = count of grade g, -1 = none -const gradeList = (bag: { [id: string]: number[] }) => - Object.keys(bag).map(id => ({ id: u32(+id), gr: K.ARRAY('s16', bag[id].map(c => (c > 0 ? c : -1))) })); - -// The game counts accessories in the equip list as well (AddAccessory adds to both). -function equipWithAccessories(u: User) { - const bag: { [id: string]: number[] } = {}; - for (const id of Object.keys(u.equip)) bag[id] = u.equip[id].slice(); - for (const a of u.accessory) addCount(bag, a.id, a.gr, 1); - return bag; -} - -// ---- users and dolls ------------------------------------------------------- - -function newUser(user_seq: number, user_name: string): User { - return { - collection: 'user', user_seq, user_name, keycard_id: '', key_inquire_id: '', - // shows the first-play powder message; with the first stamp card this makes the 2 kira cards - // the wiki gives players without a key - powder_num: 1, - stamp_num: 0, stamp_conv_num: 0, stamp_bonus: 0, balance: 0, ticket_half_price: false, - ticket_rc_first: true, // the first rival card print guarantees a higher rarity (wiki) - equip: {}, material: {}, decoseal: {}, accessory: [], next_aseq: 1, cardbg: [], - release_state: 0, user_flags: [0, 0, 0, 0], last_time: 0, user_marker: 0, - mission_no: 1, // 0 would never start the missions - question_id: 0, sort_type: 0, disp_skill: 0, enemy: [], last_enemy: [], friendly: {}, gifts_used: [], - }; -} - -// Same as the game does locally: level 1, starting equips (grade 0), which also go into the closet. -async function newDoll(u: User, doll_id: number, doll_name: string): Promise { - const init = DOLL_INIT[doll_id] || [0, 0, 0, 0]; - const doll: Doll = { - collection: 'doll', doll_seq: await nextSeq('doll'), user_seq: u.user_seq, doll_id, doll_name, - doll_seed: rand(23328), // growth type + 3 * five stat offsets in base 6; 23328 and up disables growth - doll_level: 1, equip: init.map(id => [id, 0]), accessory: [0, 0], makeup: [0, 0], luck_offset: 0, - }; - for (const id of init) addCount(u.equip, id, 0, 1); - await DB.Insert(doll); - return doll; -} - -// "New doll" on the entry menu stays disabled while release_state is 0 (label disable_lv5). -async function updateRelease(u: User) { - const dolls = await DB.Find({ collection: 'doll', user_seq: u.user_seq }); - if (dolls.some(d => d.doll_level >= 5)) u.release_state = 1; -} - -// guess: 1 = played again within a day, 2 = back after two weeks, 0 = otherwise -function greeting(last: number) { - if (!last) return 0; - const days = (now() - last) / 86400; - return days < 1 ? 1 : days >= 14 ? 2 : 0; -} - -// ---- handlers -------------------------------------------------------------- - -const getVersion: EPR = async (info, data, send) => { - const limited = U.GetConfig('limited_enemy') ? 1 : 0; - send.object({ - expire: u32(600), - episode: u8(EPISODE), - phase: u8(PHASE), - marker: u8(U.GetConfig('marker')), - campaign: { - trial_play: bool(false), - trial_shop: bool(U.GetConfig('trial_shop')), // one free salon try per player; the game keeps the flag - starkira_free: bool(U.GetConfig('starkira_free')), - stamp_double: bool(U.GetConfig('stamp_double')), - limited_enemy: K.ARRAY('bool', [0, 0, limited, 0, 0, 0, 0, 0]), // only slot 2 has an enemy (team 63) - }, - question: { id: u8(0), reward: { type: u8(0), id: u32(0), grade: u8(0) } }, - }); -}; - -const addUser: EPR = async (info, data, send) => { - const u = newUser(await nextSeq('user'), $(data).str('user_name', '')); - const d = await newDoll(u, $(data).number('doll_id', 0), $(data).str('doll_name', '')); - await DB.Insert(u); - send.object({ - user_seq: u32(u.user_seq), - doll_seq: u32(d.doll_seq), - doll_seed: u32(d.doll_seed), - powder_num: u32(u.powder_num), - stamp_num: u32(u.stamp_num), - mission_no: u8(u.mission_no), - question_id: u8(u.question_id), - ticket_rc_first: bool(u.ticket_rc_first), - }); -}; - -const addDoll: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (!u) return send.deny(); - const d = await newDoll(u, $(data).number('doll_id', 0), $(data).str('doll_name', '')); - await saveUser(u); - send.object({ doll_seq: u32(d.doll_seq), doll_seed: u32(d.doll_seed) }); -}; - -const getCardInfo: EPR = async (info, data, send) => { - const otocard_id = $(data).str('otocard_id', ''); - const card = await DB.FindOne({ collection: 'card', otocard_id }); - const u = card && (await getUser(card.user_seq)); - const d = card && (await getDoll(card.doll_seq)); - if (!u || !d) { - // status 1: the game plays offline with what the QR holds - log(`getCardInfo: unknown card ${otocard_id}`, {}); - return send.deny(); - } - const worn = card.accessory; - send.object({ - user_seq: u32(u.user_seq), - user_name: K.ITEM('str', u.user_name), - doll_seq: u32(d.doll_seq), - doll_id: u32(d.doll_id), - doll_name: K.ITEM('str', d.doll_name), - doll_level: u32(d.doll_level), - doll_seed: u32(d.doll_seed), - locked: bool(!!u.keycard_id), // tied to a key card: reads the key next, closet limit 999 - luck_offset: u32(d.luck_offset), - equip: [...card.equip, ...worn.map(a => [a.id, a.gr])].map(e => ({ equip_id: u32(e[0]), equip_grade: u8(e[1]) })), - accessory: worn.map(accessoryNode), - score: { - release_state: u32(u.release_state), - stamp_conv_num: u32(u.stamp_conv_num), - user_flags: K.ARRAY('u8', u.user_flags), - last_time: K.ITEM('time', u.last_time), - curr_time: K.ITEM('time', now()), - greeting_level: u8(greeting(u.last_time)), - user_marker: u8(u.user_marker), - mission_no: u8(u.mission_no), - setting: { sort_type: u8(u.sort_type), disp_skill: u8(u.disp_skill) }, - question_id: u8(u.question_id), - enemy: u.enemy.map(e => ({ enemy_team_id: u32(e[0]), enemy_team_level: u32(e[1]) })), - last_enemy: u.last_enemy.map(e => ({ episode: u8(e[0]), enemy_team_id: u32(e[1]), enemy_team_level: u32(e[2]) })), - enemy_detail: { i: Object.keys(u.friendly).map(t => ({ id: K.ITEM('u16', +t), pt: u32(u.friendly[t]) })) }, - }, - // the bonus printed on this card; the game applies it to one battle - offset: { kira_hp: u8(card.kira[0]), kira_atk: u8(card.kira[1]), kira_mat: u8(card.kira[2]), kira_spd: u8(card.kira[3]) }, - active_key: bool(!!u.keycard_id), - version: { episode: u8(EPISODE), phase: u8(PHASE) }, - makeup: { hair_color: u8(d.makeup[0]), eye_color: u8(d.makeup[1]) }, - closet: { - powder_num: u32(u.powder_num), - stamp_num: u32(u.stamp_num), - wallet: { - balance: u32(u.balance), - ticket_half_price: bool(u.ticket_half_price), - ticket_rc_first: bool(u.ticket_rc_first), - }, - equip: { i: gradeList(equipWithAccessories(u)) }, - material: { i: gradeList(u.material) }, - decoseal: Object.keys(u.decoseal).slice(0, 1).map(id => ({ decoseal_id: u32(+id), decoseal_num: u32(u.decoseal[id]) })), - accessory: { - i: u.accessory.map(a => ({ aseq: u32(a.aseq), id: u32(a.id), gr: u8(a.gr), offset: K.ARRAY('u8', a.offset) })), - }, - cardbg: u.cardbg.slice(0, 4).map(id => ({ id: u8(id) })), - }, - }); -}; - -const checkKeyUser: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - const keycard_id = $(data).str('keycard_id', ''); - if (!u || !u.keycard_id || u.keycard_id !== keycard_id) { - log('checkKeyUser: not this user\'s key', { keycard_id, bound: u && u.keycard_id }); - return send.status(10); // the game shows "key does not match" and lets the player rescan - } - send.object({ keycard_id: K.ITEM('str', u.keycard_id), key_inquire_id: K.ITEM('str', u.key_inquire_id) }); -}; - -// A new key replaces the old one (first key or "remake"). The id goes into the key card's QR. -const bindKeyUser: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (!u) return send.deny(); - u.keycard_id = await newId(); - u.key_inquire_id = await newId(); - // the first key comes with the tailor tutorial's result; the game adds it only when is_first is set - const bonus = $(data).bool('is_first') ? makeAccessory(u, FIRST_KEY_ACCESSORY, 0) : EMPTY_ACCESSORY; - await saveUser(u); - send.object({ - keycard_id: K.ITEM('str', u.keycard_id), - key_inquire_id: K.ITEM('str', u.key_inquire_id), - accessory: accessoryNode(bonus), - }); -}; - -const setCardInfo: EPR = async (info, data, send) => { - const d = await getDoll($(data).number('doll_seq')); - const u = d && (await getUser(d.user_seq)); - if (!u || !d) return send.deny(); - d.doll_level = Math.max(1, Math.min(31, $(data).number('doll_level', d.doll_level))); - d.doll_name = $(data).str('doll_name', d.doll_name); - d.equip = $(data).elements('equip').slice(0, 4).map(e => [e.number('equip_id', 0), e.number('equip_grade', 0)]); - d.accessory = ($(data).numbers('accessory_seq') || []).concat([0, 0]).slice(0, 2); - await saveDoll(d); - - // kira_type 0 normal, 1 star, 2 gold, 3 star while starkira_free is on. - // A kira card gives +1 to 1-3 random stats (wiki); the card shows each as one digit. - const kira_type = $(data).number('kira_type', 0); - const kira = [0, 0, 0, 0]; - if (kira_type) for (const k of [0, 1, 2, 3].sort(() => Math.random() - 0.5).slice(0, 1 + rand(3))) kira[k] = 1; - if (kira_type === 1) u.powder_num = Math.max(0, u.powder_num - 1); // the game spends one star powder locally - const decoseal_id = $(data).number('decoseal_id', 0); - if (decoseal_id && u.decoseal[decoseal_id]) u.decoseal[decoseal_id]--; // guess: nothing else reports it - await updateRelease(u); - await saveUser(u); - - // Snapshot the worn accessories. One sold since comes from the card this play started with. - const from_otocard_id = $(data).str('otocard_id', ''); - const from = from_otocard_id ? await DB.FindOne({ collection: 'card', otocard_id: from_otocard_id }) : null; - const accessory = d.accessory.map(s => (s && (u.accessory.find(a => a.aseq === s) || - (from && from.accessory.find(a => a.aseq === s)))) || EMPTY_ACCESSORY); - const card: Card = { - collection: 'card', otocard_id: await newId(), inquire_id: await newId(), user_seq: u.user_seq, - doll_seq: d.doll_seq, kira_type, kira, equip: d.equip, accessory, decoseal_id, from_otocard_id, created: now(), - }; - await DB.Insert(card); - log('setCardInfo', { otocard_id: card.otocard_id, kira_type, kira }); - send.object({ - otocard_id: K.ITEM('str', card.otocard_id), - inquire_id: K.ITEM('str', card.inquire_id), - offset: { kira_hp: u8(kira[0]), kira_atk: u8(kira[1]), kira_mat: u8(kira[2]), kira_spd: u8(kira[3]) }, - }); -}; - -const report: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (!u) return send.success(); - const b = $(data).element('battle'); - if (b) { - const team = b.number('enemy_team_id', 0); - const level = b.number('enemy_team_level', 0); - const episode = b.number('episode', 0); - // battle_result 0 win, 1 lose, 2 draw; the game adds a beaten pair to score/enemy only on a win - if (team && b.number('battle_result', 1) === 0 && u.enemy.length < 200 && - !u.enemy.some(e => e[0] === team && e[1] === level)) u.enemy.push([team, level]); - if (team) u.last_enemy = [[episode, team, level], ...u.last_enemy.filter(e => e[0] !== episode)].slice(0, 6); - } - u.user_flags = $(data).numbers('user_flags', u.user_flags); - u.user_marker = $(data).number('user_marker', u.user_marker); - u.sort_type = $(data).number('setting.sort_type', u.sort_type); - u.disp_skill = $(data).number('setting.disp_skill', u.disp_skill); - u.last_time = now(); - const d = await getDoll($(data).number('doll_seq')); - if (d && d.user_seq === u.user_seq) { - d.doll_level = Math.max(1, Math.min(31, $(data).number('doll_level', d.doll_level))); - await saveDoll(d); - } - await updateRelease(u); - await saveUser(u); - send.success(); -}; - -// Only gold and friend points: doll levels are raised by the game itself. -const addExp: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (!u) return send.deny(); - u.balance = Math.min(MAX_BALANCE, u.balance + $(data).number('gold.add_amount', 0)); - const team = $(data).number('friendly.enemy_team_id', 0); - if (team) u.friendly[team] = (u.friendly[team] || 0) + $(data).number('friendly.add_point', 0); - await saveUser(u); - send.object({ balance: u32(u.balance), friend_point: u32(team ? u.friendly[team] : 0) }); -}; - -const addItem: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (!u) return send.deny(); - // accessory[i] must line up with item[i]; rows that are not accessories stay zero - const made = $(data).elements('item').map(i => - giveItem(u, i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0)) || EMPTY_ACCESSORY); - const gift = $(data).str('gift_code', ''); - // gift_type 4: the present for an offline card, which also gives one stamp (official notice) - if ($(data).number('gift_type', 0) === 4 && gift && !u.gifts_used.includes(gift)) { - u.gifts_used.push(gift); - u.stamp_bonus++; - } - await saveUser(u); - send.object({ accessory: made.map(accessoryNode) }); -}; - -// item: what the player threw away (one row per piece). drop: this play's drops that are kept. -const delItem: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (!u) return send.deny(); - for (const i of $(data).elements('drop')) giveItem(u, i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0)); - for (const i of $(data).elements('item')) { - await takeItem(u, i.number('accessory_seq', 0), i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), 1); - } - await saveUser(u); - send.success(); -}; - -const sellItem: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (!u) return send.deny(); - const items = $(data).elements('item'); - const soldTemp = items.map(i => i.number('accessory_seq', 0)).filter(s => s >= TEMP_ASEQ); - // drops first (sell scene after a full closet): a dropped accessory sold right away comes back as TEMP_ASEQ + n - const made: Accessory[] = []; - $(data).elements('drop').forEach((i, n) => { - const type = i.number('item_type', 0), id = i.number('item_id', 0), grade = i.number('item_grade', 0); - if (isAccessory(type, id) && soldTemp.includes(TEMP_ASEQ + n)) return; - const a = giveItem(u, type, id, grade); - if (a) made.push(a); - }); - for (const i of items) { - const aseq = i.number('accessory_seq', 0); - if (aseq >= TEMP_ASEQ) continue; - await takeItem(u, aseq, i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), i.number('item_num', 1)); - } - // the game computes the balance after selling (prices from pricelist.csv) and never reads ours - u.balance = Math.max(0, Math.min(MAX_BALANCE, $(data).number('balance', u.balance))); - await saveUser(u); - send.object({ balance: u32(u.balance), accessory: made.map(accessoryNode) }); -}; - -const composition: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (!u) return send.deny(); - for (const k of ['item_in_1', 'item_in_2']) { - const i = $(data).element(k); - if (i) await takeItem(u, i.number('accessory_seq', 0), i.number('item_type', 0), i.number('item_id', 0), i.number('item_grade', 0), 1); - } - const r = $(data).element('item_result'); - const made = r ? giveItem(u, r.number('item_type', 0), r.number('item_id', 0), r.number('item_grade', 0)) : null; - await saveUser(u); - send.object({ accessory: accessoryNode(made || EMPTY_ACCESSORY) }); -}; - -const setMakeup: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - const d = await getDoll($(data).number('doll_seq')); - if (!u || !d) return send.deny(); - d.makeup = [$(data).number('makeup.hair_color', 0), $(data).number('makeup.eye_color', 0)]; - // payment is only the last purchase; the game sends the balance after paying - u.balance = Math.max(0, Math.min(MAX_BALANCE, $(data).number('balance', u.balance))); - if ($(data).number('ticket_type', 0) === 1) u.ticket_half_price = false; - await saveDoll(d); - await saveUser(u); - send.object({ balance: u32(u.balance) }); -}; - -// The stamp card has 10 squares; a full card turns into star powder. -const addStamp: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (!u) return send.deny(); - // one stamp per play (official), plus stamps from gifts; the game animates old -> after one by one - const after = u.stamp_num + (U.GetConfig('stamp_double') ? 2 : 1) + u.stamp_bonus; - u.stamp_bonus = 0; - const full = Math.floor(after / 10); - u.stamp_num = after % 10; - u.powder_num += full; // guess: one powder per full card - u.stamp_conv_num += full; - await saveUser(u); - send.object({ - powder_num: u32(u.powder_num), - stamp_num: u32(u.stamp_num), - stamp_conv_rate: u32(10), // squares per card; the game divides by it - stamp_after_num: u32(after), - }); -}; - -const reportMission: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - const n = $(data).number('mission_no', 0); - if (u && u.mission_no === n) { - u.mission_no = Math.min(n + 1, 9); // the game treats every mission below mission_no as done - // all 8 letters: the original wallpaper (closet/cardbg adds to the wallpapers the game offers) - if (u.mission_no === 9 && !u.cardbg.includes(LETTER_WALLPAPER) && u.cardbg.length < 4) u.cardbg.push(LETTER_WALLPAPER); - await saveUser(u); - } - send.success(); -}; - -// score/question_id is the questionnaire still to show; no questionnaires are offered, so it stays 0. -const reportQuestion: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - if (u) { - u.question_id = 0; - await saveUser(u); - } - send.success(); -}; - -// Gift QRs: only type 4 (the offline card the game prints when setCardInfo fails) is honoured. -// The apology gift itself is taken later by addItem with gift_type 4. Past campaigns answer 5 (expired). -const receiveGift: EPR = async (info, data, send) => { - const u = await getUser($(data).number('user_seq')); - const gift = $(data).element('info'); - const type = gift ? gift.number('gift_type', 0) : 0; - const code = gift ? gift.str('gift_code', '') : ''; - const status = type !== 4 ? 5 : u && u.gifts_used.includes(code) ? 2 : 1; - send.object({ - powder_num: u32(u ? u.powder_num : 0), // always overwrites the game's count - status: [{ gift_type: u8(type), gift_status: u8(status) }], - }); -}; - -const receiveItem: EPR = async (info, data, send) => { - send.object({ gift_type: u8(7), gift_status: u8(5) }); -}; - -// The game spends ticket_rc_first itself when a rival card is chosen and tells nobody else. -const setRivalCard: EPR = async (info, data, send) => { - const card = await DB.FindOne({ collection: 'card', otocard_id: $(data).str('otocard_id', '') }); - const u = card && (await getUser(card.user_seq)); - if (u && u.ticket_rc_first) { - u.ticket_rc_first = false; - await saveUser(u); - } - send.success(); -}; +// field names, types and limits follow its psmap tables, and every value follows how game.dll uses it +// (see README). Values marked "guess" are server policy the game code does not decide. +import { addDoll, addUser, getCardInfo } from './handlers/card'; +import { receiveGift, receiveItem } from './handlers/gift'; +import { addItem, composition, delItem, sellItem, setMakeup } from './handlers/item'; +import { bindKeyUser, checkKeyUser } from './handlers/key'; +import { addExp, addStamp, report, reportMission, reportQuestion } from './handlers/play'; +import { setCardInfo, setRivalCard } from './handlers/print'; +import { getVersion } from './handlers/version'; export function register() { R.GameCode('NCG'); @@ -648,29 +46,35 @@ export function register() { }); R.Route('game.getVersion', getVersion); + R.Route('game.addUser', addUser); R.Route('game.addDoll', addDoll); R.Route('game.getCardInfo', getCardInfo); + R.Route('game.cancel', true); + R.Route('game.checkKeyUser', checkKeyUser); R.Route('game.bindKeyUser', bindKeyUser); R.Route('game.copyKeyCard', true); - R.Route('game.cancel', true); + R.Route('game.setCardInfo', setCardInfo); + R.Route('game.setRivalCard', setRivalCard); + R.Route('game.checkRivalCard', true); + R.Route('game.report', report); R.Route('game.addExp', addExp); + R.Route('game.addStamp', addStamp); + R.Route('game.reportMission', reportMission); + R.Route('game.reportQuestion', reportQuestion); + R.Route('game.reportTrial', true); + R.Route('game.addItem', addItem); R.Route('game.delItem', delItem); R.Route('game.sellItem', sellItem); R.Route('game.composition', composition); R.Route('game.setMakeup', setMakeup); - R.Route('game.addStamp', addStamp); - R.Route('game.reportMission', reportMission); - R.Route('game.reportQuestion', reportQuestion); - R.Route('game.reportTrial', true); + R.Route('game.receiveGift', receiveGift); R.Route('game.receiveItem', receiveItem); - R.Route('game.checkRivalCard', true); - R.Route('game.setRivalCard', setRivalCard); R.Unhandled(async (info, data, send) => { console.error(`[otoca] unhandled ${info.module}.${info.method}: ${JSON.stringify(data)}`); diff --git a/otoca@asphyxia/models/card.ts b/otoca@asphyxia/models/card.ts new file mode 100644 index 0000000..daee2ea --- /dev/null +++ b/otoca@asphyxia/models/card.ts @@ -0,0 +1,18 @@ +import { Accessory } from './user'; + +export interface Card { + collection: 'card'; + otocard_id: string; + inquire_id: string; + user_seq: number; + doll_seq: number; + kira_type: number; + kira: number[]; // hp, atk, mat, spd bonus printed on this card + // the coord printed on this card: scanning an old card plays the latest doll in that card's coord, + // even with items sold since then + equip: number[][]; + accessory: Accessory[]; + decoseal_id: number; + from_otocard_id: string; + created: number; +} diff --git a/otoca@asphyxia/models/counter.ts b/otoca@asphyxia/models/counter.ts new file mode 100644 index 0000000..516d5a0 --- /dev/null +++ b/otoca@asphyxia/models/counter.ts @@ -0,0 +1,5 @@ +export interface Counter { + collection: 'counter'; + name: string; + value: number; +} diff --git a/otoca@asphyxia/models/doll.ts b/otoca@asphyxia/models/doll.ts new file mode 100644 index 0000000..6e7928e --- /dev/null +++ b/otoca@asphyxia/models/doll.ts @@ -0,0 +1,13 @@ +export interface Doll { + collection: 'doll'; + doll_seq: number; + user_seq: number; + doll_id: number; + doll_name: string; + doll_seed: number; + doll_level: number; + equip: number[][]; // 4 slots [equip_id, grade]: weapon, top, bottom, shoes + accessory: number[]; // 2 slots of accessory_seq, 0 = empty + makeup: number[]; // hair_color, eye_color + luck_offset: number; +} diff --git a/otoca@asphyxia/models/user.ts b/otoca@asphyxia/models/user.ts new file mode 100644 index 0000000..7d87867 --- /dev/null +++ b/otoca@asphyxia/models/user.ts @@ -0,0 +1,39 @@ +export interface Accessory { + aseq: number; + id: number; + gr: number; + offset: number[]; // param_offset, skill_offset_1..3 (percent of the equip's base values) +} + +export interface User { + collection: 'user'; + user_seq: number; + user_name: string; + keycard_id: string; + key_inquire_id: string; + powder_num: number; + stamp_num: number; + stamp_conv_num: number; + stamp_bonus: number; // stamps from gifts, pressed with the next addStamp + balance: number; + ticket_half_price: boolean; + ticket_rc_first: boolean; + equip: { [id: string]: number[] }; // count per grade 0..2, accessories not included + material: { [id: string]: number[] }; + decoseal: { [id: string]: number }; + accessory: Accessory[]; + next_aseq: number; + cardbg: number[]; + release_state: number; + user_flags: number[]; + last_time: number; + user_marker: number; + mission_no: number; + question_id: number; + sort_type: number; + disp_skill: number; + enemy: number[][]; // [team, level] of every team/level beaten + last_enemy: number[][]; // [episode, team, level], newest first + friendly: { [team: string]: number }; + gifts_used: string[]; +} diff --git a/otoca@asphyxia/player.ts b/otoca@asphyxia/player.ts new file mode 100644 index 0000000..b5723cf --- /dev/null +++ b/otoca@asphyxia/player.ts @@ -0,0 +1,48 @@ +// Creating players and dolls, and the per-player state the game only reads. +import { addCount } from './closet'; +import { DOLL_INIT } from './data'; +import { nextSeq } from './db'; +import { Doll } from './models/doll'; +import { User } from './models/user'; +import { now, rand } from './utils'; + +export function newUser(user_seq: number, user_name: string): User { + return { + collection: 'user', user_seq, user_name, keycard_id: '', key_inquire_id: '', + // shows the first-play powder message; with the first stamp card this makes the 2 kira cards + // the wiki gives players without a key + powder_num: 1, + stamp_num: 0, stamp_conv_num: 0, stamp_bonus: 0, balance: 0, ticket_half_price: false, + ticket_rc_first: true, // the first rival card print guarantees a higher rarity (wiki) + equip: {}, material: {}, decoseal: {}, accessory: [], next_aseq: 1, cardbg: [], + release_state: 0, user_flags: [0, 0, 0, 0], last_time: 0, user_marker: 0, + mission_no: 1, // 0 would never start the missions + question_id: 0, sort_type: 0, disp_skill: 0, enemy: [], last_enemy: [], friendly: {}, gifts_used: [], + }; +} + +// Same as the game does locally: level 1, starting equips (grade 0), which also go into the closet. +export async function newDoll(u: User, doll_id: number, doll_name: string): Promise { + const init = DOLL_INIT[doll_id] || [0, 0, 0, 0]; + const doll: Doll = { + collection: 'doll', doll_seq: await nextSeq('doll'), user_seq: u.user_seq, doll_id, doll_name, + doll_seed: rand(23328), // growth type + 3 * five stat offsets in base 6; 23328 and up disables growth + doll_level: 1, equip: init.map(id => [id, 0]), accessory: [0, 0], makeup: [0, 0], luck_offset: 0, + }; + for (const id of init) addCount(u.equip, id, 0, 1); + await DB.Insert(doll); + return doll; +} + +// "New doll" on the entry menu stays disabled while release_state is 0 (label disable_lv5). +export async function updateRelease(u: User) { + const dolls = await DB.Find({ collection: 'doll', user_seq: u.user_seq }); + if (dolls.some(d => d.doll_level >= 5)) u.release_state = 1; +} + +// guess: 1 = played again within a day, 2 = back after two weeks, 0 = otherwise +export function greeting(last: number) { + if (!last) return 0; + const days = (now() - last) / 86400; + return days < 1 ? 1 : days >= 14 ? 2 : 0; +} diff --git a/otoca@asphyxia/utils.ts b/otoca@asphyxia/utils.ts new file mode 100644 index 0000000..4b63107 --- /dev/null +++ b/otoca@asphyxia/utils.ts @@ -0,0 +1,9 @@ +export const now = () => Math.floor(Date.now() / 1000); +export const rand = (n: number) => Math.floor(Math.random() * n); +export const u8 = (v: number) => K.ITEM('u8', v || 0); +export const u32 = (v: number) => K.ITEM('u32', v || 0); +export const bool = (v: boolean) => K.ITEM('bool', !!v); + +export function log(method: string, data: any) { + console.log(`[otoca] ${method} ${JSON.stringify(data)}`); +} From ec4c3614b793f1e68af6db1a6487204a5ec79b69 Mon Sep 17 00:00:00 2001 From: iamsub <294696795+iamiamsub@users.noreply.github.com> Date: Tue, 29 Sep 2026 18:08:12 +0900 Subject: [PATCH 6/7] otoca: choose the rival card pack in the WebUI The pack of the rival cards offered after a play comes from the game's version: rivalcard_packdata picks the last row at or below it, and 5-6 to 5-11 unlock the same content otherwise (only the title-screen pack icon differs). Always sending phase 11 fixed the pack to Le Fenice. The new setting "Rival card pack" (one of the five packs, or Random) picks the phase sent by getVersion and getCardInfo. getCardInfo's version is applied when the card is scanned, so Random draws a pack for each play; plays without a scan use getVersion's. --- otoca@asphyxia/README.md | 6 +++++- otoca@asphyxia/data.ts | 12 ++++++++++-- otoca@asphyxia/handlers/card.ts | 5 +++-- otoca@asphyxia/handlers/version.ts | 14 +++++++++++--- otoca@asphyxia/index.ts | 8 ++++++++ 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/otoca@asphyxia/README.md b/otoca@asphyxia/README.md index d1a3a2d..7ae78f8 100644 --- a/otoca@asphyxia/README.md +++ b/otoca@asphyxia/README.md @@ -23,7 +23,8 @@ How it works status 1, and the game then plays offline. - The key card (`game.bindKeyUser`) is a second printed card tied to the player. Once a key is bound, scanning a card asks for the key card (`game.checkKeyUser`) and the closet holds 999 items instead of 50. - - `game.getVersion` and getCardInfo both send episode 5 / phase 11, the newest the game knows. + - `game.getVersion` and getCardInfo both send episode 5 and a phase from 7 to 11. Those phases unlock the + same content and differ only in the rival card pack (and its icon on the title screen). - Without a camera, printed card images can be scanned with a hook that feeds an image to the game's camera library (libcamera.dll) in place of the camera frame. @@ -39,6 +40,9 @@ Settings - Limited-time enemy: shows the one limited-time enemy the data has (episode 3). - Double stamps: two stamps per play. - Free star kira: star kira cards need no star powder. + - Rival card pack: the pack the rival cards offered after a play come from, one of the five packs or + Random (a new draw for each play). The game takes the pack from the version it got when the card + was scanned; plays without a scan (new players) use the one from the attract loop. Server-side rules (from the arcade as documented by the fan wiki and the official site) - Kira cards give +1 to 1-3 random stats. diff --git a/otoca@asphyxia/data.ts b/otoca@asphyxia/data.ts index aa37d43..2cbfdd4 100644 --- a/otoca@asphyxia/data.ts +++ b/otoca@asphyxia/data.ts @@ -1,8 +1,16 @@ // Values taken from the game's data (data_ncg/paramdb) and the arcade's rules. -// versiondata.csv: base 5-2, newest 5-11. getCardInfo's version is applied the same way, so both send this. +// versiondata.csv: base 5-2, newest 5-11. 5-6 to 5-11 unlock the same content; only the rival card pack +// (rivalcard_packdata.csv: the last row at or below the version) and its title-screen icon differ, +// so the phase picks the pack. export const EPISODE = 5; -export const PHASE = 11; +export const RIVAL_PACKS = [ + { name: 'ジュエル・ソング', phase: 7 }, + { name: 'ミッドサマーナイト・ドリーム', phase: 8 }, + { name: 'スターズ・シャイニング', phase: 9 }, + { name: 'ファイアリー・エンジェル', phase: 10 }, + { name: 'レ・フェニーチ', phase: 11 }, +]; // Alphabet of the ids the game packs into its QR codes (5 bits per char). export const ID_CHARS = '0123456789ABCDEFGHJKLMNPRSTUWXYZ'; // equipdata.csv rows with kind 4 (accessories): they carry an accessory_seq. diff --git a/otoca@asphyxia/handlers/card.ts b/otoca@asphyxia/handlers/card.ts index 470e8b4..0fe1b6f 100644 --- a/otoca@asphyxia/handlers/card.ts +++ b/otoca@asphyxia/handlers/card.ts @@ -1,9 +1,10 @@ // New players and dolls, and scanning a printed card. import { accessoryNode, equipWithAccessories, gradeList } from '../closet'; -import { EPISODE, PHASE } from '../data'; +import { EPISODE } from '../data'; import { getCard, getDoll, getUser, nextSeq, saveUser } from '../db'; import { greeting, newDoll, newUser } from '../player'; import { bool, log, now, u8, u32 } from '../utils'; +import { phase } from './version'; export const addUser: EPR = async (info, data, send) => { const u = newUser(await nextSeq('user'), $(data).str('user_name', '')); @@ -70,7 +71,7 @@ export const getCardInfo: EPR = async (info, data, send) => { // the bonus printed on this card; the game applies it to one battle offset: { kira_hp: u8(card.kira[0]), kira_atk: u8(card.kira[1]), kira_mat: u8(card.kira[2]), kira_spd: u8(card.kira[3]) }, active_key: bool(!!u.keycard_id), - version: { episode: u8(EPISODE), phase: u8(PHASE) }, + version: { episode: u8(EPISODE), phase: u8(phase()) }, makeup: { hair_color: u8(d.makeup[0]), eye_color: u8(d.makeup[1]) }, closet: { powder_num: u32(u.powder_num), diff --git a/otoca@asphyxia/handlers/version.ts b/otoca@asphyxia/handlers/version.ts index 4b63f9b..52434a1 100644 --- a/otoca@asphyxia/handlers/version.ts +++ b/otoca@asphyxia/handlers/version.ts @@ -1,5 +1,13 @@ -import { EPISODE, PHASE } from '../data'; -import { bool, u8, u32 } from '../utils'; +import { EPISODE, RIVAL_PACKS } from '../data'; +import { bool, rand, u8, u32 } from '../utils'; + +// The phase of the configured rival card pack ("Random" draws one on every call). The game picks the pack +// from the version it holds when the card choice opens: getCardInfo's sets it for that play, getVersion's +// for plays without a card scan (new players) and for the pack icon on the title screen. +export function phase() { + const pack = RIVAL_PACKS.find(p => p.name === U.GetConfig('rival_pack')); + return (pack || RIVAL_PACKS[rand(RIVAL_PACKS.length)]).phase; +} // Sent by ealocal every `expire` seconds from the attract loop; the game waits for the first one at boot. export const getVersion: EPR = async (info, data, send) => { @@ -7,7 +15,7 @@ export const getVersion: EPR = async (info, data, send) => { send.object({ expire: u32(600), episode: u8(EPISODE), - phase: u8(PHASE), + phase: u8(phase()), marker: u8(U.GetConfig('marker')), campaign: { trial_play: bool(false), diff --git a/otoca@asphyxia/index.ts b/otoca@asphyxia/index.ts index 766e851..2a24697 100644 --- a/otoca@asphyxia/index.ts +++ b/otoca@asphyxia/index.ts @@ -1,6 +1,7 @@ // otoca d'or (NCG). The XRPC module "game" (service "local") is implemented by the game's ealocal.dll; // field names, types and limits follow its psmap tables, and every value follows how game.dll uses it // (see README). Values marked "guess" are server policy the game code does not decide. +import { RIVAL_PACKS } from './data'; import { addDoll, addUser, getCardInfo } from './handlers/card'; import { receiveGift, receiveItem } from './handlers/gift'; import { addItem, composition, delItem, sellItem, setMakeup } from './handlers/item'; @@ -38,6 +39,13 @@ export function register() { type: 'boolean', default: true, }); + R.Config('rival_pack', { + name: 'Rival card pack', + desc: 'The pack the rival cards offered after a play come from. Random draws one for each play.', + type: 'string', + options: ['Random', ...RIVAL_PACKS.map(p => p.name)], + default: 'Random', + }); R.Config('starkira_free', { name: 'Free star kira', desc: 'Star kira cards need no star powder.', From f4e14e0c041db38f083ff0844bfb2b56285e4e40 Mon Sep 17 00:00:00 2001 From: iamsub <294696795+iamiamsub@users.noreply.github.com> Date: Tue, 29 Sep 2026 23:18:25 +0900 Subject: [PATCH 7/7] otoca: end the letters with the 8th and give the wallpaper then A full pass over the mission code showed the previous analysis read the achievement check one slot off: mission k (2-9) is checked against the flag recorded for k-1 (FUN_100219c0's jump table), so mission 1 is the first play and Nyandora's 8 letters are missions 2-9. SELECTCARD plays the "wallpaper complete" animation when mission 9 is done. The plugin stopped at 9 and gave the wallpaper when reaching it, one letter early, then ignored the report of 9, so the 8th letter came back every play. Now reporting 9 adds wallpaper 29 and moves to 10, which the game treats as no mission. Wallpaper 29 is confirmed by afp_letter.ifs, which completes card_bg 0018. --- otoca@asphyxia/README.md | 3 ++- otoca@asphyxia/data.ts | 5 ++++- otoca@asphyxia/handlers/play.ts | 13 ++++++++----- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/otoca@asphyxia/README.md b/otoca@asphyxia/README.md index 7ae78f8..62cc503 100644 --- a/otoca@asphyxia/README.md +++ b/otoca@asphyxia/README.md @@ -50,7 +50,8 @@ Server-side rules (from the arcade as documented by the fan wiki and the officia - One stamp per play; a full card of 10 gives one star powder. New players start with one powder and the first rival card ticket. The first key card comes with the Pink March Ribbon. - "New doll" unlocks once any doll reaches level 5. - - All 8 letters from Nyandora give a wallpaper (which one is inferred from the data). + - Nyandora's 8 letters are missions 2-9 (mission 1 is the first play). The 8th letter completes the + wallpaper (card_bg 0018, the one the game's letter data shows), and then the missions end. - No questionnaires, and old campaign gift cards answer "expired". The game's own offline card (printed when setCardInfo fails) gives its apology gift and a stamp once. diff --git a/otoca@asphyxia/data.ts b/otoca@asphyxia/data.ts index 2cbfdd4..b39d6c5 100644 --- a/otoca@asphyxia/data.ts +++ b/otoca@asphyxia/data.ts @@ -30,6 +30,9 @@ export const ACCESSORY_OFFSET = [ [[80, 100], [100, 100], [100, 100], [80, 100]], ]; export const FIRST_KEY_ACCESSORY = 144; // ピンクマーチリボン, made in the tailor tutorial when the first key is made -export const LETTER_WALLPAPER = 29; // guess: card_bg_0018 (nyandora night sky), the reward for all 8 letters +// cardbg.csv 29 = card_bg 0018 (Nyandora's night sky), the wallpaper afp_letter.ifs completes; no release table +// unlocks it, so it only comes through closet/cardbg. +export const LETTER_WALLPAPER = 29; +export const LAST_MISSION = 9; // the 8th letter export const TEMP_ASEQ = 1000000000; // the game's own placeholder seqs for this play's drops (sell scene) export const MAX_BALANCE = 9999999; diff --git a/otoca@asphyxia/handlers/play.ts b/otoca@asphyxia/handlers/play.ts index 735c846..39e1a44 100644 --- a/otoca@asphyxia/handlers/play.ts +++ b/otoca@asphyxia/handlers/play.ts @@ -1,5 +1,5 @@ // What a play reports: battle results, gold and friend points, stamps, letters and questionnaires. -import { LETTER_WALLPAPER, MAX_BALANCE } from '../data'; +import { LAST_MISSION, LETTER_WALLPAPER, MAX_BALANCE } from '../data'; import { getDoll, getUser, saveDoll, saveUser } from '../db'; import { updateRelease } from '../player'; import { now, u32 } from '../utils'; @@ -63,13 +63,16 @@ export const addStamp: EPR = async (info, data, send) => { }); }; +// Missions: 1 is the first play (always done, no letter); 2-9 are Nyandora's 8 letters, each checked against the +// flag the previous number records (game.dll FUN_100219c0); finishing 9 completes the wallpaper. The game only +// reports and never advances mission_no itself, and anything above 9 means no mission. export const reportMission: EPR = async (info, data, send) => { const u = await getUser($(data).number('user_seq')); const n = $(data).number('mission_no', 0); - if (u && u.mission_no === n) { - u.mission_no = Math.min(n + 1, 9); // the game treats every mission below mission_no as done - // all 8 letters: the original wallpaper (closet/cardbg adds to the wallpapers the game offers) - if (u.mission_no === 9 && !u.cardbg.includes(LETTER_WALLPAPER) && u.cardbg.length < 4) u.cardbg.push(LETTER_WALLPAPER); + if (u && n >= 1 && n <= LAST_MISSION && u.mission_no === n) { + u.mission_no = n + 1; + // the 8th letter: the original wallpaper (closet/cardbg adds to the wallpapers the game offers) + if (n === LAST_MISSION && !u.cardbg.includes(LETTER_WALLPAPER) && u.cardbg.length < 4) u.cardbg.push(LETTER_WALLPAPER); await saveUser(u); } send.success();