This commit is contained in:
2023-09-23 16:24:32 +00:00
parent ffd3ab681e
commit bfbc0134de
31 changed files with 304 additions and 181 deletions
+25
View File
@@ -0,0 +1,25 @@
import type { StateIncrementer } from '@sammo/server_util';
import SchemaSequence from './schema/SchemaSequence.js';
import { InvalidArgument } from '@sammo/util';
export function MongoSequenceFactory(collectionName: string): StateIncrementer{
return async (increase: number) => {
if(increase <= 0){
throw new InvalidArgument('increase must be > 0');
}
increase = Math.ceil(increase);
const result = await SchemaSequence.findOneAndUpdate({
collectionName,
}, {
$inc: {
nextSeq: increase,
},
}, {
upsert: true,
new: true,
});
return result.nextSeq;
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
import 'dotenv/config';
import { connect, Mongoose } from "mongoose";
import { unwrap } from './util/unwrap.js';
import { unwrap } from '@sammo/util';
const dbConfig = {
host: unwrap(process.env.GAME_DB_HOST),
+3
View File
@@ -0,0 +1,3 @@
import { resolve } from "node:path";
export const rootPath = resolve(resolve(), '../..');
+49
View File
@@ -0,0 +1,49 @@
import dotenv from 'dotenv';
import { rootPath } from './constPath.js';
import { unwrap } from '@sammo/util';
let init = false;
const dirPath = rootPath
console.log(rootPath);
function initDotEnv() {
if (process.env['NODE_ENV'] == 'production') {
dotenv.config({ path: `${dirPath}/.env.production.local` });
dotenv.config({ path: `${dirPath}/.env.local` });
dotenv.config({ path: `${dirPath}/.env.production` });
}
else {
dotenv.config({ path: `${dirPath}/.env.development.local` });
dotenv.config({ path: `${dirPath}/.env.local` });
dotenv.config({ path: `${dirPath}/.env.development` });
}
dotenv.config();
init = true;
}
if (!init) {
initDotEnv();
}
let _ownConfig: ReturnType<typeof generateConfig>|undefined = undefined;
function generateConfig() {
return {
port: parseInt(process.env['SERVER_PORT'] ?? "3001"),
sessionSecret: unwrap(process.env['SESSION_SECRET']),
gatewayHost: unwrap(process.env.GATEWAY_HOST),
gatewayPort: Number(unwrap(process.env.GATEWAY_PORT)),
apiRootPath: process.env['API_ROOT_PATH'] ?? '/api',
} as const;
}
export function serverConfig() {
if(_ownConfig !== undefined){
return _ownConfig;
}
if(!init){
initDotEnv();
}
_ownConfig = generateConfig();
return _ownConfig;
}
@@ -1,6 +1,6 @@
import type { SessionCtx } from "@sammo/server_util";
import type { LoginCtx } from "./ReqLogin.js";
import type { SessionCtx } from "./StartSession.js";
import type { ProcDecoratorGenerator } from "./base.js";
import type { ProcDecoratorGenerator } from "@strpc/express/proc_decorator";
export type GameLoginCtx = {
generalID: number;
@@ -12,11 +12,11 @@ export function ReqGameLogin<Q extends LoginCtx & SessionCtx>(): ProcDecoratorGe
return async (ctx) => {
//NOTE: 게임 서버별로 gameLoginCtx를 따로 가져야 하는가?
let gameLoginCtx = ctx.session.getItem<GameLoginCtx>(`gameLoginCtx`);
if(gameLoginCtx){
if(gameLoginCtx.gameLoginDate >= ctx.loginDate){
if (gameLoginCtx) {
if (gameLoginCtx.gameLoginDate >= ctx.loginDate) {
return [{
result: true,
},{
}, {
...gameLoginCtx,
...ctx,
}];
@@ -25,11 +25,12 @@ export function ReqGameLogin<Q extends LoginCtx & SessionCtx>(): ProcDecoratorGe
}
//TODO: DB에서 generalID를 가져오는 로직
return [{
result: false,
type: 'Required GameLogin',
info: 'NotYetImplemented'
}, ctx];
}, {
...ctx
}];
}
}
+3 -3
View File
@@ -1,6 +1,6 @@
import type { ServerActionType } from "../schema/gateway/User.js";
import type { SessionCtx } from "./StartSession.js";
import type { ProcDecorator } from "./base.js";
import type { SessionCtx } from "@sammo/server_util";
import type { ProcDecorator } from "@strpc/express/proc_decorator";
import type { ServerActionType } from "@sammo/gateway_server/exports"
export type LoginCtx = {
userID: number;
@@ -0,0 +1,23 @@
import { Schema, model, Types } from 'mongoose';
interface ISchemaSequence {
_id: Types.ObjectId;
collectionName: string;
nextSeq: number;
}
export const SchemaSequence = new Schema<ISchemaSequence>({
collectionName: { type: String, required: true },
nextSeq: {
type: Number, required: true,
get: (v: number) => Math.ceil(v),
set: (v: number) => Math.ceil(v),
default: 0,
},
}, { autoIndex: true, autoCreate: true })
.index({ collectionName: 1 }, { unique: true })
;
export default model<ISchemaSequence>('SchemaSequence', SchemaSequence);