diff --git a/src/api.ts b/src/api.ts new file mode 100644 index 0000000..f3b88c0 --- /dev/null +++ b/src/api.ts @@ -0,0 +1,112 @@ +import express, { Application } from "express"; +import { prisma } from "."; +import { Module } from "./module"; + + +export default class ApiModule extends Module { + register(app: Application): void { + app.use(express.urlencoded({ + type: '*/*', + extended: true + })); + + app.use(express.json({ + type: '*/*' + })); + + // API Get Requests + // Get Current Competition Id + app.get('/api/get_competition_id', async (req, res) => { + + // Get current date + let date = Math.floor(new Date().getTime() / 1000); + + // Message Response + let message: any = { + competitionId: 1 // default + }; + + // Get current / previous active OCM Event + let ocmEventDate = await prisma.oCMEvent.findFirst({ + where: { + // qualifyingPeriodStartAt is less than current date + qualifyingPeriodStartAt: { lte: date }, + + // competitionEndAt is greater than current date + competitionEndAt: { gte: date }, + }, + orderBy: [ + { + dbId: 'desc' + }, + { + competitionEndAt: 'desc', + }, + ], + select:{ + competitionId: true + } + }); + + if(ocmEventDate) + { + message.competitionId = ocmEventDate.competitionId; + + // Send the response to the client + res.send(message); + } + else{ + ocmEventDate = await prisma.oCMEvent.findFirst({ + orderBy: { + dbId: 'desc' + }, + select:{ + competitionId: true + } + }); + + message.competitionId = ocmEventDate!.competitionId; + + // Send the response to the client + res.send(message); + } + }); + + // Get Competition Ranking + app.get('/api/get_competition_ranking', async (req, res) => { + + // Get url query + let competitionId = Number(req.query.competitionId); + + // Message Response + let message: any = { + cars: [] + }; + + // Get all of the cars matching the query + message.cars = await prisma.oCMTally.findMany({ + where:{ + competitionId: competitionId + }, + orderBy: { + result: 'desc' + }, + include:{ + car: { + select:{ + carId: true, + name: true, + visualModel: true, + level: true, + title: true, + regionId: true, + } + } + } + }); + + // Send the response to the client + res.send(message); + }); + } +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index df3baad..25223b0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,7 @@ import fs from 'fs'; import bodyParser from 'body-parser'; import AllnetModule from './allnet'; import MuchaModule from './mucha'; +import ApiModule from './api'; import { Config } from './config'; import process from 'process'; import * as Sentry from '@sentry/node'; @@ -30,8 +31,13 @@ const appRouter = Router(); const PORT_ALLNET = 80; const PORT_MUCHA = 10082; const PORT_BNGI = 9002; +const PORT_API = 9003; const app = express(); +const muchaApp = express(); +const allnetApp = express(); +const apiApp = express(); + app.use(bodyParser.raw({ type: '*/*' })); @@ -51,9 +57,6 @@ if (useSentry) { }); } -const muchaApp = express(); -const allnetApp = express(); - // Get the current timestamp let timestamp: string = common.getTimeStamp(); @@ -77,6 +80,11 @@ allnetApp.use((req, res, next) => { next() }); +apiApp.use((req, res, next) => { + console.log(timestamp+` [ API] ${req.method} ${req.url}`); + next() +}); + // Get all of the files in the modules directory let dirs = fs.readdirSync('dist/modules'); // Loop over the files @@ -108,11 +116,12 @@ app.all('*', (req, res) => { // Register the ALL.NET / Mucha Server new AllnetModule().register(allnetApp); new MuchaModule().register(muchaApp); +new ApiModule().register(apiApp); // Sentry is in use if (useSentry) { - // Use the sentry error handler + // Use the sentry error handler app.use(Sentry.Handlers.errorHandler()); } @@ -141,3 +150,8 @@ https.createServer({key, cert}, muchaApp).listen(PORT_MUCHA, '0.0.0.0', 511, () https.createServer({key, cert}, app).listen(PORT_BNGI, '0.0.0.0', 511, () => { console.log(`Game server listening on port ${PORT_BNGI}!`); }) + +// Create the API server +http.createServer(apiApp).listen(PORT_API, '0.0.0.0', 511, () => { + console.log(`API server listening on port ${PORT_API}!`); +}) diff --git a/start.bat b/start.bat index 5b15038..0d6f8f6 100644 --- a/start.bat +++ b/start.bat @@ -1,2 +1,2 @@ -yarn dev +node dist pause \ No newline at end of file