Files
plugins/otoca@asphyxia/handlers/key.ts
iamsub 7518cd2abc 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.
2026-09-29 15:32:48 +09:00

32 lines
1.5 KiB
TypeScript

// 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),
});
};