대충이런느낌

This commit is contained in:
2023-08-08 17:10:15 +00:00
parent c7d0702e66
commit e381dd5a7e
10 changed files with 296 additions and 94 deletions
+5 -2
View File
@@ -1,4 +1,5 @@
import { LoginCtx } from "./ReqLogin";
import { SessionCtx } from "./StartSession";
import type { ProcDecoratorGenerator } from "./base";
import type { Request, Response } from "express";
@@ -7,10 +8,12 @@ export interface UserLevelCtx {
userLevel: number;
}
export function ParseUserLevel<Q extends LoginCtx>(): ProcDecoratorGenerator<UserLevelCtx, Q> {
export function ParseUserLevel<Q extends LoginCtx & SessionCtx>(): ProcDecoratorGenerator<UserLevelCtx, Q> {
return (inCtx) => {
const userLevel = 123;
inCtx.setValue('userLevel', userLevel);
return {
userLevel: 123,
userLevel,
...inCtx,
}
}
+18 -3
View File
@@ -1,15 +1,30 @@
import { LoginCtx } from "./ReqLogin";
import { SessionCtx } from "./StartSession";
import type { ProcDecoratorGenerator } from "./base";
import type { Request, Response } from "express";
import { delay } from "../../util/delay.js";
export type GameLoginCtx = {
generalID: number;
}
export function ReqGameLogin<Q extends LoginCtx>(): ProcDecoratorGenerator<GameLoginCtx, Q> {
return (inCtx) => {
export function ReqGameLogin<Q extends LoginCtx & SessionCtx>(): ProcDecoratorGenerator<GameLoginCtx, Q> {
return async (inCtx) => {
const ctx: Q & Partial<GameLoginCtx> = inCtx;
if (ctx.generalID !== undefined) {
return {
generalID: ctx.generalID,
...inCtx,
};
}
console.log('Something GameLogin');
await delay(0);
inCtx.setValue('generalID', ctx.userID * 4);
return {
generalID: inCtx.userID * 4,
generalID: ctx.userID * 4,
...inCtx,
}
}
+15 -12
View File
@@ -1,20 +1,23 @@
import type { Empty, ProcDecoratorPrePostGenerator } from "./base";
import { SessionCtx } from "./StartSession";
import type { InvalidProc, ProcDecoratorGenerator } from "./base";
export type LoginCtx = {
userID: number;
}
export function ReqLogin<Q extends object = Empty>(): ProcDecoratorPrePostGenerator<LoginCtx, Q> {
return [
(inCtx) => {
export function ReqLogin<Q extends SessionCtx>(): ProcDecoratorGenerator<LoginCtx, Q> {
return (inCtx) => {
const ctx: Q & Partial<LoginCtx> = inCtx;
const userID = ctx.userID;
if (userID === undefined) {
return {
userID: 999,
...inCtx,
};
},
(inCtx) => {
console.log('ReqLogin post');
return inCtx;
result: false,
reason: 'Required Login',
invalidProcSymbol: 'ReqLogin'
} satisfies InvalidProc;
}
]
return ctx as LoginCtx & Q;
}
}
+39
View File
@@ -0,0 +1,39 @@
import type { Empty, ProcDecoratorGenerator } from "./base";
export type SessionCtx = {
clearSession: () => Promise<void>;
deleteValue: (key: string) => Promise<void>;
getValue: <T>(key: string) => Promise<T | undefined>;
setValue: (key: string, value: unknown) => Promise<void>;
sessionRaw: {
raw: object;
changed: boolean;
};
}
export function StartSession<Q extends object = Empty>(): ProcDecoratorGenerator<SessionCtx, Q> {
return (inCtx) => {
const raw: object = {};
return {
clearSession: () => Promise.resolve(),
deleteValue: async (key: string) => {
console.log('deleteValue', key);
if (raw[key] !== undefined) {
delete raw[key];
}
},
getValue: async <T>(key: string) => {
return raw[key] as T | undefined;
},
setValue: async (key: string, value: unknown) => {
console.log('setValue', key, value);
raw[key] = value;
},
sessionRaw: {
raw,
changed: false,
},
...inCtx,
};
}
}