finalized build
authorxangelo <git@xangelo.ca>
Thu, 1 Jun 2023 17:15:09 +0000 (13:15 -0400)
committerxangelo <git@xangelo.ca>
Thu, 1 Jun 2023 17:15:09 +0000 (13:15 -0400)
17 files changed:
.gitignore
package.json
seeds/monsters.ts
seeds/shop_items.ts
src/client/index.ts
src/server/api.ts
src/server/equipment.ts
src/server/inventory.ts
src/server/lib/redis.ts [deleted file]
src/server/lib/router.ts [deleted file]
src/server/lib/server.ts [deleted file]
src/server/monster.ts
src/server/player.ts
src/server/shopItem.ts
src/shared/equipped.ts
src/shared/player.ts
tsconfig.json

index 2d7ec5ce8ba1908fdb6be5c6999f89716b24a06b..ddc3c2a73e0c0ad88960d9b519fde96db66a2328 100644 (file)
@@ -1,2 +1,3 @@
 .env
 node_modules/
+dist/
index be1a13ea93131b6d8e612275322c7b9ba5d5e738..55c29b47c27d32bd3a26e30fc9684cb5a716aefb 100644 (file)
@@ -2,7 +2,8 @@
   "name": "control-up-takehome",
   "scripts": {
     "up": "npx prisma migrate dev --name \"init\"",
-    "start": "npx ts-node src/api.ts",
+    "start": "pm2 start dist/server/api.js",
+    "build": "npx webpack && npx tsc",
     "dev:client": "npx webpack -w",
     "dev:server": "npx nodemon src/server/api.ts"
   },
index 270d3f082422eff7e568407269c6ed668da0409e..a3d3ab8995af6539b53d24bc5455f1bd68e30c5f 100644 (file)
@@ -2,7 +2,7 @@ import { Knex } from "knex";
 import { join } from 'path';
 import { readFile } from 'fs';
 import { promisify } from 'util';
-import {Monster} from "@shared/monsters";
+import {Monster} from "../src/shared/monsters";
 
 const read = promisify(readFile);
 
index 43e04f11676487ee9d802e7231c7d4e703dc8c11..ea9e1085aa015d3532f5dc0d1e9a9735290d5c3a 100644 (file)
@@ -1,9 +1,11 @@
-import {ShopItem} from "@shared/inventory";
+import {ShopItem} from "../src/shared/inventory";
 import { Knex } from "knex";
 
 export async function seed(knex: Knex): Promise<void> {
   // Deletes ALL existing entries
   await knex("shop_items").del();
+  await knex("equipped").del();
+  await knex("inventory").del();
 
   const data: Partial<ShopItem>[] = [
     {
index 6c549d2dc402ada4b9986acc33c015dc69bb08e4..c6624e8961a1505242916095ebdaa718abdd1374 100644 (file)
@@ -1,15 +1,15 @@
-import {Message} from '@shared/message';
+import {Message} from '../shared/message';
 import { io } from 'socket.io-client';
 import $ from 'jquery';
-import {expToLevel, maxHp, Player} from '@shared/player';
+import {expToLevel, maxHp, Player} from '../shared/player';
 import { authToken, http } from './http';
 import { CustomEventManager } from './events';
-import {MonsterForFight, MonsterForList} from '@shared/monsters';
-import {FightRound} from '@shared/fight';
-import { city } from '@shared/map'
+import {MonsterForFight, MonsterForList} from '../shared/monsters';
+import {FightRound} from '../shared/fight';
+import { city } from '../shared/map'
 import { v4 as uuid } from 'uuid';
-import {InventoryItem, ShopItem, SubType} from '@shared/inventory';
-import {EquippedItemDetails} from '@shared/equipped';
+import {ShopItem, SubType} from '../shared/inventory';
+import {EquippedItemDetails} from '../shared/equipped';
 
 const cache = new Map<string, any>();
 const events = new CustomEventManager();
index 2fa3317656d099a85a8d16f8e166ee99203e145b..9407457ce1b55a084c5bd987f5316c4369bee9bb 100644 (file)
@@ -3,21 +3,20 @@ import { join } from 'path';
 import express, {Request, Response} from 'express';
 import http from 'http';
 import { Server, Socket } from 'socket.io';
-import { logger } from '@lib/logger';
+import { logger } from './lib/logger';
 import { loadPlayer, createPlayer, updatePlayer } from './player';
-import { redis } from '@lib/redis';
 import * as _ from 'lodash';
-import {broadcastMessage} from '@shared/message';
-import {expToLevel, maxHp, Player, professionList} from '@shared/player';
+import {broadcastMessage} from '../shared/message';
+import {expToLevel, maxHp, Player, professionList} from '../shared/player';
 import {clearFight, createFight, getMonsterList, loadMonster, loadMonsterFromFight, saveFightState} from './monster';
-import {FightRound} from '@shared/fight';
+import {FightRound} from '../shared/fight';
 import {addInventoryItem, getEquippedItems, getInventory, getInventoryItem, updateAp} from './inventory';
-import {Monster, MonsterForList} from '@shared/monsters';
+import {Monster, MonsterForList} from '../shared/monsters';
 import {getShopItem, listShopItems} from './shopItem';
 import {equip, unequip} from './equipment';
 import { v4 as uuid } from 'uuid';
-import {calculateMaxAp, EquippedItemDetails} from '@shared/equipped';
-import {ArmourType, SubType} from '@shared/inventory';
+import {EquippedItemDetails} from '../shared/equipped';
+import {ArmourType, SubType} from '../shared/inventory';
 
 dotenv();
 
@@ -29,6 +28,8 @@ app.use(express.json());
 
 const io = new Server(server);
 
+const cache: Record<string, any> = {};
+
 function calcAp(inventoryItem: EquippedItemDetails[], socket: Socket) {
   const ap: Record<SubType, {currentAp: number, maxAp: number}> | Record<any, any> = {};
   inventoryItem.forEach(item => {
@@ -58,7 +59,7 @@ io.on('connection', async socket => {
     player = await createPlayer();
   }
 
-  await redis.set(`token:${player.id}`, socket.id);
+  cache[`token:${player.id}`] = socket.id;
 
   logger.log(`Socket [${socket.id}] auth token: ${player.id}`);
 
index ae5637b3f0d4ab99f349ce3ef2ca64703e3c3930..e48c8b5613ee2a80d643d58e70ecf8f3cfd111a4 100644 (file)
@@ -1,6 +1,6 @@
-import {db} from "@lib/db";
-import {EquippedItem, EquippedItemDetails} from "@shared/equipped";
-import {InventoryItem} from "@shared/inventory";
+import {db} from "./lib/db";
+import {EquippedItem, EquippedItemDetails} from "../shared/equipped";
+import {InventoryItem} from "../shared/inventory";
 
 export async function getEquippedItems(playerId: string): Promise<EquippedItem[]> {
   return db.raw<EquippedItemDetails[]>(`
index 5c6ea3d2d5f454a6f6f3c4b2bede5e7aff5464a2..20e6a3a5a12362cd86cc36c535279abd0af7fca5 100644 (file)
@@ -1,6 +1,6 @@
-import {InventoryItem, ShopItem} from "@shared/inventory";
-import { db} from '@lib/db';
-import {EquippedItemDetails} from "@shared/equipped";
+import {InventoryItem, ShopItem} from "../shared/inventory";
+import { db} from './lib/db';
+import {EquippedItemDetails} from "../shared/equipped";
 
 
 export async function addInventoryItem(playerId: string, item: ShopItem) {
diff --git a/src/server/lib/redis.ts b/src/server/lib/redis.ts
deleted file mode 100644 (file)
index b0990b1..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-import { Redis } from 'ioredis';
-
-export const redis = new Redis();
diff --git a/src/server/lib/router.ts b/src/server/lib/router.ts
deleted file mode 100644 (file)
index 6784c52..0000000
+++ /dev/null
@@ -1,91 +0,0 @@
-import { Router } from 'express';
-import { TObject } from '@sinclair/typebox';
-import { TypeCompiler } from '@sinclair/typebox/compiler';
-import type { Request, Response } from 'express';
-
-type Schema = {
-  input?: TObject<any>,
-  output?: TObject<any>
-};
-
-export type HttpHandler<I, O> = (params: I, rawReq: Request, rawRes: Response) => Promise<O>;
-
-class InvalidUserInputError extends Error {
-  statusCode: number;
-  constructor(msg: string) {
-    super(msg);
-    this.statusCode = 400;
-  }
-}
-
-export class HttpRouter {
-  router: Router;
-  prefix: string;
-  constructor(prefix: string = '') {
-    this.router = Router();
-    this.prefix = prefix;
-  }
-
-       wrap<I, O>(schema: Schema, handler: HttpHandler<I, O>) {
-               return async function (req: Request, res: Response) {
-                       try {
-                               const start = Date.now();
-                               console.info(`Req: ${req.method.toUpperCase()} ${req.originalUrl}`);
-
-        const rawParams = {
-          body: req.body,
-          params: req.params,
-          query: req.query,
-          headers: req.headers
-        };
-
-        if(schema.input) {
-          const validator = TypeCompiler.Compile(schema.input);
-          const validatorOutput = validator.Check(rawParams);
-          if(!validatorOutput) {
-            const errors = [...validator.Errors(rawParams)];
-            console.error('Validation Errors', errors);
-            throw new InvalidUserInputError('Invalid user input');
-          }
-        }
-
-                               const output = await handler(rawParams as I, req, res);
-                               console.info(`Runtime: ${Date.now() - start}ms`);
-
-                               if(output === undefined) {
-                                       res.statusCode = 204;
-                               }
-                               else if(typeof output === 'string') {
-                                       res.send(output);
-                               }
-                               else {
-                                       res.json(output);
-                               }
-                       }
-                       catch(e) {
-                               console.error(e);
-        res.status(e.statusCode || 500).send({
-          error: e.message
-        });
-                       }
-                       finally {
-                               res.end();
-                       }
-               }
-       }
-
-       get<I, O>(schema: Schema, endpoint: string, handler: HttpHandler<I, O>): void {
-               console.debug(`Mapped GET ${this.prefix+endpoint}`);
-               this.router.get(endpoint, this.wrap<I, O>(schema, handler));
-       }
-
-       post<I, O>(schema: Schema, endpoint: string, handler: HttpHandler<I, O>): void {
-               console.debug(`Mapped POST ${this.prefix+endpoint}`);
-               this.router.post(endpoint, this.wrap<I, O>(schema, handler));
-       }
-
-       delete<I, O>(schema: Schema, endpoint: string, handler: HttpHandler<I, O>): void {
-               console.debug(`Mapped DELETE ${this.prefix+endpoint}`);
-               this.router.post(endpoint, this.wrap<I, O>(schema, handler));
-       }
-}
diff --git a/src/server/lib/server.ts b/src/server/lib/server.ts
deleted file mode 100644 (file)
index 15133ef..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-import express from 'express';
-import bodyParser from 'body-parser';
-import { HttpRouter } from './router';
-
-export class HttpServer {
-       server: express.Application;
-       port: number;
-       constructor(port: string | number = '9090') {
-               this.port = parseInt(port.toString(), 10);
-    this.server = express();
-               this.configureMiddleWare();
-       }
-
-       configureMiddleWare() {
-               this.server.use(express.json());
-               this.server.use(bodyParser.json());
-    this.server.use(bodyParser.urlencoded({ extended: true }));
-       }
-
-  useRouter(router: HttpRouter) {
-    this.server.use(router.prefix, router.router);
-  }
-
-       start(fn?: any): void {
-               console.info(`Listening on port ${this.port}`);
-               this.server.listen(this.port, fn?.bind(this));
-       }
-}
-
-export let server: HttpServer;
-
-export function createServer(port?: string | number): HttpServer {
-  if(!server) {
-    server = new HttpServer(port);
-  }
-
-  return server;
-}
index f26856269d44b91127134ee322c37712b9ebcba0..3992f4097e0d686b51a37fa3bccb2a5587064d4b 100644 (file)
@@ -1,5 +1,5 @@
-import { db } from '@lib/db';
-import { Fight, Monster } from '@shared/monsters';
+import { db } from './lib/db';
+import { Fight, Monster } from '../shared/monsters';
 
 export async function getMonsterList(): Promise<Monster[]> {
   const res: Monster[] = await db.select('*')
index 6c0c25ee1f71b28b5b652fde5fb8cf016e32d7dc..fe8585caa5968a286e7059693e4cf55998884dc5 100644 (file)
@@ -1,5 +1,5 @@
-import { db } from '@lib/db';
-import {Player, maxHp} from "@shared/player";
+import { db } from './lib/db';
+import {Player, maxHp} from "../shared/player";
 import { random } from 'lodash';
 
 export async function loadPlayer(authToken: string): Promise<Player> {
index b89955c3cc2c6851e5832ce730fab54d0633e82d..924d87a9428705a9fee166213953a2f639e1bf14 100644 (file)
@@ -1,5 +1,5 @@
-import { db } from '@lib/db';
-import {ShopItem} from '@shared/inventory';
+import { db } from './lib/db';
+import {ShopItem} from '../shared/inventory';
 
 export function listShopItems(where: Partial<ShopItem>): Promise<ShopItem[]> {
   return db.select('*').from<ShopItem>('shop_items')
index b55b40e00154ba84cb4debfd8db84f9da783e496..fc2575e23efb67663f60f2dc5b01bcf7c812332c 100644 (file)
@@ -1,4 +1,4 @@
-import {InventoryItem, InventoryType, SubType} from "@shared/inventory";
+import {InventoryItem, InventoryType, SubType} from "./inventory";
 
 export type EquippedItem = {
   item_id: string;
index b08cdc91cd774f53cb31fe853a5c113ef258dee1..975830527575d22ad155d6c59cb61e2d9b691944 100644 (file)
@@ -1,5 +1,3 @@
-import * as _ from 'lodash';
-
 export type Player = {
   id: string,
   profession: Profession,
index ba8b1c92fd6bf2bb0186605636001a9ac46f032d..47b69bd289823e2a3d2545dce673f133e0622532 100644 (file)
@@ -8,16 +8,10 @@
         "preserveConstEnums": true,
         "sourceMap": true,
         "baseUrl": "src",
-        "paths": {},
+        "outDir": "dist",
         "emitDecoratorMetadata": true,
         "experimentalDecorators": true,
-        "resolveJsonModule": true,
-        "paths": {
-          "@lib/*": ["server/lib/*"],
-          "@shared/*": ["shared/*"],
-        }
+        "resolveJsonModule": true
     },
-    "ts-node": {
-      "require": ["tsconfig-paths/register"]
-    }
+    "include": ["src/server/api.ts"]
 }