From: xangelo Date: Thu, 1 Jun 2023 17:15:09 +0000 (-0400) Subject: finalized build X-Git-Tag: v0.0.1~90 X-Git-Url: https://git.xangelo.ca/?a=commitdiff_plain;h=feb3571cafb9474d0e01b96dfeadc7c918f6b95d;p=risinglegends.git finalized build --- diff --git a/.gitignore b/.gitignore index 2d7ec5c..ddc3c2a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .env node_modules/ +dist/ diff --git a/package.json b/package.json index be1a13e..55c29b4 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/seeds/monsters.ts b/seeds/monsters.ts index 270d3f0..a3d3ab8 100644 --- a/seeds/monsters.ts +++ b/seeds/monsters.ts @@ -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); diff --git a/seeds/shop_items.ts b/seeds/shop_items.ts index 43e04f1..ea9e108 100644 --- a/seeds/shop_items.ts +++ b/seeds/shop_items.ts @@ -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 { // Deletes ALL existing entries await knex("shop_items").del(); + await knex("equipped").del(); + await knex("inventory").del(); const data: Partial[] = [ { diff --git a/src/client/index.ts b/src/client/index.ts index 6c549d2..c6624e8 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -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(); const events = new CustomEventManager(); diff --git a/src/server/api.ts b/src/server/api.ts index 2fa3317..9407457 100644 --- a/src/server/api.ts +++ b/src/server/api.ts @@ -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 = {}; + function calcAp(inventoryItem: EquippedItemDetails[], socket: Socket) { const ap: Record | Record = {}; 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}`); diff --git a/src/server/equipment.ts b/src/server/equipment.ts index ae5637b..e48c8b5 100644 --- a/src/server/equipment.ts +++ b/src/server/equipment.ts @@ -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 { return db.raw(` diff --git a/src/server/inventory.ts b/src/server/inventory.ts index 5c6ea3d..20e6a3a 100644 --- a/src/server/inventory.ts +++ b/src/server/inventory.ts @@ -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 index b0990b1..0000000 --- a/src/server/lib/redis.ts +++ /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 index 6784c52..0000000 --- a/src/server/lib/router.ts +++ /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, - output?: TObject -}; - -export type HttpHandler = (params: I, rawReq: Request, rawRes: Response) => Promise; - -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(schema: Schema, handler: HttpHandler) { - 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(schema: Schema, endpoint: string, handler: HttpHandler): void { - console.debug(`Mapped GET ${this.prefix+endpoint}`); - this.router.get(endpoint, this.wrap(schema, handler)); - } - - post(schema: Schema, endpoint: string, handler: HttpHandler): void { - console.debug(`Mapped POST ${this.prefix+endpoint}`); - this.router.post(endpoint, this.wrap(schema, handler)); - } - - delete(schema: Schema, endpoint: string, handler: HttpHandler): void { - console.debug(`Mapped DELETE ${this.prefix+endpoint}`); - this.router.post(endpoint, this.wrap(schema, handler)); - } -} diff --git a/src/server/lib/server.ts b/src/server/lib/server.ts deleted file mode 100644 index 15133ef..0000000 --- a/src/server/lib/server.ts +++ /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; -} diff --git a/src/server/monster.ts b/src/server/monster.ts index f268562..3992f40 100644 --- a/src/server/monster.ts +++ b/src/server/monster.ts @@ -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 { const res: Monster[] = await db.select('*') diff --git a/src/server/player.ts b/src/server/player.ts index 6c0c25e..fe8585c 100644 --- a/src/server/player.ts +++ b/src/server/player.ts @@ -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 { diff --git a/src/server/shopItem.ts b/src/server/shopItem.ts index b89955c..924d87a 100644 --- a/src/server/shopItem.ts +++ b/src/server/shopItem.ts @@ -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): Promise { return db.select('*').from('shop_items') diff --git a/src/shared/equipped.ts b/src/shared/equipped.ts index b55b40e..fc2575e 100644 --- a/src/shared/equipped.ts +++ b/src/shared/equipped.ts @@ -1,4 +1,4 @@ -import {InventoryItem, InventoryType, SubType} from "@shared/inventory"; +import {InventoryItem, InventoryType, SubType} from "./inventory"; export type EquippedItem = { item_id: string; diff --git a/src/shared/player.ts b/src/shared/player.ts index b08cdc9..9758305 100644 --- a/src/shared/player.ts +++ b/src/shared/player.ts @@ -1,5 +1,3 @@ -import * as _ from 'lodash'; - export type Player = { id: string, profession: Profession, diff --git a/tsconfig.json b/tsconfig.json index ba8b1c9..47b69bd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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"] }