From 0660180c5382106e25f7d2c445ebb51c4b352bec Mon Sep 17 00:00:00 2001 From: hide_d Date: Tue, 28 Dec 2021 04:28:26 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20APIPathGen=20=EB=8F=84=EC=9A=B0?= =?UTF-8?q?=EB=AF=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/ts/APIPath.ts | 34 ++++++++++++++++++++++++++++++++++ hwe/ts/defs.ts | 19 ++++++++++++++++--- hwe/ts/util/APIPathGen.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 hwe/ts/APIPath.ts create mode 100644 hwe/ts/util/APIPathGen.ts diff --git a/hwe/ts/APIPath.ts b/hwe/ts/APIPath.ts new file mode 100644 index 00000000..76145b53 --- /dev/null +++ b/hwe/ts/APIPath.ts @@ -0,0 +1,34 @@ +import { APIPathGen } from "./util/APIPathGen"; + +const apiRealPath = { + Command: { + GetReservedCommand: '', + PushCommand: '', + RepeatCommand: '', + ReserveCommand: '', + }, + General: { + Join: '', + }, + InheritAction: { + BuyHiddenBuff: '', + BuyRandomUnique: '', + BuySpecificUnique: '', + ResetSpecialWar: '', + ResetTurnTime: '', + SetNextSpecialWar: '', + }, + Misc: { UploadImage: '' }, + NationCommand: { + GetReservedCommand: '', + PushCommand: '', + RepeatCommand: '', + ReserveCommand: '', + }, + Nation: { + SetNotice: '', + SetScoutMsg: '', + }, +} as const; + +export const APIPath = APIPathGen(apiRealPath); \ No newline at end of file diff --git a/hwe/ts/defs.ts b/hwe/ts/defs.ts index f7e0fc3e..c5840288 100644 --- a/hwe/ts/defs.ts +++ b/hwe/ts/defs.ts @@ -163,11 +163,11 @@ export const NoneValue = 'None' as const; export type Optional = { [Property in keyof Type]+?: Type[Property]; - }; +}; export type OptionalFull = { - [Property in keyof Type]: Type[Property]|undefined; - }; + [Property in keyof Type]: Type[Property] | undefined; +}; export type commandItem = { value: string; @@ -210,3 +210,16 @@ export type ChiefResponse = { values: commandItem[]; }[]; }; + + +type diplomacyInfo = { + name: string, + color?: string, +} +export type diplomacyState = 0 | 1 | 2 | 7; +export const diplomacyStateInfo: Record = { + 0: { name: '교전', color: 'red' }, + 1: { name: '선포중', color: 'magenta' }, + 2: { name: '통상' }, + 7: { name: '불가침', color: 'green' }, +} diff --git a/hwe/ts/util/APIPathGen.ts b/hwe/ts/util/APIPathGen.ts new file mode 100644 index 00000000..e0f6b291 --- /dev/null +++ b/hwe/ts/util/APIPathGen.ts @@ -0,0 +1,30 @@ +type SubValue = string | { [property: string]: SubValue }; + +const hasKey = >(obj: T, k: string | symbol | number): k is keyof T => + k in obj; + +export function APIPathGen(obj: Record, path?: string[]): Record { + return new Proxy(obj, { + get(target, key: K) { + if (path === undefined) { + path = [key]; + } + else { + path.push(key); + } + + + if (hasKey(target, key)) { + const next: V = target[key]; + if (typeof (next) === 'string') { + return path.join('/'); + } + if (typeof (next) === 'object') { + return APIPathGen(next, path); + } + throw 'unknown'; + } + throw `${path.join('/')} is not exists`; + } + }) +} \ No newline at end of file