Commit 72fc9542 authored by nanahira's avatar nanahira

migrate constants to auto-managed constants

parent 94a0677b
......@@ -37,3 +37,4 @@ ehthumbs.db
Desktop.ini
$RECYCLE.BIN/
.DS_Store
/scripts
......@@ -9,7 +9,7 @@ const underscore_1 = __importDefault(require("underscore"));
const structs_json_1 = __importDefault(require("./data/structs.json"));
const typedefs_json_1 = __importDefault(require("./data/typedefs.json"));
const proto_structs_json_1 = __importDefault(require("./data/proto_structs.json"));
const constants_json_1 = __importDefault(require("./data/constants.json"));
const load_constants_1 = __importDefault(require("./load-constants"));
class Handler {
constructor(handler, synchronous) {
this.handler = handler;
......@@ -56,7 +56,7 @@ class YGOProMessagesHelper {
this.structs_declaration = structs_json_1.default;
this.typedefs = typedefs_json_1.default;
this.proto_structs = proto_structs_json_1.default;
this.constants = constants_json_1.default;
this.constants = (0, load_constants_1.default)();
}
initStructs() {
this.structs = new Map();
......
......@@ -3,7 +3,7 @@ import _ from "underscore";
import structs_declaration from "./data/structs.json";
import typedefs from "./data/typedefs.json";
import proto_structs from "./data/proto_structs.json";
import constants from "./data/constants.json";
import loadConstants from "./load-constants";
import net from "net";
......@@ -102,7 +102,7 @@ export class YGOProMessagesHelper {
this.structs_declaration = structs_declaration;
this.typedefs = typedefs;
this.proto_structs = proto_structs;
this.constants = constants;
this.constants = loadConstants();
}
initStructs() {
......
......@@ -12,52 +12,6 @@
"5": "TYPE_PLAYER6",
"7": "TYPE_OBSERVER"
},
"CTOS": {
"1": "RESPONSE",
"2": "UPDATE_DECK",
"3": "HAND_RESULT",
"4": "TP_RESULT",
"16": "PLAYER_INFO",
"17": "CREATE_GAME",
"18": "JOIN_GAME",
"19": "LEAVE_GAME",
"20": "SURRENDER",
"21": "TIME_CONFIRM",
"22": "CHAT",
"23": "EXTERNAL_ADDRESS",
"32": "HS_TODUELIST",
"33": "HS_TOOBSERVER",
"34": "HS_READY",
"35": "HS_NOTREADY",
"36": "HS_KICK",
"37": "HS_START",
"48": "REQUEST_FIELD"
},
"STOC": {
"1": "GAME_MSG",
"2": "ERROR_MSG",
"3": "SELECT_HAND",
"4": "SELECT_TP",
"5": "HAND_RESULT",
"6": "TP_RESULT",
"7": "CHANGE_SIDE",
"8": "WAITING_SIDE",
"9": "DECK_COUNT",
"17": "CREATE_GAME",
"18": "JOIN_GAME",
"19": "TYPE_CHANGE",
"20": "LEAVE_GAME",
"21": "DUEL_START",
"22": "DUEL_END",
"23": "REPLAY",
"24": "TIME_LIMIT",
"25": "CHAT",
"32": "HS_PLAYER_ENTER",
"33": "HS_PLAYER_CHANGE",
"34": "HS_WATCH_CHANGE",
"35": "TEAMMATE_SURRENDER",
"48": "FIELD_FINISH"
},
"PLAYERCHANGE":{
"8": "OBSERVE",
"9": "READY",
......@@ -75,203 +29,6 @@
"1": "MATCH",
"2": "TAG"
},
"MSG": {
"1": "RETRY",
"2": "HINT",
"3": "WAITING",
"4": "START",
"5": "WIN",
"6": "UPDATE_DATA",
"7": "UPDATE_CARD",
"8": "REQUEST_DECK",
"10": "SELECT_BATTLECMD",
"11": "SELECT_IDLECMD",
"12": "SELECT_EFFECTYN",
"13": "SELECT_YESNO",
"14": "SELECT_OPTION",
"15": "SELECT_CARD",
"16": "SELECT_CHAIN",
"18": "SELECT_PLACE",
"19": "SELECT_POSITION",
"20": "SELECT_TRIBUTE",
"21": "SORT_CHAIN",
"22": "SELECT_COUNTER",
"23": "SELECT_SUM",
"24": "SELECT_DISFIELD",
"25": "SORT_CARD",
"26": "SELECT_UNSELECT_CARD",
"30": "CONFIRM_DECKTOP",
"31": "CONFIRM_CARDS",
"32": "SHUFFLE_DECK",
"33": "SHUFFLE_HAND",
"34": "REFRESH_DECK",
"35": "SWAP_GRAVE_DECK",
"36": "SHUFFLE_SET_CARD",
"37": "REVERSE_DECK",
"38": "DECK_TOP",
"39": "MSG_SHUFFLE_EXTRA",
"40": "NEW_TURN",
"41": "NEW_PHASE",
"42": "CONFIRM_EXTRATOP",
"50": "MOVE",
"53": "POS_CHANGE",
"54": "SET",
"55": "SWAP",
"56": "FIELD_DISABLED",
"60": "SUMMONING",
"61": "SUMMONED",
"62": "SPSUMMONING",
"63": "SPSUMMONED",
"64": "FLIPSUMMONING",
"65": "FLIPSUMMONED",
"70": "CHAINING",
"71": "CHAINED",
"72": "CHAIN_SOLVING",
"73": "CHAIN_SOLVED",
"74": "CHAIN_END",
"75": "CHAIN_NEGATED",
"76": "CHAIN_DISABLED",
"80": "CARD_SELECTED",
"81": "RANDOM_SELECTED",
"83": "BECOME_TARGET",
"90": "DRAW",
"91": "DAMAGE",
"92": "RECOVER",
"93": "EQUIP",
"94": "LPUPDATE",
"95": "UNEQUIP",
"96": "CARD_TARGET",
"97": "CANCEL_TARGET",
"100": "PAY_LPCOST",
"101": "ADD_COUNTER",
"102": "REMOVE_COUNTER",
"110": "ATTACK",
"111": "BATTLE",
"112": "ATTACK_DISABLED",
"113": "DAMAGE_STEP_START",
"114": "DAMAGE_STEP_END",
"120": "MISSED_EFFECT",
"121": "BE_CHAIN_TARGET",
"122": "CREATE_RELATION",
"123": "RELEASE_RELATION",
"130": "TOSS_COIN",
"131": "TOSS_DICE",
"132": "ROCK_PAPER_SCISSORS",
"133": "HAND_RES",
"140": "ANNOUNCE_RACE",
"141": "ANNOUNCE_ATTRIB",
"142": "ANNOUNCE_CARD",
"143": "ANNOUNCE_NUMBER",
"160": "CARD_HINT",
"161": "TAG_SWAP",
"162": "RELOAD_FIELD",
"163": "AI_NAME",
"164": "SHOW_HINT",
"170": "MATCH_KILL",
"180": "CUSTOM_MSG"
},
"TIMING":{
"1": "DRAW_PHASE",
"2": "STANDBY_PHASE",
"4": "MAIN_END",
"8": "BATTLE_START",
"16": "BATTLE_END",
"32": "END_PHASE",
"64": "SUMMON",
"128": "SPSUMMON",
"256": "FLIPSUMMON",
"512": "MSET",
"1024": "SSET",
"2048": "POS_CHANGE",
"4096": "ATTACK",
"8192": "DAMAGE_STEP",
"16384": "DAMAGE_CAL",
"32768": "CHAIN_END",
"65536": "DRAW",
"131072": "DAMAGE",
"262144": "RECOVER",
"524288": "DESTROY",
"1048576": "REMOVE",
"2097152": "TOHAND",
"4194304": "TODECK",
"8388608": "TOGRAVE",
"16777216": "BATTLE_PHASE",
"33554432": "EQUIP"
},
"TYPES": {
"TYPE_MONSTER": 1,
"TYPE_SPELL": 2,
"TYPE_TRAP": 4,
"TYPE_NORMAL": 16,
"TYPE_EFFECT": 32,
"TYPE_FUSION": 64,
"TYPE_RITUAL": 128,
"TYPE_TRAPMONSTER": 256,
"TYPE_SPIRIT": 512,
"TYPE_UNION": 1024,
"TYPE_DUAL": 2048,
"TYPE_TUNER": 4096,
"TYPE_SYNCHRO": 8192,
"TYPE_TOKEN": 16384,
"TYPE_QUICKPLAY": 65536,
"TYPE_CONTINUOUS": 131072,
"TYPE_EQUIP": 262144,
"TYPE_FIELD": 524288,
"TYPE_COUNTER": 1048576,
"TYPE_FLIP": 2097152,
"TYPE_TOON": 4194304,
"TYPE_XYZ": 8388608,
"TYPE_PENDULUM": 16777216,
"TYPE_SPSUMMON": 33554432,
"TYPE_LINK": 67108864
},
"RACES": {
"RACE_WARRIOR": 1,
"RACE_SPELLCASTER": 2,
"RACE_FAIRY": 4,
"RACE_FIEND": 8,
"RACE_ZOMBIE": 16,
"RACE_MACHINE": 32,
"RACE_AQUA": 64,
"RACE_PYRO": 128,
"RACE_ROCK": 256,
"RACE_WINDBEAST": 512,
"RACE_PLANT": 1024,
"RACE_INSECT": 2048,
"RACE_THUNDER": 4096,
"RACE_DRAGON": 8192,
"RACE_BEAST": 16384,
"RACE_BEASTWARRIOR": 32768,
"RACE_DINOSAUR": 65536,
"RACE_FISH": 131072,
"RACE_SEASERPENT": 262144,
"RACE_REPTILE": 524288,
"RACE_PSYCHO": 1048576,
"RACE_DEVINE": 2097152,
"RACE_CREATORGOD": 4194304,
"RACE_WYRM": 8388608,
"RACE_CYBERS": 16777216,
"RACE_ILLUSION": 33554432
},
"ATTRIBUTES": {
"ATTRIBUTE_EARTH": 1,
"ATTRIBUTE_WATER": 2,
"ATTRIBUTE_FIRE": 4,
"ATTRIBUTE_WIND": 8,
"ATTRIBUTE_LIGHT": 16,
"ATTRIBUTE_DARK": 32,
"ATTRIBUTE_DEVINE": 64
},
"LINK_MARKERS": {
"LINK_MARKER_BOTTOM_LEFT": 1,
"LINK_MARKER_BOTTOM": 2,
"LINK_MARKER_BOTTOM_RIGHT": 4,
"LINK_MARKER_LEFT": 8,
"LINK_MARKER_RIGHT": 32,
"LINK_MARKER_TOP_LEFT": 64,
"LINK_MARKER_TOP": 128,
"LINK_MARKER_TOP_RIGHT": 256
},
"DUEL_STAGE": {
"BEGIN": 0,
"FINGER": 1,
......
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadConstants = void 0;
const ygopro_msg_encode_1 = require("ygopro-msg-encode");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const loadConstantsJson = () => {
const filePath = path_1.default.join(__dirname, "data", "constants.json");
const raw = fs_1.default.readFileSync(filePath, "utf8");
return JSON.parse(raw);
};
const addMissingNumberEntries = (target, source, prefix) => {
for (const [key, value] of Object.entries(source)) {
if (!key.startsWith(prefix)) {
continue;
}
if (target[key] === undefined) {
target[key] = value;
}
}
};
const addMissingStringEntries = (target, source, prefix) => {
for (const [key, value] of Object.entries(source)) {
if (!key.startsWith(prefix)) {
continue;
}
const codeKey = String(value);
if (target[codeKey] === undefined) {
target[codeKey] = key.slice(prefix.length);
}
}
};
const legacyMsgFallback = {
"3": "WAITING",
"4": "START",
"6": "UPDATE_DATA",
"8": "REQUEST_DECK",
"21": "SORT_CHAIN",
"34": "REFRESH_DECK",
"39": "MSG_SHUFFLE_EXTRA",
"80": "CARD_SELECTED",
"95": "UNEQUIP",
"121": "BE_CHAIN_TARGET",
"122": "CREATE_RELATION",
"123": "RELEASE_RELATION",
};
const toConstantName = (name) => name
.replace(/^_+|_+$/g, "")
.replace(/([a-z0-9])([A-Z])/g, "$1_$2")
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2")
.toUpperCase();
const normalizeProtoName = (name) => name
.replace("TO_OBSERVER", "TOOBSERVER")
.replace("TO_DUELIST", "TODUELIST")
.replace("HS_NOT_READY", "HS_NOTREADY")
.replace("KICK", "HS_KICK");
const buildProtoMap = (registry, classPrefix) => {
const out = {};
for (const [id, cls] of registry.protos.entries()) {
const rawName = cls.name.startsWith(classPrefix)
? cls.name.slice(classPrefix.length)
: cls.name;
const name = normalizeProtoName(toConstantName(rawName));
out[String(id)] = name;
}
return out;
};
const loadConstants = () => {
const result = loadConstantsJson();
if (!result.TYPES)
result.TYPES = {};
if (!result.RACES)
result.RACES = {};
if (!result.ATTRIBUTES)
result.ATTRIBUTES = {};
if (!result.LINK_MARKERS)
result.LINK_MARKERS = {};
if (!result.MSG)
result.MSG = {};
if (!result.TIMING)
result.TIMING = {};
if (!result.CTOS)
result.CTOS = {};
if (!result.STOC)
result.STOC = {};
if (!result.NETWORK)
result.NETWORK = {};
if (!result.NETPLAYER)
result.NETPLAYER = {};
if (!result.PLAYERCHANGE)
result.PLAYERCHANGE = {};
if (!result.ERRMSG)
result.ERRMSG = {};
if (!result.MODE)
result.MODE = {};
if (!result.DUEL_STAGE)
result.DUEL_STAGE = {};
if (!result.COLORS)
result.COLORS = {};
addMissingNumberEntries(result.TYPES, ygopro_msg_encode_1.OcgcoreCommonConstants, "TYPE_");
addMissingNumberEntries(result.RACES, ygopro_msg_encode_1.OcgcoreCommonConstants, "RACE_");
addMissingNumberEntries(result.ATTRIBUTES, ygopro_msg_encode_1.OcgcoreCommonConstants, "ATTRIBUTE_");
addMissingNumberEntries(result.ATTRIBUTES, ygopro_msg_encode_1.OcgcoreScriptConstants, "ATTRIBUTE_");
addMissingNumberEntries(result.LINK_MARKERS, ygopro_msg_encode_1.OcgcoreCommonConstants, "LINK_MARKER_");
addMissingStringEntries(result.MSG, ygopro_msg_encode_1.OcgcoreCommonConstants, "MSG_");
addMissingStringEntries(result.TIMING, ygopro_msg_encode_1.OcgcoreScriptConstants, "TIMING_");
for (const [code, name] of Object.entries(legacyMsgFallback)) {
result.MSG[code] = name;
}
const ctosMap = buildProtoMap(ygopro_msg_encode_1.YGOProCtos, "YGOProCtos");
const stocMap = buildProtoMap(ygopro_msg_encode_1.YGOProStoc, "YGOProStoc");
const mismatches = [];
for (const [code, name] of Object.entries(ctosMap)) {
if (result.CTOS[code] !== undefined && result.CTOS[code] !== name) {
mismatches.push(`CTOS ${code}: ${result.CTOS[code]} != ${name}`);
}
result.CTOS[code] = name;
}
for (const [code, name] of Object.entries(stocMap)) {
if (result.STOC[code] !== undefined && result.STOC[code] !== name) {
mismatches.push(`STOC ${code}: ${result.STOC[code]} != ${name}`);
}
result.STOC[code] = name;
}
if (mismatches.length) {
throw new Error(`CTOS/STOC name mismatch between constants.json and registry:\\n${mismatches.join("\\n")}`);
}
if (result.RACES.RACE_CYBERS === undefined &&
result.RACES.RACE_CYBERSE !== undefined) {
result.RACES.RACE_CYBERS = result.RACES.RACE_CYBERSE;
}
return result;
};
exports.loadConstants = loadConstants;
exports.default = exports.loadConstants;
import {
OcgcoreCommonConstants,
OcgcoreScriptConstants,
YGOProCtos,
YGOProStoc,
} from "ygopro-msg-encode";
import fs from "fs";
import path from "path";
export interface ConstantsShape {
TYPES: Record<string, number>;
RACES: Record<string, number>;
ATTRIBUTES: Record<string, number>;
LINK_MARKERS: Record<string, number>;
DUEL_STAGE: Record<string, number>;
COLORS: Record<string, number>;
TIMING: Record<string, string>;
NETWORK: Record<string, string>;
NETPLAYER: Record<string, string>;
CTOS: Record<string, string>;
STOC: Record<string, string>;
PLAYERCHANGE: Record<string, string>;
ERRMSG: Record<string, string>;
MODE: Record<string, string>;
MSG: Record<string, string>;
}
type StringMap = Record<string, string>;
type NumberMap = Record<string, number>;
const loadConstantsJson = (): Partial<ConstantsShape> => {
const filePath = path.join(__dirname, "data", "constants.json");
const raw = fs.readFileSync(filePath, "utf8");
return JSON.parse(raw) as Partial<ConstantsShape>;
};
const addMissingNumberEntries = (
target: NumberMap,
source: Record<string, number>,
prefix: string
) => {
for (const [key, value] of Object.entries(source)) {
if (!key.startsWith(prefix)) {
continue;
}
if (target[key] === undefined) {
target[key] = value;
}
}
};
const addMissingStringEntries = (
target: StringMap,
source: Record<string, number>,
prefix: string
) => {
for (const [key, value] of Object.entries(source)) {
if (!key.startsWith(prefix)) {
continue;
}
const codeKey = String(value);
if (target[codeKey] === undefined) {
target[codeKey] = key.slice(prefix.length);
}
}
};
const legacyMsgFallback: Record<string, string> = {
"3": "WAITING",
"4": "START",
"6": "UPDATE_DATA",
"8": "REQUEST_DECK",
"21": "SORT_CHAIN",
"34": "REFRESH_DECK",
"39": "MSG_SHUFFLE_EXTRA",
"80": "CARD_SELECTED",
"95": "UNEQUIP",
"121": "BE_CHAIN_TARGET",
"122": "CREATE_RELATION",
"123": "RELEASE_RELATION",
};
const toConstantName = (name: string) =>
name
.replace(/^_+|_+$/g, "")
.replace(/([a-z0-9])([A-Z])/g, "$1_$2")
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2")
.toUpperCase();
const normalizeProtoName = (name: string) =>
name
.replace("TO_OBSERVER", "TOOBSERVER")
.replace("TO_DUELIST", "TODUELIST")
.replace("HS_NOT_READY", "HS_NOTREADY")
.replace("KICK", "HS_KICK");
const buildProtoMap = (
registry: { protos: Map<number, { name: string }> },
classPrefix: string
) => {
const out: Record<string, string> = {};
for (const [id, cls] of registry.protos.entries()) {
const rawName = cls.name.startsWith(classPrefix)
? cls.name.slice(classPrefix.length)
: cls.name;
const name = normalizeProtoName(toConstantName(rawName));
out[String(id)] = name;
}
return out;
};
export const loadConstants = () => {
const result = loadConstantsJson();
if (!result.TYPES) result.TYPES = {};
if (!result.RACES) result.RACES = {};
if (!result.ATTRIBUTES) result.ATTRIBUTES = {};
if (!result.LINK_MARKERS) result.LINK_MARKERS = {};
if (!result.MSG) result.MSG = {};
if (!result.TIMING) result.TIMING = {};
if (!result.CTOS) result.CTOS = {};
if (!result.STOC) result.STOC = {};
if (!result.NETWORK) result.NETWORK = {};
if (!result.NETPLAYER) result.NETPLAYER = {};
if (!result.PLAYERCHANGE) result.PLAYERCHANGE = {};
if (!result.ERRMSG) result.ERRMSG = {};
if (!result.MODE) result.MODE = {};
if (!result.DUEL_STAGE) result.DUEL_STAGE = {};
if (!result.COLORS) result.COLORS = {};
addMissingNumberEntries(result.TYPES, OcgcoreCommonConstants, "TYPE_");
addMissingNumberEntries(result.RACES, OcgcoreCommonConstants, "RACE_");
addMissingNumberEntries(result.ATTRIBUTES, OcgcoreCommonConstants, "ATTRIBUTE_");
addMissingNumberEntries(result.ATTRIBUTES, OcgcoreScriptConstants, "ATTRIBUTE_");
addMissingNumberEntries(result.LINK_MARKERS, OcgcoreCommonConstants, "LINK_MARKER_");
addMissingStringEntries(result.MSG, OcgcoreCommonConstants, "MSG_");
addMissingStringEntries(result.TIMING, OcgcoreScriptConstants, "TIMING_");
for (const [code, name] of Object.entries(legacyMsgFallback)) {
result.MSG[code] = name;
}
const ctosMap = buildProtoMap(YGOProCtos, "YGOProCtos");
const stocMap = buildProtoMap(YGOProStoc, "YGOProStoc");
const mismatches: string[] = [];
for (const [code, name] of Object.entries(ctosMap)) {
if (result.CTOS[code] !== undefined && result.CTOS[code] !== name) {
mismatches.push(`CTOS ${code}: ${result.CTOS[code]} != ${name}`);
}
result.CTOS[code] = name;
}
for (const [code, name] of Object.entries(stocMap)) {
if (result.STOC[code] !== undefined && result.STOC[code] !== name) {
mismatches.push(`STOC ${code}: ${result.STOC[code]} != ${name}`);
}
result.STOC[code] = name;
}
if (mismatches.length) {
throw new Error(
`CTOS/STOC name mismatch between constants.json and registry:\\n${mismatches.join("\\n")}`
);
}
if (
result.RACES.RACE_CYBERS === undefined &&
result.RACES.RACE_CYBERSE !== undefined
) {
result.RACES.RACE_CYBERS = result.RACES.RACE_CYBERSE;
}
return result as ConstantsShape;
};
export default loadConstants;
......@@ -34,6 +34,7 @@
"underscore.string": "^3.3.6",
"ws": "^8.9.0",
"ygopro-deck-encode": "^1.0.15",
"ygopro-msg-encode": "^1.1.5",
"ygopro-yrp-encode": "^1.0.1"
},
"devDependencies": {
......@@ -2613,9 +2614,9 @@
}
},
"node_modules/typed-reflector": {
"version": "1.0.11",
"resolved": "https://registry.npmjs.org/typed-reflector/-/typed-reflector-1.0.11.tgz",
"integrity": "sha512-OhryVYaR+tBEW9Yt2PsPqAniNfbVk1idKbnLxBCBPUSHVRm+Ajik/QxifoJUuGoaXAZDLW9JlJTO6ctXGZX9gQ==",
"version": "1.0.14",
"resolved": "https://registry.npmjs.org/typed-reflector/-/typed-reflector-1.0.14.tgz",
"integrity": "sha512-53t+jhADytooqNMpP6+hg/IYhFB3SYxGxrpn045XFEBLzn9mEzQmEVTQtdWg7hToPWvdOCMe5AGH1jlAPK5YFA==",
"license": "MIT",
"dependencies": {
"reflect-metadata": "^0.1.13"
......@@ -3192,6 +3193,17 @@
"integrity": "sha512-NMvgWuC3SKant50RDu0bHa3QIRlwBhdTR3bNDrMpNthTTQmCCq8i2HZaWFiCmYIBi9fR3W9CkFIgK6hI4d+PzQ==",
"license": "MIT"
},
"node_modules/ygopro-msg-encode": {
"version": "1.1.5",
"resolved": "https://registry.npmjs.org/ygopro-msg-encode/-/ygopro-msg-encode-1.1.5.tgz",
"integrity": "sha512-scsIDy7aBksNXCDiIEij23JIpVF1bosGvUJ2OGtfbcmuMEMbrUlX6JQp179AL86sjTkQg8DJ8cYj4UzEYMrrtQ==",
"license": "MIT",
"dependencies": {
"typed-reflector": "^1.0.14",
"ygopro-deck-encode": "^1.0.15",
"ygopro-yrp-encode": "^1.0.1"
}
},
"node_modules/ygopro-yrp-encode": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/ygopro-yrp-encode/-/ygopro-yrp-encode-1.0.1.tgz",
......@@ -5134,9 +5146,9 @@
}
},
"typed-reflector": {
"version": "1.0.11",
"resolved": "https://registry.npmjs.org/typed-reflector/-/typed-reflector-1.0.11.tgz",
"integrity": "sha512-OhryVYaR+tBEW9Yt2PsPqAniNfbVk1idKbnLxBCBPUSHVRm+Ajik/QxifoJUuGoaXAZDLW9JlJTO6ctXGZX9gQ==",
"version": "1.0.14",
"resolved": "https://registry.npmjs.org/typed-reflector/-/typed-reflector-1.0.14.tgz",
"integrity": "sha512-53t+jhADytooqNMpP6+hg/IYhFB3SYxGxrpn045XFEBLzn9mEzQmEVTQtdWg7hToPWvdOCMe5AGH1jlAPK5YFA==",
"requires": {
"reflect-metadata": "^0.1.13"
}
......@@ -5551,6 +5563,16 @@
"resolved": "https://registry.npmjs.org/ygopro-deck-encode/-/ygopro-deck-encode-1.0.15.tgz",
"integrity": "sha512-NMvgWuC3SKant50RDu0bHa3QIRlwBhdTR3bNDrMpNthTTQmCCq8i2HZaWFiCmYIBi9fR3W9CkFIgK6hI4d+PzQ=="
},
"ygopro-msg-encode": {
"version": "1.1.5",
"resolved": "https://registry.npmjs.org/ygopro-msg-encode/-/ygopro-msg-encode-1.1.5.tgz",
"integrity": "sha512-scsIDy7aBksNXCDiIEij23JIpVF1bosGvUJ2OGtfbcmuMEMbrUlX6JQp179AL86sjTkQg8DJ8cYj4UzEYMrrtQ==",
"requires": {
"typed-reflector": "^1.0.14",
"ygopro-deck-encode": "^1.0.15",
"ygopro-yrp-encode": "^1.0.1"
}
},
"ygopro-yrp-encode": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/ygopro-yrp-encode/-/ygopro-yrp-encode-1.0.1.tgz",
......
......@@ -10,7 +10,7 @@ var fs = require('fs');
var initSqlJs = require('sql.js');
var loadJSON = require('load-json-file').sync;
var config = loadJSON('./config/deckstats.json'); //{ "deckpath": "../decks", "dbfile": "cards.cdb" }
var constants = loadJSON('./data/constants.json');
var constants = require('./load-constants').loadConstants();
var ALL_MAIN_CARDS={};
var ALL_SIDE_CARDS={};
......
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fromPartialCompat = exports.applyYGOProMsgStructCompat = exports.defineYGOProMsgStructCompat = exports.YGOProMsgStructCompat = void 0;
const ygopro_deck_encode_1 = __importDefault(require("ygopro-deck-encode"));
const ygopro_msg_encode_1 = require("ygopro-msg-encode");
exports.YGOProMsgStructCompat = new Map();
const defineYGOProMsgStructCompat = (cls, field, getterAndSetter) => {
if (!exports.YGOProMsgStructCompat.has(cls)) {
exports.YGOProMsgStructCompat.set(cls, []);
}
exports.YGOProMsgStructCompat.get(cls).push((inst) => {
Object.defineProperty(inst, field, {
get() {
return getterAndSetter.get(inst);
},
set(value) {
getterAndSetter.set(inst, value);
}
});
});
};
exports.defineYGOProMsgStructCompat = defineYGOProMsgStructCompat;
const applyYGOProMsgStructCompat = (inst) => {
const compatList = exports.YGOProMsgStructCompat.get(inst.constructor);
if (compatList) {
compatList.forEach(compat => compat(inst));
}
return inst;
};
exports.applyYGOProMsgStructCompat = applyYGOProMsgStructCompat;
const fromPartialCompat = (cls, input) => {
const inst1 = new cls();
(0, exports.applyYGOProMsgStructCompat)(inst1);
Object.assign(inst1, input);
const inst2 = new cls().fromPartial(inst1);
(0, exports.applyYGOProMsgStructCompat)(inst2);
return inst2;
};
exports.fromPartialCompat = fromPartialCompat;
const compatDeckState = new WeakMap();
const getCompatDeckState = (inst) => {
let state = compatDeckState.get(inst);
if (!state) {
state = {};
compatDeckState.set(inst, state);
}
return state;
};
(0, exports.defineYGOProMsgStructCompat)(ygopro_msg_encode_1.YGOProStocChat, "player", {
get(inst) {
return inst.player_type;
},
set(inst, value) {
inst.player_type = value;
},
});
(0, exports.defineYGOProMsgStructCompat)(ygopro_msg_encode_1.YGOProStocDeckCount, "mainc_s", {
get(inst) {
return inst.player0DeckCount?.main ?? 0;
},
set(inst, value) {
if (!inst.player0DeckCount) {
inst.player0DeckCount = new ygopro_msg_encode_1.YGOProStocDeckCount_DeckInfo();
}
inst.player0DeckCount.main = value;
},
});
(0, exports.defineYGOProMsgStructCompat)(ygopro_msg_encode_1.YGOProStocDeckCount, "sidec_s", {
get(inst) {
return inst.player0DeckCount?.side ?? 0;
},
set(inst, value) {
if (!inst.player0DeckCount) {
inst.player0DeckCount = new ygopro_msg_encode_1.YGOProStocDeckCount_DeckInfo();
}
inst.player0DeckCount.side = value;
},
});
(0, exports.defineYGOProMsgStructCompat)(ygopro_msg_encode_1.YGOProStocDeckCount, "extrac_s", {
get(inst) {
return inst.player0DeckCount?.extra ?? 0;
},
set(inst, value) {
if (!inst.player0DeckCount) {
inst.player0DeckCount = new ygopro_msg_encode_1.YGOProStocDeckCount_DeckInfo();
}
inst.player0DeckCount.extra = value;
},
});
(0, exports.defineYGOProMsgStructCompat)(ygopro_msg_encode_1.YGOProStocDeckCount, "mainc_o", {
get(inst) {
return inst.player1DeckCount?.main ?? 0;
},
set(inst, value) {
if (!inst.player1DeckCount) {
inst.player1DeckCount = new ygopro_msg_encode_1.YGOProStocDeckCount_DeckInfo();
}
inst.player1DeckCount.main = value;
},
});
(0, exports.defineYGOProMsgStructCompat)(ygopro_msg_encode_1.YGOProStocDeckCount, "sidec_o", {
get(inst) {
return inst.player1DeckCount?.side ?? 0;
},
set(inst, value) {
if (!inst.player1DeckCount) {
inst.player1DeckCount = new ygopro_msg_encode_1.YGOProStocDeckCount_DeckInfo();
}
inst.player1DeckCount.side = value;
},
});
(0, exports.defineYGOProMsgStructCompat)(ygopro_msg_encode_1.YGOProStocDeckCount, "extrac_o", {
get(inst) {
return inst.player1DeckCount?.extra ?? 0;
},
set(inst, value) {
if (!inst.player1DeckCount) {
inst.player1DeckCount = new ygopro_msg_encode_1.YGOProStocDeckCount_DeckInfo();
}
inst.player1DeckCount.extra = value;
},
});
(0, exports.defineYGOProMsgStructCompat)(ygopro_msg_encode_1.YGOProCtosUpdateDeck, "mainc", {
get(inst) {
return inst.deck.main.length + inst.deck.extra.length;
},
set(inst, value) {
const state = getCompatDeckState(inst);
state.mainc = value;
},
});
(0, exports.defineYGOProMsgStructCompat)(ygopro_msg_encode_1.YGOProCtosUpdateDeck, "sidec", {
get(inst) {
return inst.deck.side.length;
},
set(inst, value) {
const state = getCompatDeckState(inst);
state.sidec = value;
},
});
(0, exports.defineYGOProMsgStructCompat)(ygopro_msg_encode_1.YGOProCtosUpdateDeck, "deckbuf", {
get(inst) {
return [...inst.deck.main, ...inst.deck.extra, ...inst.deck.side];
},
set(inst, value) {
const state = getCompatDeckState(inst);
const deckbuf = Array.isArray(value) ? value.slice() : [];
state.deckbuf = deckbuf;
if (!inst.deck) {
inst.deck = new ygopro_deck_encode_1.default();
}
const hasMainc = state.mainc !== undefined;
const hasSidec = state.sidec !== undefined;
if (!hasMainc && !hasSidec) {
inst.deck.main = deckbuf.slice();
inst.deck.extra = [];
inst.deck.side = [];
return;
}
if (hasMainc && !hasSidec) {
const mainc = Math.max(0, state.mainc | 0);
const mainWithExtra = deckbuf.slice(0, mainc);
const side = deckbuf.slice(mainc);
inst.deck.main = mainWithExtra.slice();
inst.deck.extra = [];
inst.deck.side = side.slice();
return;
}
if (!hasMainc && hasSidec) {
const sidec = Math.max(0, state.sidec | 0);
const split = Math.max(0, deckbuf.length - sidec);
const mainWithExtra = deckbuf.slice(0, split);
const side = deckbuf.slice(split);
inst.deck.main = mainWithExtra.slice();
inst.deck.extra = [];
inst.deck.side = side.slice();
return;
}
const mainc = Math.max(0, state.mainc | 0);
const sidec = Math.max(0, state.sidec | 0);
const mainWithExtra = deckbuf.slice(0, mainc);
const side = deckbuf.slice(mainc, mainc + sidec);
inst.deck.main = mainWithExtra.slice();
inst.deck.extra = [];
inst.deck.side = side.slice();
},
});
import YGOProDeck from "ygopro-deck-encode";
import {
YGOProCtosUpdateDeck,
YGOProCtosBase, YGOProStocBase,
YGOProStocChat,
YGOProStocDeckCount,
YGOProStocDeckCount_DeckInfo,
} from "ygopro-msg-encode";
export const YGOProMsgStructCompat = new Map<new (...args: any[]) => YGOProCtosBase | YGOProStocBase, ((inst: YGOProCtosBase | YGOProStocBase) => void)[]>();
export const defineYGOProMsgStructCompat = <T extends YGOProCtosBase | YGOProStocBase>(cls: new (...args: any[]) => T, field: string, getterAndSetter: {
get: (inst: T) => any,
set: (inst: T, value: any) => void
}) => {
if (!YGOProMsgStructCompat.has(cls)) {
YGOProMsgStructCompat.set(cls, []);
}
YGOProMsgStructCompat.get(cls)!.push((inst: YGOProCtosBase | YGOProStocBase) => {
Object.defineProperty(inst, field, {
get() {
return getterAndSetter.get(inst as T);
},
set(value) {
getterAndSetter.set(inst as T, value);
}
})
})
}
export const applyYGOProMsgStructCompat = (inst: YGOProCtosBase | YGOProStocBase) => {
const compatList = YGOProMsgStructCompat.get(inst.constructor as typeof YGOProCtosBase | typeof YGOProStocBase);
if (compatList) {
compatList.forEach(compat => compat(inst));
}
return inst;
}
export const fromPartialCompat = <T extends YGOProCtosBase | YGOProStocBase>(
cls: new (...args: any[]) => T,
input: Partial<T>
): T => {
const inst1 = new cls();
applyYGOProMsgStructCompat(inst1);
Object.assign(inst1, input);
const inst2 = new cls().fromPartial(inst1 as Partial<T>) as T;
applyYGOProMsgStructCompat(inst2);
return inst2;
};
const compatDeckState = new WeakMap<
YGOProCtosUpdateDeck,
{ mainc?: number; sidec?: number; deckbuf?: number[] }
>();
const getCompatDeckState = (inst: YGOProCtosUpdateDeck) => {
let state = compatDeckState.get(inst);
if (!state) {
state = {};
compatDeckState.set(inst, state);
}
return state;
};
defineYGOProMsgStructCompat(YGOProStocChat, "player", {
get(inst) {
return inst.player_type;
},
set(inst, value) {
inst.player_type = value;
},
});
defineYGOProMsgStructCompat(YGOProStocDeckCount, "mainc_s", {
get(inst) {
return inst.player0DeckCount?.main ?? 0;
},
set(inst, value) {
if (!inst.player0DeckCount) {
inst.player0DeckCount = new YGOProStocDeckCount_DeckInfo();
}
inst.player0DeckCount.main = value;
},
});
defineYGOProMsgStructCompat(YGOProStocDeckCount, "sidec_s", {
get(inst) {
return inst.player0DeckCount?.side ?? 0;
},
set(inst, value) {
if (!inst.player0DeckCount) {
inst.player0DeckCount = new YGOProStocDeckCount_DeckInfo();
}
inst.player0DeckCount.side = value;
},
});
defineYGOProMsgStructCompat(YGOProStocDeckCount, "extrac_s", {
get(inst) {
return inst.player0DeckCount?.extra ?? 0;
},
set(inst, value) {
if (!inst.player0DeckCount) {
inst.player0DeckCount = new YGOProStocDeckCount_DeckInfo();
}
inst.player0DeckCount.extra = value;
},
});
defineYGOProMsgStructCompat(YGOProStocDeckCount, "mainc_o", {
get(inst) {
return inst.player1DeckCount?.main ?? 0;
},
set(inst, value) {
if (!inst.player1DeckCount) {
inst.player1DeckCount = new YGOProStocDeckCount_DeckInfo();
}
inst.player1DeckCount.main = value;
},
});
defineYGOProMsgStructCompat(YGOProStocDeckCount, "sidec_o", {
get(inst) {
return inst.player1DeckCount?.side ?? 0;
},
set(inst, value) {
if (!inst.player1DeckCount) {
inst.player1DeckCount = new YGOProStocDeckCount_DeckInfo();
}
inst.player1DeckCount.side = value;
},
});
defineYGOProMsgStructCompat(YGOProStocDeckCount, "extrac_o", {
get(inst) {
return inst.player1DeckCount?.extra ?? 0;
},
set(inst, value) {
if (!inst.player1DeckCount) {
inst.player1DeckCount = new YGOProStocDeckCount_DeckInfo();
}
inst.player1DeckCount.extra = value;
},
});
defineYGOProMsgStructCompat(YGOProCtosUpdateDeck, "mainc", {
get(inst) {
return inst.deck.main.length + inst.deck.extra.length;
},
set(inst, value) {
const state = getCompatDeckState(inst);
state.mainc = value;
},
});
defineYGOProMsgStructCompat(YGOProCtosUpdateDeck, "sidec", {
get(inst) {
return inst.deck.side.length;
},
set(inst, value) {
const state = getCompatDeckState(inst);
state.sidec = value;
},
});
defineYGOProMsgStructCompat(YGOProCtosUpdateDeck, "deckbuf", {
get(inst) {
return [...inst.deck.main, ...inst.deck.extra, ...inst.deck.side];
},
set(inst, value) {
const state = getCompatDeckState(inst);
const deckbuf = Array.isArray(value) ? value.slice() : [];
state.deckbuf = deckbuf;
if (!inst.deck) {
inst.deck = new YGOProDeck();
}
const hasMainc = state.mainc !== undefined;
const hasSidec = state.sidec !== undefined;
if (!hasMainc && !hasSidec) {
inst.deck.main = deckbuf.slice();
inst.deck.extra = [];
inst.deck.side = [];
return;
}
if (hasMainc && !hasSidec) {
const mainc = Math.max(0, state.mainc | 0);
const mainWithExtra = deckbuf.slice(0, mainc);
const side = deckbuf.slice(mainc);
inst.deck.main = mainWithExtra.slice();
inst.deck.extra = [];
inst.deck.side = side.slice();
return;
}
if (!hasMainc && hasSidec) {
const sidec = Math.max(0, state.sidec | 0);
const split = Math.max(0, deckbuf.length - sidec);
const mainWithExtra = deckbuf.slice(0, split);
const side = deckbuf.slice(split);
inst.deck.main = mainWithExtra.slice();
inst.deck.extra = [];
inst.deck.side = side.slice();
return;
}
const mainc = Math.max(0, state.mainc | 0);
const sidec = Math.max(0, state.sidec | 0);
const mainWithExtra = deckbuf.slice(0, mainc);
const side = deckbuf.slice(mainc, mainc + sidec);
inst.deck.main = mainWithExtra.slice();
inst.deck.extra = [];
inst.deck.side = side.slice();
},
});
......@@ -22,7 +22,7 @@ var loadJSON = require('load-json-file').sync;
var auth = require('./ygopro-auth.js');
var constants = loadJSON('./data/constants.json');
var constants = require('./load-constants').loadConstants();
var settings = loadJSON('./config/config.json');
config = settings.modules.pre_util;
......
......@@ -21,7 +21,7 @@ var loadJSON = require('load-json-file').sync;
var auth = require('./ygopro-auth.js');
var constants = loadJSON('./data/constants.json');
var constants = require('./load-constants').loadConstants();
var settings = loadJSON('./config/config.json');
config = settings.modules.update_util;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment