1
0
mirror of https://github.com/shiroikitsu8/Bayshore_6r_legacy.git synced 2024-11-28 09:20:54 +01:00

api for ocm ranking (alpha)

This commit is contained in:
ghkkk090 2022-09-08 22:18:29 +07:00
parent 26f29cdf79
commit 1d7f124119
3 changed files with 131 additions and 5 deletions

112
src/api.ts Normal file
View File

@ -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);
});
}
}

View File

@ -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}!`);
})

View File

@ -1,2 +1,2 @@
yarn dev
node dist
pause